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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
package dhcpv6
import (
"encoding/binary"
"errors"
"net"
"strconv"
"testing"
"github.com/insomniacslk/dhcp/iana"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/u-root/uio/rand"
)
func randomReadMock(value []byte, n int, err error) func([]byte) (int, error) {
return func(b []byte) (int, error) {
copy(b, value)
return n, err
}
}
type GenerateTransactionIDTestSuite struct {
suite.Suite
random []byte
}
func (s *GenerateTransactionIDTestSuite) SetupTest() {
s.random = make([]byte, 16)
}
func (s *GenerateTransactionIDTestSuite) TearDown() {
randomRead = rand.Read
}
func (s *GenerateTransactionIDTestSuite) TestErrors() {
// Error is returned from random number generator
e := errors.New("mocked error")
randomRead = randomReadMock(s.random, 0, e)
_, err := GenerateTransactionID()
s.Assert().Equal(e, err)
// Less than 4 bytes are generated
randomRead = randomReadMock(s.random, 2, nil)
_, err = GenerateTransactionID()
s.Assert().EqualError(err, "invalid random sequence: shorter than 3 bytes")
}
func (s *GenerateTransactionIDTestSuite) TestSuccess() {
binary.BigEndian.PutUint32(s.random, 0x01020300)
randomRead = randomReadMock(s.random, 3, nil)
tid, err := GenerateTransactionID()
s.Require().NoError(err)
s.Assert().Equal(TransactionID{0x1, 0x2, 0x3}, tid)
}
func TestGenerateTransactionIDTestSuite(t *testing.T) {
suite.Run(t, new(GenerateTransactionIDTestSuite))
}
func TestNewMessage(t *testing.T) {
d, err := NewMessage()
require.NoError(t, err)
require.NotNil(t, d)
require.Equal(t, MessageTypeSolicit, d.Type())
require.NotEqual(t, 0, d.TransactionID)
require.Empty(t, d.Options)
}
func TestDecapsulateRelayIndex(t *testing.T) {
m := Message{}
r1, err := EncapsulateRelay(&m, MessageTypeRelayForward, net.IPv6linklocalallnodes, net.IPv6interfacelocalallnodes)
require.NoError(t, err)
r2, err := EncapsulateRelay(r1, MessageTypeRelayForward, net.IPv6loopback, net.IPv6linklocalallnodes)
require.NoError(t, err)
r3, err := EncapsulateRelay(r2, MessageTypeRelayForward, net.IPv6unspecified, net.IPv6linklocalallrouters)
require.NoError(t, err)
first, err := DecapsulateRelayIndex(r3, 0)
require.NoError(t, err)
relay, ok := first.(*RelayMessage)
require.True(t, ok)
require.Equal(t, relay.HopCount, uint8(1))
require.Equal(t, relay.LinkAddr, net.IPv6loopback)
require.Equal(t, relay.PeerAddr, net.IPv6linklocalallnodes)
second, err := DecapsulateRelayIndex(r3, 1)
require.NoError(t, err)
relay, ok = second.(*RelayMessage)
require.True(t, ok)
require.Equal(t, relay.HopCount, uint8(0))
require.Equal(t, relay.LinkAddr, net.IPv6linklocalallnodes)
require.Equal(t, relay.PeerAddr, net.IPv6interfacelocalallnodes)
third, err := DecapsulateRelayIndex(r3, 2)
require.NoError(t, err)
_, ok = third.(*Message)
require.True(t, ok)
rfirst, err := DecapsulateRelayIndex(r3, -1)
require.NoError(t, err)
relay, ok = rfirst.(*RelayMessage)
require.True(t, ok)
require.Equal(t, relay.HopCount, uint8(0))
require.Equal(t, relay.LinkAddr, net.IPv6linklocalallnodes)
require.Equal(t, relay.PeerAddr, net.IPv6interfacelocalallnodes)
_, err = DecapsulateRelayIndex(r3, -2)
require.Error(t, err)
}
func TestAddOption(t *testing.T) {
d := Message{}
require.Empty(t, d.Options)
opt := OptionGeneric{OptionCode: 0, OptionData: []byte{}}
d.AddOption(&opt)
require.Equal(t, Options{&opt}, d.Options.Options)
}
func TestToBytes(t *testing.T) {
d := Message{
MessageType: MessageTypeSolicit,
TransactionID: TransactionID{0xa, 0xb, 0xc},
}
d.AddOption(&OptionGeneric{OptionCode: 0, OptionData: []byte{}})
bytes := d.ToBytes()
expected := []byte{01, 0xa, 0xb, 0xc, 0x00, 0x00, 0x00, 0x00}
require.Equal(t, expected, bytes)
}
func TestFromAndToBytes(t *testing.T) {
expected := [][]byte{
{01, 0xab, 0xcd, 0xef, 0x00, 0x00, 0x00, 0x00},
[]byte("0000\x00\x01\x00\x0e\x00\x01000000000000"),
}
t.Parallel()
for i, packet := range expected {
t.Run(strconv.Itoa(i), func(t *testing.T) {
d, err := FromBytes(packet)
require.NoError(t, err)
toBytes := d.ToBytes()
require.Equal(t, packet, toBytes)
})
}
}
func TestFromBytesInvalid(t *testing.T) {
expected := [][]byte{
{},
{30},
{12},
}
t.Parallel()
for i, packet := range expected {
t.Run(strconv.Itoa(i), func(t *testing.T) {
_, err := FromBytes(packet)
require.Error(t, err)
})
}
}
func TestNewAdvertiseFromSolicit(t *testing.T) {
s := Message{
MessageType: MessageTypeSolicit,
TransactionID: TransactionID{0xa, 0xb, 0xc},
}
s.AddOption(OptClientID(&DUIDLLT{}))
a, err := NewAdvertiseFromSolicit(&s, WithServerID(&DUIDLLT{}))
require.NoError(t, err)
require.Equal(t, a.TransactionID, s.TransactionID)
require.Equal(t, a.Type(), MessageTypeAdvertise)
}
func TestNewReplyFromMessage(t *testing.T) {
msg := Message{
TransactionID: TransactionID{0xa, 0xb, 0xc},
MessageType: MessageTypeConfirm,
}
var duid DUIDLLT
msg.AddOption(OptClientID(&duid))
msg.AddOption(OptServerID(&duid))
rep, err := NewReplyFromMessage(&msg, WithServerID(&duid))
require.NoError(t, err)
require.Equal(t, rep.TransactionID, msg.TransactionID)
require.Equal(t, rep.Type(), MessageTypeReply)
msg.MessageType = MessageTypeRenew
rep, err = NewReplyFromMessage(&msg, WithServerID(&duid))
require.NoError(t, err)
require.Equal(t, rep.TransactionID, msg.TransactionID)
require.Equal(t, rep.Type(), MessageTypeReply)
msg.MessageType = MessageTypeRebind
rep, err = NewReplyFromMessage(&msg, WithServerID(&duid))
require.NoError(t, err)
require.Equal(t, rep.TransactionID, msg.TransactionID)
require.Equal(t, rep.Type(), MessageTypeReply)
msg.MessageType = MessageTypeRelease
rep, err = NewReplyFromMessage(&msg, WithServerID(&duid))
require.NoError(t, err)
require.Equal(t, rep.TransactionID, msg.TransactionID)
require.Equal(t, rep.Type(), MessageTypeReply)
msg.MessageType = MessageTypeInformationRequest
rep, err = NewReplyFromMessage(&msg, WithServerID(&duid))
require.NoError(t, err)
require.Equal(t, rep.TransactionID, msg.TransactionID)
require.Equal(t, rep.Type(), MessageTypeReply)
msg.MessageType = MessageTypeSolicit
_, err = NewReplyFromMessage(&msg)
require.Error(t, err)
msg.MessageType = MessageTypeSolicit
msg.AddOption(&OptionGeneric{OptionCode: OptionRapidCommit})
_, err = NewReplyFromMessage(&msg)
require.NoError(t, err)
msg.Options.Del(OptionRapidCommit)
}
func TestNewMessageTypeSolicit(t *testing.T) {
hwAddr, err := net.ParseMAC("24:0A:9E:9F:EB:2B")
require.NoError(t, err)
duid := &DUIDLL{
HWType: iana.HWTypeEthernet,
LinkLayerAddr: hwAddr,
}
s, err := NewSolicit(hwAddr, WithClientID(duid))
require.NoError(t, err)
require.Equal(t, s.Type(), MessageTypeSolicit)
// Check CID
cduid := s.Options.ClientID()
require.NotNil(t, cduid)
require.Equal(t, cduid, duid)
// Check ORO
oro := s.Options.RequestedOptions()
require.Contains(t, oro, OptionDNSRecursiveNameServer)
require.Contains(t, oro, OptionDomainSearchList)
require.Equal(t, len(oro), 2)
// Check IA_NA
iaid := [4]byte{hwAddr[2], hwAddr[3], hwAddr[4], hwAddr[5]}
iana := s.Options.OneIANA()
require.NotNil(t, iana)
require.Equal(t, iaid, iana.IaId)
}
func TestGetTransactionIDMessage(t *testing.T) {
message, err := NewMessage()
require.NoError(t, err)
transactionID, err := GetTransactionID(message)
require.NoError(t, err)
require.Equal(t, transactionID, message.TransactionID)
}
func TestGetTransactionIDRelay(t *testing.T) {
message, err := NewMessage()
require.NoError(t, err)
relay, err := EncapsulateRelay(message, MessageTypeRelayForward, nil, nil)
require.NoError(t, err)
transactionID, err := GetTransactionID(relay)
require.NoError(t, err)
require.Equal(t, transactionID, message.TransactionID)
}
// TODO test NewMessageTypeSolicit
// test String and Summary
|