-
Notifications
You must be signed in to change notification settings - Fork 8
/
adapter_endpoints_test.go
95 lines (76 loc) · 2.57 KB
/
adapter_endpoints_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
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_RegisterAdapterEndpoint(t *testing.T) {
t.Run("registers the endpoint", 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, AF, AFRegisterID).Return(Frame{
MessageType: SRSP,
Subsystem: AF,
CommandID: AFRegisterReplyID,
Payload: []byte{0x00},
})
err := zstack.RegisterAdapterEndpoint(ctx, 0x01, 0x0104, 0x0001, 0x01, []zigbee.ClusterID{0x0001}, []zigbee.ClusterID{0x0002})
assert.NoError(t, err)
unpiMock.AssertCalls(t)
frame := c.CapturedCalls[0].Frame
assert.Equal(t, []byte{0x01, 0x04, 0x01, 0x01, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x01, 0x02, 0x00}, frame.Payload)
})
t.Run("returns an error if the query fails", 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, AF, AFRegisterID).Return(Frame{
MessageType: SRSP,
Subsystem: AF,
CommandID: AFRegisterReplyID,
Payload: []byte{0x01},
})
err := zstack.RegisterAdapterEndpoint(ctx, 0x01, 0x0104, 0x0001, 0x01, []zigbee.ClusterID{0x0001}, []zigbee.ClusterID{0x0002})
assert.Error(t, err)
assert.Equal(t, ErrorZFailure, err)
unpiMock.AssertCalls(t)
})
}
func Test_EndpointRegisterMessages(t *testing.T) {
t.Run("verify AFRegister marshals", func(t *testing.T) {
req := AFRegister{
Endpoint: 1,
AppProfileId: 2,
AppDeviceId: 3,
AppDeviceVersion: 4,
LatencyReq: 5,
AppInClusters: []zigbee.ClusterID{0x10},
AppOutClusters: []zigbee.ClusterID{0x20},
}
data, err := bytecodec.Marshal(req)
assert.NoError(t, err)
assert.Equal(t, []byte{0x01, 0x02, 0x00, 0x03, 0x00, 0x04, 0x05, 0x01, 0x10, 0x00, 0x01, 0x20, 0x00}, data)
})
t.Run("verify AFRegisterReply marshals", func(t *testing.T) {
req := AFRegisterReply{
Status: 1,
}
data, err := bytecodec.Marshal(req)
assert.NoError(t, err)
assert.Equal(t, []byte{0x01}, data)
})
}