2022-06-06 14:26:38 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/backend/mem"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
2021-08-07 20:52:05 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2022-06-06 14:26:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test saving a blob and loading it again, with varying buffer sizes.
|
|
|
|
// Also a regression test for #3783.
|
|
|
|
func FuzzSaveLoadBlob(f *testing.F) {
|
|
|
|
f.Fuzz(func(t *testing.T, blob []byte, buflen uint) {
|
|
|
|
if buflen > 64<<20 {
|
|
|
|
// Don't allocate enormous buffers. We're not testing the allocator.
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
|
|
|
id := restic.Hash(blob)
|
2022-12-11 09:41:22 +00:00
|
|
|
repo := TestRepositoryWithBackend(t, mem.New(), 2)
|
2022-06-06 14:26:38 +00:00
|
|
|
|
2021-08-07 20:52:05 +00:00
|
|
|
var wg errgroup.Group
|
|
|
|
repo.StartPackUploader(context.TODO(), &wg)
|
|
|
|
|
2022-05-01 12:26:57 +00:00
|
|
|
_, _, _, err := repo.SaveBlob(context.TODO(), restic.DataBlob, blob, id, false)
|
2022-06-06 14:26:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = repo.Flush(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := repo.LoadBlob(context.TODO(), restic.DataBlob, id, make([]byte, buflen))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if restic.Hash(buf) != id {
|
|
|
|
t.Fatal("mismatch")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|