diff --git a/Makefile b/Makefile index 8eca87c..3245a1b 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,8 @@ vet: $(GO) vet $(VETPACKAGES) lint: - $(GOLINT) $(GOFILES) + #$(GOLINT) $(GOFILES) + revive -formatter friendly ./... test: $(GO) test $(VETPACKAGES) diff --git a/base_packet.go b/base_packet.go index 99556d1..f0de2fe 100644 --- a/base_packet.go +++ b/base_packet.go @@ -6,29 +6,23 @@ import ( // basePacket is the base type of the NodePacket and PrimitivePacket type basePacket struct { - tag *Tag - // tagbuf []byte + tag *Tag length int - // lenbuf []byte valbuf []byte buf *bytes.Buffer } -// func (bp *basePacket) buildBuf() { -// bp.buf = append(bp.tagbuf, bp.lenbuf...) -// bp.buf = append(bp.buf, bp.valbuf...) -// } - -// GetRawBytes get raw bytes of this packet +// GetRawBytes get all raw bytes of this packet func (bp *basePacket) GetRawBytes() []byte { return bp.buf.Bytes() } +// Length return the length of Val this packet func (bp *basePacket) Length() int { return bp.length } -// SeqID returns Tag Key +// SeqID returns Tag of this packet func (bp *basePacket) SeqID() byte { return bp.tag.SeqID() } @@ -38,7 +32,7 @@ func (bp *basePacket) IsSlice() bool { return bp.tag.IsSlice() } -// GetValBuf get raw buffer of NodePacket +// GetValBuf get raw buffer of Val of this packet func (bp *basePacket) GetValBuf() []byte { return bp.valbuf } diff --git a/encoder.go b/encoder.go index 19b9a82..9e049ee 100644 --- a/encoder.go +++ b/encoder.go @@ -2,6 +2,7 @@ package y3 import ( "bytes" + "fmt" "github.com/yomorun/y3/encoding" ) @@ -39,7 +40,7 @@ func (enc *encoder) addRawPacket(en iEncoder) { // setTag write tag as seqID func (enc *encoder) writeTag() { if enc.seqID > 0x3F { - panic("sid should be in [0..0x7F]") + panic(fmt.Errorf("sid should be in [0..0x3F]")) } if enc.isNode { enc.seqID = enc.seqID | 0x80 @@ -51,12 +52,7 @@ func (enc *encoder) writeTag() { } func (enc *encoder) writeLengthBuf() { - // vallen := enc.valBuf.Len() vallen := len(enc.valbuf) - if vallen < 0 { - panic("length must greater than 0") - } - size := encoding.SizeOfPVarInt32(int32(vallen)) codec := encoding.VarCodec{Size: size} tmp := make([]byte, size) diff --git a/encoder_test.go b/encoder_test.go new file mode 100644 index 0000000..224c1c0 --- /dev/null +++ b/encoder_test.go @@ -0,0 +1,44 @@ +package y3 + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEncoderWriteTagErrorSeqID(t *testing.T) { + enc := &encoder{ + seqID: 0x40, + } + assert.PanicsWithError(t, "sid should be in [0..0x3F]", enc.writeTag) +} + +func TestEncoderWriteTagIsNode(t *testing.T) { + enc := &encoder{ + seqID: 0x00, + isNode: true, + buf: new(bytes.Buffer), + } + enc.writeTag() + assert.EqualValues(t, 0x80, enc.seqID) +} + +func TestEncoderWriteTagIsPrimitive(t *testing.T) { + enc := &encoder{ + seqID: 0x00, + buf: new(bytes.Buffer), + } + enc.writeTag() + assert.EqualValues(t, 0x00, enc.seqID) +} + +func TestEncoderWriteTagIsSlice(t *testing.T) { + enc := &encoder{ + seqID: 0x00, + isArray: true, + buf: new(bytes.Buffer), + } + enc.writeTag() + assert.EqualValues(t, 0x40, enc.seqID) +} diff --git a/encoding/pvarbool.go b/encoding/pvarbool.go index 6d5a78f..58fbdb7 100644 --- a/encoding/pvarbool.go +++ b/encoding/pvarbool.go @@ -11,6 +11,11 @@ func (codec *VarCodec) EncodePVarBool(buffer []byte, value bool) error { // DecodePVarBool decode to value as PVarBool from buffer func (codec *VarCodec) DecodePVarBool(buffer []byte, value *bool) error { + if len(buffer) == 0 { + *value = false + return nil + } + var tmp int64 var err = codec.decodePVarInt(buffer, &tmp) if tmp == 1 { diff --git a/node_decoder.go b/node_decoder.go index 0e5447a..04a4c17 100644 --- a/node_decoder.go +++ b/node_decoder.go @@ -26,7 +26,7 @@ func parsePayload(b []byte) (consumedBytes int, ifNodePacket bool, np *NodePacke return state.ConsumedBytes, false, nil, pp, err } -// DecodeNodePacket parse out whole buffer to a NodePacket +// DecodeToNodePacket parse out whole buffer to a NodePacket func DecodeToNodePacket(buf []byte, pct *NodePacket) (consumedBytes int, err error) { if len(buf) == 0 { return 0, errors.New("empty buf") diff --git a/node_encoder.go b/node_encoder.go index 13aec56..bfa9aad 100644 --- a/node_encoder.go +++ b/node_encoder.go @@ -6,13 +6,13 @@ import ( // NodePacketEncoder used for encode a node packet type NodePacketEncoder struct { - encoder + *encoder } // NewNodePacketEncoder returns an Encoder for node packet func NewNodePacketEncoder(sid byte) *NodePacketEncoder { nodeEnc := &NodePacketEncoder{ - encoder: encoder{ + encoder: &encoder{ isNode: true, buf: new(bytes.Buffer), }, diff --git a/node_test.go b/node_test.go index 4213e3a..354715e 100644 --- a/node_test.go +++ b/node_test.go @@ -57,11 +57,11 @@ func TestSubEmptyNode(t *testing.T) { func TestSimple1Node(t *testing.T) { sub := NewPrimitivePacketEncoder(0x01) sub.SetInt32Value(-1) - assert.Equal(t, []byte{0x01, 0x01, 0x7F}, sub.Encode()) + assert.Equal(t, []byte{0x01, 0x01, 0xFF}, sub.Encode()) node := NewNodePacketEncoder(0x04) node.AddPrimitivePacket(sub) - assert.Equal(t, []byte{0x84, 0x03, 0x01, 0x01, 0x7F}, node.Encode()) + assert.Equal(t, []byte{0x84, 0x03, 0x01, 0x01, 0xFF}, node.Encode()) res := &NodePacket{} consumedBytes, err := DecodeToNodePacket(node.Encode(), res) @@ -74,9 +74,10 @@ func TestSimple1Node(t *testing.T) { val, ok := res.PrimitivePackets[1] assert.EqualValues(t, true, ok) if ok { + assert.Equal(t, []byte{0x01, 0x01, 0xFF}, val.GetRawBytes()) v, err := val.ToInt32() assert.NoError(t, err) - assert.Equal(t, []byte{0x01, 0x01, 0x7F}, val.GetRawBytes()) + assert.Equal(t, []byte{0x01, 0x01, 0xFF}, val.GetRawBytes()) assert.Equal(t, int32(-1), v) } assert.Equal(t, 5, consumedBytes) @@ -91,7 +92,7 @@ func TestSimple1Node(t *testing.T) { // 0x03 (node value length is 4 bytes) // 0x01, 0x01, 0x7F (pvarint: -1) func TestSimpleNodes(t *testing.T) { - buf := []byte{0x85, 0x05, 0x84, 0x03, 0x01, 0x01, 0x7F} + buf := []byte{0x85, 0x05, 0x84, 0x03, 0x01, 0x01, 0xFF} res := &NodePacket{} consumedBytes, err := DecodeToNodePacket(buf, res) assert.NoError(t, err) @@ -102,14 +103,14 @@ func TestSimpleNodes(t *testing.T) { n, ok := res.NodePackets[0x04] assert.Equal(t, true, ok) - assert.Equal(t, []byte{0x84, 0x03, 0x01, 0x01, 0x7F}, n.GetRawBytes()) + assert.Equal(t, []byte{0x84, 0x03, 0x01, 0x01, 0xFF}, n.GetRawBytes()) assert.Equal(t, 0, len(n.NodePackets)) assert.Equal(t, 1, len(n.PrimitivePackets)) assert.EqualValues(t, 0x04, n.SeqID()) val, ok := n.PrimitivePackets[0x01] assert.EqualValues(t, true, ok) - assert.Equal(t, []byte{0x01, 0x01, 0x7F}, val.GetRawBytes()) + assert.Equal(t, []byte{0x01, 0x01, 0xFF}, val.GetRawBytes()) if ok { v, err := val.ToInt32() assert.NoError(t, err) @@ -129,7 +130,7 @@ func TestSimpleNodes(t *testing.T) { // 0x01, 0x01, 0x7F (pvarint: -1) // 0x02, 0x01, 0x01 (pvarint: 1) func TestSimple2Nodes(t *testing.T) { - buf := []byte{0x83, 0x06, 0x01, 0x01, 0x7F, 0x02, 0x01, 0x01} + buf := []byte{0x83, 0x06, 0x01, 0x01, 0xFF, 0x02, 0x01, 0x01} res := &NodePacket{} consumedBytes, err := DecodeToNodePacket(buf, res) assert.NoError(t, err) @@ -140,16 +141,16 @@ func TestSimple2Nodes(t *testing.T) { v1, ok := res.PrimitivePackets[0x01] assert.EqualValues(t, true, ok) - v, err := v1.ToInt() + v, err := v1.ToInt32() assert.NoError(t, err) - assert.Equal(t, []byte{0x01, 0x01, 0x7F}, v1.GetRawBytes()) + assert.Equal(t, []byte{0x01, 0x01, 0xFF}, v1.GetRawBytes()) assert.EqualValues(t, -1, v) assert.NoError(t, err) v2, ok := res.PrimitivePackets[0x02] assert.EqualValues(t, true, ok) assert.Equal(t, []byte{0x02, 0x01, 0x01}, v2.GetRawBytes()) - v, err = v2.ToInt() + v, err = v2.ToInt32() assert.NoError(t, err) assert.EqualValues(t, 1, v) } @@ -175,7 +176,7 @@ func TestSimple2Nodes(t *testing.T) { // 0x03 (node value length is 4 bytes) // 0x01, 0x01, 0x7E (varint: -2) func TestComplexNodes(t *testing.T) { - buf := []byte{0x85, 0x0D, 0x84, 0x06, 0x01, 0x01, 0x7F, 0x02, 0x01, 0x43, 0x83, 0x03, 0x01, 0x01, 0x7E} + buf := []byte{0x85, 0x0D, 0x84, 0x06, 0x01, 0x01, 0xFF, 0x02, 0x01, 0x43, 0x83, 0x03, 0x01, 0x01, 0xFE} res := &NodePacket{} consumedBytes, err := DecodeToNodePacket(buf, res) assert.NoError(t, err) @@ -186,13 +187,13 @@ func TestComplexNodes(t *testing.T) { n1, ok := res.NodePackets[0x04] assert.EqualValues(t, true, ok) - assert.Equal(t, []byte{0x84, 0x06, 0x01, 0x01, 0x7F, 0x02, 0x01, 0x43}, n1.GetRawBytes()) + assert.Equal(t, []byte{0x84, 0x06, 0x01, 0x01, 0xFF, 0x02, 0x01, 0x43}, n1.GetRawBytes()) assert.Equal(t, 2, len(n1.PrimitivePackets)) n1p1, ok := n1.PrimitivePackets[0x01] assert.EqualValues(t, true, ok) - assert.Equal(t, []byte{0x01, 0x01, 0x7F}, n1p1.GetRawBytes()) - vn1p1, err := n1p1.ToInt() + assert.Equal(t, []byte{0x01, 0x01, 0xFF}, n1p1.GetRawBytes()) + vn1p1, err := n1p1.ToInt32() assert.NoError(t, err) assert.EqualValues(t, -1, vn1p1) @@ -205,13 +206,13 @@ func TestComplexNodes(t *testing.T) { n2, ok := res.NodePackets[0x03] assert.EqualValues(t, true, ok) - assert.Equal(t, []byte{0x83, 0x03, 0x01, 0x01, 0x7E}, n2.GetRawBytes()) + assert.Equal(t, []byte{0x83, 0x03, 0x01, 0x01, 0xFE}, n2.GetRawBytes()) assert.Equal(t, 1, len(n2.PrimitivePackets)) n2p1, ok := n2.PrimitivePackets[0x01] assert.EqualValues(t, true, ok) - assert.Equal(t, []byte{0x01, 0x01, 0x7E}, n2p1.GetRawBytes()) - vn2p1, err := n2p1.ToInt() + assert.Equal(t, []byte{0x01, 0x01, 0xFE}, n2p1.GetRawBytes()) + vn2p1, err := n2p1.ToInt32() assert.NoError(t, err) assert.EqualValues(t, -2, vn2p1) } diff --git a/parser.go b/parser.go index ad51bd4..a6f574d 100644 --- a/parser.go +++ b/parser.go @@ -3,11 +3,13 @@ package y3 import ( "bytes" "errors" + "fmt" "io" "github.com/yomorun/y3/encoding" ) +// ReadPacket will try to read a Y3 encoded packet from the reader func ReadPacket(reader io.Reader) ([]byte, error) { tag, err := readByte(reader) if err != nil { @@ -20,7 +22,7 @@ func ReadPacket(reader io.Reader) ([]byte, error) { // write y3.Tag bytes buf.WriteByte(tag) - // read y3.Length bytes, a varint format. + // read y3.Length bytes, a varint format lenbuf := bytes.Buffer{} for { b, err := readByte(reader) @@ -39,6 +41,12 @@ func ReadPacket(reader io.Reader) ([]byte, error) { if err != nil { return nil, err } + + // validate len decoded from stream + if len < 0 { + return nil, fmt.Errorf("y3.ReadPacket() get lenbuf=(%# x), decode len=(%v)", lenbuf.Bytes(), len) + } + // write y3.Length bytes buf.Write(lenbuf.Bytes()) diff --git a/primitive_decoder.go b/primitive_decoder.go index bc18ac7..5bc8b8f 100644 --- a/primitive_decoder.go +++ b/primitive_decoder.go @@ -8,18 +8,21 @@ import ( "github.com/yomorun/y3/encoding" ) -type decodeState struct { +// DecodeState represents the state of decoding +type DecodeState struct { + // ConsumedBytes is the bytes consumed by decoder ConsumedBytes int - SizeL int + // SizeL is the bytes length of value + SizeL int } -// DecodePrimitivePacket parse out whole buffer to a PrimitivePacket +// DecodeToPrimitivePacket parse out whole buffer to a PrimitivePacket // // Examples: // [0x01, 0x01, 0x01] -> Key=0x01, Value=0x01 // [0x41, 0x06, 0x03, 0x01, 0x61, 0x04, 0x01, 0x62] -> key=0x03, value=0x61; key=0x04, value=0x62 -func DecodeToPrimitivePacket(buf []byte, p *PrimitivePacket) (decodeState, error) { - decoder := decodeState{ +func DecodeToPrimitivePacket(buf []byte, p *PrimitivePacket) (*DecodeState, error) { + decoder := &DecodeState{ ConsumedBytes: 0, SizeL: 0, } @@ -29,7 +32,7 @@ func DecodeToPrimitivePacket(buf []byte, p *PrimitivePacket) (decodeState, error } p.basePacket = &basePacket{ - valbuf: buf, + valbuf: []byte{}, buf: &bytes.Buffer{}, } @@ -37,7 +40,7 @@ func DecodeToPrimitivePacket(buf []byte, p *PrimitivePacket) (decodeState, error // first byte is `Tag` p.tag = NewTag(buf[pos]) p.buf.WriteByte(buf[pos]) - pos += 1 + pos++ decoder.ConsumedBytes = pos // read `Varint` from buf for `Length of value` @@ -59,10 +62,15 @@ func DecodeToPrimitivePacket(buf []byte, p *PrimitivePacket) (decodeState, error decoder.ConsumedBytes = pos decoder.SizeL = codec.Size + // if length<0, error on decoding + if bufLen < 0 { + return decoder, errors.New("invalid y3 packet, negative length") + } + // the length of value p.length = int(bufLen) if p.length == 0 { - p.valbuf = []byte{} + p.valbuf = nil return decoder, nil } diff --git a/primitive_encoder.go b/primitive_encoder.go index b842a77..6a7c77f 100644 --- a/primitive_encoder.go +++ b/primitive_encoder.go @@ -8,40 +8,39 @@ import ( // PrimitivePacketEncoder used for encode a primitive packet type PrimitivePacketEncoder struct { - encoder + *encoder } // NewPrimitivePacketEncoder return an Encoder for primitive packet func NewPrimitivePacketEncoder(sid byte) *PrimitivePacketEncoder { - primEnc := &PrimitivePacketEncoder{ - encoder: encoder{ + prim := &PrimitivePacketEncoder{ + encoder: &encoder{ isNode: false, buf: new(bytes.Buffer), }, } - primEnc.seqID = sid - return primEnc + prim.seqID = sid + return prim } // SetInt32Value encode int32 value func (enc *PrimitivePacketEncoder) SetInt32Value(v int32) { - size := encoding.SizeOfPVarInt32(v) + size := encoding.SizeOfNVarInt32(v) codec := encoding.VarCodec{Size: size} enc.valbuf = make([]byte, size) - err := codec.EncodePVarInt32(enc.valbuf, v) + err := codec.EncodeNVarInt32(enc.valbuf, v) if err != nil { panic(err) } - // enc.valBuf.Write(buf) } // SetUInt32Value encode uint32 value func (enc *PrimitivePacketEncoder) SetUInt32Value(v uint32) { - size := encoding.SizeOfPVarUInt32(v) + size := encoding.SizeOfNVarUInt32(v) codec := encoding.VarCodec{Size: size} enc.valbuf = make([]byte, size) - err := codec.EncodePVarUInt32(enc.valbuf, v) + err := codec.EncodeNVarUInt32(enc.valbuf, v) if err != nil { panic(err) } @@ -49,10 +48,10 @@ func (enc *PrimitivePacketEncoder) SetUInt32Value(v uint32) { // SetInt64Value encode int64 value func (enc *PrimitivePacketEncoder) SetInt64Value(v int64) { - size := encoding.SizeOfPVarInt64(v) + size := encoding.SizeOfNVarInt64(v) codec := encoding.VarCodec{Size: size} enc.valbuf = make([]byte, size) - err := codec.EncodePVarInt64(enc.valbuf, v) + err := codec.EncodeNVarInt64(enc.valbuf, v) if err != nil { panic(err) } @@ -60,10 +59,10 @@ func (enc *PrimitivePacketEncoder) SetInt64Value(v int64) { // SetUInt64Value encode uint64 value func (enc *PrimitivePacketEncoder) SetUInt64Value(v uint64) { - size := encoding.SizeOfPVarUInt64(v) + size := encoding.SizeOfNVarUInt64(v) codec := encoding.VarCodec{Size: size} enc.valbuf = make([]byte, size) - err := codec.EncodePVarUInt64(enc.valbuf, v) + err := codec.EncodeNVarUInt64(enc.valbuf, v) if err != nil { panic(err) } @@ -104,8 +103,6 @@ func (enc *PrimitivePacketEncoder) SetBoolValue(v bool) { // SetStringValue encode string func (enc *PrimitivePacketEncoder) SetStringValue(v string) { - // buf := []byte(v) - // enc.valBuf.Write(buf) enc.valbuf = []byte(v) } diff --git a/primitive_packet.go b/primitive_packet.go index b911d01..c1ef826 100644 --- a/primitive_packet.go +++ b/primitive_packet.go @@ -15,25 +15,19 @@ type PrimitivePacket struct { // ToInt32 parse raw as int32 value func (p *PrimitivePacket) ToInt32() (int32, error) { var val int32 - codec := encoding.VarCodec{} - err := codec.DecodePVarInt32(p.basePacket.valbuf, &val) + codec := encoding.VarCodec{Size: len(p.valbuf)} + err := codec.DecodeNVarInt32(p.basePacket.valbuf, &val) if err != nil { return 0, err } return val, nil } -// ToInt parse raw as int value -func (p *PrimitivePacket) ToInt() (int, error) { - v, err := p.ToInt32() - return int(v), err -} - // ToUInt32 parse raw as uint32 value func (p *PrimitivePacket) ToUInt32() (uint32, error) { var val uint32 - codec := encoding.VarCodec{} - err := codec.DecodePVarUInt32(p.valbuf, &val) + codec := encoding.VarCodec{Size: len(p.valbuf)} + err := codec.DecodeNVarUInt32(p.valbuf, &val) if err != nil { return 0, err } @@ -43,8 +37,8 @@ func (p *PrimitivePacket) ToUInt32() (uint32, error) { // ToInt64 parse raw as int64 value func (p *PrimitivePacket) ToInt64() (int64, error) { var val int64 - codec := encoding.VarCodec{} - err := codec.DecodePVarInt64(p.valbuf, &val) + codec := encoding.VarCodec{Size: len(p.valbuf)} + err := codec.DecodeNVarInt64(p.valbuf, &val) if err != nil { return 0, err } @@ -54,8 +48,8 @@ func (p *PrimitivePacket) ToInt64() (int64, error) { // ToUInt64 parse raw as uint64 value func (p *PrimitivePacket) ToUInt64() (uint64, error) { var val uint64 - codec := encoding.VarCodec{} - err := codec.DecodePVarUInt64(p.valbuf, &val) + codec := encoding.VarCodec{Size: len(p.valbuf)} + err := codec.DecodeNVarUInt64(p.valbuf, &val) if err != nil { return 0, err } diff --git a/primitive_test.go b/primitive_test.go index 15182af..35e1c87 100644 --- a/primitive_test.go +++ b/primitive_test.go @@ -6,19 +6,55 @@ import ( "github.com/stretchr/testify/assert" ) -func TestLackLengthPrimitivePacket(t *testing.T) { +func TestNilLengthPrimitivePacket(t *testing.T) { buf := []byte{0x01} p := &PrimitivePacket{} state, err := DecodeToPrimitivePacket(buf, p) - assert.Error(t, err) + assert.Error(t, err, "invalid y3 packet minimal size") assert.EqualValues(t, 0, state.ConsumedBytes) assert.EqualValues(t, 0, state.SizeL) +} + +// test for { 0x04: nil } +func TestZeroLengthPrimitivePacket(t *testing.T) { + buf := []byte{0x04, 0x00, 0x03} + p := &PrimitivePacket{} + state, err := DecodeToPrimitivePacket(buf, p) + assert.NoError(t, err) + assert.Equal(t, []byte{0x04, 0x00}, p.GetRawBytes()) + + assert.EqualValues(t, 2, state.ConsumedBytes) + assert.EqualValues(t, 1, state.SizeL) + assert.Equal(t, 0, len(p.valbuf)) p = &PrimitivePacket{} - state, err = DecodeToPrimitivePacket([]byte{0x01, 0x00}, p) + state, err = DecodeToPrimitivePacket([]byte{0x04, 0x00}, p) assert.NoError(t, err) assert.EqualValues(t, 2, state.ConsumedBytes) assert.EqualValues(t, 1, state.SizeL) + assert.EqualValues(t, false, p.IsSlice()) +} + +func TestNagetiveLengthPrimitivePacket(t *testing.T) { + buf := []byte{0x04, 0x74, 0x01, 0x01} + p := &PrimitivePacket{} + state, err := DecodeToPrimitivePacket(buf, p) + assert.Error(t, err, "invalid y3 packet, negative length") + assert.EqualValues(t, 2, state.ConsumedBytes) + assert.EqualValues(t, 1, state.SizeL) + assert.Equal(t, []byte{0x04, 0x74}, p.GetRawBytes()) + assert.Equal(t, []byte{}, p.GetValBuf()) +} + +func TestWrongLenPrimitivePacket(t *testing.T) { + buf := []byte{0x0A, 0x70, 0x01, 0x02} + p := &PrimitivePacket{} + state, err := DecodeToPrimitivePacket(buf, p) + assert.Error(t, err, "invalid y3 packet, negative length") + assert.EqualValues(t, 2, state.ConsumedBytes) + assert.EqualValues(t, 1, state.SizeL) + assert.Equal(t, []byte{0x0A, 0x70}, p.GetRawBytes()) + assert.Equal(t, []byte{}, p.GetValBuf()) } // test for { 0x04: -1 } @@ -39,170 +75,238 @@ func TestPacketRead(t *testing.T) { assert.EqualValues(t, 1, state.SizeL) } -// test for { 0x0A: [0x81, 0x7F] } -func TestParseInt32(t *testing.T) { - buf := []byte{0x0A, 0x02, 0x81, 0x7F} - p := &PrimitivePacket{} +// test for { 0x0A: 255 } +func TestInt32(t *testing.T) { + v := 255 + p := NewPrimitivePacketEncoder(0x0A) + p.SetInt32Value(int32(v)) + buf := p.Encode() - state, err := DecodeToPrimitivePacket(buf, p) - assert.NoError(t, err) - assert.Equal(t, buf, p.GetRawBytes()) + assert.Equal(t, []byte{0x0A, 0x02, 0x00, 0xFF}, buf) + packet := &PrimitivePacket{} + state, err := DecodeToPrimitivePacket(buf, packet) + assert.NoError(t, err) + assert.Equal(t, buf, packet.GetRawBytes()) + f, e := packet.ToInt32() + assert.NoError(t, e) + assert.EqualValues(t, v, f) assert.EqualValues(t, 4, state.ConsumedBytes) assert.EqualValues(t, 1, state.SizeL) +} - target, err := p.ToInt() - assert.NoError(t, err) - assert.EqualValues(t, 255, target) - - v1, err := p.ToUInt32() - assert.NoError(t, err) - assert.EqualValues(t, 255, v1) - - v2, err := p.ToInt64() - assert.NoError(t, err) - assert.EqualValues(t, 255, v2) +// test for { 0x0A: 255 } +func TestInt64(t *testing.T) { + v := 255 + p := NewPrimitivePacketEncoder(0x0A) + p.SetInt64Value(int64(v)) + buf := p.Encode() - v3, err := p.ToUInt64() - assert.NoError(t, err) - assert.EqualValues(t, 255, v3) + assert.Equal(t, []byte{0x0A, 0x02, 0x00, 0xFF}, buf) - v4, err := p.ToBool() + packet := &PrimitivePacket{} + state, err := DecodeToPrimitivePacket(buf, packet) assert.NoError(t, err) - assert.EqualValues(t, false, v4) + assert.Equal(t, buf, packet.GetRawBytes()) + f, e := packet.ToInt64() + assert.NoError(t, e) + assert.EqualValues(t, v, f) + assert.EqualValues(t, 4, state.ConsumedBytes) + assert.EqualValues(t, 1, state.SizeL) } -// test for { 0x03: [0x3F, 0x9D, 0x70, 0xA4] } -func TestParseFloat32(t *testing.T) { - buf := []byte{0x03, 0x04, 0x3F, 0x9D, 0x70, 0xA4} - p := &PrimitivePacket{} +// test for { 0x0A: 255 } +func TestUInt32(t *testing.T) { + v := 255 + p := NewPrimitivePacketEncoder(0x0A) + p.SetUInt32Value(uint32(v)) + buf := p.Encode() - _, err := DecodeToPrimitivePacket(buf, p) - assert.NoError(t, err) - assert.Equal(t, buf, p.GetRawBytes()) + assert.Equal(t, []byte{0x0A, 0x02, 0x00, 0xFF}, buf) - v4, err := p.ToFloat32() + packet := &PrimitivePacket{} + state, err := DecodeToPrimitivePacket(buf, packet) assert.NoError(t, err) - assert.EqualValues(t, 1.23, v4) + assert.Equal(t, buf, packet.GetRawBytes()) + f, e := packet.ToUInt32() + assert.NoError(t, e) + assert.EqualValues(t, v, f) + assert.EqualValues(t, 4, state.ConsumedBytes) + assert.EqualValues(t, 1, state.SizeL) } -// test for { 0x03: [0x3F, 0xF3, 0xAE, 0x14, 0x7A, 0xE1, 0x47, 0xAE] } -func TestParseFloat64(t *testing.T) { - buf := []byte{0x03, 0x08, 0x3F, 0xF3, 0xAE, 0x14, 0x7A, 0xE1, 0x47, 0xAE} - p := &PrimitivePacket{} +// test for { 0x0A: 255 } +func TestUInt64(t *testing.T) { + v := 255 + p := NewPrimitivePacketEncoder(0x0A) + p.SetUInt64Value(uint64(v)) + buf := p.Encode() - _, err := DecodeToPrimitivePacket(buf, p) - assert.NoError(t, err) - assert.Equal(t, buf, p.GetRawBytes()) + assert.Equal(t, []byte{0x0A, 0x02, 0x00, 0xFF}, buf) - v4, err := p.ToFloat64() + packet := &PrimitivePacket{} + state, err := DecodeToPrimitivePacket(buf, packet) assert.NoError(t, err) - assert.EqualValues(t, 1.23, v4) + assert.Equal(t, buf, packet.GetRawBytes()) + f, e := packet.ToUInt64() + assert.NoError(t, e) + assert.EqualValues(t, v, f) + assert.EqualValues(t, 4, state.ConsumedBytes) + assert.EqualValues(t, 1, state.SizeL) } -// test for { 0x0B: "C" } -func TestParseString(t *testing.T) { - buf := []byte{0x0B, 0x01, 0x43} - expectedValue := "C" - p := &PrimitivePacket{} - - state, err := DecodeToPrimitivePacket(buf, p) +// test for { 0x0A: 1 } +func TestFloat32(t *testing.T) { + var v float32 = 1 + expect := []byte{0x0A, 0x02, 0x3F, 0x80} + p := NewPrimitivePacketEncoder(0x0A) + p.SetFloat32Value(float32(v)) + buf := p.Encode() + assert.Equal(t, expect, buf) + + packet := &PrimitivePacket{} + state, err := DecodeToPrimitivePacket(buf, packet) assert.NoError(t, err) - assert.Equal(t, buf, p.GetRawBytes()) - - assert.EqualValues(t, 3, state.ConsumedBytes) + f, e := packet.ToFloat32() + assert.NoError(t, e) + assert.EqualValues(t, v, f) + assert.Equal(t, buf, packet.GetRawBytes()) + assert.EqualValues(t, 4, state.ConsumedBytes) assert.EqualValues(t, 1, state.SizeL) +} - target, err := p.ToUTF8String() +// test for { 0x0A: 255 } +func TestFloat64(t *testing.T) { + var v float64 = 1 + expect := []byte{0x0A, 0x02, 0x3F, 0xF0} + p := NewPrimitivePacketEncoder(0x0A) + p.SetFloat64Value(float64(v)) + buf := p.Encode() + assert.Equal(t, expect, buf) + + packet := &PrimitivePacket{} + state, err := DecodeToPrimitivePacket(buf, packet) assert.NoError(t, err) - assert.Equal(t, expectedValue, target) + f, e := packet.ToFloat64() + assert.NoError(t, e) + assert.EqualValues(t, v, f) + assert.Equal(t, buf, packet.GetRawBytes()) + assert.EqualValues(t, 4, state.ConsumedBytes) + assert.EqualValues(t, 1, state.SizeL) } -// test for { 0x04: nil } -func TestEmptyPrimitivePacket(t *testing.T) { - buf := []byte{0x04, 0x00, 0x03} - p := &PrimitivePacket{} - state, err := DecodeToPrimitivePacket(buf, p) - assert.NoError(t, err) - assert.Equal(t, []byte{0x04, 0x00}, p.GetRawBytes()) +// test for { 0x0B: "yomo" } +func TestString(t *testing.T) { + expect := []byte{0x0B, 0x04, 0x79, 0x6F, 0x6D, 0x6F} + v := "yomo" + p := NewPrimitivePacketEncoder(0x0B) + p.SetStringValue(v) + buf := p.Encode() + assert.Equal(t, expect, buf) - assert.EqualValues(t, 2, state.ConsumedBytes) + packet := &PrimitivePacket{} + + state, err := DecodeToPrimitivePacket(buf, packet) + assert.NoError(t, err) + assert.Equal(t, buf, packet.GetRawBytes()) + assert.Equal(t, []byte{0x79, 0x6F, 0x6D, 0x6F}, p.GetValBuf()) + target, err := packet.ToUTF8String() + assert.NoError(t, err) + assert.Equal(t, v, target) + assert.EqualValues(t, 6, state.ConsumedBytes) assert.EqualValues(t, 1, state.SizeL) - assert.Equal(t, 0, len(p.valbuf)) } // test for { 0x0C: "" } func TestParseEmptyString(t *testing.T) { - buf := []byte{0x0C, 0x00} - expectedValue := "" - p := &PrimitivePacket{} + expect := []byte{0x0C, 0x00} + v := "" + p := NewPrimitivePacketEncoder(0x0C) + p.SetStringValue(v) + buf := p.Encode() + assert.Equal(t, expect, buf) - state, err := DecodeToPrimitivePacket(buf, p) - assert.NoError(t, err) - assert.Equal(t, buf, p.GetRawBytes()) - assert.Equal(t, false, p.IsSlice()) + packet := &PrimitivePacket{} + state, err := DecodeToPrimitivePacket(buf, packet) + assert.NoError(t, err) + assert.Equal(t, buf, packet.GetRawBytes()) + target, err := packet.ToUTF8String() + assert.NoError(t, err) + assert.Equal(t, v, target) assert.EqualValues(t, 2, state.ConsumedBytes) assert.EqualValues(t, 1, state.SizeL) +} + +// test for { 0x2C: true } +func TestPrimitivePacketBool(t *testing.T) { + expect := []byte{0x2C, 0x01, 0x01} + v := true + p := NewPrimitivePacketEncoder(0x2C) + p.SetBoolValue(v) + buf := p.Encode() + assert.Equal(t, expect, buf) + + packet := &PrimitivePacket{} - target, err := p.ToUTF8String() + state, err := DecodeToPrimitivePacket(buf, packet) assert.NoError(t, err) - assert.Equal(t, expectedValue, target) + assert.Equal(t, buf, packet.GetRawBytes()) + target, err := packet.ToBool() + assert.NoError(t, err) + assert.Equal(t, v, target) + assert.EqualValues(t, 3, state.ConsumedBytes) + assert.EqualValues(t, 1, state.SizeL) } -func TestPrimitivePacketEncodeBytesAndString(t *testing.T) { - buf := []byte("yomo") - p := NewPrimitivePacketEncoder(0x02) - p.SetBytesValue(buf) - res := p.Encode() - assert.Equal(t, []byte{0x02, 0x04, 0x79, 0x6F, 0x6D, 0x6F}, res) - assert.Equal(t, []byte{0x79, 0x6F, 0x6D, 0x6F}, p.GetValBuf()) +// test for { 0x2C: false } +func TestPrimitivePacketBool2(t *testing.T) { + buf := []byte{0x2C, 0x00} + packet := &PrimitivePacket{} - p = NewPrimitivePacketEncoder(0x02) - p.SetStringValue("yomo") - res = p.Encode() - assert.Equal(t, []byte{0x02, 0x04, 0x79, 0x6F, 0x6D, 0x6F}, res) - assert.Equal(t, []byte{0x79, 0x6F, 0x6D, 0x6F}, p.GetValBuf()) + state, err := DecodeToPrimitivePacket(buf, packet) + assert.NoError(t, err) + assert.Equal(t, buf, packet.GetRawBytes()) + target, err := packet.ToBool() + assert.NoError(t, err) + assert.Equal(t, false, target) + assert.EqualValues(t, 2, state.ConsumedBytes) + assert.EqualValues(t, 1, state.SizeL) +} + +// test for { 0x2C: false } +func TestPrimitivePacketBool3(t *testing.T) { + buf := []byte{0x2C, 0x01, 0x08} + packet := &PrimitivePacket{} + + state, err := DecodeToPrimitivePacket(buf, packet) + assert.NoError(t, err) + assert.Equal(t, []byte{0x2C, 0x01, 0x08}, packet.GetRawBytes()) + target, err := packet.ToBool() + assert.NoError(t, err) + assert.Equal(t, false, target) + assert.EqualValues(t, 3, state.ConsumedBytes) + assert.EqualValues(t, 1, state.SizeL) } -func TestPrimitivePacketEncodeNumbers(t *testing.T) { - p := NewPrimitivePacketEncoder(0x03) - p.SetInt32Value(-128) - res := p.Encode() - assert.Equal(t, []byte{0x03, 0x02, 0xFF, 0x00}, res) - - p = NewPrimitivePacketEncoder(0x03) - p.SetUInt32Value(128) - res = p.Encode() - assert.Equal(t, []byte{0x03, 0x02, 0x81, 0x00}, res) - - p = NewPrimitivePacketEncoder(0x03) - p.SetInt64Value(-128) - res = p.Encode() - assert.Equal(t, []byte{0x03, 0x02, 0xFF, 0x00}, res) - - p = NewPrimitivePacketEncoder(0x03) - p.SetUInt64Value(128) - res = p.Encode() - assert.Equal(t, []byte{0x03, 0x02, 0x81, 0x00}, res) - - p = NewPrimitivePacketEncoder(0x03) - p.SetFloat32Value(1.23) - res = p.Encode() - assert.Equal(t, []byte{0x03, 0x04, 0x3F, 0x9D, 0x70, 0xA4}, res) - - p = NewPrimitivePacketEncoder(0x03) - p.SetFloat64Value(1.23) - res = p.Encode() - assert.Equal(t, []byte{0x03, 0x08, 0x3F, 0xF3, 0xAE, 0x14, 0x7A, 0xE1, 0x47, 0xAE}, res) - - p = NewPrimitivePacketEncoder(0x03) - p.SetBoolValue(true) - res = p.Encode() - assert.Equal(t, []byte{0x03, 0x01, 0x01}, res) - - p = NewPrimitivePacketEncoder(0x03) - p.SetBoolValue(false) - res = p.Encode() - assert.Equal(t, []byte{0x03, 0x01, 0x00}, res) +func TestPrimitivePacketBytes(t *testing.T) { + v := make([]byte, 255) + for i := 0; i < 255; i++ { + v[i] = byte(i) + } + expect := append([]byte{0x01, 0x81, 0x7F}, v...) + p := NewPrimitivePacketEncoder(0x01) + p.SetBytesValue(v) + buf := p.Encode() + assert.Equal(t, expect, buf) + + packet := &PrimitivePacket{} + state, err := DecodeToPrimitivePacket(buf, packet) + assert.NoError(t, err) + assert.Equal(t, buf, packet.GetRawBytes()) + target := packet.ToBytes() + assert.NoError(t, err) + assert.Equal(t, v, target) + assert.EqualValues(t, 255+2+1, state.ConsumedBytes) + assert.EqualValues(t, 2, state.SizeL) }