-
Notifications
You must be signed in to change notification settings - Fork 8
/
node_receive_message_test.go
119 lines (102 loc) · 3.17 KB
/
node_receive_message_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
package zstack
import (
"context"
"github.com/shimmeringbee/bytecodec"
"github.com/shimmeringbee/persistence/impl/memory"
. "github.com/shimmeringbee/unpi"
unpiTest "github.com/shimmeringbee/unpi/testing"
"github.com/shimmeringbee/zigbee"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func Test_ReceiveMessage(t *testing.T) {
t.Run("messages which are received with a known network to ieee mapping are sent to event stream", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
unpiMock := unpiTest.NewMockAdapter()
defer unpiMock.AssertCalls(t)
zstack := New(unpiMock, memory.New())
defer unpiMock.Stop()
zstack.nodeTable.addOrUpdate(zigbee.IEEEAddress(0x1122334455667788), zigbee.NetworkAddress(0x1000))
zstack.startMessageReceiver()
go func() {
time.Sleep(10 * time.Millisecond)
msg := AfIncomingMsg{
GroupID: 0x01,
ClusterID: 0x02,
SourceAddress: 0x1000,
SourceEndpoint: 3,
DestinationEndpoint: 4,
WasBroadcast: true,
LinkQuality: 55,
SecurityUse: true,
TimeStamp: 123412,
Sequence: 63,
Data: []byte{0x01, 0x02},
}
data, _ := bytecodec.Marshal(&msg)
unpiMock.InjectOutgoing(Frame{
MessageType: AREQ,
Subsystem: AF,
CommandID: AfIncomingMsgID,
Payload: data,
})
}()
event, err := zstack.ReadEvent(ctx)
assert.NoError(t, err)
incommingMsg, ok := event.(zigbee.NodeIncomingMessageEvent)
assert.True(t, ok)
expectedMsg := zigbee.NodeIncomingMessageEvent{
Node: zigbee.Node{
IEEEAddress: 0x1122334455667788,
NetworkAddress: 0x1000,
LogicalType: 0xff,
LQI: 0,
Depth: 0,
LastDiscovered: time.Time{},
LastReceived: time.Time{},
},
IncomingMessage: zigbee.IncomingMessage{
GroupID: 0x01,
SourceAddress: zigbee.SourceAddress{
IEEEAddress: 0x1122334455667788,
NetworkAddress: 0x1000,
},
Broadcast: true,
Secure: true,
LinkQuality: 55,
Sequence: 63,
ApplicationMessage: zigbee.ApplicationMessage{
ClusterID: 0x02,
SourceEndpoint: 3,
DestinationEndpoint: 4,
Data: []byte{0x01, 0x02},
},
},
}
assert.Equal(t, expectedMsg, incommingMsg)
n, _ := zstack.nodeTable.getByNetwork(zigbee.NetworkAddress(0x1000))
assert.Equal(t, uint8(55), n.LQI)
})
}
func Test_IncomingMessage(t *testing.T) {
t.Run("verify AfIncomingMsg marshals", func(t *testing.T) {
req := AfIncomingMsg{
GroupID: 0x0102,
ClusterID: 0x0304,
SourceAddress: 0x0506,
SourceEndpoint: 0x07,
DestinationEndpoint: 0x08,
WasBroadcast: true,
LinkQuality: 0x0a,
SecurityUse: true,
TimeStamp: 0x10111213,
Sequence: 0x0d,
Data: []byte{0x00, 0x01, 0x02},
}
data, err := bytecodec.Marshal(req)
assert.NoError(t, err)
assert.Equal(t, []byte{0x02, 0x01, 0x04, 0x03, 0x06, 0x05, 0x07, 0x08, 0x01, 0x0a, 0x01, 0x13, 0x12, 0x11, 0x10, 0x0d, 0x03, 0x00, 0x01, 0x02}, data)
})
}