Commit 5a088ffa authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent cf62ac88
......@@ -15,11 +15,21 @@
package neo
import (
hexpkg "encoding/hex"
"reflect"
"testing"
"unsafe"
)
// decode string as hex; panic on error
func hex(s string) string {
b, err := hexpkg.DecodeString(s)
if err != nil {
panic(err)
}
return string(b)
}
func TestPktHeader(t *testing.T) {
// make sure PktHeader is really packed
if unsafe.Sizeof(PktHead{}) != 10 {
......@@ -27,43 +37,81 @@ func TestPktHeader(t *testing.T) {
}
}
func testPktMarshal(t *testing.T, pkt NEODecoder, encoded string) {
typ := reflect.TypeOf(pkt).Elem() // type of *pkt
pkt2 := reflect.New(typ).Interface().(NEODecoder)
defer func() {
if e := recover(); e != nil {
t.Errorf("%v: panic ↓↓↓:", typ)
panic(e) // to show traceback
}
}()
// TODO check encoding
// check decoding
data := encoded + "noise"
n, err := pkt2.NEODecode([]byte(data)) // XXX
if err != nil {
t.Errorf("%v: decode error %v", typ, err)
}
if n != len(encoded) {
t.Errorf("%v: nread = %v ; want %v", typ, n, len(encoded))
}
if !reflect.DeepEqual(pkt2, pkt) {
t.Errorf("%v: decode result unexpected: %v ; want %v", typ, pkt2, pkt)
}
// decode must overflow on cut data TODO reenable
/*
for l := len(encoded)-1; l >= 0; l-- {
data = encoded[:l] // XXX also check on original byte [:l] ?
n, err = pkt2.NEODecode([]byte(data)) // XXX
if !(n==0 && err==ErrDecodeOverflow) {
t.Errorf("%v: decode overflow not detected on [:%v]", typ, l)
}
}
*/
}
// test encoding/decoding of packets
func TestPktMarshal(t *testing.T) {
var testv = []struct {
pkt NEODecoder //interface {NEOEncoder; NEODecoder}
encoded string // []byte
} {
// empty
{&Ping{}, ""},
// uint32, string XXX string -> Notify?
{&Error{Code: 0x01020304, Message: "hello"}, "\x01\x02\x03\x04\x00\x00\x00\x05hello"},
}
for _, tt := range testv {
// TODO check encoding
// check decoding
data := tt.encoded + "noise"
typ := reflect.TypeOf(tt.pkt).Elem() // type of *pkt
pkt2 := reflect.New(typ).Interface().(NEODecoder)
n, err := pkt2.NEODecode([]byte(data)) // XXX
if err != nil {
t.Errorf("%v: decode error %v", typ, err)
}
if n != len(tt.encoded) {
t.Errorf("%v: nread = %v ; want %v", typ, n, len(tt.encoded))
}
// Oid, Tid, bool, Checksum, []byte
{&StoreObject{
Oid: 0x0102030405060708,
Serial: 0x0a0b0c0d0e0f0102,
Compression: false,
Checksum: Checksum{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}, // XXX simpler?
Data: []byte("hello world"),
DataSerial: 0x0a0b0c0d0e0f0103,
Tid: 0x0a0b0c0d0e0f0104,
Unlock: true,
},
if !reflect.DeepEqual(pkt2, tt.pkt) {
t.Errorf("%v: decode result unexpected: %v ; want %v", typ, pkt2, tt.pkt)
}
hex("01020304050607080a0b0c0d0e0f010200") +
hex("0102030405060708090a0b0c0d0e0f1011121314") +
hex("0000000b") + "hello world" +
hex("0a0b0c0d0e0f01030a0b0c0d0e0f010401")},
// decode must overflow on cut data
for l := len(tt.encoded)-1; l >= 0; l-- {
data = tt.encoded[:l] // XXX also check on original byte [:l] ?
n, err = pkt2.NEODecode([]byte(data)) // XXX
if !(n==0 && err==ErrDecodeOverflow) {
t.Errorf("%v: decode overflow not detected on [:%v]", typ, l)
}
// TODO bool, [], map
// TODO Address, Checksum, Tid, PTid
}
// bool: StoreObject, AnswerGetObject
}
for _, tt := range testv {
testPktMarshal(t, tt.pkt, tt.encoded)
}
}
......@@ -11,7 +11,7 @@
// See COPYING file for full licensing terms.
// NEO. Protocol definition. Code generator
// TODO text what it does (generates code for proto.go)
// TODO text what it does (generates marshal code for proto.go)
// +build ignore
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment