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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
package dhcpv6
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegation(t *testing.T) {
data := []byte{
1, 0, 0, 0, // IAID
0, 0, 0, 1, // T1
0, 0, 0, 2, // T2
0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime
0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime
36, // IAPrefix prefixLength
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
}
opt, err := ParseOptIAForPrefixDelegation(data)
require.NoError(t, err)
require.Equal(t, len(data), opt.Length())
require.Equal(t, OptionIAPD, opt.Code())
require.Equal(t, []byte{1, 0, 0, 0}, opt.IAID())
require.Equal(t, uint32(1), opt.T1())
require.Equal(t, uint32(2), opt.T2())
}
func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegationInvalidLength(t *testing.T) {
data := []byte{
1, 0, 0, 0, // IAID
0, 0, 0, 1, // T1
// truncated from here
}
_, err := ParseOptIAForPrefixDelegation(data)
require.Error(t, err)
}
func TestOptIAForPrefixDelegationParseOptIAForPrefixDelegationInvalidOptions(t *testing.T) {
data := []byte{
1, 0, 0, 0, // IAID
0, 0, 0, 1, // T1
0, 0, 0, 2, // T2
0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime
0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime
36, // IAPrefix prefixLength
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // IAPrefix ipv6Prefix missing last byte
}
_, err := ParseOptIAForPrefixDelegation(data)
require.Error(t, err)
}
func TestOptIAForPrefixDelegationGetOneOption(t *testing.T) {
buf := []byte{
0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime
0xee, 0xff, 0x00, 0x11, // validLifetime
36, // prefixLength
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // ipv6Prefix
}
oaddr, err := ParseOptIAPrefix(buf)
if err != nil {
t.Fatal(err)
}
opt := OptIAForPrefixDelegation{}
opt.SetOptions(oaddr.ToBytes())
require.Equal(t, oaddr, opt.GetOneOption(OptionIAPrefix))
}
func TestOptIAForPrefixDelegationGetOneOptionMissingOpt(t *testing.T) {
buf := []byte{
0xaa, 0xbb, 0xcc, 0xdd, // preferredLifetime
0xee, 0xff, 0x00, 0x11, // validLifetime
36, // prefixLength
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // ipv6Prefix
}
oaddr, err := ParseOptIAPrefix(buf)
if err != nil {
t.Fatal(err)
}
opt := OptIAForPrefixDelegation{}
opt.SetOptions(oaddr.ToBytes())
require.Equal(t, nil, opt.GetOneOption(OptionDNSRecursiveNameServer))
}
func TestOptIAForPrefixDelegationDelOption(t *testing.T) {
optiana1 := OptIAForPrefixDelegation{}
optiana2 := OptIAForPrefixDelegation{}
optiaaddr := OptIAPrefix{}
optsc := OptStatusCode{}
var buf = make([]byte, 0)
buf = append(buf, optsc.ToBytes()...)
buf = append(buf, optiaaddr.ToBytes()...)
buf = append(buf, optiaaddr.ToBytes()...)
optiana1.SetOptions(buf)
optiana1.DelOption(OptionIAPrefix)
require.Equal(t, optiana1.Options(), optsc.ToBytes())
buf = make([]byte, 0)
buf = append(buf, optiaaddr.ToBytes()...)
buf = append(buf, optsc.ToBytes()...)
buf = append(buf, optiaaddr.ToBytes()...)
optiana2.SetOptions(buf)
optiana2.DelOption(OptionIAPrefix)
require.Equal(t, optiana2.Options(), optsc.ToBytes())
}
func TestOptIAForPrefixDelegationToBytes(t *testing.T) {
oaddr := OptIAPrefix{}
oaddr.SetPreferredLifetime(0xaabbccdd)
oaddr.SetValidLifetime(0xeeff0011)
oaddr.SetPrefixLength(36)
oaddr.SetIPv6Prefix([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})
opt := OptIAForPrefixDelegation{}
opt.SetIAID([4]byte{1, 2, 3, 4})
opt.SetT1(12345)
opt.SetT2(54321)
opt.SetOptions(oaddr.ToBytes())
expected := []byte{
0, 25, // OptionIAPD
0, 41, // length
1, 2, 3, 4, // IA ID
0, 0, 0x30, 0x39, // T1 = 12345
0, 0, 0xd4, 0x31, // T2 = 54321
0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime
0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime
36, // IAPrefix prefixLength
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
}
require.Equal(t, expected, opt.ToBytes())
}
func TestOptIAForPrefixDelegationSetOptionsTooShort(t *testing.T) {
buf := []byte{
0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime
0xee, 0xff, // truncated half-way through validLifetime
}
oaddr := OptIAForPrefixDelegation{}
err := oaddr.SetOptions(buf)
require.Error(t, err, "SetOptions() should return an error if invalid options are set")
}
func TestOptIAForPrefixDelegationString(t *testing.T) {
data := []byte{
1, 0, 0, 0, // IAID
0, 0, 0, 1, // T1
0, 0, 0, 2, // T2
0, 26, 0, 25, // 26 = IAPrefix Option, 25 = length
0xaa, 0xbb, 0xcc, 0xdd, // IAPrefix preferredLifetime
0xee, 0xff, 0x00, 0x11, // IAPrefix validLifetime
36, // IAPrefix prefixLength
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, // IAPrefix ipv6Prefix
}
opt, err := ParseOptIAForPrefixDelegation(data)
require.NoError(t, err)
str := opt.String()
require.Contains(
t, str,
"IAID=[1 0 0 0]",
"String() should return the IAID",
)
require.Contains(
t, str,
"t1=1, t2=2",
"String() should return the T1/T2 options",
)
require.Contains(
t, str,
"options=[",
"String() should return a list of options",
)
}
|