| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
) |
| 12 |
|
| 13 |
// chunkID returns the ID desync would assign to the given plain data. |
| 14 |
func chunkID(b []byte) ChunkID { return Digest.Sum(b) } |
| 15 |
|
| 16 |
// TestIndexReadSeekerSizeMismatchPanic ensures that an index declaring a chunk |
| 17 |
// size larger than the actual stored chunk results in an error rather than a |
| 18 |
// "slice bounds out of range" panic in the read path. |
| 19 |
func TestIndexReadSeekerSizeMismatchPanic(t *testing.T) { |
| 20 |
data := []byte("data") // real chunk is 4 bytes |
| 21 |
store := &TestStore{Chunks: map[ChunkID][]byte{chunkID(data): data}} |
| 22 |
|
| 23 |
// The index lies: it claims the chunk is 1000 bytes long. |
| 24 |
idx := Index{ |
| 25 |
Index: FormatIndex{ChunkSizeMax: ChunkSizeMaxDefault}, |
| 26 |
Chunks: []IndexChunk{ |
| 27 |
{ID: chunkID(data), Start: 0, Size: 1000}, |
| 28 |
}, |
| 29 |
} |
| 30 |
|
| 31 |
r := NewIndexReadSeeker(idx, store) |
| 32 |
|
| 33 |
// Seek past the real chunk length but within the declared size. |
| 34 |
_, err := r.Seek(200, io.SeekStart) |
| 35 |
require.NoError(t, err) |
| 36 |
|
| 37 |
buf := make([]byte, 16) |
| 38 |
require.NotPanics(t, func() { |
| 39 |
_, err = r.Read(buf) |
| 40 |
}) |
| 41 |
require.Error(t, err) |
| 42 |
assert.Contains(t, err.Error(), "unexpected size for chunk") |
| 43 |
} |
| 44 |
|
| 45 |
// TestIndexReadSeekerSizeMismatchNoLoop ensures that a short (under-sized) chunk |
| 46 |
// that is not the last one causes Read to return an error promptly instead of |
| 47 |
// spinning in a zero-progress loop. |
| 48 |
func TestIndexReadSeekerSizeMismatchNoLoop(t *testing.T) { |
| 49 |
short := []byte("data") // real chunk is 4 bytes |
| 50 |
tail := []byte("trailing") // a normal following chunk |
| 51 |
store := &TestStore{Chunks: map[ChunkID][]byte{ |
| 52 |
chunkID(short): short, |
| 53 |
chunkID(tail): tail, |
| 54 |
}} |
| 55 |
|
| 56 |
// First chunk claims 1000 bytes but only 4 are stored, and it's followed by |
| 57 |
// another chunk so the "last chunk" short-read break does not apply. |
| 58 |
idx := Index{ |
| 59 |
Index: FormatIndex{ChunkSizeMax: ChunkSizeMaxDefault}, |
| 60 |
Chunks: []IndexChunk{ |
| 61 |
{ID: chunkID(short), Start: 0, Size: 1000}, |
| 62 |
{ID: chunkID(tail), Start: 1000, Size: uint64(len(tail))}, |
| 63 |
}, |
| 64 |
} |
| 65 |
|
| 66 |
r := NewIndexReadSeeker(idx, store) |
| 67 |
|
| 68 |
done := make(chan error, 1) |
| 69 |
go func() { |
| 70 |
buf := make([]byte, 64) |
| 71 |
_, err := r.Read(buf) |
| 72 |
done <- err |
| 73 |
}() |
| 74 |
|
| 75 |
select { |
| 76 |
case err := <-done: |
| 77 |
require.Error(t, err) |
| 78 |
assert.Contains(t, err.Error(), "unexpected size for chunk") |
| 79 |
case <-time.After(5 * time.Second): |
| 80 |
t.Fatal("Read did not return; likely spinning in a zero-progress loop") |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// TestIndexReadSeekerValid verifies the read path still returns the correct |
| 85 |
// content for a well-formed multi-chunk index, including a null chunk served |
| 86 |
// from memory, and that seeking works. |
| 87 |
func TestIndexReadSeekerValid(t *testing.T) { |
| 88 |
head := []byte("hello, world") |
| 89 |
null := make([]byte, ChunkSizeMaxDefault) |
| 90 |
tail := []byte("goodbye, world") |
| 91 |
|
| 92 |
store := &TestStore{Chunks: map[ChunkID][]byte{ |
| 93 |
chunkID(head): head, |
| 94 |
chunkID(tail): tail, |
| 95 |
// the null chunk is intentionally not stored; it must be served from memory |
| 96 |
}} |
| 97 |
|
| 98 |
idx := Index{ |
| 99 |
Index: FormatIndex{ChunkSizeMax: ChunkSizeMaxDefault}, |
| 100 |
Chunks: []IndexChunk{ |
| 101 |
{ID: chunkID(head), Start: 0, Size: uint64(len(head))}, |
| 102 |
{ID: NewNullChunk(ChunkSizeMaxDefault).ID, Start: uint64(len(head)), Size: ChunkSizeMaxDefault}, |
| 103 |
{ID: chunkID(tail), Start: uint64(len(head)) + ChunkSizeMaxDefault, Size: uint64(len(tail))}, |
| 104 |
}, |
| 105 |
} |
| 106 |
|
| 107 |
want := bytes.Join([][]byte{head, null, tail}, nil) |
| 108 |
|
| 109 |
// Full sequential read. |
| 110 |
r := NewIndexReadSeeker(idx, store) |
| 111 |
got, err := io.ReadAll(r) |
| 112 |
require.NoError(t, err) |
| 113 |
assert.Equal(t, want, got) |
| 114 |
|
| 115 |
// Seek to the start of the last chunk and read its content. |
| 116 |
off := int64(len(head)) + int64(ChunkSizeMaxDefault) |
| 117 |
_, err = r.Seek(off, io.SeekStart) |
| 118 |
require.NoError(t, err) |
| 119 |
got, err = io.ReadAll(r) |
| 120 |
require.NoError(t, err) |
| 121 |
assert.Equal(t, tail, got) |
| 122 |
} |
| 123 |
|
| 124 |
// An index of a zero-length file has no chunks. Reading and seeking should |
| 125 |
// behave like an empty file rather than panic. |
| 126 |
func TestIndexReadSeekerEmptyIndex(t *testing.T) { |
| 127 |
idx := Index{ |
| 128 |
Index: FormatIndex{ChunkSizeMax: ChunkSizeMaxDefault}, |
| 129 |
} |
| 130 |
store := &TestStore{} |
| 131 |
|
| 132 |
r := NewIndexReadSeeker(idx, store) |
| 133 |
|
| 134 |
got, err := io.ReadAll(r) |
| 135 |
require.NoError(t, err) |
| 136 |
assert.Empty(t, got) |
| 137 |
|
| 138 |
// Seeking to the start (and end, they're the same) is allowed |
| 139 |
pos, err := r.Seek(0, io.SeekStart) |
| 140 |
require.NoError(t, err) |
| 141 |
assert.Equal(t, int64(0), pos) |
| 142 |
|
| 143 |
pos, err = r.Seek(0, io.SeekEnd) |
| 144 |
require.NoError(t, err) |
| 145 |
assert.Equal(t, int64(0), pos) |
| 146 |
|
| 147 |
buf := make([]byte, 16) |
| 148 |
n, err := r.Read(buf) |
| 149 |
assert.Equal(t, 0, n) |
| 150 |
assert.Equal(t, io.EOF, err) |
| 151 |
} |