-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathfixedslicereader_test.go
291 lines (266 loc) · 7.33 KB
/
fixedslicereader_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package bits_test
import (
"fmt"
"testing"
"github.com/Eyevinn/mp4ff/bits"
)
func TestFixedSliceReader(t *testing.T) {
t.Run("read ints", func(t *testing.T) {
nrBytes := 64
data := make([]byte, nrBytes)
for i := 0; i < nrBytes; i++ {
data[i] = byte(i)
}
sr := bits.NewFixedSliceReader(data)
if sr.Length() != nrBytes {
t.Errorf("sliceReader length = %d instead of %d", sr.Length(), nrBytes)
}
for i := 0; i < nrBytes+2; i++ {
val := sr.ReadUint8()
switch {
case i < nrBytes:
if i != int(val) {
t.Errorf("val at %d not %d but %d", i, i, val)
}
default: // Read passed end
if sr.AccError() == nil {
t.Errorf("should have had an accumulated error")
}
if val != 0 {
t.Errorf("should have zero value")
}
}
}
sr = bits.NewFixedSliceReader(data)
nrVals := nrBytes / 2
for i := 0; i < nrVals+2; i++ {
val := sr.ReadUint16()
switch {
case i < nrVals:
expectedVal := uint16(expVal(i, 2))
if val != expectedVal {
t.Errorf("val at %d not %d but %d", i, expectedVal, val)
}
default: // Read passed end
verifyAccErrorInt(t, sr, int(val))
}
}
sr = bits.NewFixedSliceReader(data)
nrVals = nrBytes / 2
for i := 0; i < nrVals+2; i++ {
val := sr.ReadInt16()
switch {
case i < nrVals:
expectedVal := int16(expVal(i, 2))
if val != expectedVal {
t.Errorf("val at %d not %d but %d", i, expectedVal, val)
}
default: // Read passed end
verifyAccErrorInt(t, sr, int(val))
}
}
sr = bits.NewFixedSliceReader(data)
nrVals = nrBytes / 3
for i := 0; i < nrVals+2; i++ {
val := sr.ReadUint24()
switch {
case i < nrVals:
expectedVal := uint32(expVal(i, 3))
if val != expectedVal {
t.Errorf("val at %d not %d but %d", i, expectedVal, val)
}
default: // Read passed end
verifyAccErrorInt(t, sr, int(val))
}
}
sr = bits.NewFixedSliceReader(data)
nrVals = nrBytes / 4
for i := 0; i < nrVals+2; i++ {
val := sr.ReadUint32()
switch {
case i < nrVals:
expectedVal := uint32(expVal(i, 4))
if val != expectedVal {
t.Errorf("val at %d not %d but %d", i, expectedVal, val)
}
default: // Read passed end
verifyAccErrorInt(t, sr, int(val))
}
}
sr = bits.NewFixedSliceReader(data)
nrVals = nrBytes / 4
for i := 0; i < nrVals+2; i++ {
val := sr.ReadInt32()
switch {
case i < nrVals:
expectedVal := int32(expVal(i, 4))
if val != expectedVal {
t.Errorf("val at %d not %d but %d", i, expectedVal, val)
}
default: // Read passed end
verifyAccErrorInt(t, sr, int(val))
}
}
sr = bits.NewFixedSliceReader(data)
nrVals = nrBytes / 8
for i := 0; i < nrVals+2; i++ {
val := sr.ReadUint64()
switch {
case i < nrVals:
expectedVal := uint64(expVal(i, 8))
if val != expectedVal {
t.Errorf("val at %d not %d but %d", i, expectedVal, val)
}
default: // Read passed end
verifyAccErrorInt(t, sr, int(val))
}
}
sr = bits.NewFixedSliceReader(data)
nrVals = nrBytes / 8
for i := 0; i < nrVals+2; i++ {
val := sr.ReadInt64()
switch {
case i < nrVals:
expectedVal := int64(expVal(i, 8))
if val != int64(expVal(i, 8)) {
t.Errorf("val at %d not %d but %d", i, expectedVal, val)
}
default: // Read passed end
verifyAccErrorInt(t, sr, int(val))
}
}
})
t.Run("read strings", func(t *testing.T) {
data := []byte("0123456789\x00abcdef")
nrBytes := len(data)
sr := bits.NewFixedSliceReader(data)
if sr.Length() != nrBytes {
t.Errorf("sliceReader length = %d instead of %d", sr.Length(), nrBytes)
}
val := sr.ReadFixedLengthString(4)
if val != "0123" {
t.Errorf(`read string is %q instead of "0123"`, val)
}
val = sr.ReadZeroTerminatedString(10)
if val != "456789" {
t.Errorf(`read string is %q instead of "456789"`, val)
}
val = sr.ReadZeroTerminatedString(2)
verifyAccErrorString(t, sr, val)
val = sr.ReadFixedLengthString(2)
verifyAccErrorString(t, sr, val)
val = sr.ReadZeroTerminatedString(2)
verifyAccErrorString(t, sr, val)
sr = bits.NewFixedSliceReader(data)
val = sr.ReadFixedLengthString(nrBytes + 2)
verifyAccErrorString(t, sr, val)
})
t.Run("read bytes", func(t *testing.T) {
data := []byte("0123456789abcdef")
nrBytes := len(data)
sr := bits.NewFixedSliceReader(data)
if sr.Length() != nrBytes {
t.Errorf("sliceReader length = %d instead of %d", sr.Length(), nrBytes)
}
val := sr.ReadBytes(4)
if string(val) != "0123" {
t.Errorf(`read bytes are %q instead of "0123"`, string(val))
}
if sr.NrRemainingBytes() != nrBytes-4 {
t.Errorf("nr remaining = %d instead of %d", sr.NrRemainingBytes(), nrBytes-4)
}
sr.SkipBytes(2)
if sr.NrRemainingBytes() != nrBytes-6 {
t.Errorf("nr remaining = %d instead of %d", sr.NrRemainingBytes(), nrBytes-6)
}
sr.SetPos(8)
if sr.NrRemainingBytes() != nrBytes-8 {
t.Errorf("nr remaining = %d instead of %d", sr.NrRemainingBytes(), nrBytes-8)
}
lookAhead := make([]byte, 8)
err := sr.LookAhead(0, lookAhead)
if err != nil {
t.Error(err)
}
if string(lookAhead) != "89abcdef" {
t.Errorf(`lookahead %q instead of "89abcdef"`, lookAhead)
}
lookAhead = make([]byte, 9)
err = sr.LookAhead(0, lookAhead)
if err == nil {
t.Errorf("should be out of bounds")
}
remaining := sr.RemainingBytes()
if string(remaining) != "89abcdef" {
t.Errorf(`remaining %q instead of "89abcdef"`, remaining)
}
// Beyond end
val = sr.ReadBytes(2)
verifyAccErrorBytes(t, sr, val)
val = sr.ReadBytes(2)
verifyAccErrorBytes(t, sr, val)
val = sr.RemainingBytes()
verifyAccErrorBytes(t, sr, val)
valNr := sr.NrRemainingBytes()
verifyAccErrorInt(t, sr, valNr)
sr.SkipBytes(2)
verifyAccErrorInt(t, sr, 0)
sr = bits.NewFixedSliceReader(data)
sr.SkipBytes(nrBytes + 2)
verifyAccErrorInt(t, sr, 0)
sr.SetPos(nrBytes + 2)
wantedErrMsg := fmt.Sprintf("attempt to set pos %d beyond slice len %d", nrBytes+2, nrBytes)
if sr.AccError().Error() != wantedErrMsg {
t.Errorf("got error msg %q instead of %q", sr.AccError().Error(), wantedErrMsg)
}
})
t.Run("read possibly zero terminated string", func(t *testing.T) {
data := []byte("hej\x00")
sr := bits.NewFixedSliceReader(data)
_, ok := sr.ReadPossiblyZeroTerminatedString(-1)
if ok {
t.Errorf("got ok but impossible")
}
val, ok := sr.ReadPossiblyZeroTerminatedString(0)
if !ok || val != "" {
t.Errorf("got %q instead of empty string", val)
}
val, ok = sr.ReadPossiblyZeroTerminatedString(4)
if !ok || val != "hej" {
t.Errorf("got %q instead of 'hej'", val)
}
})
}
func verifyAccErrorInt(t *testing.T, sr *bits.FixedSliceReader, val int) {
t.Helper()
if sr.AccError() == nil {
t.Errorf("should have had an accumulated error")
}
if val != 0 {
t.Errorf("should have zero value")
}
}
func verifyAccErrorString(t *testing.T, sr *bits.FixedSliceReader, val string) {
t.Helper()
if sr.AccError() == nil {
t.Errorf("should have had an accumulated error")
}
if val != "" {
t.Errorf("should be empty string")
}
}
func verifyAccErrorBytes(t *testing.T, sr *bits.FixedSliceReader, val []byte) {
if sr.AccError() == nil {
t.Errorf("should have had an accumulated error")
}
if len(val) != 0 {
t.Errorf("should be empty slice")
}
}
func expVal(i, nrBytes int) (val uint64) {
for j := 0; j < nrBytes; j++ {
byteVal := uint64(i*nrBytes + j)
val += byteVal << (8 * (nrBytes - 1 - j))
}
return val
}