MCPcopy
hub / github.com/kopia/kopia / AssertGetBlob

Function AssertGetBlob

internal/blobtesting/asserts.go:36–89  ·  view source on GitHub ↗

AssertGetBlob asserts that the specified BLOB has correct content.

(ctx context.Context, t *testing.T, s blob.Storage, blobID blob.ID, expected []byte)

Source from the content-addressed store, hash-verified

34
35// AssertGetBlob asserts that the specified BLOB has correct content.
36func AssertGetBlob(ctx context.Context, t *testing.T, s blob.Storage, blobID blob.ID, expected []byte) {
37 t.Helper()
38
39 var b gather.WriteBuffer
40 defer b.Close()
41
42 err := s.GetBlob(ctx, blobID, 0, -1, &b)
43 require.NoErrorf(t, err, "GetBlob(%v)", blobID)
44
45 if v := b.ToByteSlice(); !bytes.Equal(v, expected) {
46 t.Fatalf("GetBlob(%v) returned %x, but expected %x", blobID, v, expected)
47 }
48
49 half := int64(len(expected) / 2)
50 if half == 0 {
51 return
52 }
53
54 err = s.GetBlob(ctx, blobID, 0, 0, &b)
55 if err != nil {
56 t.Fatalf("GetBlob(%v) returned error %v, expected data: %v", blobID, err, expected)
57 return
58 }
59
60 if b.Length() != 0 {
61 t.Fatalf("GetBlob(%v) returned non-zero length: %v", blobID, b.Length())
62 return
63 }
64
65 err = s.GetBlob(ctx, blobID, 0, half, &b)
66 if err != nil {
67 t.Fatalf("GetBlob(%v) returned error %v, expected data: %v", blobID, err, expected)
68 return
69 }
70
71 if v := b.ToByteSlice(); !bytes.Equal(v, expected[0:half]) {
72 t.Fatalf("GetBlob(%v) returned %x, but expected %x", blobID, v, expected[0:half])
73 }
74
75 err = s.GetBlob(ctx, blobID, half, int64(len(expected))-half, &b)
76 if err != nil {
77 t.Fatalf("GetBlob(%v) returned error %v, expected data: %v", blobID, err, expected)
78 return
79 }
80
81 if v := b.ToByteSlice(); !bytes.Equal(v, expected[len(expected)-int(half):]) {
82 t.Fatalf("GetBlob(%v) returned %x, but expected %x", blobID, v, expected[len(expected)-int(half):])
83 }
84
85 AssertInvalidOffsetLength(ctx, t, s, blobID, -3, 1)
86 AssertInvalidOffsetLength(ctx, t, s, blobID, int64(len(expected)), 3)
87 AssertInvalidOffsetLength(ctx, t, s, blobID, int64(len(expected)-1), 3)
88 AssertInvalidOffsetLength(ctx, t, s, blobID, int64(len(expected)+1), 3)
89}
90
91// AssertInvalidOffsetLength verifies that the given combination of (offset,length) fails on GetBlob().
92func AssertInvalidOffsetLength(ctx context.Context, t *testing.T, s blob.Storage, blobID blob.ID, offset, length int64) {

Callers 2

TestOwnWritesFunction · 0.92
VerifyStorageFunction · 0.85

Calls 8

CloseMethod · 0.95
ToByteSliceMethod · 0.95
LengthMethod · 0.95
HelperMethod · 0.80
EqualMethod · 0.80
FatalfMethod · 0.80
GetBlobMethod · 0.65

Tested by 1

TestOwnWritesFunction · 0.74