-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket_test.go
95 lines (87 loc) · 3.62 KB
/
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
package ssp_test
import (
"fmt"
"testing"
ssp "github.com/christianschmizz/go-smiley"
"github.com/stretchr/testify/assert"
)
func TestDecodeEncodeCommand(t *testing.T) {
testcases := []struct {
Data []byte
SequenceFlag bool
SlaveID uint8
Code uint8
}{
{[]byte{0x7F, 0x80, 0x01, 0x01, 0x06, 0x02}, true, 0, uint8(ssp.Reset)},
{[]byte{0x7F, 0x80, 0x01, 0x0C, 0x2B, 0x82}, true, 0, uint8(ssp.GetSerialNumber)},
{[]byte{0x7F, 0x80, 0x01, 0x0A, 0x3F, 0x82}, true, 0, uint8(ssp.Enable)},
{[]byte{0x7F, 0x80, 0x01, 0x09, 0x35, 0x82}, true, 0, uint8(ssp.Disable)},
{[]byte{0x7F, 0x80, 0x01, 0x08, 0x30, 0x02}, true, 0, uint8(ssp.RejectBanknote)},
{[]byte{0x7F, 0x80, 0x01, 0xf0, 0x23, 0x80}, true, 0, uint8(ssp.OK)},
{[]byte{0x7f, 0x00, 0x01, 0x11, 0x66, 0x08}, false, 0, uint8(ssp.Sync)},
{[]byte{0x7f, 0x00, 0x11, 0xf0, 0x4e, 0x56, 0x53, 0x30, 0x30, 0x39, 0x31, 0x30, 0x39, 0x32, 0x35, 0x30, 0x30, 0x30, 0x30, 0x30, 0x45, 0xa4}, false, 0, uint8(ssp.GetFirmwareVersion)},
{[]byte{0x7f, 0x80, 0x09, 0xf0, 0x45, 0x55, 0x52, 0x30, 0x31, 0x30, 0x31, 0x35, 0xde, 0x2a}, true, 0, uint8(ssp.GetDatasetVersion)},
{[]byte{0x7f, 0x00, 0x1f, 0xf0, 0x00, 0x30, 0x31, 0x30, 0x39, 0x45, 0x55, 0x52, 0x00, 0x00, 0x05, 0x07, 0x01, 0x02, 0x04, 0x0a, 0x14, 0x28, 0x64, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x64, 0x04, 0x42, 0x9c}, false, 0, uint8(ssp.SetupRequest)},
}
for _, tc := range testcases {
t.Run(fmt.Sprintf("decode %d", tc.Code), func(t *testing.T) {
packet, err := ssp.Decode(tc.Data)
if assert.NoError(t, err) {
assert.Equal(t, tc.SequenceFlag, packet.SequenceFlag())
assert.Equal(t, tc.SlaveID, packet.SlaveID())
assert.Equal(t, tc.Code, uint8(packet.Command().Code))
}
data, err := ssp.Encode(packet)
if assert.NoError(t, err) {
assert.Equal(t, tc.Data, data)
}
})
}
}
func TestDecodeEncodeResponse(t *testing.T) {
testcases := []struct {
CommandCode ssp.CommandCode
Data []byte
SequenceFlag bool
SlaveID uint8
Code uint8
}{
{ssp.GetFirmwareVersion, []byte{0x7f, 0x00, 0x11, 0xf0, 0x4e, 0x56, 0x53, 0x30, 0x30, 0x39, 0x31, 0x30, 0x39, 0x32, 0x35, 0x30, 0x30, 0x30, 0x30, 0x30, 0x45, 0xa4}, false, 0, uint8(ssp.OK)},
{ssp.GetDatasetVersion, []byte{0x7f, 0x80, 0x09, 0xf0, 0x45, 0x55, 0x52, 0x30, 0x31, 0x30, 0x31, 0x35, 0xde, 0x2a}, true, 0, uint8(ssp.OK)},
{ssp.SetupRequest, []byte{0x7f, 0x00, 0x1f, 0xf0, 0x00, 0x30, 0x31, 0x30, 0x39, 0x45, 0x55, 0x52, 0x00, 0x00, 0x05, 0x07, 0x01, 0x02, 0x04, 0x0a, 0x14, 0x28, 0x64, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x64, 0x04, 0x42, 0x9c}, false, 0, uint8(ssp.OK)},
}
for _, tc := range testcases {
t.Run(fmt.Sprintf("decode %d", tc.Code), func(t *testing.T) {
packet, err := ssp.Decode(tc.Data)
if assert.NoError(t, err) {
assert.Equal(t, tc.SequenceFlag, packet.SequenceFlag())
assert.Equal(t, tc.SlaveID, packet.SlaveID())
assert.Equal(t, tc.Code, uint8(packet.Response().Code))
assert.Len(t, packet.Data(), int(tc.Data[2]))
}
data, err := ssp.Encode(packet)
if assert.NoError(t, err) {
assert.Equal(t, tc.Data, data)
}
})
}
}
func TestEncode(t *testing.T) {
testcases := []struct {
Data []byte
SequenceFlag bool
SlaveID uint8
Code uint8
}{
{[]byte{0x80, 0x01, 0x01, 0x06, 0x02}, true, 0, uint8(ssp.Reset)},
{[]byte{0x80, 0x01, 0xf0, 0x23, 0x80}, true, 0, uint8(ssp.OK)},
}
for i, tc := range testcases {
t.Run(fmt.Sprintf("testcase %d", i), func(t *testing.T) {
data, err := ssp.Encode(ssp.NewPacket(tc.Code, tc.Data, 0))
if assert.NoError(t, err) {
assert.Equal(t, []byte{}, data)
}
})
}
}