-
Notifications
You must be signed in to change notification settings - Fork 157
/
fhdr_test.go
408 lines (374 loc) · 12.8 KB
/
fhdr_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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package lorawan
import (
"bytes"
"errors"
"fmt"
"log"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestDevAddr(t *testing.T) {
Convey("Given a set of tests", t, func() {
tests := []struct {
Name string
DevAddr DevAddr
NetIDType int
NwkID []byte
Bytes []byte
String string
}{
{
Name: "NetID type 0",
DevAddr: DevAddr{91, 255, 255, 255},
NetIDType: 0,
NwkID: []byte{45},
Bytes: []byte{255, 255, 255, 91},
String: "5bffffff",
},
{
Name: "NetID type 1",
DevAddr: DevAddr{173, 255, 255, 255},
NetIDType: 1,
NwkID: []byte{45},
Bytes: []byte{255, 255, 255, 173},
String: "adffffff",
},
{
Name: "NetID type 2",
DevAddr: DevAddr{214, 223, 255, 255},
NetIDType: 2,
NwkID: []byte{1, 109},
Bytes: []byte{255, 255, 223, 214},
String: "d6dfffff",
},
{
Name: "NetID type 3",
DevAddr: DevAddr{235, 111, 255, 255},
NetIDType: 3,
NwkID: []byte{5, 183},
Bytes: []byte{255, 255, 111, 235},
String: "eb6fffff",
},
{
Name: "NetID type 4",
DevAddr: DevAddr{245, 182, 255, 255},
NetIDType: 4,
NwkID: []byte{11, 109},
Bytes: []byte{255, 255, 182, 245},
String: "f5b6ffff",
},
{
Name: "NetID type 5",
DevAddr: DevAddr{250, 219, 127, 255},
NetIDType: 5,
NwkID: []byte{22, 219},
Bytes: []byte{255, 127, 219, 250},
String: "fadb7fff",
},
{
Name: "NetID type 6",
DevAddr: DevAddr{253, 109, 183, 255},
NetIDType: 6,
NwkID: []byte{91, 109},
Bytes: []byte{255, 183, 109, 253},
String: "fd6db7ff",
},
{
Name: "NetID type 7",
DevAddr: DevAddr{254, 182, 219, 127},
NetIDType: 7,
NwkID: []byte{1, 109, 182},
Bytes: []byte{127, 219, 182, 254},
String: "feb6db7f",
},
}
for i, test := range tests {
Convey(fmt.Sprintf("Testing: %s [%d]", test.Name, i), func() {
So(test.DevAddr.NetIDType(), ShouldEqual, test.NetIDType)
So(test.DevAddr.NwkID(), ShouldResemble, test.NwkID)
b, err := test.DevAddr.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, test.Bytes)
var devAddr DevAddr
So(devAddr.UnmarshalBinary(test.Bytes), ShouldBeNil)
So(devAddr, ShouldResemble, test.DevAddr)
b, err = test.DevAddr.MarshalText()
So(err, ShouldBeNil)
So(string(b), ShouldEqual, test.String)
So(devAddr.UnmarshalText([]byte(test.String)), ShouldBeNil)
So(devAddr, ShouldEqual, test.DevAddr)
})
}
})
}
func TestDevAddrSetAddrPrefix(t *testing.T) {
Convey("Given a set of tests", t, func() {
tests := []struct {
Name string
BeforeDevAddr DevAddr
NetID []NetID
ExpectedDevAddr []DevAddr
}{
{
Name: "Set type 0 prefix",
BeforeDevAddr: DevAddr{255, 255, 255, 255},
NetID: []NetID{{0, 0, 0}, {0, 0, 63}},
ExpectedDevAddr: []DevAddr{{1, 255, 255, 255}, {127, 255, 255, 255}},
},
{
Name: "Set type 1 prefix",
BeforeDevAddr: DevAddr{255, 255, 255, 255},
NetID: []NetID{{32, 0, 0}, {32, 0, 63}},
ExpectedDevAddr: []DevAddr{{128, 255, 255, 255}, {191, 255, 255, 255}},
},
{
Name: "Set type 2 prefix",
BeforeDevAddr: DevAddr{255, 255, 255, 255},
NetID: []NetID{{64, 0, 0}, {64, 1, 255}},
ExpectedDevAddr: []DevAddr{{192, 15, 255, 255}, {223, 255, 255, 255}},
},
{
Name: "Set type 3 prefix",
BeforeDevAddr: DevAddr{255, 255, 255, 255},
NetID: []NetID{{96, 0, 0}, {111, 255, 255}},
ExpectedDevAddr: []DevAddr{{224, 1, 255, 255}, {239, 255, 255, 255}},
},
{
Name: "Set type 4 prefix",
BeforeDevAddr: DevAddr{255, 255, 255, 255},
NetID: []NetID{{128, 0, 0}, {159, 255, 255}},
ExpectedDevAddr: []DevAddr{{240, 0, 127, 255}, {247, 255, 255, 255}},
},
{
Name: "Set type 5 prefix",
BeforeDevAddr: DevAddr{255, 255, 255, 255},
NetID: []NetID{{160, 0, 0}, {191, 255, 255}},
ExpectedDevAddr: []DevAddr{{248, 0, 31, 255}, {251, 255, 255, 255}},
},
{
Name: "Set type 6 prefix",
BeforeDevAddr: DevAddr{255, 255, 255, 255},
NetID: []NetID{{192, 0, 0}, {223, 255, 255}},
ExpectedDevAddr: []DevAddr{{252, 0, 3, 255}, {253, 255, 255, 255}},
},
{
Name: "Set type 7 prefix",
BeforeDevAddr: DevAddr{255, 255, 255, 255},
NetID: []NetID{{224, 0, 0}, {255, 255, 255}},
ExpectedDevAddr: []DevAddr{{254, 0, 0, 127}, {254, 255, 255, 255}},
},
}
for i, test := range tests {
Convey(fmt.Sprintf("Testing: %s [%d]", test.Name, i), func() {
for i := range test.NetID {
So(test.BeforeDevAddr.IsNetID(test.NetID[i]), ShouldBeFalse)
test.BeforeDevAddr.SetAddrPrefix(test.NetID[i])
So(test.BeforeDevAddr, ShouldEqual, test.ExpectedDevAddr[i])
So(test.BeforeDevAddr.IsNetID(test.NetID[i]), ShouldBeTrue)
}
})
}
})
}
func TestFCtrl(t *testing.T) {
Convey("Given a set of tests", t, func() {
testTable := []struct {
FCtrl FCtrl
ExpectedBytes []byte
ExpectedError error
}{
{
FCtrl: FCtrl{fOptsLen: 16},
ExpectedError: errors.New("lorawan: max value of FOptsLen is 15"),
},
{
FCtrl: FCtrl{ADR: true, ADRACKReq: false, ACK: false, FPending: false, fOptsLen: 2},
ExpectedBytes: []byte{130},
},
{
FCtrl: FCtrl{ADR: false, ADRACKReq: true, ACK: false, FPending: false, fOptsLen: 3},
ExpectedBytes: []byte{67},
},
{
FCtrl: FCtrl{ADR: false, ADRACKReq: false, ACK: true, FPending: false, fOptsLen: 4},
ExpectedBytes: []byte{36},
},
{
FCtrl: FCtrl{ADR: false, ADRACKReq: false, ACK: false, FPending: true, fOptsLen: 5},
ExpectedBytes: []byte{21},
},
{
FCtrl: FCtrl{ADR: false, ADRACKReq: false, ACK: false, ClassB: true, fOptsLen: 5},
ExpectedBytes: []byte{21},
},
{
FCtrl: FCtrl{ADR: true, ADRACKReq: true, ACK: true, FPending: true, fOptsLen: 6},
ExpectedBytes: []byte{246},
},
{
FCtrl: FCtrl{ADR: true, ADRACKReq: true, ACK: true, ClassB: true, fOptsLen: 6},
ExpectedBytes: []byte{246},
},
}
for i, test := range testTable {
Convey(fmt.Sprintf("Testing: %+v [%d]", test.FCtrl, i), func() {
b, err := test.FCtrl.MarshalBinary()
if test.ExpectedError != nil {
Convey("Then the expected error is returned", func() {
So(err, ShouldNotBeNil)
So(err, ShouldResemble, test.ExpectedError)
})
return
}
So(err, ShouldBeNil)
So(b, ShouldResemble, test.ExpectedBytes)
Convey("Then unmarshal and marshal results in the same byteslice", func() {
var fCtrl FCtrl
So(fCtrl.UnmarshalBinary(b), ShouldBeNil)
b2, err := fCtrl.MarshalBinary()
So(err, ShouldBeNil)
So(b2, ShouldResemble, b)
})
})
}
})
}
func TestFHDR(t *testing.T) {
Convey("Given an empty FHDR", t, func() {
var h FHDR
Convey("Then MarshalBinary returns []byte{0, 0, 0, 0, 0, 0, 0}", func() {
b, err := h.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, []byte{0, 0, 0, 0, 0, 0, 0})
})
Convey("Given the FCnt contains a value > 16 bits", func() {
h.FCnt = 65795
Convey("Then only the least-significant 16 bits are marshalled", func() {
b, err := h.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, []byte{0, 0, 0, 0, 0, 3, 1})
})
})
Convey("Given DevAddr=67305985, FCtrl=FCtrl(ADR=true, ADRACKReq=false, ACK=true, FPending=true), Fcnt=5, FOpts=[]MACCommand{(CID=LinkCheckAns, Payload=LinkCheckAnsPayload(Margin=7, GwCnt=9))}", func() {
h.DevAddr = DevAddr([4]byte{1, 2, 3, 4})
h.FCtrl = FCtrl{ADR: true, ADRACKReq: false, ACK: true, FPending: true}
h.FCnt = 5
h.FOpts = []Payload{
&MACCommand{CID: LinkCheckAns, Payload: &LinkCheckAnsPayload{Margin: 7, GwCnt: 9}},
}
Convey("Then MarshalBinary returns []byte{4, 3, 2, 1, 179, 5, 0, 2, 7, 9}", func() {
b, err := h.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, []byte{4, 3, 2, 1, 179, 5, 0, 2, 7, 9})
})
})
Convey("Given FOpts contains 5 times MACCommand{(CID=LinkCheckAns, Payload=LinkCheckAnsPayload(Margin=7, GwCnt=9))}", func() {
for i := 0; i < 5; i++ {
h.FOpts = append(h.FOpts, &MACCommand{CID: LinkCheckAns, Payload: &LinkCheckAnsPayload{Margin: 7, GwCnt: 9}})
}
Convey("Then MarshalBinary does not return an error", func() {
_, err := h.MarshalBinary()
So(err, ShouldBeNil)
})
})
Convey("Given FOpts contains 6 times MACCommand{(CID=LinkCheckAns, Payload=LinkCheckAnsPayload(Margin=7, GwCnt=9))}", func() {
for i := 0; i < 6; i++ {
h.FOpts = append(h.FOpts, &MACCommand{CID: LinkCheckAns, Payload: &LinkCheckAnsPayload{Margin: 7, GwCnt: 9}})
}
Convey("Then MarshalBinary does return an error", func() {
_, err := h.MarshalBinary()
So(err, ShouldResemble, errors.New("lorawan: max number of FOpts bytes is 15"))
})
})
Convey("Given uplink=false and slice []byte{4, 3, 2, 1, 179, 5, 0, 2, 7, 9}", func() {
b := []byte{4, 3, 2, 1, 179, 5, 0, 2, 7, 9}
Convey("Then UnmarshalBinary does not return an error", func() {
err := h.UnmarshalBinary(false, b)
So(err, ShouldBeNil)
h.FOpts, err = decodeDataPayloadToMACCommands(false, h.FOpts)
So(err, ShouldBeNil)
Convey("Then DevAddr=[4]{1, 2, 3, 4}", func() {
So(h.DevAddr, ShouldEqual, DevAddr([4]byte{1, 2, 3, 4}))
})
Convey("Then FCtrl=FCtrl(ADR=true, ADRACKReq=false, ACK=true, FPending=true, fOptsLen=3)", func() {
So(h.FCtrl, ShouldResemble, FCtrl{ADR: true, ADRACKReq: false, ACK: true, FPending: true, ClassB: true, fOptsLen: 3})
})
Convey("Then len(FOpts)=1", func() {
So(h.FOpts, ShouldHaveLength, 1)
Convey("Then CID=LinkCheckAns", func() {
So(h.FOpts[0].(*MACCommand).CID, ShouldEqual, LinkCheckAns)
})
})
Convey("Then Payload=LinkCheckAnsPayload(Margin=7, GwCnt=9)", func() {
p, ok := h.FOpts[0].(*MACCommand).Payload.(*LinkCheckAnsPayload)
So(ok, ShouldBeTrue)
So(p, ShouldResemble, &LinkCheckAnsPayload{Margin: 7, GwCnt: 9})
})
})
})
Convey("Given uplink=false and slice []byte{4, 3, 2, 1, 181, 5, 0, 2, 7, 9, 78, 79} (one known mac-command and some unknown data)", func() {
b := []byte{4, 3, 2, 1, 181, 5, 0, 2, 7, 9, 78, 79}
var logBytes bytes.Buffer
log.SetOutput(&logBytes)
Convey("Then UnmarshalBinary does not return an error", func() {
err := h.UnmarshalBinary(false, b)
So(err, ShouldBeNil)
h.FOpts, err = decodeDataPayloadToMACCommands(false, h.FOpts)
So(err, ShouldBeNil)
Convey("Then DevAddr=[4]{1, 2, 3, 4}", func() {
So(h.DevAddr, ShouldEqual, DevAddr([4]byte{1, 2, 3, 4}))
})
Convey("Then FCtrl=FCtrl(ADR=true, ADRACKReq=false, ACK=true, FPending=true, fOptsLen=5)", func() {
So(h.FCtrl, ShouldResemble, FCtrl{ADR: true, ADRACKReq: false, ACK: true, FPending: true, ClassB: true, fOptsLen: 5})
})
Convey("Then len(FOpts)=3", func() {
So(h.FOpts, ShouldHaveLength, 3)
Convey("Then CID=LinkCheckAns", func() {
So(h.FOpts[0].(*MACCommand).CID, ShouldEqual, LinkCheckAns)
})
})
Convey("Then the remaining mac data is still available", func() {
So(h.FOpts[1].(*MACCommand).CID, ShouldEqual, 78)
So(h.FOpts[2].(*MACCommand).CID, ShouldEqual, 79)
})
Convey("Then Payload=LinkCheckAnsPayload(Margin=7, GwCnt=9)", func() {
p, ok := h.FOpts[0].(*MACCommand).Payload.(*LinkCheckAnsPayload)
So(ok, ShouldBeTrue)
So(p, ShouldResemble, &LinkCheckAnsPayload{Margin: 7, GwCnt: 9})
})
})
})
Convey("Given uplink=false and slice []byte{1, 2, 3, 4, 179, 5, 0, 2, 7}", func() {
b := []byte{1, 2, 3, 4, 179, 5, 0, 2, 7}
Convey("Then UnmarshalBinary returns an error", func() {
err := h.UnmarshalBinary(false, b)
So(err, ShouldBeNil)
h.FOpts, err = decodeDataPayloadToMACCommands(false, h.FOpts)
So(err, ShouldResemble, errors.New("lorawan: not enough remaining bytes"))
})
})
Convey("Given FOpts with a MACCommand with non-empty NewChannelReqPayload", func() {
m := MACCommand{
CID: NewChannelReq,
Payload: &NewChannelReqPayload{
ChIndex: 2,
Freq: 868100000,
MaxDR: 5,
MinDR: 1,
},
}
h.FOpts = []Payload{&m}
Convey("When it is transformed into binary", func() {
b, err := h.MarshalBinary()
So(err, ShouldBeNil)
Convey("Then it can be converted back to the original payload", func() {
actual := FHDR{}
So(actual.UnmarshalBinary(false, b), ShouldBeNil)
actual.FOpts, err = decodeDataPayloadToMACCommands(false, actual.FOpts)
So(err, ShouldBeNil)
So(actual.FOpts, ShouldResemble, []Payload{&m})
})
})
})
})
}