forked from brocaar/lorawan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphypayload_test.go
1124 lines (981 loc) · 30.1 KB
/
phypayload_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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package lorawan
import (
"database/sql/driver"
"encoding/base64"
"encoding/hex"
"fmt"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestAES128Key(t *testing.T) {
Convey("Given an empty AES128Key", t, func() {
var key AES128Key
Convey("When the value is [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8}", func() {
key = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8}
Convey("Then MarshalText returns 01020304050607080102030405060708", func() {
b, err := key.MarshalText()
So(err, ShouldBeNil)
So(string(b), ShouldEqual, "01020304050607080102030405060708")
})
Convey("Then Value returns the expected value", func() {
v, err := key.Value()
So(err, ShouldBeNil)
So(v, ShouldResemble, driver.Value(key[:]))
})
})
Convey("Given the string 01020304050607080102030405060708", func() {
str := "01020304050607080102030405060708"
Convey("Then UnmarshalText returns AES128Key{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8}", func() {
err := key.UnmarshalText([]byte(str))
So(err, ShouldBeNil)
So(key, ShouldResemble, AES128Key{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8})
})
})
Convey("Given []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}", func() {
b := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
Convey("Then Scan scans the value correctly.", func() {
So(key.Scan(b), ShouldBeNil)
So(key[:], ShouldResemble, b)
})
})
})
}
func TestMHDR(t *testing.T) {
Convey("Given an empty MHDR", t, func() {
var h MHDR
Convey("Then MarshalBinary returns []byte{0}", func() {
b, err := h.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, []byte{0})
})
Convey("Given MType=Proprietary, Major=LoRaWANR1", func() {
h.MType = Proprietary
h.Major = LoRaWANR1
Convey("Then MarshalBinary returns []byte{224}", func() {
b, err := h.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, []byte{224})
})
})
Convey("Given a slice []byte{224}", func() {
b := []byte{224}
Convey("Then UnmarshalBinary returns a MHDR with MType=Proprietary, Major=LoRaWANR1", func() {
err := h.UnmarshalBinary(b)
So(err, ShouldBeNil)
So(h, ShouldResemble, MHDR{MType: Proprietary, Major: LoRaWANR1})
})
})
})
}
func TestPHYPayloadMACPayloadLoRaWAN10(t *testing.T) {
Convey("Given a set of test for LoRaWAN 1.0", t, func() {
var fPort1 uint8 = 1
var fPort0 uint8
mac1 := MACCommand{
CID: LinkCheckReq,
Payload: nil,
}
mac2 := MACCommand{
CID: LinkADRAns,
Payload: &LinkADRAnsPayload{
ChannelMaskACK: true,
DataRateACK: false,
PowerACK: true,
},
}
testTable := []struct {
Name string
PHYPayload PHYPayload
NwkSEncKey AES128Key
AppSKey AES128Key
Bytes []byte
}{
{
Name: "FRMPayload data",
PHYPayload: PHYPayload{
MHDR: MHDR{
MType: UnconfirmedDataUp,
Major: LoRaWANR1,
},
MACPayload: &MACPayload{
FHDR: FHDR{
DevAddr: DevAddr{1, 2, 3, 4},
FCtrl: FCtrl{
ADR: true,
},
FCnt: 1,
},
FPort: &fPort1,
FRMPayload: []Payload{
&DataPayload{Bytes: []byte("hello")},
},
},
MIC: MIC{214, 195, 181, 130},
},
NwkSEncKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
AppSKey: AES128Key{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
Bytes: []byte{64, 4, 3, 2, 1, 128, 1, 0, 1, 166, 148, 100, 38, 21, 214, 195, 181, 130},
},
{
Name: "Mac-commands in FOpts",
PHYPayload: PHYPayload{
MHDR: MHDR{
MType: UnconfirmedDataUp,
Major: LoRaWANR1,
},
MACPayload: &MACPayload{
FHDR: FHDR{
DevAddr: DevAddr{1, 2, 3, 4},
FOpts: []Payload{
&mac1,
&mac2,
},
},
FPort: &fPort1,
FRMPayload: []Payload{
&DataPayload{Bytes: []byte{1, 2, 3, 4}},
},
},
MIC: MIC{182, 77, 192, 57},
},
NwkSEncKey: AES128Key{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
AppSKey: AES128Key{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
Bytes: []byte{64, 4, 3, 2, 1, 3, 0, 0, 2, 3, 5, 1, 106, 55, 152, 245, 182, 77, 192, 57},
},
{
Name: "Mac-commands in FRMPayload",
PHYPayload: PHYPayload{
MHDR: MHDR{
MType: UnconfirmedDataUp,
Major: LoRaWANR1,
},
MACPayload: &MACPayload{
FPort: &fPort0,
FHDR: FHDR{
DevAddr: DevAddr{1, 2, 3, 4},
},
FRMPayload: []Payload{
&mac1,
&mac2,
},
},
MIC: MIC{238, 106, 165, 8},
},
NwkSEncKey: AES128Key{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
AppSKey: AES128Key{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
Bytes: []byte{64, 4, 3, 2, 1, 0, 0, 0, 0, 105, 54, 158, 238, 106, 165, 8},
},
}
for i, test := range testTable {
Convey(fmt.Sprintf("Testing: %s [%d]", test.Name, i), func() {
var phy PHYPayload
So(phy.UnmarshalBinary(test.Bytes), ShouldBeNil)
var ok bool
var err error
switch phy.MHDR.MType {
case UnconfirmedDataUp, ConfirmedDataUp:
ok, err = phy.ValidateUplinkDataMIC(LoRaWAN1_0, 0, 0, 0, test.NwkSEncKey, AES128Key{})
case UnconfirmedDataDown, ConfirmedDataDown:
ok, err = phy.ValidateDownlinkDataMIC(LoRaWAN1_0, 0, test.NwkSEncKey)
default:
t.Fatalf("unexpected MType %s", phy.MHDR.MType)
}
So(err, ShouldBeNil)
So(ok, ShouldBeTrue)
So(phy.DecodeFOptsToMACCommands(), ShouldBeNil)
So(phy.DecryptFRMPayload(test.AppSKey), ShouldBeNil)
if macPL, ok := phy.MACPayload.(*MACPayload); ok {
macPL.FHDR.FCtrl.fOptsLen = 0
}
So(phy, ShouldResemble, test.PHYPayload)
So(test.PHYPayload.EncryptFRMPayload(test.AppSKey), ShouldBeNil)
switch test.PHYPayload.MHDR.MType {
case UnconfirmedDataUp, ConfirmedDataUp:
err = test.PHYPayload.SetUplinkDataMIC(LoRaWAN1_0, 0, 0, 0, test.NwkSEncKey, AES128Key{})
case UnconfirmedDataDown, ConfirmedDataDown:
err = test.PHYPayload.SetDownlinkDataMIC(LoRaWAN1_0, 0, test.NwkSEncKey)
default:
t.Fatalf("unexpected MType %s", test.PHYPayload.MHDR.MType)
}
So(err, ShouldBeNil)
b, err := test.PHYPayload.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, test.Bytes)
})
}
})
}
func TestPHYPayloadMACPayloadLoRaWAN11(t *testing.T) {
Convey("Given a set of tests for LoRaWAN 1.1", t, func() {
var fPort1 uint8 = 1
var fPort0 uint8
mac1 := MACCommand{
CID: LinkCheckReq,
Payload: nil,
}
mac2 := MACCommand{
CID: LinkADRAns,
Payload: &LinkADRAnsPayload{
ChannelMaskACK: true,
DataRateACK: false,
PowerACK: true,
},
}
testTable := []struct {
Name string
PHYPayload PHYPayload
SNwkSIntKey AES128Key
FNwkSIntKey AES128Key
NwkSEncKey AES128Key
AppSKey AES128Key
Bytes []byte
EncryptFOpts bool
}{
{
Name: "FRMPayload data",
PHYPayload: PHYPayload{
MHDR: MHDR{
MType: UnconfirmedDataUp,
Major: LoRaWANR1,
},
MACPayload: &MACPayload{
FHDR: FHDR{
DevAddr: DevAddr{1, 2, 3, 4},
FCtrl: FCtrl{
ADR: true,
},
FCnt: 1,
},
FPort: &fPort1,
FRMPayload: []Payload{
&DataPayload{Bytes: []byte("hello")},
},
},
MIC: MIC{118, 18, 54, 106},
},
SNwkSIntKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
FNwkSIntKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3},
NwkSEncKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4},
AppSKey: AES128Key{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
Bytes: []byte{64, 4, 3, 2, 1, 128, 1, 0, 1, 166, 148, 100, 38, 21, 118, 18, 54, 106},
},
{
Name: "FRMPayload data with ACK (in this case the confirmed fCnt is used in the mic)",
PHYPayload: PHYPayload{
MHDR: MHDR{
MType: UnconfirmedDataUp,
Major: LoRaWANR1,
},
MACPayload: &MACPayload{
FHDR: FHDR{
DevAddr: DevAddr{1, 2, 3, 4},
FCtrl: FCtrl{
ADR: true,
ACK: true,
},
FCnt: 1,
},
FPort: &fPort1,
FRMPayload: []Payload{
&DataPayload{Bytes: []byte("hello")},
},
},
MIC: MIC{248, 66, 196, 185},
},
SNwkSIntKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
FNwkSIntKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3},
NwkSEncKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4},
AppSKey: AES128Key{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
Bytes: []byte{64, 4, 3, 2, 1, 160, 1, 0, 1, 166, 148, 100, 38, 21, 248, 66, 196, 185},
},
{
Name: "Mac-commands in FOpts (encrypted, using NFCntDown)",
PHYPayload: PHYPayload{
MHDR: MHDR{
MType: UnconfirmedDataDown,
Major: LoRaWANR1,
},
MACPayload: &MACPayload{
FHDR: FHDR{
DevAddr: DevAddr{1, 2, 3, 4},
FOpts: []Payload{
&MACCommand{
CID: LinkCheckAns,
Payload: &LinkCheckAnsPayload{
Margin: 7,
GwCnt: 1,
},
},
},
},
},
MIC: MIC{226, 79, 31, 159},
},
SNwkSIntKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
FNwkSIntKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3},
NwkSEncKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4},
Bytes: []byte{96, 4, 3, 2, 1, 3, 0, 0, 223, 180, 241, 226, 79, 31, 159},
EncryptFOpts: true,
},
{
Name: "Mac-commands in FOpts (encrypted, using AFCntDown encryption flag)",
PHYPayload: PHYPayload{
MHDR: MHDR{
MType: UnconfirmedDataDown,
Major: LoRaWANR1,
},
MACPayload: &MACPayload{
FHDR: FHDR{
DevAddr: DevAddr{1, 2, 3, 4},
FOpts: []Payload{
&MACCommand{
CID: LinkCheckAns,
Payload: &LinkCheckAnsPayload{
Margin: 7,
GwCnt: 1,
},
},
},
},
FPort: &fPort1,
},
MIC: MIC{119, 112, 30, 163},
},
SNwkSIntKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
FNwkSIntKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3},
NwkSEncKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4},
Bytes: []byte{96, 4, 3, 2, 1, 3, 0, 0, 2, 7, 1, 1, 119, 112, 30, 163},
},
{
Name: "Mac-commands in FRMPayload",
PHYPayload: PHYPayload{
MHDR: MHDR{
MType: UnconfirmedDataUp,
Major: LoRaWANR1,
},
MACPayload: &MACPayload{
FPort: &fPort0,
FHDR: FHDR{
DevAddr: DevAddr{1, 2, 3, 4},
},
FRMPayload: []Payload{
&mac1,
&mac2,
},
},
MIC: MIC{250, 147, 27, 215},
},
SNwkSIntKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
FNwkSIntKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3},
NwkSEncKey: AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4},
AppSKey: AES128Key{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
Bytes: []byte{64, 4, 3, 2, 1, 0, 0, 0, 0, 105, 54, 158, 250, 147, 27, 215},
},
}
for i, test := range testTable {
Convey(fmt.Sprintf("Testing: %s [%d]", test.Name, i), func() {
var phy PHYPayload
So(phy.UnmarshalBinary(test.Bytes), ShouldBeNil)
var ok bool
var err error
// test MIC
switch phy.MHDR.MType {
case UnconfirmedDataUp, ConfirmedDataUp:
ok, err = phy.ValidateUplinkDataMIC(LoRaWAN1_1, 1, 2, 3, test.FNwkSIntKey, test.SNwkSIntKey)
case UnconfirmedDataDown, ConfirmedDataDown:
ok, err = phy.ValidateDownlinkDataMIC(LoRaWAN1_1, 1, test.SNwkSIntKey)
default:
t.Fatalf("unexpected MType %s", phy.MHDR.MType)
}
So(err, ShouldBeNil)
if !ok {
var mic MIC
switch phy.MHDR.MType {
case UnconfirmedDataUp, ConfirmedDataUp:
mic, err = phy.calculateUplinkDataMIC(LoRaWAN1_1, 1, 2, 3, test.FNwkSIntKey, test.SNwkSIntKey)
case UnconfirmedDataDown, ConfirmedDataDown:
mic, err = phy.calculateDownlinkDataMIC(LoRaWAN1_1, 1, test.SNwkSIntKey)
default:
t.Fatalf("unexpected MType %s", phy.MHDR.MType)
}
fmt.Printf("expected mic: %s (%v)\n", mic, mic[:])
}
So(ok, ShouldBeTrue)
// test MICF
if phy.MHDR.MType == UnconfirmedDataUp || phy.MHDR.MType == ConfirmedDataUp {
ok, err := phy.ValidateUplinkDataMICF(test.FNwkSIntKey)
So(err, ShouldBeNil)
So(ok, ShouldBeTrue)
}
if test.EncryptFOpts {
So(phy.DecryptFOpts(test.NwkSEncKey), ShouldBeNil)
} else {
So(phy.DecodeFOptsToMACCommands(), ShouldBeNil)
}
So(phy.DecryptFRMPayload(test.AppSKey), ShouldBeNil)
if macPL, ok := phy.MACPayload.(*MACPayload); ok {
macPL.FHDR.FCtrl.fOptsLen = 0
}
So(phy, ShouldResemble, test.PHYPayload)
So(test.PHYPayload.EncryptFRMPayload(test.AppSKey), ShouldBeNil)
if test.EncryptFOpts {
So(test.PHYPayload.EncryptFOpts(test.NwkSEncKey), ShouldBeNil)
}
switch test.PHYPayload.MHDR.MType {
case UnconfirmedDataUp, ConfirmedDataUp:
err = test.PHYPayload.SetUplinkDataMIC(LoRaWAN1_1, 1, 2, 3, test.FNwkSIntKey, test.SNwkSIntKey)
case UnconfirmedDataDown, ConfirmedDataDown:
err = test.PHYPayload.SetDownlinkDataMIC(LoRaWAN1_1, 1, test.SNwkSIntKey)
default:
t.Fatalf("unexpected MType %s", test.PHYPayload.MHDR.MType)
}
So(err, ShouldBeNil)
b, err := test.PHYPayload.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, test.Bytes)
})
}
})
}
func TestPHYPayloadJoinRequest(t *testing.T) {
Convey("Given a set of known and an empty PHYPayload", t, func() {
data, err := base64.StdEncoding.DecodeString("AAQDAgEEAwIBBQQDAgUEAwItEGqZDhI=")
So(err, ShouldBeNil)
var phy PHYPayload
Convey("Then UnmarshalBinary does not fail", func() {
So(phy.UnmarshalBinary(data), ShouldBeNil)
Convey("Then the MHDR contains the expected data", func() {
So(phy.MHDR.MType, ShouldEqual, JoinRequest)
So(phy.MHDR.Major, ShouldEqual, LoRaWANR1)
})
Convey("Then the MIC is valid", func() {
appKey := [16]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
valid, err := phy.ValidateUplinkJoinMIC(appKey)
So(err, ShouldBeNil)
So(valid, ShouldBeTrue)
})
Convey("Then the MACPayload is of type *JoinRequestPayload", func() {
jrPl, ok := phy.MACPayload.(*JoinRequestPayload)
So(ok, ShouldBeTrue)
Convey("Then the JoinRequestPayload contains the expected data", func() {
So(jrPl.JoinEUI, ShouldResemble, EUI64{1, 2, 3, 4, 1, 2, 3, 4})
So(jrPl.DevEUI, ShouldResemble, EUI64{2, 3, 4, 5, 2, 3, 4, 5})
So(jrPl.DevNonce, ShouldEqual, 4141)
})
})
Convey("When marshalling the PHYPayload", func() {
b, err := phy.MarshalBinary()
So(err, ShouldBeNil)
Convey("Then it equals to the input data", func() {
So(b, ShouldResemble, data)
})
})
})
})
}
func TestPHYPayloadJoinAccept(t *testing.T) {
Convey("Given an empty PHYPayload with empty JoinAcceptPayload", t, func() {
p := PHYPayload{MACPayload: &JoinAcceptPayload{}}
Convey("Given an encrypted JoinAccept payload 20493eeb51fba2116f810edb3742975142", func() {
jaBytes, err := hex.DecodeString("20493eeb51fba2116f810edb3742975142")
So(err, ShouldBeNil)
Convey("Then UnmarshalBinary does not return an error", func() {
So(p.UnmarshalBinary(jaBytes), ShouldBeNil)
Convey("Given the AppKey 00112233445566778899aabbccddeeff", func() {
var appKey AES128Key
appKeyBytes, err := hex.DecodeString("00112233445566778899aabbccddeeff")
So(err, ShouldBeNil)
So(appKeyBytes, ShouldHaveLength, 16)
copy(appKey[:], appKeyBytes)
Convey("Then decrypting does not return an error", func() {
So(p.DecryptJoinAcceptPayload(appKey), ShouldBeNil)
Convey("Then the MACPayload is of type *JoinAcceptPayload", func() {
jaPL, ok := p.MACPayload.(*JoinAcceptPayload)
So(ok, ShouldBeTrue)
Convey("Then the JoinNonce is 5704647", func() {
So(jaPL.JoinNonce, ShouldEqual, JoinNonce(5704647))
})
Convey("Then the NetID is [3]byte{34, 17, 1}", func() {
So(jaPL.HomeNetID, ShouldEqual, NetID{34, 17, 1})
})
Convey("Then the DevAddr is [4]byte{2, 3, 25, 128}", func() {
So([4]byte(jaPL.DevAddr), ShouldEqual, [4]byte{2, 3, 25, 128})
})
Convey("Then the DLSettings is empty", func() {
So(jaPL.DLSettings, ShouldResemble, DLSettings{})
})
Convey("Then the RXDelay = 0", func() {
So(jaPL.RXDelay, ShouldEqual, 0)
})
})
Convey("Then the MIC is [4]byte{67, 72, 91, 188}", func() {
So(p.MIC, ShouldEqual, MIC{67, 72, 91, 188})
})
Convey("Then the MIC is valid", func() {
ok, err := p.ValidateDownlinkJoinMIC(JoinRequestType, EUI64{}, 0, appKey)
So(err, ShouldBeNil)
So(ok, ShouldBeTrue)
})
})
})
})
})
Convey("Given a JoinAccept with JoinNonce=5704647, NetID=[3]byte{34, 17, 1}, DevAddr=[4]byte{2, 3, 25, 128}", func() {
p.MHDR = MHDR{
MType: JoinAccept,
Major: LoRaWANR1,
}
p.MACPayload = &JoinAcceptPayload{
JoinNonce: 5704647,
HomeNetID: [3]byte{34, 17, 1},
DevAddr: [4]byte{2, 3, 25, 128},
}
Convey("Given the AppKey 00112233445566778899aabbccddeeff", func() {
var appKey AES128Key
appKeyBytes, err := hex.DecodeString("00112233445566778899aabbccddeeff")
So(err, ShouldBeNil)
So(appKeyBytes, ShouldHaveLength, 16)
copy(appKey[:], appKeyBytes)
Convey("Then SetMIC does not fail", func() {
So(p.SetDownlinkJoinMIC(JoinRequestType, EUI64{}, 0, appKey), ShouldBeNil)
Convey("Then the MIC is [4]byte{67, 72, 91, 188}", func() {
So(p.MIC, ShouldEqual, MIC{67, 72, 91, 188})
})
Convey("Then encrypting does not fail", func() {
So(p.EncryptJoinAcceptPayload(appKey), ShouldBeNil)
Convey("Then the hex representation of the packet is 20493eeb51fba2116f810edb3742975142", func() {
b, err := p.MarshalBinary()
So(err, ShouldBeNil)
So(hex.EncodeToString(b), ShouldEqual, "20493eeb51fba2116f810edb3742975142")
})
})
})
})
})
})
}
func TestPHYPayloadRejoinRequest02(t *testing.T) {
Convey("Given a PHYPayload and a key", t, func() {
phy := PHYPayload{
MHDR: MHDR{
MType: RejoinRequest,
Major: LoRaWANR1,
},
MACPayload: &RejoinRequestType02Payload{
RejoinType: 2,
NetID: NetID{1, 2, 3},
DevEUI: EUI64{1, 2, 3, 4, 5, 6, 7, 8},
RJCount0: 219,
},
}
var key AES128Key
Convey("Then SetMIC sets the expected MIC", func() {
So(phy.SetUplinkJoinMIC(key), ShouldBeNil)
So(phy.MIC, ShouldEqual, MIC{60, 134, 66, 174})
valid, err := phy.ValidateUplinkJoinMIC(key)
So(err, ShouldBeNil)
So(valid, ShouldBeTrue)
Convey("Then MarshalBinary returns the expected value", func() {
b, err := phy.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, []byte{192, 2, 3, 2, 1, 8, 7, 6, 5, 4, 3, 2, 1, 219, 0, 60, 134, 66, 174})
Convey("Then UnmarshalBinary returns the expected object", func() {
var newPhy PHYPayload
So(newPhy.UnmarshalBinary(b), ShouldBeNil)
So(newPhy, ShouldResemble, phy)
})
})
})
})
}
func TestPHYPayloadRejoinRequest1(t *testing.T) {
Convey("Given a PHYPayload and a key", t, func() {
phy := PHYPayload{
MHDR: MHDR{
MType: RejoinRequest,
Major: LoRaWANR1,
},
MACPayload: &RejoinRequestType1Payload{
RejoinType: 1,
JoinEUI: EUI64{1, 2, 3, 4, 5, 6, 7, 8},
DevEUI: EUI64{9, 10, 11, 12, 13, 14, 15, 16},
RJCount1: 219,
},
}
var key AES128Key
Convey("Then SetMIC sets the expected MIC", func() {
So(phy.SetUplinkJoinMIC(key), ShouldBeNil)
So(phy.MIC, ShouldEqual, MIC{234, 195, 16, 114})
Convey("Then MarshalBinary returns the expected value", func() {
b, err := phy.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, []byte{192, 1, 8, 7, 6, 5, 4, 3, 2, 1, 16, 15, 14, 13, 12, 11, 10, 9, 219, 0, 234, 195, 16, 114})
Convey("Then UnmarshalBinary returns the expected object", func() {
var newPhy PHYPayload
So(newPhy.UnmarshalBinary(b), ShouldBeNil)
So(newPhy, ShouldResemble, phy)
})
})
})
})
}
func ExamplePHYPayload_lorawan10Encode() {
nwkSKey := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
appSKey := [16]byte{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
fPort := uint8(10)
phy := PHYPayload{
MHDR: MHDR{
MType: ConfirmedDataUp,
Major: LoRaWANR1,
},
MACPayload: &MACPayload{
FHDR: FHDR{
DevAddr: DevAddr([4]byte{1, 2, 3, 4}),
FCtrl: FCtrl{
ADR: false,
ADRACKReq: false,
ACK: false,
},
FCnt: 0,
FOpts: []Payload{
&MACCommand{
CID: DevStatusAns,
Payload: &DevStatusAnsPayload{
Battery: 115,
Margin: 7,
},
},
},
},
FPort: &fPort,
FRMPayload: []Payload{&DataPayload{Bytes: []byte{1, 2, 3, 4}}},
},
}
if err := phy.EncryptFRMPayload(appSKey); err != nil {
panic(err)
}
if err := phy.SetUplinkDataMIC(LoRaWAN1_0, 0, 0, 0, nwkSKey, AES128Key{}); err != nil {
panic(err)
}
str, err := phy.MarshalText()
if err != nil {
panic(err)
}
bytes, err := phy.MarshalBinary()
if err != nil {
panic(err)
}
phyJSON, err := phy.MarshalJSON()
if err != nil {
panic(err)
}
fmt.Println(string(str))
fmt.Println(bytes)
fmt.Println(string(phyJSON))
// Output:
// gAQDAgEDAAAGcwcK4mTU9+EX0sA=
// [128 4 3 2 1 3 0 0 6 115 7 10 226 100 212 247 225 23 210 192]
// {"mhdr":{"mType":"ConfirmedDataUp","major":"LoRaWANR1"},"macPayload":{"fhdr":{"devAddr":"01020304","fCtrl":{"adr":false,"adrAckReq":false,"ack":false,"fPending":false,"classB":false},"fCnt":0,"fOpts":[{"cid":"DevStatusReq","payload":{"battery":115,"margin":7}}]},"fPort":10,"frmPayload":[{"bytes":"4mTU9w=="}]},"mic":"e117d2c0"}
}
func ExamplePHYPayload_lorawan10Decode() {
nwkSKey := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
appSKey := [16]byte{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
var phy PHYPayload
// use use UnmarshalBinary when decoding a byte-slice
if err := phy.UnmarshalText([]byte("gAQDAgEDAAAGcwcK4mTU9+EX0sA=")); err != nil {
panic(err)
}
ok, err := phy.ValidateUplinkDataMIC(LoRaWAN1_0, 0, 0, 0, nwkSKey, AES128Key{})
if err != nil {
panic(err)
}
if !ok {
panic("invalid mic")
}
if err := phy.DecodeFOptsToMACCommands(); err != nil {
panic(err)
}
phyJSON, err := phy.MarshalJSON()
if err != nil {
panic(err)
}
if err := phy.DecryptFRMPayload(appSKey); err != nil {
panic(err)
}
macPL, ok := phy.MACPayload.(*MACPayload)
if !ok {
panic("*MACPayload expected")
}
pl, ok := macPL.FRMPayload[0].(*DataPayload)
if !ok {
panic("*DataPayload expected")
}
fmt.Println(string(phyJSON))
fmt.Println(pl.Bytes)
// Output:
// {"mhdr":{"mType":"ConfirmedDataUp","major":"LoRaWANR1"},"macPayload":{"fhdr":{"devAddr":"01020304","fCtrl":{"adr":false,"adrAckReq":false,"ack":false,"fPending":false,"classB":false},"fCnt":0,"fOpts":[{"cid":"DevStatusReq","payload":{"battery":115,"margin":7}}]},"fPort":10,"frmPayload":[{"bytes":"4mTU9w=="}]},"mic":"e117d2c0"}
// [1 2 3 4]
}
func ExamplePHYPayload_lorawan11EncryptedFoptsEncode() {
sNwkSIntKey := [16]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
nwkSEncKey := [16]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2}
appSKey := [16]byte{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
var fport1 uint8 = 1
phy := PHYPayload{
MHDR: MHDR{
MType: UnconfirmedDataDown,
Major: LoRaWANR1,
},
MACPayload: &MACPayload{
FHDR: FHDR{
DevAddr: DevAddr{1, 2, 3, 4},
FOpts: []Payload{
&MACCommand{
CID: LinkCheckAns,
Payload: &LinkCheckAnsPayload{
Margin: 7,
GwCnt: 1,
},
},
},
},
FPort: &fport1,
FRMPayload: []Payload{
&DataPayload{Bytes: []byte{1, 2, 3, 4}},
},
},
}
if err := phy.EncryptFOpts(nwkSEncKey); err != nil {
panic(err)
}
if err := phy.EncryptFRMPayload(appSKey); err != nil {
panic(err)
}
if err := phy.SetDownlinkDataMIC(LoRaWAN1_1, 0, sNwkSIntKey); err != nil {
panic(err)
}
str, err := phy.MarshalText()
if err != nil {
panic(err)
}
bytes, err := phy.MarshalBinary()
if err != nil {
panic(err)
}
phyJSON, err := phy.MarshalJSON()
if err != nil {
panic(err)
}
fmt.Println(string(str))
fmt.Println(bytes)
fmt.Println(string(phyJSON))
// Output:
// YAQDAgEDAAAirAoB8LRo3ape0To=
// [96 4 3 2 1 3 0 0 34 172 10 1 240 180 104 221 170 94 209 58]
// {"mhdr":{"mType":"UnconfirmedDataDown","major":"LoRaWANR1"},"macPayload":{"fhdr":{"devAddr":"01020304","fCtrl":{"adr":false,"adrAckReq":false,"ack":false,"fPending":false,"classB":false},"fCnt":0,"fOpts":[{"bytes":"IqwK"}]},"fPort":1,"frmPayload":[{"bytes":"8LRo3Q=="}]},"mic":"aa5ed13a"}
}
func ExamplePHYPayload_lorawan11EncryptedFoptsDecode() {
sNwkSIntKey := [16]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
nwkSEncKey := [16]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2}
appSKey := [16]byte{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
var phy PHYPayload
if err := phy.UnmarshalText([]byte("YAQDAgEDAAAirAoB8LRo3ape0To=")); err != nil {
panic(err)
}
ok, err := phy.ValidateDownlinkDataMIC(LoRaWAN1_1, 0, sNwkSIntKey)
if err != nil {
panic(err)
}
if !ok {
panic("invalid mic")
}
if err := phy.DecryptFOpts(nwkSEncKey); err != nil {
panic(err)
}
if err := phy.DecryptFRMPayload(appSKey); err != nil {
panic(err)
}
phyJSON, err := phy.MarshalJSON()
if err != nil {
panic(err)
}
fmt.Println(string(phyJSON))
// Output:
// {"mhdr":{"mType":"UnconfirmedDataDown","major":"LoRaWANR1"},"macPayload":{"fhdr":{"devAddr":"01020304","fCtrl":{"adr":false,"adrAckReq":false,"ack":false,"fPending":false,"classB":false},"fCnt":0,"fOpts":[{"cid":"LinkCheckReq","payload":{"margin":7,"gwCnt":1}}]},"fPort":1,"frmPayload":[{"bytes":"AQIDBA=="}]},"mic":"aa5ed13a"}
}
func ExamplePHYPayload_proprietaryEncode() {
phy := PHYPayload{
MHDR: MHDR{
MType: Proprietary,
Major: LoRaWANR1,
},
MACPayload: &DataPayload{Bytes: []byte{5, 6, 7, 8, 9, 10}},
MIC: MIC{1, 2, 3, 4},
}
str, err := phy.MarshalText()
if err != nil {
panic(err)
}
bytes, err := phy.MarshalBinary()
if err != nil {
panic(err)
}
phyJSON, err := phy.MarshalJSON()
if err != nil {
panic(err)
}
fmt.Println(string(str))
fmt.Println(bytes)
fmt.Println(string(phyJSON))
// Output:
// 4AUGBwgJCgECAwQ=
// [224 5 6 7 8 9 10 1 2 3 4]
// {"mhdr":{"mType":"Proprietary","major":"LoRaWANR1"},"macPayload":{"bytes":"BQYHCAkK"},"mic":"01020304"}
}
func ExamplePHYPayload_proprietaryDecode() {
var phy PHYPayload
if err := phy.UnmarshalText([]byte("4AUGBwgJCgECAwQ=")); err != nil {
panic(err)
}
phyJSON, err := phy.MarshalJSON()
if err != nil {
panic(err)
}
pl, ok := phy.MACPayload.(*DataPayload)
if !ok {
panic("*DataPayload expected")
}
fmt.Println(phy.MIC)
fmt.Println(pl.Bytes)
fmt.Println(string(phyJSON))
// Output:
// 01020304
// [5 6 7 8 9 10]
// {"mhdr":{"mType":"Proprietary","major":"LoRaWANR1"},"macPayload":{"bytes":"BQYHCAkK"},"mic":"01020304"}
}
func ExamplePHYPayload_joinRequest() {
appKey := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
phy := PHYPayload{
MHDR: MHDR{
MType: JoinRequest,
Major: LoRaWANR1,
},
MACPayload: &JoinRequestPayload{
JoinEUI: [8]byte{1, 1, 1, 1, 1, 1, 1, 1},
DevEUI: [8]byte{2, 2, 2, 2, 2, 2, 2, 2},
DevNonce: 771,
},
}
if err := phy.SetUplinkJoinMIC(appKey); err != nil {
panic(err)
}
str, err := phy.MarshalText()
if err != nil {
panic(err)
}
bytes, err := phy.MarshalBinary()
if err != nil {