2013-12-22 22:29:23 +01:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var testdata = []struct {
|
|
|
|
data []byte
|
|
|
|
packet *packet
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]byte{0x20, 0x12, 0x10, 0x25,
|
|
|
|
0x12, 0x34, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x05,
|
2013-12-22 23:13:51 +01:00
|
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00},
|
2013-12-22 22:29:23 +01:00
|
|
|
&packet{
|
|
|
|
magic: 0x20121025,
|
|
|
|
port: 0x1234,
|
|
|
|
id: "hello",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]byte{0x20, 0x12, 0x10, 0x25,
|
|
|
|
0x34, 0x56, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x08,
|
2013-12-22 23:13:51 +01:00
|
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x21, 0x21,
|
|
|
|
0x00, 0x00, 0x00, 0x04,
|
|
|
|
0x01, 0x02, 0x03, 0x04},
|
2013-12-22 22:29:23 +01:00
|
|
|
&packet{
|
|
|
|
magic: 0x20121025,
|
|
|
|
port: 0x3456,
|
|
|
|
id: "hello!!!",
|
2013-12-22 23:13:51 +01:00
|
|
|
ip: []byte{1, 2, 3, 4},
|
2013-12-22 22:29:23 +01:00
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]byte{0x19, 0x76, 0x03, 0x09,
|
|
|
|
0x00, 0x00, 0x00, 0x06,
|
|
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x00, 0x00},
|
|
|
|
&packet{
|
|
|
|
magic: 0x19760309,
|
|
|
|
id: "hello!",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]byte{0x20, 0x12, 0x10, 0x25,
|
|
|
|
0x12, 0x34, 0x12, 0x34, // reserved bits not set to zero
|
|
|
|
0x00, 0x00, 0x00, 0x06,
|
2013-12-22 23:13:51 +01:00
|
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00},
|
2013-12-22 22:29:23 +01:00
|
|
|
nil,
|
|
|
|
errFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]byte{0x20, 0x12, 0x10, 0x25,
|
|
|
|
0x12, 0x34, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x06,
|
2013-12-22 23:13:51 +01:00
|
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, // missing padding
|
|
|
|
0x00, 0x00, 0x00, 0x00},
|
2013-12-22 22:29:23 +01:00
|
|
|
nil,
|
|
|
|
errFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]byte{0x19, 0x77, 0x03, 0x09, // incorrect magic
|
|
|
|
0x00, 0x00, 0x00, 0x06,
|
|
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x00, 0x00},
|
|
|
|
nil,
|
|
|
|
errBadMagic,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]byte{0x19, 0x76, 0x03, 0x09,
|
|
|
|
0x6c, 0x6c, 0x6c, 0x6c, // length exceeds packet size
|
|
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x00, 0x00},
|
|
|
|
nil,
|
|
|
|
errFormat,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]byte{0x19, 0x76, 0x03, 0x09,
|
|
|
|
0x00, 0x00, 0x00, 0x06,
|
|
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x00, 0x00,
|
|
|
|
0x23}, // extra data at the end
|
|
|
|
nil,
|
|
|
|
errFormat,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodePacket(t *testing.T) {
|
|
|
|
for i, test := range testdata {
|
|
|
|
p, err := decodePacket(test.data)
|
|
|
|
if err != test.err {
|
|
|
|
t.Errorf("%d: unexpected error %v", i, err)
|
|
|
|
} else {
|
|
|
|
if !reflect.DeepEqual(p, test.packet) {
|
|
|
|
t.Errorf("%d: incorrect packet\n%v\n%v", i, test.packet, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEncodePacket(t *testing.T) {
|
|
|
|
for i, test := range testdata {
|
|
|
|
if test.err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
buf := encodePacket(*test.packet)
|
|
|
|
if bytes.Compare(buf, test.data) != 0 {
|
|
|
|
t.Errorf("%d: incorrect encoded packet\n% x\n% 0x", i, test.data, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-22 23:13:51 +01:00
|
|
|
|
|
|
|
var ipstrTests = []struct {
|
|
|
|
d []byte
|
|
|
|
s string
|
|
|
|
}{
|
|
|
|
{[]byte{192, 168, 34}, ""},
|
|
|
|
{[]byte{192, 168, 0, 34}, "192.168.0.34"},
|
|
|
|
{[]byte{0x20, 0x01, 0x12, 0x34,
|
|
|
|
0x34, 0x56, 0x56, 0x78,
|
|
|
|
0x78, 0x00, 0x00, 0xdc,
|
|
|
|
0x00, 0x00, 0x43, 0x54}, "2001:1234:3456:5678:7800:00dc:0000:4354"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIPStr(t *testing.T) {
|
|
|
|
for _, tc := range ipstrTests {
|
|
|
|
s1 := ipStr(tc.d)
|
|
|
|
if s1 != tc.s {
|
|
|
|
t.Errorf("Incorrect ipstr %q != %q", tc.s, s1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|