-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnode_description_test.go
133 lines (112 loc) · 3.86 KB
/
node_description_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
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"
"golang.org/x/sync/semaphore"
"testing"
"time"
)
func Test_QueryNodeDescription(t *testing.T) {
t.Run("returns an success on query, response for requested network address is received", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
unpiMock := unpiTest.NewMockAdapter()
zstack := New(unpiMock, memory.New())
zstack.sem = semaphore.NewWeighted(8)
defer unpiMock.Stop()
unpiMock.On(SREQ, ZDO, ZdoNodeDescReqID).Return(Frame{
MessageType: SRSP,
Subsystem: ZDO,
CommandID: ZdoNodeDescReqReplyID,
Payload: []byte{0x00},
})
go func() {
time.Sleep(10 * time.Millisecond)
unpiMock.InjectOutgoing(Frame{
MessageType: AREQ,
Subsystem: ZDO,
CommandID: ZdoNodeDescRspID,
Payload: []byte{0x00, 0x20, 0x01, 0x00, 0x40, 0x02, 0x02, 0x03, 0x05, 0x04, 0x06, 0x08, 0x07, 0x0a, 0x09, 0x0c, 0x0b, 0x0d},
})
}()
zstack.nodeTable.addOrUpdate(zigbee.IEEEAddress(0x11223344556677), zigbee.NetworkAddress(0x4000))
nodeDescription, err := zstack.QueryNodeDescription(ctx, zigbee.IEEEAddress(0x11223344556677))
assert.NoError(t, err)
assert.Equal(t, zigbee.NodeDescription{
LogicalType: zigbee.EndDevice,
ManufacturerCode: 0x0405,
}, nodeDescription)
unpiMock.AssertCalls(t)
})
}
func Test_NodeDescriptionMessages(t *testing.T) {
t.Run("verify ZdoNodeDescReq marshals", func(t *testing.T) {
req := ZdoNodeDescReq{
DestinationAddress: zigbee.NetworkAddress(0x2000),
OfInterestAddress: zigbee.NetworkAddress(0x4000),
}
data, err := bytecodec.Marshal(req)
assert.NoError(t, err)
assert.Equal(t, []byte{0x00, 0x20, 0x00, 0x40}, data)
})
t.Run("verify ZdoNodeDescReqReply marshals", func(t *testing.T) {
req := ZdoNodeDescReqReply{
Status: 1,
}
data, err := bytecodec.Marshal(req)
assert.NoError(t, err)
assert.Equal(t, []byte{0x01}, data)
})
t.Run("ZdoNodeDescReqReply returns true if success", func(t *testing.T) {
g := ZdoNodeDescReqReply{Status: ZSuccess}
assert.True(t, g.WasSuccessful())
})
t.Run("ZdoNodeDescReqReply returns false if not success", func(t *testing.T) {
g := ZdoNodeDescReqReply{Status: ZFailure}
assert.False(t, g.WasSuccessful())
})
t.Run("verify ZdoNodeDescRsp marshals", func(t *testing.T) {
req := ZdoNodeDescRsp{
SourceAddress: zigbee.NetworkAddress(0x2000),
Status: 1,
OfInterestAddress: zigbee.NetworkAddress(0x4000),
Capabilities: ZdoNodeDescRspCapabilities{
LogicalType: zigbee.Router,
},
NodeFrequencyBand: 0x00,
APSFlags: 0x02,
MacCapabilitiesFlags: 0x03,
ManufacturerCode: 0x0405,
MaxBufferSize: 0x06,
MaxInTransferSize: 0x0708,
ServerMask: ZdoNodeDescRspServerMask{
Reserved0: 0,
Reserved1: 0,
BackupDiscoveryCache: false,
PrimaryDiscoveryCache: false,
BackupBindingTableCache: false,
PrimaryBindingTableCache: false,
BackupTrustCenter: true,
PrimaryTrustCenter: false,
},
MaxOutTransferSize: 0x0b0c,
DescriptorCapabilities: 0x0d,
}
data, err := bytecodec.Marshal(req)
assert.NoError(t, err)
assert.Equal(t, []byte{0x00, 0x20, 0x01, 0x00, 0x40, 0x01, 0x02, 0x03, 0x05, 0x04, 0x06, 0x08, 0x07, 0x00, 0x02, 0x0c, 0x0b, 0x0d}, data)
})
t.Run("ZdoNodeDescRsp returns true if success", func(t *testing.T) {
g := ZdoNodeDescRsp{Status: ZSuccess}
assert.True(t, g.WasSuccessful())
})
t.Run("ZdoNodeDescRsp returns false if not success", func(t *testing.T) {
g := ZdoNodeDescRsp{Status: ZFailure}
assert.False(t, g.WasSuccessful())
})
}