github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/allegrosql/server/packetio_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package server
    15  
    16  import (
    17  	"bufio"
    18  	"bytes"
    19  	"net"
    20  	"time"
    21  
    22  	. "github.com/whtcorpsinc/check"
    23  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    24  )
    25  
    26  type PacketIOTestSuite struct {
    27  }
    28  
    29  var _ = Suite(new(PacketIOTestSuite))
    30  
    31  func (s *PacketIOTestSuite) TestWrite(c *C) {
    32  	// Test write one packet
    33  	var outBuffer bytes.Buffer
    34  	pkt := &packetIO{bufWriter: bufio.NewWriter(&outBuffer)}
    35  	err := pkt.writePacket([]byte{0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03})
    36  	c.Assert(err, IsNil)
    37  	err = pkt.flush()
    38  	c.Assert(err, IsNil)
    39  	c.Assert(outBuffer.Bytes(), DeepEquals, []byte{0x03, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03})
    40  
    41  	// Test write more than one packet
    42  	outBuffer.Reset()
    43  	largeInput := make([]byte, allegrosql.MaxPayloadLen+4)
    44  	pkt = &packetIO{bufWriter: bufio.NewWriter(&outBuffer)}
    45  	err = pkt.writePacket(largeInput)
    46  	c.Assert(err, IsNil)
    47  	err = pkt.flush()
    48  	c.Assert(err, IsNil)
    49  	res := outBuffer.Bytes()
    50  	c.Assert(res[0], Equals, byte(0xff))
    51  	c.Assert(res[1], Equals, byte(0xff))
    52  	c.Assert(res[2], Equals, byte(0xff))
    53  	c.Assert(res[3], Equals, byte(0))
    54  }
    55  
    56  func (s *PacketIOTestSuite) TestRead(c *C) {
    57  	var inBuffer bytes.Buffer
    58  	_, err := inBuffer.Write([]byte{0x01, 0x00, 0x00, 0x00, 0x01})
    59  	c.Assert(err, IsNil)
    60  	// Test read one packet
    61  	brc := newBufferedReadConn(&bytesConn{inBuffer})
    62  	pkt := newPacketIO(brc)
    63  	bytes, err := pkt.readPacket()
    64  	c.Assert(err, IsNil)
    65  	c.Assert(pkt.sequence, Equals, uint8(1))
    66  	c.Assert(bytes, DeepEquals, []byte{0x01})
    67  
    68  	inBuffer.Reset()
    69  	buf := make([]byte, allegrosql.MaxPayloadLen+9)
    70  	buf[0] = 0xff
    71  	buf[1] = 0xff
    72  	buf[2] = 0xff
    73  	buf[3] = 0
    74  	buf[2+allegrosql.MaxPayloadLen] = 0x00
    75  	buf[3+allegrosql.MaxPayloadLen] = 0x00
    76  	buf[4+allegrosql.MaxPayloadLen] = 0x01
    77  	buf[7+allegrosql.MaxPayloadLen] = 0x01
    78  	buf[8+allegrosql.MaxPayloadLen] = 0x0a
    79  
    80  	_, err = inBuffer.Write(buf)
    81  	c.Assert(err, IsNil)
    82  	// Test read multiple packets
    83  	brc = newBufferedReadConn(&bytesConn{inBuffer})
    84  	pkt = newPacketIO(brc)
    85  	bytes, err = pkt.readPacket()
    86  	c.Assert(err, IsNil)
    87  	c.Assert(pkt.sequence, Equals, uint8(2))
    88  	c.Assert(len(bytes), Equals, allegrosql.MaxPayloadLen+1)
    89  	c.Assert(bytes[allegrosql.MaxPayloadLen], DeepEquals, byte(0x0a))
    90  }
    91  
    92  type bytesConn struct {
    93  	b bytes.Buffer
    94  }
    95  
    96  func (c *bytesConn) Read(b []byte) (n int, err error) {
    97  	return c.b.Read(b)
    98  }
    99  
   100  func (c *bytesConn) Write(b []byte) (n int, err error) {
   101  	return 0, nil
   102  }
   103  
   104  func (c *bytesConn) Close() error {
   105  	return nil
   106  }
   107  
   108  func (c *bytesConn) LocalAddr() net.Addr {
   109  	return nil
   110  }
   111  
   112  func (c *bytesConn) RemoteAddr() net.Addr {
   113  	return nil
   114  }
   115  
   116  func (c *bytesConn) SetDeadline(t time.Time) error {
   117  	return nil
   118  }
   119  
   120  func (c *bytesConn) SetReadDeadline(t time.Time) error {
   121  	return nil
   122  }
   123  
   124  func (c *bytesConn) SetWriteDeadline(t time.Time) error {
   125  	return nil
   126  }