-
Notifications
You must be signed in to change notification settings - Fork 2
/
ndp_test.go
68 lines (63 loc) · 2.01 KB
/
ndp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ndp
import (
"bytes"
"reflect"
"testing"
)
func TestEncDecDomainName(t *testing.T) {
tests := []struct {
name []string
encoded []byte
}{
// TODO: fix this
//{[]string{"."}, []byte{0, 0, 0, 0, 0, 0, 0, 0}},
{[]string{"foo.bar."}, []byte{3, 102, 111, 111, 3, 98, 97, 114, 0, 0, 0, 0, 0, 0, 0, 0}},
{[]string{"golang.org."}, []byte{6, 103, 111, 108, 97, 110, 103, 3, 111, 114, 103, 0, 0, 0, 0, 0}},
{[]string{"basement.golang.org."}, []byte{8, 98, 97, 115, 101, 109, 101, 110, 116, 6, 103, 111, 108, 97, 110, 103, 3, 111, 114, 103, 0, 0, 0, 0}},
{[]string{"basement.golang.org.", "golang.org."}, []byte{8, 98, 97, 115, 101, 109, 101, 110, 116, 6, 103, 111, 108, 97, 110, 103, 3, 111, 114, 103, 0, 6, 103, 111, 108, 97, 110, 103, 3, 111, 114, 103, 0, 0, 0, 0, 0, 0, 0, 0}},
}
for _, test := range tests {
// encoding
encoded := encDomainName(test.name)
if bytes.Compare(encoded, test.encoded) != 0 {
t.Errorf("failed to encode %s to %v, result was %v", test.name, test.encoded, encoded)
}
// decoding
name := decDomainName(test.encoded)
if !reflect.DeepEqual(name, test.name) {
t.Errorf("failed to decode %v to %s, result was %s", test.encoded, test.name, name)
}
}
// total length may not exceed 255 bytes
encoded := encDomainName([]string{
// many labels
"aaaa.aaaa.aaaa",
"bbbb.bbbb.bbbb",
"cccc.cccc.cccc",
"dddd.dddd.dddd",
"eeee.eeee.eeee",
"ffff.ffff.ffff",
"gggg.gggg.gggg",
"hhhh.hhhh.hhhh",
"iiii.iiii.iiii",
"jjjj.jjjj.jjjj",
"kkkk.kkkk.kkkk",
"llll.llll.llll",
"mmmm.mmmm.mmmm",
"nnnn.nnnn.nnnn",
"oooo.oooo.oooo",
"pppp.pppp.pppp",
"qqqq.qqqq.qqqq",
})
if len(encoded) != 255 {
t.Errorf("expected truncated encoding of 255, not %d", len(encoded))
}
// individual label length may not exceed 63 bytes
encoded = encDomainName([]string{
// very long label
"abcdefghijlmnopqrstuvwyxzabcdefghijlmnopqrstuvwyxzabcdefghijlmnopqrstuvwyxz.foo",
})
if len(encoded) != 72 {
t.Errorf("expected truncated encoding of 72, not %d", len(encoded))
}
}