mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-17 10:35:11 +00:00
139 lines
2.8 KiB
Go
139 lines
2.8 KiB
Go
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,
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00},
|
|
&Packet{
|
|
Magic: 0x20121025,
|
|
Port: 0x1234,
|
|
ID: "hello",
|
|
},
|
|
nil,
|
|
},
|
|
{
|
|
[]byte{0x20, 0x12, 0x10, 0x25,
|
|
0x34, 0x56, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x08,
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x21, 0x21,
|
|
0x00, 0x00, 0x00, 0x04,
|
|
0x01, 0x02, 0x03, 0x04},
|
|
&Packet{
|
|
Magic: 0x20121025,
|
|
Port: 0x3456,
|
|
ID: "hello!!!",
|
|
IP: []byte{1, 2, 3, 4},
|
|
},
|
|
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,
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00},
|
|
nil,
|
|
errFormat,
|
|
},
|
|
{
|
|
[]byte{0x20, 0x12, 0x10, 0x25,
|
|
0x12, 0x34, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x06,
|
|
0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, // missing padding
|
|
0x00, 0x00, 0x00, 0x00},
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|