-
Notifications
You must be signed in to change notification settings - Fork 8
/
joining_test.go
169 lines (132 loc) · 4.62 KB
/
joining_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
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_PermitJoin(t *testing.T) {
t.Run("permit join for all routers sends message to all routers permitting joining", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
unpiMock := unpiTest.NewMockAdapter()
zstack := New(unpiMock, memory.New())
zstack.sem = semaphore.NewWeighted(8)
defer unpiMock.Stop()
c := unpiMock.On(SREQ, ZDO, ZDOMgmtPermitJoinRequestID).Return(Frame{
MessageType: SRSP,
Subsystem: ZDO,
CommandID: ZDOMgmtPermitJoinRequestReplyID,
Payload: []byte{0x00},
})
err := zstack.PermitJoin(ctx, true)
assert.NoError(t, err)
unpiMock.AssertCalls(t)
assert.Equal(t, []byte{0xfc, 0xff, 0xff, 0x00}, c.CapturedCalls[0].Frame.Payload)
assert.Equal(t, OnAllRouters, zstack.NetworkProperties.JoinState)
})
t.Run("permit join for the coordinator sends message to coordinator permitting joining", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
unpiMock := unpiTest.NewMockAdapter()
zstack := New(unpiMock, memory.New())
zstack.sem = semaphore.NewWeighted(8)
defer unpiMock.Stop()
zstack.NetworkProperties.NetworkAddress = zigbee.NetworkAddress(0x0102)
c := unpiMock.On(SREQ, ZDO, ZDOMgmtPermitJoinRequestID).Return(Frame{
MessageType: SRSP,
Subsystem: ZDO,
CommandID: ZDOMgmtPermitJoinRequestReplyID,
Payload: []byte{0x00},
})
err := zstack.PermitJoin(ctx, false)
assert.NoError(t, err)
unpiMock.AssertCalls(t)
assert.Equal(t, []byte{0x02, 0x01, 0xff, 0x00}, c.CapturedCalls[0].Frame.Payload)
assert.Equal(t, OnCoordinator, zstack.NetworkProperties.JoinState)
})
t.Run("permit join rejection by adapter errors", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
unpiMock := unpiTest.NewMockAdapter()
zstack := New(unpiMock, memory.New())
zstack.sem = semaphore.NewWeighted(8)
defer unpiMock.Stop()
unpiMock.On(SREQ, ZDO, ZDOMgmtPermitJoinRequestID).Return(Frame{
MessageType: SRSP,
Subsystem: ZDO,
CommandID: ZDOMgmtPermitJoinRequestReplyID,
Payload: []byte{0x01},
})
err := zstack.PermitJoin(ctx, true)
assert.Error(t, err)
unpiMock.AssertCalls(t)
})
}
func Test_DenyJoin(t *testing.T) {
t.Run("denying join sends message to all routers disabling joining", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
unpiMock := unpiTest.NewMockAdapter()
zstack := New(unpiMock, memory.New())
zstack.sem = semaphore.NewWeighted(8)
defer unpiMock.Stop()
c := unpiMock.On(SREQ, ZDO, ZDOMgmtPermitJoinRequestID).Return(Frame{
MessageType: SRSP,
Subsystem: ZDO,
CommandID: ZDOMgmtPermitJoinRequestReplyID,
Payload: []byte{0x00},
})
zstack.NetworkProperties.JoinState = OnCoordinator
err := zstack.DenyJoin(ctx)
assert.NoError(t, err)
unpiMock.AssertCalls(t)
assert.Equal(t, []byte{0xfc, 0xff, 0x00, 0x00}, c.CapturedCalls[0].Frame.Payload)
assert.Equal(t, Off, zstack.NetworkProperties.JoinState)
})
t.Run("denying join rejection by adapter errors", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
unpiMock := unpiTest.NewMockAdapter()
zstack := New(unpiMock, memory.New())
zstack.sem = semaphore.NewWeighted(8)
defer unpiMock.Stop()
unpiMock.On(SREQ, ZDO, ZDOMgmtPermitJoinRequestID).Return(Frame{
MessageType: SRSP,
Subsystem: ZDO,
CommandID: ZDOMgmtPermitJoinRequestReplyID,
Payload: []byte{0x01},
})
err := zstack.DenyJoin(ctx)
assert.Error(t, err)
unpiMock.AssertCalls(t)
})
}
func Test_ZDOMgmtPermitJoin(t *testing.T) {
t.Run("ZDOMgmtPermitJoinRequest", func(t *testing.T) {
s := ZDOMgmtPermitJoinRequest{
Destination: 0x0102,
Duration: 0x03,
TCSignificance: 0x04,
}
actualBytes, err := bytecodec.Marshal(s)
expectedBytes := []byte{0x02, 0x01, 0x03, 0x04}
assert.NoError(t, err)
assert.Equal(t, expectedBytes, actualBytes)
})
t.Run("ZDOMgmtPermitJoinRequestReply", func(t *testing.T) {
s := ZDOMgmtPermitJoinRequestReply{
Status: 0x01,
}
actualBytes, err := bytecodec.Marshal(s)
expectedBytes := []byte{0x01}
assert.NoError(t, err)
assert.Equal(t, expectedBytes, actualBytes)
})
}