forked from brocaar/chirpstack-network-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rx_packet_test.go
227 lines (192 loc) · 6.48 KB
/
rx_packet_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
package loraserver
import (
"errors"
"testing"
"time"
"github.com/brocaar/loracontrol"
"github.com/brocaar/lorawan"
. "github.com/smartystreets/goconvey/convey"
)
type testApplicationBackend struct {
callCount int
err error
}
func (b *testApplicationBackend) Send(appEUI lorawan.EUI64, rxPackets loracontrol.RXPackets) error {
b.callCount = b.callCount + 1
return b.err
}
func (b *testApplicationBackend) SetClient(c *loracontrol.Client) {}
func (b *testApplicationBackend) Close() error {
return nil
}
func (b *testApplicationBackend) Receive() chan loracontrol.TXPacket {
return make(chan loracontrol.TXPacket)
}
type testGatewayBackend struct {
rxPacketChan chan loracontrol.RXPacket
}
func (b *testGatewayBackend) SetClient(c *loracontrol.Client) {}
func (b *testGatewayBackend) Send(txPacket loracontrol.TXPacket) error {
return nil
}
func (b *testGatewayBackend) Receive() chan loracontrol.RXPacket {
return b.rxPacketChan
}
func (b *testGatewayBackend) Close() error {
return nil
}
func TestHandleGatewayPackets(t *testing.T) {
config := getConfig()
Convey("Given a Client connected to a clean Redis database", t, func() {
appBackend := &testApplicationBackend{}
gwBackend := &testGatewayBackend{
rxPacketChan: make(chan loracontrol.RXPacket, 1),
}
client, err := loracontrol.NewClient(
loracontrol.SetStorageBackend(loracontrol.NewRedisBackend(config.RedisServer, config.RedisPassword)),
loracontrol.SetApplicationBackend(appBackend),
loracontrol.SetGatewayBackend(gwBackend),
)
So(err, ShouldBeNil)
So(client.Storage().FlushAll(), ShouldBeNil)
nwkSKey := lorawan.AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
appSKey := lorawan.AES128Key{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}
devAddr := lorawan.DevAddr{1, 1, 1, 1}
Convey("Given a test RXPacket", func() {
macPL := lorawan.NewMACPayload(true)
macPL.FHDR = lorawan.FHDR{
DevAddr: devAddr,
FCtrl: lorawan.FCtrl{},
FCnt: 10,
}
macPL.FPort = 1
macPL.FRMPayload = []lorawan.Payload{
&lorawan.DataPayload{Bytes: []byte("abc123")},
}
So(macPL.EncryptFRMPayload(appSKey), ShouldBeNil)
phy := lorawan.NewPHYPayload(true)
phy.MHDR = lorawan.MHDR{
MType: lorawan.UnconfirmedDataUp,
Major: lorawan.LoRaWANR1,
}
phy.MACPayload = macPL
So(phy.SetMIC(nwkSKey), ShouldBeNil)
rxPacket := loracontrol.RXPacket{
RXInfo: loracontrol.RXInfo{
MAC: lorawan.EUI64{1, 2, 3, 4, 5, 6, 7, 8},
Time: time.Now().UTC(),
Timestamp: 708016819,
Frequency: 868.5,
Channel: 2,
RFChain: 1,
CRCStatus: 1,
Modulation: "LORA",
DataRate: loracontrol.DataRate{LoRa: "SF7BW125"},
CodingRate: "4/5",
RSSI: -51,
LoRaSNR: 7,
Size: 16,
},
PHYPayload: phy,
}
rxPackets := loracontrol.RXPackets{
{
RXInfo: loracontrol.RXInfo{
MAC: lorawan.EUI64{1, 2, 3, 4, 5, 6, 7, 8},
Time: time.Now().UTC(),
Timestamp: 708016819,
Frequency: 868.5,
Channel: 2,
RFChain: 1,
CRCStatus: 1,
Modulation: "LORA",
DataRate: loracontrol.DataRate{LoRa: "SF7BW125"},
CodingRate: "4/5",
RSSI: -51,
LoRaSNR: 7,
Size: 16,
},
PHYPayload: phy,
},
}
Convey("When calling handleGatewayPacket", func() {
err := handleGatewayPacket(rxPacket, client)
Convey("Then an error is returned that the node-session does not exists", func() {
So(err, ShouldResemble, errors.New("node-session does not exist"))
})
})
Convey("Given the node-session is in the database", func() {
nodeSession := loracontrol.NodeSession{
DevAddr: devAddr,
DevEUI: [8]byte{1, 1, 1, 1, 1, 1, 1, 1},
NwkSKey: nwkSKey,
FCntUp: 10,
}
So(client.NodeSession().CreateExpire(nodeSession), ShouldBeNil)
Convey("Given the node is in the database", func() {
node := loracontrol.Node{
DevEUI: [8]byte{1, 1, 1, 1, 1, 1, 1, 1},
AppEUI: [8]byte{2, 2, 2, 2, 2, 2, 2, 2},
AppKey: appSKey,
}
So(client.Node().Create(node), ShouldBeNil)
Convey("Given the application is in the database", func() {
app := loracontrol.Application{
AppEUI: [8]byte{2, 2, 2, 2, 2, 2, 2, 2},
}
So(client.Application().Create(app), ShouldBeNil)
Convey("Then handleGatewayPacket does not return an error", func() {
err := handleGatewayPacket(rxPacket, client)
So(err, ShouldBeNil)
Convey("Then the app backend Send was called once", func() {
So(appBackend.callCount, ShouldEqual, 1)
})
Convey("Then FCntUp on the node-session is incremented", func() {
n, err := client.NodeSession().Get(nodeSession.DevAddr)
So(err, ShouldBeNil)
So(n.FCntUp, ShouldEqual, nodeSession.FCntUp+1)
})
})
Convey("When calling HandleGatewayPackets", func() {
gwBackend.rxPacketChan <- rxPackets[0]
close(gwBackend.rxPacketChan)
HandleGatewayPackets(client)
Convey("Then the packet has been sent by the app backend", func() {
time.Sleep(time.Millisecond * 200)
So(appBackend.callCount, ShouldEqual, 1)
})
})
Convey("When the FCnt is invalid", func() {
nodeSession.FCntUp = 11
So(client.NodeSession().UpdateExpire(nodeSession), ShouldBeNil)
Convey("Then handleGatewayPacket returns an invalid FCnt error", func() {
err := handleGatewayPacket(rxPacket, client)
So(err, ShouldResemble, errors.New("invalid FCnt or too many dropped frames"))
})
})
Convey("When the NwkSKey is invalid", func() {
nodeSession.NwkSKey[0] = 0
So(client.NodeSession().UpdateExpire(nodeSession), ShouldBeNil)
Convey("Then handleGatewayPacket returns an invalid MIC error", func() {
err := handleGatewayPacket(rxPacket, client)
So(err, ShouldResemble, errors.New("invalid MIC"))
})
})
Convey("When the application backend returns an error", func() {
appBackend.err = errors.New("BOOM!")
Convey("When calling handleGatewayPacket", func() {
err := handleGatewayPacket(rxPacket, client)
So(err, ShouldResemble, errors.New("BOOM!"))
Convey("Then FCntUp on the node-session is not incremented", func() {
n, err := client.NodeSession().Get(nodeSession.DevAddr)
So(err, ShouldBeNil)
So(n.FCntUp, ShouldEqual, nodeSession.FCntUp)
})
})
})
})
})
})
})
})
}