-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsemioctet_test.go
216 lines (210 loc) · 3.55 KB
/
semioctet_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
213
214
215
216
// SPDX-License-Identifier: MIT
//
// Copyright © 2018 Kent Gibson <[email protected]>.
package semioctet_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/warthog618/sms/encoding/semioctet"
)
func TestDecode(t *testing.T) {
patterns := []struct {
name string
inDst []byte
inSrc []byte
out []byte
outReadCount int
err error
}{
{
"nil",
nil,
nil,
nil,
0,
nil,
},
{
"nil dst",
nil,
[]byte{0x10, 0x32, 0x54, 0x76},
nil,
0,
nil,
},
{
"nil src",
make([]byte, 3),
nil,
[]byte{},
0,
nil,
},
{
"empty dst",
[]byte{},
[]byte{0x10, 0x32, 0x54, 0x76},
[]byte{},
0,
nil,
},
{
"empty src",
make([]byte, 3),
[]byte{},
[]byte{},
0,
nil,
},
{
"limit src",
make([]byte, 8),
[]byte{0x10, 0x32, 0x54, 0x76},
[]byte("01234567"),
4,
nil,
},
{
"fill limit src",
make([]byte, 8),
[]byte{0x10, 0x32, 0x54, 0xf6},
[]byte("0123456"),
4,
nil,
},
{
"fill limit even dst",
make([]byte, 6),
[]byte{0x10, 0x32, 0x54, 0xf6, 0x98},
[]byte("012345"),
3,
nil,
},
{
"no fill limit even dst",
make([]byte, 6),
[]byte{0x10, 0x32, 0x54, 0x76, 0x98},
[]byte("012345"),
3,
nil,
},
{
"fill limit odd dst",
make([]byte, 5),
[]byte{0x10, 0x32, 0xf4, 0x76},
[]byte("01234"),
3,
nil,
},
{
"alphabet",
make([]byte, 15),
[]byte{0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe},
[]byte("0123456789*#abc"),
8,
nil,
},
{
"no fill limit even dst",
make([]byte, 6),
[]byte{0x10, 0x32, 0x54, 0x76, 0x98},
[]byte("012345"),
3,
nil,
},
{
"no fill limit odd dst",
make([]byte, 5),
[]byte{0x10, 0x32, 0x54, 0x76},
nil,
3,
semioctet.ErrMissingFill,
},
{
"skip inter fill",
make([]byte, 10),
[]byte{0x10, 0x32, 0xF4, 0x76, 0xF8},
[]byte("01234678"),
5,
nil,
},
}
for _, p := range patterns {
f := func(t *testing.T) {
dst, count, err := semioctet.Decode(p.inDst, p.inSrc)
assert.Equal(t, p.err, err)
assert.Equal(t, p.outReadCount, count)
assert.Equal(t, p.out, dst)
}
t.Run(p.name, f)
}
}
func TestEncode(t *testing.T) {
patterns := []struct {
name string
in []byte
out []byte
err error
}{
{
"nil",
nil,
nil,
nil,
},
{
"empty src",
[]byte{},
[]byte{},
nil,
},
{
"even src",
[]byte("01234567"),
[]byte{0x10, 0x32, 0x54, 0x76},
nil,
},
{
"odd src",
[]byte("0123456"),
[]byte{0x10, 0x32, 0x54, 0xf6},
nil,
},
{
"alphabet",
[]byte("0123456789*#abc"),
[]byte{0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe},
nil,
},
{
"invalid digit",
[]byte("012345D6789"),
nil,
semioctet.ErrInvalidDigit('D'),
},
}
for _, p := range patterns {
f := func(t *testing.T) {
dst, err := semioctet.Encode(p.in)
assert.Equal(t, p.err, err)
assert.Equal(t, p.out, dst)
}
t.Run(p.name, f)
}
}
// TestErrInvalidDigit tests that the errors can be stringified.
// It is fragile, as it compares the strings exactly, but its main purpose is
// to confirm the Error function doesn't recurse, as that is bad.
func TestErrInvalidOctet(t *testing.T) {
patterns := []byte{0x00, 0xa0, 0x0a, 0x9a, 0xa9, 0xff}
for _, p := range patterns {
f := func(t *testing.T) {
err := semioctet.ErrInvalidDigit(p)
expected := fmt.Sprintf("semioctet: invalid digit: '%c' - 0x%x", byte(p), int(p))
s := err.Error()
assert.Equal(t, expected, s)
}
t.Run(fmt.Sprintf("%x", p), f)
}
}