-
Notifications
You must be signed in to change notification settings - Fork 2
/
backend_test.go
631 lines (588 loc) · 18.2 KB
/
backend_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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
package erasurecode
import (
"bytes"
"fmt"
"hash/crc32"
"math/rand"
"os"
"sync"
"testing"
)
var validParamGroups = []struct {
name string
params []Params
}{
{"liberasurecode", []Params{
{Name: "liberasurecode_rs_vand", K: 2, M: 1},
{Name: "liberasurecode_rs_vand", K: 10, M: 4},
{Name: "liberasurecode_rs_vand", K: 4, M: 3},
{Name: "liberasurecode_rs_vand", K: 8, M: 4},
{Name: "liberasurecode_rs_vand", K: 15, M: 4},
}},
{"isa_l", []Params{
{Name: "isa_l_rs_vand", K: 2, M: 1},
{Name: "isa_l_rs_vand", K: 10, M: 4},
{Name: "isa_l_rs_vand", K: 4, M: 3},
{Name: "isa_l_rs_vand", K: 8, M: 4},
{Name: "isa_l_rs_vand", K: 15, M: 4},
{Name: "isa_l_rs_cauchy", K: 2, M: 1},
{Name: "isa_l_rs_cauchy", K: 10, M: 4},
{Name: "isa_l_rs_cauchy", K: 4, M: 3},
{Name: "isa_l_rs_cauchy", K: 8, M: 4},
{Name: "isa_l_rs_cauchy", K: 15, M: 4},
}},
{"jerasure", []Params{
{Name: "jerasure_rs_vand", K: 2, M: 1},
{Name: "jerasure_rs_vand", K: 10, M: 4},
{Name: "jerasure_rs_vand", K: 4, M: 3},
{Name: "jerasure_rs_vand", K: 8, M: 4},
{Name: "jerasure_rs_vand", K: 15, M: 4},
{Name: "jerasure_rs_cauchy", K: 2, M: 1},
{Name: "jerasure_rs_cauchy", K: 10, M: 4},
{Name: "jerasure_rs_cauchy", K: 4, M: 3},
{Name: "jerasure_rs_cauchy", K: 8, M: 4},
{Name: "jerasure_rs_cauchy", K: 15, M: 4, W: 5},
}},
}
var testPatterns = [][]byte{
bytes.Repeat([]byte{0x00}, 1),
bytes.Repeat([]byte{0xff}, 1),
bytes.Repeat([]byte{0x00}, 1<<10),
bytes.Repeat([]byte{0xff}, 1<<10),
bytes.Repeat([]byte{0x00}, 1<<20),
bytes.Repeat([]byte{0xff}, 1<<20),
bytes.Repeat([]byte{0xf0, 0x0f}, 512),
bytes.Repeat([]byte{0xde, 0xad, 0xbe, 0xef}, 256),
bytes.Repeat([]byte{0xaa}, 1024),
bytes.Repeat([]byte{0x55}, 1024),
}
func shuf(src [][]byte) [][]byte {
dest := make([][]byte, len(src))
perm := rand.Perm(len(src))
for i, v := range perm {
dest[v] = src[i]
}
return dest
}
func TestGetVersion(t *testing.T) {
v := GetVersion()
t.Logf("INFO: Using liberasurecode version %s", v)
if v.Major != 1 {
t.Errorf("Expected major version number 1, not %d", v.Major)
}
if v.Less(Version{1, 4, 0}) {
t.Errorf("liberasurecode_get_version was introduced in 1.4.0; got %v", v)
}
}
func TestInitBackend(t *testing.T) {
for _, group := range validParamGroups {
for _, params := range group.params {
backend, err := InitBackend(params)
if !BackendIsAvailable(params.Name) {
if err == nil {
t.Errorf("Expected EBACKENDNOTAVAIL")
}
continue
}
if err != nil {
t.Errorf("%q", err)
continue
}
if backend.libecDesc <= 0 {
t.Errorf("Expected backend descriptor > 0, got %d", backend.libecDesc)
}
if err = backend.Close(); err != nil {
t.Errorf("%q", err)
}
if err = backend.Close(); err == nil {
t.Errorf("Expected error when closing an already-closed backend.")
}
}
}
}
func TestInitBackendFailure(t *testing.T) {
cases := []struct {
params Params
want string
}{
{Params{Name: "liberasurecode_rs_vand", K: -1, M: 1},
"instance_create() returned EINVALIDPARAMS"},
{Params{Name: "liberasurecode_rs_vand", K: 10, M: -1},
"instance_create() returned EINVALIDPARAMS"},
{Params{Name: "non-existent-backend", K: 10, M: 4},
"unsupported backend \"non-existent-backend\""},
{Params{Name: "", K: 10, M: 4},
"unsupported backend \"\""},
{Params{Name: "liberasurecode_rs_vand", K: 20, M: 20},
"instance_create() returned EINVALIDPARAMS"},
{Params{Name: "flat_xor_hd", K: 4, M: 4, HD: 3},
"instance_create() returned EBACKENDINITERR"},
}
for _, args := range cases {
backend, err := InitBackend(args.params)
if err == nil {
t.Errorf("Expected error when calling InitBackend(%v)",
args.params)
_ = backend.Close()
continue
}
if err.Error() != args.want {
t.Errorf("InitBackend(%v) produced error %q, want %q",
args.params, err, args.want)
}
if backend.libecDesc != 0 {
t.Errorf("InitBackend(%v) produced backend with descriptor %v, want 0",
args.params, backend.libecDesc)
_ = backend.Close()
}
}
}
func TestEncodeDecode(t *testing.T) {
for _, group := range validParamGroups {
t.Run(group.name, func(t *testing.T) {
var ranOne = false
for _, params := range group.params {
if !BackendIsAvailable(params.Name) {
continue
}
ranOne = true
backend, err := InitBackend(params)
if err != nil {
t.Errorf("Error creating backend %v: %q", params, err)
continue
}
for patternIndex, pattern := range testPatterns {
frags, err := backend.Encode(pattern)
if err != nil {
t.Errorf("Error encoding %v: %q", params, err)
break
}
expectedVersion := GetVersion()
for index, frag := range frags {
info := GetFragmentInfo(frag)
if info.Index != index {
t.Errorf("Expected frag %v to have index %v; got %v", index, index, info.Index)
}
if info.Size != len(frag)-80 { // 80 == sizeof (struct fragment_header_s)
t.Errorf("Expected frag %v to have size %v; got %v", index, len(frag)-80, info.Size)
}
if info.OrigDataSize != uint64(len(pattern)) {
t.Errorf("Expected frag %v to have orig_data_size %v; got %v", index, len(pattern), info.OrigDataSize)
}
if info.BackendName != params.Name {
t.Errorf("Expected frag %v to have backend %v; got %v", index, params.Name, info.BackendName)
}
if info.ErasureCodeVersion != expectedVersion {
t.Errorf("Expected frag %v to have EC version %v; got %v", index, expectedVersion, info.ErasureCodeVersion)
}
if !info.IsValid {
t.Errorf("Expected frag %v to be valid", index)
}
if !expectedVersion.Less(Version{1, 6, 0}) {
if expectedChecksum := crc32.ChecksumIEEE(frag[0:59]); info.MetadataChecksum != expectedChecksum {
t.Errorf("Expected frag %v to have metadata CRC %x; got %x", index, expectedChecksum, info.MetadataChecksum)
}
}
}
decode := func(frags [][]byte, description string) bool {
data, err := backend.Decode(frags)
if err != nil {
t.Errorf("%v: %v: %q for pattern %d", description, backend, err, patternIndex)
return false
} else if !bytes.Equal(data, pattern) {
t.Errorf("%v: Expected %v to roundtrip pattern %d, got %q", description, backend, patternIndex, data)
return false
}
return true
}
var good bool
good = decode(frags, "all frags")
good = good && decode(shuf(frags), "all frags, shuffled")
good = good && decode(frags[:params.K], "data frags")
good = good && decode(shuf(frags[:params.K]), "shuffled data frags")
good = good && decode(frags[params.M:], "with parity frags")
good = good && decode(shuf(frags[params.M:]), "shuffled parity frags")
if !good {
// If one pattern fails to round-trip, no point trying others for these args
break
}
}
if _, err := backend.Decode([][]byte{}); err == nil {
t.Errorf("Expected error when decoding from empty fragment array")
}
err = backend.Close()
if err != nil {
t.Errorf("Error closing backend %v: %q", backend, err)
}
}
if !ranOne {
t.Skip()
}
})
}
}
func TestReconstruct(t *testing.T) {
for _, group := range validParamGroups {
t.Run(group.name, func(t *testing.T) {
var ranOne = false
for _, params := range group.params {
if !BackendIsAvailable(params.Name) {
continue
}
ranOne = true
backend, err := InitBackend(params)
if err != nil {
t.Errorf("Error creating backend %v: %q", params, err)
_ = backend.Close()
continue
}
for patternIndex, pattern := range testPatterns {
frags, err := backend.Encode(pattern)
if err != nil {
t.Errorf("Error encoding %v: %q", params, err)
}
reconstruct := func(recon_frags [][]byte, frag_index int, description string) bool {
data, err := backend.Reconstruct(recon_frags, frag_index)
if err != nil {
t.Errorf("%v: %v: %q for pattern %d", description, backend, err, patternIndex)
return false
} else if !bytes.Equal(data, frags[frag_index]) {
t.Errorf("%v: Expected %v to roundtrip pattern %d, got %q", description, backend, patternIndex, data)
return false
}
return true
}
var good bool
good = reconstruct(shuf(frags[:params.K]), params.K+params.M-1, "last frag from data frags")
good = good && reconstruct(shuf(frags[params.M:]), 0, "first frag with parity frags")
if !good {
break
}
}
if _, err := backend.Reconstruct([][]byte{}, 0); err == nil {
t.Errorf("Expected error when reconstructing from empty fragment array")
}
err = backend.Close()
if err != nil {
t.Errorf("Error closing backend %v: %q", backend, err)
}
}
if !ranOne {
t.Skip()
}
})
}
}
func TestIsInvalidFragment(t *testing.T) {
for _, group := range validParamGroups {
t.Run(group.name, func(t *testing.T) {
var ranOne = false
for _, params := range group.params {
if !BackendIsAvailable(params.Name) {
continue
}
ranOne = true
backend, err := InitBackend(params)
if err != nil {
t.Errorf("Error creating backend %v: %q", params, err)
_ = backend.Close()
continue
}
for patternIndex, pattern := range testPatterns {
frags, err := backend.Encode(pattern)
if err != nil {
t.Errorf("Error encoding %v: %q", params, err)
continue
}
for index, frag := range frags {
if backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly invalid for pattern %d", backend, index, patternIndex)
}
fragCopy := make([]byte, len(frag))
copy(fragCopy, frag)
// corrupt the frag
corruptedByte := rand.Intn(len(frag))
for 71 <= corruptedByte && corruptedByte < 80 {
// in the alignment padding -- try again
corruptedByte = rand.Intn(len(frag))
}
frag[corruptedByte] ^= 0xff
if !backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly valid after inverting byte %d for pattern %d", backend, index, corruptedByte, patternIndex)
}
if corruptedByte < 4 || 8 <= corruptedByte && corruptedByte <= 59 {
/** corruption is in metadata; claim we were created by a version of
* libec that predates metadata checksums. Note that
* Note that a corrupted fragment size (bytes 4-7) will lead to a
* segfault when we try to verify the fragment -- there's a reason
* we added metadata checksums!
*/
copy(frag[63:67], []byte{9, 1, 1, 0})
if 20 <= corruptedByte && corruptedByte <= 53 {
/** Corrupted data checksum type or data checksum
* We may or may not detect this type of error; in particular,
* - if data checksum type is not in ec_checksum_type_t,
* it is ignored
* - if data checksum is mangled, we may still be valid
* under the "alternative" CRC32; this seems more likely
* with the byte inversion when the data is short
* Either way, though, clearing the checksum type should make
* it pass.
*/
frag[20] = 0
if backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly invalid after clearing metadata crc and disabling data crc", backend, index)
}
} else if corruptedByte >= 54 || 0 <= corruptedByte && corruptedByte < 4 {
/** Some corruptions of some bytes are still detectable. Since we're
* inverting the byte, we can detect:
* - frag index -- bytes 0-3
* - data checksum type -- byte 20
* - data checksum mismatch -- byte 54
* - backend id -- byte 55
* - backend version -- bytes 56-59
*/
if !backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly still valid after clearing metadata crc", backend, index)
}
} else {
if backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly invalid after clearing metadata crc", backend, index)
}
}
} else if corruptedByte >= 67 {
copy(frag[20:25], []byte{1, 0, 0, 0, 0})
// And since we've changed the metadata, roll back version as above...
copy(frag[63:67], []byte{9, 1, 1, 0})
if backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly invalid after clearing data crc", backend, index)
t.FailNow()
}
}
frag[corruptedByte] ^= 0xff
copy(frag[63:67], fragCopy[63:67])
copy(frag[20:25], fragCopy[20:25])
if !bytes.Equal(frag, fragCopy) {
for i, orig := range fragCopy {
if frag[i] != orig {
t.Logf("%v != %v at index %v", frag[i], orig, i)
}
}
t.Fatal(corruptedByte, frag, fragCopy)
}
frag[corruptedByte]++
if !backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly valid after incrementing byte %d for pattern %d", backend, index, corruptedByte, patternIndex)
}
frag[corruptedByte] -= 2
if corruptedByte >= 63 && corruptedByte < 67 && frag[corruptedByte] != 0xff {
if backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly invalid after decrementing version byte %d for pattern %d", backend, index, corruptedByte, patternIndex)
}
} else {
if !backend.IsInvalidFragment(frag) {
t.Errorf("%v: frag %v unexpectedly valid after decrementing byte %d for pattern %d", backend, index, corruptedByte, patternIndex)
}
}
}
}
err = backend.Close()
if err != nil {
t.Errorf("Error closing backend %v: %q", backend, err)
}
}
if !ranOne {
t.Skip()
}
})
}
}
func TestBackendIsAvailable(t *testing.T) {
requiredBackends := []string{
"null",
"flat_xor_hd",
"liberasurecode_rs_vand",
}
optionalBackends := []string{
"isa_l_rs_vand",
"isa_l_rs_cauchy",
"jerasure_rs_vand",
"jerasure_rs_cauchy",
"shss",
"libphazr",
}
for _, name := range requiredBackends {
if !BackendIsAvailable(name) {
t.Errorf("%v is not available", name)
}
}
for _, name := range optionalBackends {
if !BackendIsAvailable(name) {
t.Logf("INFO: backend not available: %v", name)
}
}
name := "fake-backend"
if BackendIsAvailable(name) {
t.Errorf("%v unexpectedly available", name)
}
}
// TestGC do multiple decode / reconstruct in concurrent goroutine.
// When enough data has been allocated and freed, the GC will wakeup
// and may destroy a block in-use. If this block is re-allocated,
// the content is most likely no longer valid and it should
// trigger an error or a crash.
func TestGC(t *testing.T) {
input := bytes.Repeat([]byte("X"), 1000000)
backend, err := InitBackend(
Params{
Name: "liberasurecode_rs_vand",
K: 2,
M: 1,
})
if err != nil {
t.Logf("Cannot run test because %s", err)
return
}
tests := []struct {
name string
testFunc func()
}{
struct {
name string
testFunc func()
}{
"Reconstruct",
func() {
vect, err := backend.Encode(input)
if err != nil {
t.Fatal("cannot encode data")
return
}
oldData := vect[0][:] // force a copy
data, err := backend.Reconstruct(vect[1:3], 0)
if err != nil {
t.Fatalf("cannot reconstruct data, %s", err)
return
}
if len(data) != len(oldData) {
t.Fatal("reconstructing failed")
return
}
},
},
struct {
name string
testFunc func()
}{
"Decode",
func() {
vect, err := backend.Encode(input)
if err != nil {
t.Fatal("cannot encode data")
return
}
data, err := backend.Decode(vect[0:2])
if err != nil {
t.Fatalf("cannot decode data: %v", err)
return
}
if len(data) != len(input) {
t.Fatal("decoding failed")
return
}
},
},
}
nbRoutines := 500
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var wg sync.WaitGroup
wg.Add(nbRoutines)
for i := 0; i < nbRoutines; i++ {
go func() {
test.testFunc()
wg.Done()
}()
}
wg.Wait()
})
}
}
func TestAvailableBackends(t *testing.T) {
for _, name := range AvailableBackends() {
backend, err := InitBackend(Params{Name: name, K: 3, M: 3, HD: 3})
if err != nil {
t.Errorf("Error creating backend %v: %q", name, err)
}
_ = backend.Close()
}
t.Logf("INFO: found %v/%v available backends", len(AvailableBackends()), len(KnownBackends))
}
func TestLegacyCRC(t *testing.T) {
if GetVersion().Less(Version{1, 6, 0}) {
t.Skip()
}
for _, val := range []string{
"",
"0",
} {
t.Run(fmt.Sprintf("LIBERASURECODE_WRITE_LEGACY_CRC=%s", val), func(t *testing.T) {
os.Setenv("LIBERASURECODE_WRITE_LEGACY_CRC", val)
input := bytes.Repeat([]byte("X"), 1000)
backend, err := InitBackend(
Params{
Name: "liberasurecode_rs_vand",
K: 2,
M: 1,
})
if err != nil {
t.Fatalf("Error creating backend: %q", err)
}
frags, err := backend.Encode(input)
if err != nil {
t.Fatalf("Error encoding frags: %q", err)
}
for index, frag := range frags {
info := GetFragmentInfo(frag)
if expectedChecksum := crc32.ChecksumIEEE(frag[0:59]); info.MetadataChecksum != expectedChecksum {
t.Errorf("Expected frag %v to have metadata CRC %x; got %x", index, expectedChecksum, info.MetadataChecksum)
}
}
})
}
if GetVersion().Less(Version{1, 6, 2}) {
t.Skip()
}
for _, val := range []string{
"1",
"00",
"true",
} {
t.Run(fmt.Sprintf("LIBERASURECODE_WRITE_LEGACY_CRC=%s", val), func(t *testing.T) {
os.Setenv("LIBERASURECODE_WRITE_LEGACY_CRC", val)
input := bytes.Repeat([]byte("X"), 1000)
backend, err := InitBackend(
Params{
Name: "liberasurecode_rs_vand",
K: 2,
M: 1,
})
if err != nil {
t.Fatalf("Error creating backend: %q", err)
}
frags, err := backend.Encode(input)
if err != nil {
t.Fatalf("Error encoding frags: %q", err)
}
for index, frag := range frags {
info := GetFragmentInfo(frag)
if !info.IsValid {
t.Errorf("Expected frag %v to be valid", index)
}
if unexpectedChecksum := crc32.ChecksumIEEE(frag[0:59]); info.MetadataChecksum == unexpectedChecksum {
t.Errorf("Expected frag %v metadata CRC to not use IEEE CRC32, but it did", index)
}
}
})
}
}