-
Notifications
You must be signed in to change notification settings - Fork 159
/
mac_command_payload_test.go
141 lines (125 loc) · 2.95 KB
/
mac_command_payload_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
package lorawan
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type macCommandPayloadTest struct {
Payload MACCommandPayload
Bytes []byte
Error error
}
type MACCommandPayloadTestSuite struct {
suite.Suite
}
func (ts MACCommandPayloadTestSuite) run(newPLFunc func() MACCommandPayload, tests []macCommandPayloadTest) {
assert := require.New(ts.T())
for _, tst := range tests {
if tst.Payload != nil {
b, err := tst.Payload.MarshalBinary()
assert.Equal(tst.Error, err)
assert.Equal(tst.Bytes, b)
// if there is a Payload and error, skip to the next test
if tst.Error != nil {
continue
}
}
pl := newPLFunc()
err := pl.UnmarshalBinary(tst.Bytes)
if tst.Error == nil {
assert.NoError(err)
} else {
assert.Equal(tst.Error, err)
}
if err == nil {
assert.Equal(tst.Payload, pl)
}
}
}
func (ts MACCommandPayloadTestSuite) TestDeviceModeIndClass() {
tests := []macCommandPayloadTest{
{
Bytes: []byte{},
Error: errors.New("lorawan: 1 byte of data is expected"),
},
{
Bytes: []byte{0x00, 0x01},
Error: errors.New("lorawan: 1 byte of data is expected"),
},
{
Payload: &DeviceModeIndPayload{
Class: DeviceModeClassA,
},
Bytes: []byte{0x00},
},
{
Payload: &DeviceModeIndPayload{
Class: DeviceModeClassC,
},
Bytes: []byte{0x02},
},
}
ts.run(func() MACCommandPayload { return &DeviceModeIndPayload{} }, tests)
}
func (ts MACCommandPayloadTestSuite) TestDeviceModeConfClass() {
tests := []macCommandPayloadTest{
{
Bytes: []byte{},
Error: errors.New("lorawan: 1 byte of data is expected"),
},
{
Bytes: []byte{0x00, 0x01},
Error: errors.New("lorawan: 1 byte of data is expected"),
},
{
Payload: &DeviceModeConfPayload{
Class: DeviceModeClassA,
},
Bytes: []byte{0x00},
},
{
Payload: &DeviceModeConfPayload{
Class: DeviceModeClassC,
},
Bytes: []byte{0x02},
},
}
ts.run(func() MACCommandPayload { return &DeviceModeConfPayload{} }, tests)
}
func (ts MACCommandPayloadTestSuite) TestTXParamSetupReqPayload() {
tests := []macCommandPayloadTest{
{
Bytes: []byte{},
Error: errors.New("lorawan: 1 byte of data is expected"),
},
{
Payload: &TXParamSetupReqPayload{
UplinkDwellTime: DwellTime400ms,
DownlinkDwelltime: DwellTimeNoLimit,
MaxEIRP: 7,
},
Bytes: []byte{0x17},
},
{
Payload: &TXParamSetupReqPayload{
UplinkDwellTime: DwellTimeNoLimit,
DownlinkDwelltime: DwellTime400ms,
MaxEIRP: 7,
},
Bytes: []byte{0x27},
},
{
Payload: &TXParamSetupReqPayload{
UplinkDwellTime: DwellTimeNoLimit,
DownlinkDwelltime: DwellTime400ms,
MaxEIRP: 16,
},
Error: errors.New("lorawan: max value of MaxEIRP is 15"),
},
}
ts.run(func() MACCommandPayload { return &TXParamSetupReqPayload{} }, tests)
}
func TestMACCommandPayloads(t *testing.T) {
suite.Run(t, new(MACCommandPayloadTestSuite))
}