AssertGetBlob asserts that the specified BLOB has correct content.
(ctx context.Context, t *testing.T, s blob.Storage, blobID blob.ID, expected []byte)
| 34 | |
| 35 | // AssertGetBlob asserts that the specified BLOB has correct content. |
| 36 | func 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(). |
| 92 | func AssertInvalidOffsetLength(ctx context.Context, t *testing.T, s blob.Storage, blobID blob.ID, offset, length int64) { |