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

.

parent cf62ac88
...@@ -15,11 +15,21 @@ ...@@ -15,11 +15,21 @@
package neo package neo
import ( import (
hexpkg "encoding/hex"
"reflect" "reflect"
"testing" "testing"
"unsafe" "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) { func TestPktHeader(t *testing.T) {
// make sure PktHeader is really packed // make sure PktHeader is really packed
if unsafe.Sizeof(PktHead{}) != 10 { if unsafe.Sizeof(PktHead{}) != 10 {
...@@ -27,43 +37,81 @@ func TestPktHeader(t *testing.T) { ...@@ -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 // test encoding/decoding of packets
func TestPktMarshal(t *testing.T) { func TestPktMarshal(t *testing.T) {
var testv = []struct { var testv = []struct {
pkt NEODecoder //interface {NEOEncoder; NEODecoder} pkt NEODecoder //interface {NEOEncoder; NEODecoder}
encoded string // []byte encoded string // []byte
} { } {
// empty
{&Ping{}, ""}, {&Ping{}, ""},
// uint32, string XXX string -> Notify?
{&Error{Code: 0x01020304, Message: "hello"}, "\x01\x02\x03\x04\x00\x00\x00\x05hello"}, {&Error{Code: 0x01020304, Message: "hello"}, "\x01\x02\x03\x04\x00\x00\x00\x05hello"},
}
for _, tt := range testv { // Oid, Tid, bool, Checksum, []byte
// TODO check encoding {&StoreObject{
Oid: 0x0102030405060708,
// check decoding Serial: 0x0a0b0c0d0e0f0102,
data := tt.encoded + "noise" Compression: false,
typ := reflect.TypeOf(tt.pkt).Elem() // type of *pkt Checksum: Checksum{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}, // XXX simpler?
pkt2 := reflect.New(typ).Interface().(NEODecoder) Data: []byte("hello world"),
n, err := pkt2.NEODecode([]byte(data)) // XXX DataSerial: 0x0a0b0c0d0e0f0103,
if err != nil { Tid: 0x0a0b0c0d0e0f0104,
t.Errorf("%v: decode error %v", typ, err) Unlock: true,
} },
if n != len(tt.encoded) {
t.Errorf("%v: nread = %v ; want %v", typ, n, len(tt.encoded))
}
if !reflect.DeepEqual(pkt2, tt.pkt) { hex("01020304050607080a0b0c0d0e0f010200") +
t.Errorf("%v: decode result unexpected: %v ; want %v", typ, pkt2, tt.pkt) hex("0102030405060708090a0b0c0d0e0f1011121314") +
} hex("0000000b") + "hello world" +
hex("0a0b0c0d0e0f01030a0b0c0d0e0f010401")},
// decode must overflow on cut data // TODO bool, [], map
for l := len(tt.encoded)-1; l >= 0; l-- { // TODO Address, Checksum, Tid, PTid
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)
}
} // bool: StoreObject, AnswerGetObject
}
for _, tt := range testv {
testPktMarshal(t, tt.pkt, tt.encoded)
} }
} }
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
// See COPYING file for full licensing terms. // See COPYING file for full licensing terms.
// NEO. Protocol definition. Code generator // 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 // +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