-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbytes_test.go
356 lines (319 loc) · 9.74 KB
/
bytes_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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package pack_test
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"reflect"
"testing/quick"
"github.com/renproject/pack"
"github.com/renproject/pack/packutil"
"github.com/renproject/surge/surgeutil"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Bytes", func() {
numTrials := 10
ts := []reflect.Type{
reflect.TypeOf(pack.NewString("")),
reflect.TypeOf(pack.NewBytes([]byte{})),
reflect.TypeOf(pack.NewBytes32([32]byte{})),
reflect.TypeOf(pack.NewBytes65([65]byte{})),
}
for _, t := range ts {
t := t
Context(fmt.Sprintf("when fuzzing %v", t), func() {
It("should not panic", func() {
Expect(func() { surgeutil.Fuzz(t) }).ToNot(Panic())
Expect(func() { packutil.JSONFuzz(t) }).ToNot(Panic())
})
})
Context(fmt.Sprintf("when marshaling and then unmarshaling %v", t), func() {
It("should return itself", func() {
for trial := 0; trial < numTrials; trial++ {
Expect(surgeutil.MarshalUnmarshalCheck(t)).To(Succeed())
Expect(packutil.JSONMarshalUnmarshalCheck(t)).To(Succeed())
}
})
})
Context(fmt.Sprintf("when marshaling %v", t), func() {
Context("when the buffer is too small", func() {
It("should return itself", func() {
for trial := 0; trial < numTrials; trial++ {
Expect(surgeutil.MarshalBufTooSmall(t)).To(Succeed())
}
})
})
Context("when the remaining memory quota is too small", func() {
It("should return itself", func() {
for trial := 0; trial < numTrials; trial++ {
Expect(surgeutil.MarshalRemTooSmall(t)).To(Succeed())
}
})
})
})
Context(fmt.Sprintf("when unmarshaling %v", t), func() {
Context("when the buffer is too small", func() {
It("should return itself", func() {
for trial := 0; trial < numTrials; trial++ {
Expect(surgeutil.UnmarshalBufTooSmall(t)).To(Succeed())
}
})
})
Context("when the remaining memory quota is too small", func() {
It("should return itself", func() {
for trial := 0; trial < numTrials; trial++ {
Expect(surgeutil.UnmarshalRemTooSmall(t)).To(Succeed())
}
})
})
})
}
Context("when unmarshaling a byte32 array", func() {
Context("when the string represents an array with a different length", func() {
It("should return an error", func() {
f := func(x [31]byte) bool {
data, err := json.Marshal(pack.NewBytes(x[:]))
Expect(err).ToNot(HaveOccurred())
b32 := pack.Bytes32{}
err = json.Unmarshal(data, &b32)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("expected len=32"))
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
})
Context("when unmarshaling a byte65 array", func() {
Context("when the string represents an array with a different length", func() {
It("should return an error", func() {
f := func(x [64]byte) bool {
data, err := json.Marshal(pack.NewBytes(x[:]))
Expect(err).ToNot(HaveOccurred())
b65 := pack.Bytes65{}
err = json.Unmarshal(data, &b65)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("expected len=65"))
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
})
Context("when creating a nil byte slice", func() {
It("should return empty bytes", func() {
Expect([]byte(pack.NewBytes(nil))).ToNot(BeNil())
Expect([]byte(pack.NewBytes(nil))).To(HaveLen(0))
})
})
Context("when comparing strings", func() {
Context("when the strings are equal", func() {
It("should return true", func() {
f := func(x string) bool {
Expect(pack.NewString(x).Equal(pack.NewString(x))).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when the strings are not equal", func() {
It("should return true", func() {
f := func(x, y string) bool {
if x == y {
return true
}
Expect(pack.NewString(x).Equal(pack.NewString(y))).To(BeFalse())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
})
Context("when comparing byte slices", func() {
Context("when the byte slices are equal", func() {
It("should return true", func() {
f := func(x []byte) bool {
Expect(pack.NewBytes(x).Equal(pack.NewBytes(x))).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when the byte slices are not equal", func() {
It("should return true", func() {
f := func(x, y []byte) bool {
if bytes.Equal(x, y) {
return true
}
Expect(pack.NewBytes(x).Equal(pack.NewBytes(y))).To(BeFalse())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
})
Context("when comparing byte32 arrays", func() {
Context("when the byte32 arrays are equal", func() {
It("should return true", func() {
f := func(x [32]byte) bool {
other := pack.NewBytes32(x)
Expect(pack.NewBytes32(x).Equal(&other)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when the byte32 arrays are not equal", func() {
It("should return true", func() {
f := func(x, y [32]byte) bool {
if bytes.Equal(x[:], y[:]) {
return true
}
other := pack.NewBytes32(y)
Expect(pack.NewBytes32(x).Equal(&other)).To(BeFalse())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
})
Context("when comparing byte65 arrays", func() {
Context("when the byte65 arrays are equal", func() {
It("should return true", func() {
f := func(x [65]byte) bool {
other := pack.NewBytes65(x)
Expect(pack.NewBytes65(x).Equal(&other)).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when the byte65 arrays are not equal", func() {
It("should return true", func() {
f := func(x, y [65]byte) bool {
if bytes.Equal(x[:], y[:]) {
return true
}
other := pack.NewBytes65(y)
Expect(pack.NewBytes65(x).Equal(&other)).To(BeFalse())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
})
Context("when marshaling and unmarshaling byte slices to and from text", func() {
It("should return itself", func() {
f := func(x pack.Bytes) bool {
data, err := x.MarshalText()
Expect(err).ToNot(HaveOccurred())
y := pack.Bytes{}
err = y.UnmarshalText(data)
Expect(err).ToNot(HaveOccurred())
return x.Equal(y)
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when marshaling and unmarshaling byte32 arrays to and from text", func() {
It("should return itself", func() {
f := func(x pack.Bytes32) bool {
data, err := x.MarshalText()
Expect(err).ToNot(HaveOccurred())
y := pack.Bytes32{}
err = y.UnmarshalText(data)
Expect(err).ToNot(HaveOccurred())
return x.Equal(&y)
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when marshaling and unmarshaling byte65 arrays to and from text", func() {
It("should return itself", func() {
f := func(x pack.Bytes65) bool {
data, err := x.MarshalText()
Expect(err).ToNot(HaveOccurred())
y := pack.Bytes65{}
err = y.UnmarshalText(data)
Expect(err).ToNot(HaveOccurred())
return x.Equal(&y)
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when stringifying strings", func() {
It("should return itself", func() {
f := func(x string) bool {
Expect(pack.NewString(x).String()).To(Equal(x))
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when stringifying byte slices", func() {
It("should return raw URL base64 encodings", func() {
f := func(x []byte) bool {
Expect(pack.NewBytes(x).String()).To(Equal(base64.RawURLEncoding.EncodeToString(x)))
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when stringifying byte32 arrays", func() {
It("should return raw URL base64 encodings", func() {
f := func(x [32]byte) bool {
Expect(pack.NewBytes32(x).String()).To(Equal(base64.RawURLEncoding.EncodeToString(x[:])))
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when getting bytes from a byte32 array", func() {
It("should a copy of the underlying bytes", func() {
f := func(x [32]byte) bool {
Expect(bytes.Equal(pack.NewBytes32(x).Bytes(), x[:])).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when stringifying byte65 slices", func() {
It("should return raw URL base64 encodings", func() {
f := func(x [65]byte) bool {
Expect(pack.NewBytes65(x).String()).To(Equal(base64.RawURLEncoding.EncodeToString(x[:])))
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when getting bytes from a byte65 array", func() {
It("should a copy of the underlying bytes", func() {
f := func(x [65]byte) bool {
Expect(bytes.Equal(pack.NewBytes65(x).Bytes(), x[:])).To(BeTrue())
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
})
})
Context("when getting type information for strings", func() {
It("should return the string type", func() {
Expect(pack.NewString("").Type().Kind()).To(Equal(pack.KindString))
})
})
Context("when getting type information for byte slices", func() {
It("should return the bytes type", func() {
Expect(pack.NewBytes(nil).Type().Kind()).To(Equal(pack.KindBytes))
})
})
Context("when getting type information for byte32 arrays", func() {
It("should return the 32-byte array type", func() {
Expect(pack.NewBytes32([32]byte{}).Type().Kind()).To(Equal(pack.KindBytes32))
})
})
Context("when getting type information for byte65 arrays", func() {
It("should return the 65-byte array type", func() {
Expect(pack.NewBytes65([65]byte{}).Type().Kind()).To(Equal(pack.KindBytes65))
})
})
})