MCPcopy
hub / github.com/kopia/kopia / verifyContentCache

Function verifyContentCache

internal/cache/content_cache_test.go:175–240  ·  view source on GitHub ↗
(t *testing.T, cc cache.ContentCache, cacheStorage cache.Storage)

Source from the content-addressed store, hash-verified

173}
174
175func verifyContentCache(t *testing.T, cc cache.ContentCache, cacheStorage cache.Storage) {
176 t.Helper()
177
178 ctx := testlogging.Context(t)
179
180 t.Run("GetContentContent", func(t *testing.T) {
181 cases := []struct {
182 contentID string
183 blobID blob.ID
184 offset int64
185 length int64
186
187 expected []byte
188 err error
189 }{
190 {"xf0f0f1", "content-1", 1, 5, []byte{2, 3, 4, 5, 6}, nil},
191 {"xf0f0f2", "content-1", 0, -1, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, nil},
192 {"xf0f0f1", "content-1", 1, 5, []byte{2, 3, 4, 5, 6}, nil},
193 {"xf0f0f2", "content-1", 0, -1, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, nil},
194 {"xf0f0f3", "no-such-content", 0, -1, nil, blob.ErrBlobNotFound},
195 {"xf0f0f4", "no-such-content", 10, 5, nil, blob.ErrBlobNotFound},
196 {"f0f0f5", "content-1", 7, 3, []byte{8, 9, 10}, nil},
197 {"xf0f0f6", "content-1", 11, 10, nil, errors.New("invalid offset: 11: invalid blob offset or length")},
198 {"xf0f0f6", "content-1", -1, 5, nil, errors.New("invalid offset: -1: invalid blob offset or length")},
199 }
200
201 var v gather.WriteBuffer
202 defer v.Close()
203
204 for _, tc := range cases {
205 err := cc.GetContent(ctx, tc.contentID, tc.blobID, tc.offset, tc.length, &v)
206 if tc.err == nil {
207 require.NoErrorf(t, err, "tc.contentID: %v", tc.contentID)
208 } else {
209 require.ErrorContainsf(t, err, tc.err.Error(), "tc.contentID: %v", tc.contentID)
210 }
211
212 if got := v.ToByteSlice(); !bytes.Equal(got, tc.expected) {
213 t.Errorf("unexpected data for %v: %x, wanted %x", tc.contentID, got, tc.expected)
214 }
215 }
216
217 verifyStorageContentList(t, cacheStorage, "f0f0f1x", "f0f0f2x", "f0f0f5")
218 })
219
220 t.Run("DataCorruption", func(t *testing.T) {
221 const cacheKey = "f0f0f1x"
222
223 var tmp gather.WriteBuffer
224 defer tmp.Close()
225
226 require.NoError(t, cacheStorage.GetBlob(ctx, cacheKey, 0, -1, &tmp))
227
228 // corrupt the data and write back
229 b := tmp.Bytes()
230 b.Slices[0][0] ^= 1
231
232 require.NoError(t, cacheStorage.PutBlob(ctx, cacheKey, b, blob.PutOptions{}))

Callers 1

TestDiskContentCacheFunction · 0.85

Calls 13

CloseMethod · 0.95
ToByteSliceMethod · 0.95
BytesMethod · 0.95
ContextFunction · 0.92
verifyStorageContentListFunction · 0.85
HelperMethod · 0.80
EqualMethod · 0.80
ErrorfMethod · 0.80
RunMethod · 0.65
GetContentMethod · 0.65
ErrorMethod · 0.65
GetBlobMethod · 0.65

Tested by

no test coverage detected