| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/require" |
| 8 |
) |
| 9 |
|
| 10 |
func TestNewChunkFromStorage(t *testing.T) { |
| 11 |
conv := Converters{Compressor{}} |
| 12 |
plain := []byte("the quick brown fox jumps over the lazy dog") |
| 13 |
id := ChunkID(Digest.Sum(plain)) |
| 14 |
storage, err := conv.toStorage(plain) |
| 15 |
require.NoError(t, err) |
| 16 |
|
| 17 |
t.Run("valid", func(t *testing.T) { |
| 18 |
c, err := NewChunkFromStorage(id, storage, conv, false) |
| 19 |
require.NoError(t, err) |
| 20 |
require.Equal(t, id, c.ID()) |
| 21 |
}) |
| 22 |
|
| 23 |
t.Run("undecodable storage data", func(t *testing.T) { |
| 24 |
// Truncated/garbage storage data fails to decompress. This must surface as |
| 25 |
// ChunkInvalid (so RepairableCache and 'verify --repair' still handle it) but |
| 26 |
// must also carry the underlying decode error rather than reporting a bogus |
| 27 |
// "does not match its hash 0000..." mismatch. |
| 28 |
_, err := NewChunkFromStorage(id, []byte("not a valid zstd stream"), conv, false) |
| 29 |
var ci ChunkInvalid |
| 30 |
require.ErrorAs(t, err, &ci) |
| 31 |
require.Error(t, ci.Err, "expected ChunkInvalid.Err to carry the underlying decode error") |
| 32 |
require.Error(t, errors.Unwrap(err), "expected the error to unwrap to the underlying decode error") |
| 33 |
}) |
| 34 |
|
| 35 |
t.Run("hash mismatch", func(t *testing.T) { |
| 36 |
var wrongID ChunkID |
| 37 |
wrongID[0] = 0x01 |
| 38 |
_, err := NewChunkFromStorage(wrongID, storage, conv, false) |
| 39 |
var ci ChunkInvalid |
| 40 |
require.ErrorAs(t, err, &ci) |
| 41 |
require.NoError(t, ci.Err, "expected ChunkInvalid.Err to be nil for a plain hash mismatch") |
| 42 |
require.Equal(t, id, ci.Sum) |
| 43 |
}) |
| 44 |
|
| 45 |
t.Run("skip verify", func(t *testing.T) { |
| 46 |
// With skipVerify even undecodable data is accepted (no verification done). |
| 47 |
_, err := NewChunkFromStorage(id, []byte("garbage"), conv, true) |
| 48 |
require.NoError(t, err) |
| 49 |
}) |
| 50 |
} |