-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframes_test.go
212 lines (190 loc) · 5.17 KB
/
frames_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package frames_test
import (
"bytes"
"fmt"
"math"
"testing"
"github.com/knei-knurow/frames"
)
var testCases = []struct {
inputHeader [2]byte
inputData []byte
expectedChecksum byte
frame []byte
}{
{
inputHeader: [2]byte{'L', 'D'},
inputData: []byte{},
expectedChecksum: 0x00,
frame: []byte{'L', 'D', 0x0, '+', '#', 0x00},
},
{
inputHeader: [2]byte{'L', 'D'},
inputData: []byte{'A'},
expectedChecksum: 0x40,
frame: []byte{'L', 'D', 0x01, '+', 'A', '#', 0x40},
},
{
inputHeader: [2]byte{'L', 'D'},
inputData: []byte{'t', 'e', 's', 't'},
expectedChecksum: 0x12,
frame: []byte{'L', 'D', 0x04, '+', 't', 'e', 's', 't', '#', 0x12},
},
{
inputHeader: [2]byte{'L', 'D'},
inputData: []byte{'d', 'u', 'p', 'c', 'i', 'a'},
expectedChecksum: 0x0c,
frame: []byte{'L', 'D', 0x06, '+', 'd', 'u', 'p', 'c', 'i', 'a', '#', 0x0c},
},
{
inputHeader: [2]byte{'L', 'D'},
inputData: []byte{'l', 'o', 'l', 'x', 'd'},
expectedChecksum: 0x76,
frame: []byte{'L', 'D', 0x05, '+', 'l', 'o', 'l', 'x', 'd', '#', 0x76},
},
{
inputHeader: [2]byte{'M', 'T'},
inputData: []byte{'d', 'o', 'n', 'd', 'u'},
expectedChecksum: 0x60,
frame: []byte{'M', 'T', 0x05, '+', 'd', 'o', 'n', 'd', 'u', '#', 0x60},
},
}
func TestCreate(t *testing.T) {
for i, tc := range testCases {
testName := fmt.Sprintf("test %d", i)
t.Run(testName, func(t *testing.T) {
gotFrame := frames.Create(tc.inputHeader, tc.inputData)
if !bytes.Equal(gotFrame.Header(), tc.inputHeader[:]) {
t.Errorf("got header % x, want header % x", gotFrame.Header(), tc.inputHeader)
}
if !bytes.Equal(gotFrame.Data(), tc.inputData) {
t.Errorf("got data % x, want data % x", gotFrame.Data(), tc.inputData)
}
if gotFrame.LenData() != len(tc.inputData) {
t.Errorf("got data length %d, want data %d", gotFrame.LenData(), len(tc.inputData))
}
if gotFrame.Checksum() != tc.expectedChecksum {
t.Errorf("got checksum % x, want checksum % x", gotFrame.Checksum(), tc.expectedChecksum)
}
if !bytes.Equal(gotFrame, tc.frame) {
t.Errorf("frame create not correct")
}
})
}
}
func TestAssemble(t *testing.T) {
for i, tc := range testCases {
testName := fmt.Sprintf("test %d", i)
t.Run(testName, func(t *testing.T) {
gotFrame := frames.Assemble(tc.inputHeader, byte(len(tc.inputData)), tc.inputData, tc.expectedChecksum)
if !bytes.Equal(gotFrame.Header(), tc.inputHeader[:]) {
t.Errorf("got header % x, want header % x", gotFrame.Header(), tc.inputHeader)
}
if !bytes.Equal(gotFrame.Data(), tc.inputData) {
t.Errorf("got data % x, want data % x", gotFrame.Data(), tc.inputData)
}
if gotFrame.Checksum() != tc.expectedChecksum {
t.Errorf("got checksum % x, want checksum % x", gotFrame.Checksum(), tc.expectedChecksum)
}
})
}
}
func TestVerify(t *testing.T) {
verifyTestCases := []struct {
frame []byte
valid bool
}{
// the shortest possible frame (len=6)
{
frame: []byte{'L', 'D', 0x0, '+', '#', 0x00},
valid: true,
},
{
frame: []byte{'L', 'D', 0x1, '+', 'A', '#', 0x40},
valid: true,
},
{
frame: []byte{'L', 'D', 0x4, '+', 't', 'e', 's', 't', '#', 0x12},
valid: true,
},
{
frame: []byte{'L', 'D', 0x6, '+', 'd', 'u', 'p', 'c', 'i', 'a', '#', 0x0c},
valid: true,
},
{
frame: []byte{'L', 'D', 0x5, '+', 'l', 'o', 'l', 'x', 'd', '#', 0x76},
valid: true,
},
{
frame: []byte{'M', 'T', 0x5, '+', 'd', 'o', 'n', 'd', 'u', '#', 0x60},
valid: true,
},
// frame with 1 digit in the header
{
frame: []byte{'M', '1', 0x5, '+', 'd', 'i', 'g', 'i', 't', '#', 0x06},
valid: true,
},
// frame with 2 digits in the header
{
frame: []byte{'1', '2', 0x6, '+', 'd', 'i', 'g', 'i', 't', 's', '#', 0x09},
valid: true,
},
// nil frame
{
frame: nil,
valid: false,
},
// empty frame
{
frame: []byte{},
valid: false,
},
// too short frame
{
frame: []byte{'x', 'd'},
valid: false,
},
// frame with invalid length
{
frame: []byte{'M', 'T', 0x6, '+', 'd', 'o', 'n', 'd', 'u', '#', 0x63},
valid: false,
},
}
for i, tc := range verifyTestCases {
testName := fmt.Sprintf("test %d", i)
t.Run(testName, func(t *testing.T) {
if frames.Verify(tc.frame) != tc.valid {
t.Errorf("frame verification failed for %s", tc.frame)
}
})
}
}
func TestRecreate(t *testing.T) {
for i, tc := range testCases {
testName := fmt.Sprintf("test %d", i)
t.Run(testName, func(t *testing.T) {
gotFrame := frames.Create(tc.inputHeader, tc.inputData)
recreatedFrame := frames.Recreate(gotFrame[:])
if !bytes.Equal(gotFrame, recreatedFrame) {
t.Error("frame recreation failed")
}
})
}
}
func FuzzCreate(f *testing.F) {
for _, tc := range testCases {
f.Add(tc.inputData)
}
f.Fuzz(func(t *testing.T, orig []byte) {
if len(orig) > math.MaxUint8 {
return
}
created := frames.Create([2]byte{'L', 'D'}, orig)
if created.LenData() != len(orig) {
t.Errorf("got data length %d, want data %d", created.LenData(), len(orig))
}
if !frames.Verify(created) {
t.Errorf("frame recreation failed")
}
})
}