-
Notifications
You must be signed in to change notification settings - Fork 8
/
shelf_test.go
693 lines (652 loc) · 17.5 KB
/
shelf_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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
// bagdb: Simple datastorage
// Copyright 2021 billy authors
// SPDX-License-Identifier: BSD-3-Clause
package billy
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
// getBlob returns a byte-slice filled with the given fill-byte
func getBlob(fill byte, size int) []byte {
buf := make([]byte, size)
for i := range buf {
buf[i] = fill
}
return buf
}
func checkBlob(fill byte, blob []byte, size int) error {
if len(blob) != size {
return fmt.Errorf("wrong size: got %d, want %d", len(blob), size)
}
for i := range blob {
if blob[i] != fill {
return fmt.Errorf("wrong data, byte %d: got %x want %x", i, blob[i], fill)
}
}
return nil
}
func TestBasicsInMemory(t *testing.T) { testBasics(t, "") }
func TestBasicsOnDisk(t *testing.T) { testBasics(t, t.TempDir()) }
func testBasics(t *testing.T, path string) {
{ // Pre-instance failures
// can't open non-existing directory
if _, err := openShelf("/baz/bonk/foobar/gazonk", 10, nil, false, false); err == nil {
t.Fatal("expected error")
}
// Can't point path to a file
if _, err := openShelf("./README.md", 10, nil, false, false); err == nil {
t.Fatal("expected error")
}
}
b, cleanup := setup(t, path)
defer cleanup()
{ // Tests on Put
// Should reject empty data
if _, err := b.Put([]byte{}); !errors.Is(err, ErrEmptyData) {
t.Fatal("expected error")
}
// Should reject nil data
if _, err := b.Put(nil); !errors.Is(err, ErrEmptyData) {
t.Fatal("expected error")
}
// Should reject oversized data (max size 200)
if _, err := b.Put(make([]byte, 201)); !errors.Is(err, ErrOversized) {
t.Fatal("expected error")
}
}
aa, _ := b.Put(getBlob(0x0a, 150))
{ // Tests on Update
// Should reject empty data
if err := b.Update([]byte{}, aa); !errors.Is(err, ErrEmptyData) {
t.Fatal("expected error")
}
// Should reject nil data
if err := b.Update(nil, aa); !errors.Is(err, ErrEmptyData) {
t.Fatal("expected error")
}
// max size 200, check oversized data
if err := b.Update(make([]byte, 201), aa); !errors.Is(err, ErrOversized) {
t.Fatal("expected error")
}
}
bb, _ := b.Put(getBlob(0x0b, 151))
cc, _ := b.Put(getBlob(0x0c, 152))
dd, err := b.Put(getBlob(0x0d, 153))
if err != nil {
t.Fatal(err)
}
get := func(slot uint64) []byte {
t.Helper()
data, err := b.Get(slot)
if err != nil {
t.Fatal(err)
}
return data
}
if err := checkBlob(0x0a, get(aa), 150); err != nil {
t.Fatal(err)
}
if err := checkBlob(0x0b, get(bb), 151); err != nil {
t.Fatal(err)
}
if err := checkBlob(0x0c, get(cc), 152); err != nil {
t.Fatal(err)
}
if err := checkBlob(0x0d, get(dd), 153); err != nil {
t.Fatal(err)
}
// Same checks, but during iteration
if err = b.Iterate(func(slot uint64, data []byte) {
if have, want := byte(slot)+0x0a, data[0]; have != want {
t.Fatalf("wrong content: have %x want %x", have, want)
}
if have, want := len(data), int(150+slot); have != want {
t.Fatalf("wrong size: have %x want %x", have, want)
}
}); err != nil {
t.Fatal(err)
}
// Delete item and place a new one there
if err := b.Delete(bb); err != nil {
t.Fatal(err)
}
// Iteration should skip over deleted items
if err = b.Iterate(func(slot uint64, data []byte) {
if have, want := byte(slot)+0x0a, data[0]; have != want {
t.Fatalf("wrong content: have %x want %x", have, want)
}
if have, want := len(data), int(150+slot); have != want {
t.Fatalf("wrong size: have %x want %x", have, want)
}
if slot == bb {
t.Fatalf("Expected not to iterate %d", bb)
}
}); err != nil {
t.Fatal(err)
}
ee, _ := b.Put(getBlob(0x0e, 154))
if err := checkBlob(0x0e, get(ee), 154); err != nil {
t.Fatal(err)
}
// Update in place
if err := b.Update(getBlob(0x0f, 35), ee); err != nil {
t.Fatal(err)
}
if err := checkBlob(0x0f, get(ee), 35); err != nil {
t.Fatal(err)
}
if err := b.Delete(aa); err != nil {
t.Fatal(err)
}
if err := b.Delete(ee); err != nil {
t.Fatal(err)
}
if err := b.Delete(cc); err != nil {
t.Fatal(err)
}
if err := b.Delete(dd); err != nil {
t.Fatal(err)
}
// Iteration should be a no-op
if err := b.Iterate(func(slot uint64, data []byte) {
t.Fatalf("Expected no iteration")
}); err != nil {
t.Fatal(err)
}
}
func writeShelfFile(name string, slotSize int, slotData []byte) error {
f, err := os.Create(name)
if err != nil {
return err
}
if err := binary.Write(f, binary.BigEndian, &shelfHeader{
Magic: Magic,
Version: curVersion,
Slotsize: uint32(slotSize),
}); err != nil {
return err
}
// Fill all the items
for i, byt := range slotData {
if byt == 0 {
continue
}
data := getBlob(byt, slotSize-itemHeaderSize)
slotdata := make([]byte, slotSize)
// write header
binary.BigEndian.PutUint32(slotdata, uint32(slotSize-itemHeaderSize))
copy(slotdata[itemHeaderSize:], data)
// write data
_, err = f.WriteAt(slotdata, int64(ShelfHeaderSize)+int64(i)*int64(slotSize))
if err != nil {
return err
}
}
return nil
}
func checkIdentical(fileA, fileB string) error {
var (
dataA []byte
dataB []byte
err error
)
dataA, err = os.ReadFile(fileA)
if err != nil {
return fmt.Errorf("failed to open %v: %v", fileA, err)
}
dataB, err = os.ReadFile(fileB)
if err != nil {
return fmt.Errorf("failed to open %v: %v", fileB, err)
}
if !bytes.Equal(dataA, dataB) {
return fmt.Errorf("data differs: \n%x\n%x", dataA, dataB)
}
return nil
}
func setup(t *testing.T, path string) (*shelf, func()) {
t.Helper()
a, err := openShelf(path, 200, nil, false, false)
if err != nil {
t.Fatal(err)
}
return a, func() {
a.Close()
}
}
// TestOversized
// - Test writing oversized data into a shelf
// - Test writing exactly-sized data into a shelf
func TestOversizedInMemory(t *testing.T) { testOversized(t, "") }
func TestOversizedOnDisk(t *testing.T) { testOversized(t, t.TempDir()) }
func testOversized(t *testing.T, path string) {
a, cleanup := setup(t, path)
defer cleanup()
for s := 190; s < 205; s++ {
data := getBlob('x', s)
slot, err := a.Put(data)
if err != nil {
if slot != 0 {
t.Fatalf("Exp slot 0 on error, got %d", slot)
}
if have := s + itemHeaderSize; have <= int(a.slotSize) {
t.Fatalf("expected to store %d bytes of data, got error", have)
}
} else {
if have := s + itemHeaderSize; have > int(a.slotSize) {
t.Fatalf("expected error storing %d bytes of data", have)
}
}
}
}
// TestErrOnClose
// - Tests reading, writing, deleting from a closed shelf
func TestErrOnCloseInMemory(t *testing.T) { testErrOnClose(t, "") }
func TestErrOnCloseOnDisk(t *testing.T) { testErrOnClose(t, t.TempDir()) }
func testErrOnClose(t *testing.T, path string) {
a, cleanup := setup(t, path)
defer cleanup()
// Write something and delete it again, to have a gap
if have, want := a.count, uint64(0); have != want {
t.Fatalf("tail error have %v want %v", have, want)
}
_, _ = a.Put(make([]byte, 3))
if have, want := a.count, uint64(1); have != want {
t.Fatalf("tail error have %v want %v", have, want)
}
_, _ = a.Put(make([]byte, 3))
if have, want := a.count, uint64(2); have != want {
t.Fatalf("tail error have %v want %v", have, want)
}
if err := a.Delete(0); err != nil {
t.Fatal(err)
}
if err := a.Close(); err != nil {
t.Fatal(err)
}
// Double-close should be a no-op
if err := a.Close(); err != nil {
t.Fatal(err)
}
if _, err := a.Put(make([]byte, 3)); !errors.Is(err, ErrClosed) {
t.Fatalf("expected error for Put on closed shelf, got %v", err)
}
if err := a.Iterate(nil); !errors.Is(err, ErrClosed) {
t.Fatalf("expected error for Iterate on closed shelf, got %v", err)
}
if _, err := a.Get(0); !errors.Is(err, ErrClosed) {
t.Fatalf("expected error for Get on closed shelf, got %v", err)
}
// Only expectation here is not to panic, basically
if err := a.Delete(0); err != nil {
t.Fatal(err)
}
if err := a.Delete(0); err != nil {
t.Fatal(err)
}
if err := a.Delete(1); err != nil {
t.Fatal(err)
}
if err := a.Delete(100); !errors.Is(err, ErrBadIndex) {
t.Fatal("exp error")
}
}
func TestBadInputInMemory(t *testing.T) { testBadInput(t, "") }
func TestBadInputOnDisk(t *testing.T) { testBadInput(t, t.TempDir()) }
func testBadInput(t *testing.T, path string) {
a, cleanup := setup(t, path)
defer cleanup()
if _, err := a.Put(make([]byte, 25)); err != nil {
t.Fatal(err)
}
if _, err := a.Put(make([]byte, 25)); err != nil {
t.Fatal(err)
}
if _, err := a.Put(make([]byte, 25)); err != nil {
t.Fatal(err)
}
if _, err := a.Put(make([]byte, 25)); err != nil {
t.Fatal(err)
}
if _, err := a.Get(uint64(0x000000FFFFFFFFFF)); !errors.Is(err, ErrBadIndex) {
t.Fatalf("expected %v, got %v", ErrBadIndex, err)
}
if _, err := a.Get(uint64(0xFFFFFFFFFFFFFFFF)); !errors.Is(err, ErrBadIndex) {
t.Fatalf("expected %v, got %v", ErrBadIndex, err)
}
if err := a.Delete(0x000FFFF); !errors.Is(err, ErrBadIndex) {
t.Fatalf("expected %v, got %v", ErrBadIndex, err)
}
if _, err := a.Put(nil); !errors.Is(err, ErrEmptyData) {
t.Fatalf("expected %v", ErrEmptyData)
}
if _, err := a.Put(make([]byte, 0)); !errors.Is(err, ErrEmptyData) {
t.Fatalf("expected %v", ErrEmptyData)
}
}
func TestCompaction(t *testing.T) {
var (
a *shelf
b *shelf
err error
pA = t.TempDir()
pB = t.TempDir()
)
if err = writeShelfFile(filepath.Join(pA, "bkt_00000010.bag"),
10, []byte{1, 0, 3, 0, 5, 0, 6, 0, 4, 0, 2, 0, 0}); err != nil {
t.Fatal(err)
}
if err = writeShelfFile(filepath.Join(pB, "bkt_00000010.bag"),
10, []byte{1, 2, 3, 4, 5, 6}); err != nil {
t.Fatal(err)
}
// The order and content that we expect for the onData
expOnData := []byte{1, 2, 3, 4, 5, 6}
var haveOnData []byte
onData := func(slot uint64, data []byte) {
haveOnData = append(haveOnData, data[0])
}
/// Now open them as shelves
a, err = openShelf(pA, 10, onData, false, false)
if err != nil {
t.Fatal(err)
}
a.Close()
b, err = openShelf(pB, 10, nil, false, false)
if err != nil {
t.Fatal(err)
}
b.Close()
if err := checkIdentical(
filepath.Join(pA, "bkt_00000010.bag"),
filepath.Join(pB, "bkt_00000010.bag")); err != nil {
t.Fatal(err)
}
// And the content of the onData callback
if !bytes.Equal(expOnData, haveOnData) {
t.Fatalf("onData wrong, expected \n%x\ngot\n%x\n", expOnData, haveOnData)
}
}
func TestGapHeap(t *testing.T) {
fill := func(gaps *sortedUniqueInts) {
gaps.Append(uint64(1))
gaps.Append(uint64(10))
gaps.Append(uint64(2))
gaps.Append(uint64(9))
gaps.Append(uint64(3))
gaps.Append(uint64(8))
gaps.Append(uint64(4))
gaps.Append(uint64(7))
gaps.Append(uint64(5))
gaps.Append(uint64(6))
}
gaps := make(sortedUniqueInts, 0)
fill(&gaps)
for i := uint64(10); len(gaps) > 0; i-- {
if have, want := gaps[len(gaps)-1], i; have != want {
t.Fatalf("have %d want %d", have, want)
}
gaps = gaps[:len(gaps)-1]
}
// Check uniqueness filter
fill(&gaps)
fill(&gaps)
fill(&gaps)
for i := uint64(10); len(gaps) > 0; i-- {
if have, want := gaps[len(gaps)-1], i; have != want {
t.Fatalf("have %d want %d", have, want)
}
gaps = gaps[:len(gaps)-1]
}
}
func TestCompaction2(t *testing.T) {
p := t.TempDir()
/// Now open them as shelves
openAndStore := func(data string) {
a, err := openShelf(p, 10, nil, false, false)
if err != nil {
t.Fatal(err)
}
if _, err := a.Put([]byte(data)); err != nil {
t.Fatal(err)
}
if err := a.Close(); err != nil {
t.Fatal(err)
}
}
openAndIterate := func() string {
var data []byte
_, err := openShelf(p, 10, func(slot uint64, x []byte) {
data = append(data, x...)
}, false, false)
if err != nil {
t.Fatal(err)
}
return string(data)
}
openAndDel := func(deletes ...int) {
a, err := openShelf(p, 10, nil, false, false)
if err != nil {
t.Fatal(err)
}
for _, id := range deletes {
if err := a.Delete(uint64(id)); err != nil {
t.Fatal(err)
}
}
if err := a.Close(); err != nil {
t.Fatal(err)
}
}
openAndStore("000000")
openAndStore("111111")
openAndStore("222222")
openAndStore("333333")
openAndStore("444444")
if have, want := openAndIterate(), "000000111111222222333333444444"; have != want {
t.Fatalf("have %v\nwant %v", have, want)
}
openAndDel(1)
// If we delete 1, then the last item should be moved into the gap
if have, want := openAndIterate(), "000000444444222222333333"; have != want {
t.Fatalf("have %v want %v", have, want)
}
openAndDel(1, 2)
// If we delete 1, then the last item should be moved into the gap
if have, want := openAndIterate(), "000000333333"; have != want {
t.Fatalf("have %v want %v", have, want)
}
}
func TestShelfRO(t *testing.T) {
p := t.TempDir()
a, err := openShelf(p, 20, nil, false, false)
if err != nil {
t.Fatal(err)
}
_, err = a.Put(make([]byte, 17))
if !errors.Is(err, ErrOversized) {
t.Fatalf("expected error")
}
// Put some items there, then delete the second to create a gap
// When we later open it as readonly, there should be mo compaction
// (so a gap at index 2), but all items should be iterated
_, err = a.Put(make([]byte, 5)) // id 0, size 5
if err != nil {
t.Fatal(err)
}
_, _ = a.Put(make([]byte, 6)) // id 1, size 6
id, _ := a.Put(make([]byte, 7)) // id 2, size 7
_, _ = a.Put(make([]byte, 8)) // id 3, size 8
_, _ = a.Put(make([]byte, 9)) // id 4, size 9
if err := a.Delete(id); err != nil {
t.Fatal(err)
}
if err := a.Close(); err != nil {
t.Fatal(err)
}
// READONLY
out := new(strings.Builder)
a, err = openShelf(p, 20, func(slot uint64, data []byte) {
fmt.Fprintf(out, "%d:%d, ", slot, len(data))
}, true, false)
if err != nil {
t.Fatal(err)
}
want := "0:5, 1:6, 3:8, 4:9, "
have := out.String()
if have != want {
t.Fatalf("have '%v'\nwant: '%v'\n", have, want)
}
if _, err := a.Put(make([]byte, 10)); !errors.Is(err, ErrReadonly) {
t.Fatal("Expected error")
}
if err := a.Delete(0); !errors.Is(err, ErrReadonly) {
t.Fatal("Expected error")
}
if err := a.Update(nil, 0); !errors.Is(err, ErrReadonly) {
t.Fatal("Expected error")
}
if err = a.Close(); err != nil {
t.Fatal(err)
}
// READ/WRITE
// We now expect the last data (4:9) to be moved to slot 2
out = new(strings.Builder)
a, err = openShelf(p, 20, func(slot uint64, data []byte) {
fmt.Fprintf(out, "%d:%d, ", slot, len(data))
}, false, false)
if err != nil {
t.Fatal(err)
}
want = "0:5, 1:6, 2:9, 3:8, "
have = out.String()
if have != want {
t.Fatalf("have '%v'\nwant: '%v'\n", have, want)
}
if err = a.Close(); err != nil {
t.Fatal(err)
}
}
func TestDelete(t *testing.T) {
p := t.TempDir()
a, err := openShelf(p, 20, nil, false, false)
if err != nil {
t.Fatal(err)
}
checkSize := func(want int) {
finfo, _ := a.f.Stat()
if have := finfo.Size(); int(have) != want {
t.Fatalf("want size %d, have %d", want, have)
}
}
checkSize(ShelfHeaderSize)
// Write 100 items
for i := 0; i < 100; i++ {
_, _ = a.Put(make([]byte, 15)) // id 1, size 6
}
checkSize(2000 + ShelfHeaderSize)
// Delete half of them, last item first
for i := 99; i >= 50; i-- {
_ = a.Delete(uint64(i))
}
checkSize(1000 + ShelfHeaderSize)
// Delete every second remaining item
for i := 50; i >= 0; i -= 2 {
_ = a.Delete(uint64(i))
}
checkSize(1000 + ShelfHeaderSize)
var (
have string
want = "1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,"
)
err = a.Iterate(func(slot uint64, data []byte) {
have = have + fmt.Sprintf("%d,", slot)
})
if err != nil {
t.Fatal(err)
}
if have != want {
t.Fatalf("have: %v\nwant: %v\n", have, want)
}
}
func TestVersion(t *testing.T) {
// header for 255-sized shelf
var (
size = uint32(100)
fname = fmt.Sprintf("bkt_%08d.bag", 100)
p = t.TempDir()
)
for i, tc := range []struct {
hdr []byte
want string
}{
{ // Wrong magic
hdr: []byte{'b', 'o', 'l', 'l', 'y', 0x00, 0x00, 0x00, 0x00, 0x00, 100},
want: "missing magic",
},
{ // Wrong size
hdr: []byte{'b', 'i', 'l', 'l', 'y', 0x00, 0x00, 0x00, 0x00, 0x00, 0xff},
want: "wrong slotsize, file:255, need:100",
},
{ // Future version
hdr: []byte{'b', 'i', 'l', 'l', 'y', 0x05, 0x39, 0x00, 0x00, 0x00, 100},
want: "wrong version: 1337",
},
{ // Too short
hdr: []byte{'b'},
want: "EOF",
},
{ // Correct magic, empty shelf
hdr: []byte{'b', 'i', 'l', 'l', 'y', 0x00, 0x00, 0x00, 0x00, 0x00, 100},
want: "",
},
} {
if err := os.WriteFile(filepath.Join(p, fname), tc.hdr, 0o777); err != nil {
t.Fatal(err)
}
_, err := openShelf(p, size, nil, false, false)
if err == nil {
if tc.want != "" {
t.Fatal("expected error")
}
} else if have := err.Error(); !strings.HasPrefix(have, tc.want) {
t.Errorf("test %d: wrong error, have '%v' want '%v'", i, have, tc.want)
}
}
}
func fuzzShelf(t *testing.T, content []byte) {
// Create a valid shelf, we only fuzz the contents, not the magic
path := t.TempDir()
file := filepath.Join(path, fmt.Sprintf("bkt_%08d.bag", 100))
head := []byte{'b', 'i', 'l', 'l', 'y', 0x00, 0x00, 0x00, 0x00, 0x00, 100}
// Append the fuzz content to the shelf
blob := append(head, content...)
if err := os.WriteFile(file, blob, 0o777); err != nil {
t.Fatal(err)
}
// Try to open the shelf and verify the errors
shelf, err := openShelf(path, 100, nil, false, false)
if err == nil {
shelf.Close()
return
}
shelf, err = openShelf(path, 100, nil, false, true)
if err != nil {
t.Fatalf("failed to recover shelf: %v", err)
}
shelf.Close()
}
// This test contains a fuzzer-finding which triggered an overflow-to-zero when
// itemHeaderSize and an items-reported-size were added.
func TestShelfContents(t *testing.T) {
data := []byte("\xff\xff\xff\xfc000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
fuzzShelf(t, data)
}
func FuzzShelfContents(f *testing.F) {
f.Fuzz(fuzzShelf)
}