forked from brocaar/lorawan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macpayload_test.go
167 lines (141 loc) · 5.53 KB
/
macpayload_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
package lorawan
import (
"errors"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestMACPayload(t *testing.T) {
Convey("Given an empty MACPayload", t, func() {
var p MACPayload
Convey("Then MarshalBinary returns []byte{0, 0, 0, 0, 0, 0, 0}", func() {
b, err := p.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, []byte{0, 0, 0, 0, 0, 0, 0})
})
Convey("Given FPort=1", func() {
fPort := uint8(1)
p.FPort = &fPort
Convey("Given FRMPayload contains a MACCommand", func() {
p.FRMPayload = []Payload{&MACCommand{CID: LinkCheckReq}}
Convey("Then MarshalBinary returns an error that FPort must be 0", func() {
_, err := p.MarshalBinary()
So(err, ShouldResemble, errors.New("lorawan: a MAC command is only allowed when FPort=0"))
})
})
})
Convey("Given FPort=nil", func() {
Convey("Given FRMPayload is not empty", func() {
p.FRMPayload = []Payload{&DataPayload{Bytes: []byte{1}}}
Convey("Then MarshalBinary returns an error that FPort must be set", func() {
_, err := p.MarshalBinary()
So(err, ShouldResemble, errors.New("lorawan: FPort must be set when FRMPayload is not empty"))
})
})
})
Convey("Given FPort=0", func() {
fPort := uint8(0)
p.FPort = &fPort
Convey("Given FOpts are set", func() {
p.FHDR.FOpts = []MACCommand{{CID: LinkCheckReq}}
Convey("Then MarshalBinary returns an error that FPort must not be 0", func() {
_, err := p.MarshalBinary()
So(err, ShouldResemble, errors.New("lorawan: FPort must not be 0 when FOpts are set"))
})
})
})
Convey("Given FHDR(DevAddr=[4]{1, 2, 3, 4}), FPort=1, FRMPayload=[]Payload{DataPayload(Bytes=[]byte{5, 6, 7})}", func() {
p.FHDR.DevAddr = DevAddr([4]byte{1, 2, 3, 4})
fPort := uint8(1)
p.FPort = &fPort
p.FRMPayload = []Payload{&DataPayload{[]byte{5, 6, 7}}}
Convey("Then MarshalBinary returns []byte{4, 3, 2, 1, 0, 0, 0, 1, 5, 6, 7}", func() {
b, err := p.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, []byte{4, 3, 2, 1, 0, 0, 0, 1, 5, 6, 7})
})
})
Convey("Given uplink=true, FHDR(DevAddr=[4]{1, 2, 3, 4}), FPort=0, FRMPayload=[]Payload{MACCommand{CID: DevStatusAns, Payload: DevStatusAnsPayload(Battery=10, Margin=20)}}", func() {
p.FHDR.DevAddr = DevAddr([4]byte{1, 2, 3, 4})
fPort := uint8(0)
p.FPort = &fPort
p.FRMPayload = []Payload{&MACCommand{CID: DevStatusAns, Payload: &DevStatusAnsPayload{Battery: 10, Margin: 20}}}
Convey("Then MarshalBinary returns []byte{4, 3, 2, 1, 0, 0, 0, 0, 6, 10, 20}", func() {
b, err := p.MarshalBinary()
So(err, ShouldBeNil)
So(b, ShouldResemble, []byte{4, 3, 2, 1, 0, 0, 0, 0, 6, 10, 20})
})
})
Convey("Given the slice []byte{4, 3, 2, 1, 0, 0}", func() {
b := []byte{4, 3, 2, 1, 0, 0}
Convey("Then UnmarshalBinary returns an error", func() {
err := p.UnmarshalBinary(true, b)
So(err, ShouldResemble, errors.New("lorawan: at least 7 bytes needed to decode FHDR"))
})
})
Convey("Given the slice []byte{4, 3, 2, 1, 3, 0, 0, 0, 0}", func() {
b := []byte{4, 3, 2, 1, 3, 0, 0, 0, 0}
Convey("Then UnmarshalBinary returns an error", func() {
err := p.UnmarshalBinary(true, b)
So(err, ShouldResemble, errors.New("lorawan: not enough bytes to decode FHDR"))
})
})
Convey("Given uplink=true and slice []byte{4, 3, 2, 1, 1, 0, 0, 2}", func() {
b := []byte{4, 3, 2, 1, 1, 0, 0, 2}
Convey("Then UnmarshalBinary returns no error", func() {
err := p.UnmarshalBinary(true, b)
So(err, ShouldBeNil)
})
})
Convey("Given uplink=true and slice []byte{4, 3, 2, 1, 0, 0, 0, 0, 6, 10}", func() {
b := []byte{4, 3, 2, 1, 0, 0, 0, 0, 6, 10}
Convey("Then UnmarshalBinary returns an error", func() {
err := p.UnmarshalBinary(true, b)
So(err, ShouldResemble, errors.New("lorawan: not enough remaining bytes"))
})
})
Convey("Given uplink=true and slice []byte{4, 3, 2, 1, 0, 0, 0, 0, 6, 10, 20}", func() {
b := []byte{4, 3, 2, 1, 0, 0, 0, 0, 6, 10, 20}
Convey("Then UnmarshalBinary does not return an error", func() {
err := p.UnmarshalBinary(true, b)
So(err, ShouldBeNil)
Convey("Then FHDR(DevAddr=[4]byte{1, 2, 3, 4})", func() {
So(p.FHDR.DevAddr, ShouldEqual, DevAddr([4]byte{1, 2, 3, 4}))
})
Convey("Then FPort=0", func() {
So(p.FPort, ShouldNotBeNil)
So(*p.FPort, ShouldEqual, 0)
})
Convey("Then FRMPayload=[]Payload{MACCommand{CID: DevStatusAns, Payload: DevStatusAnsPayload(Battery=10, Margin=20)}}", func() {
So(p.FRMPayload, ShouldHaveLength, 1)
mac, ok := p.FRMPayload[0].(*MACCommand)
So(ok, ShouldBeTrue)
So(mac.CID, ShouldEqual, DevStatusAns)
pl, ok := mac.Payload.(*DevStatusAnsPayload)
So(ok, ShouldBeTrue)
So(pl.Battery, ShouldEqual, 10)
So(pl.Margin, ShouldEqual, 20)
})
})
})
Convey("Given the slice []byte{4,3, 2, 1, 0, 0, 0, 1, 6, 10, 20}", func() {
b := []byte{4, 3, 2, 1, 0, 0, 0, 1, 6, 10, 20}
Convey("Then UnmarshalBinary does not return an error", func() {
err := p.UnmarshalBinary(false, b)
So(err, ShouldBeNil)
Convey("Then FHDR(DevAddr=[4]byte{1, 2, 3, 4})", func() {
So(p.FHDR.DevAddr, ShouldEqual, DevAddr([4]byte{1, 2, 3, 4}))
})
Convey("Then FPort=1", func() {
So(p.FPort, ShouldNotBeNil)
So(*p.FPort, ShouldEqual, 1)
})
Convey("Then FRMPayload=[]Payload{DataPayload([]byte{6, 10, 20})}", func() {
So(p.FRMPayload, ShouldHaveLength, 1)
pl, ok := p.FRMPayload[0].(*DataPayload)
So(ok, ShouldBeTrue)
So(pl.Bytes, ShouldResemble, []byte{6, 10, 20})
})
})
})
})
}