func TestLoadLargeBuffer(t *testing.T) { b := mem.New() for i := 0; i < 20; i++ { data := Random(23+i, rand.Intn(MiB)+500*KiB) id := backend.Hash(data) err := b.Save(backend.Handle{Name: id.String(), Type: backend.Data}, data) OK(t, err) buf := make([]byte, len(data)+100) buf, err = backend.LoadAll(b, backend.Handle{Type: backend.Data, Name: id.String()}, buf) OK(t, err) if len(buf) != len(data) { t.Errorf("length of returned buffer does not match, want %d, got %d", len(data), len(buf)) continue } if !bytes.Equal(buf, data) { t.Errorf("wrong data returned") continue } } }
func TestShortPack(t *testing.T) { k := crypto.NewRandomKey() bufs, packData, packSize := newPack(t, k, []int{23}) b := mem.New() id := restic.Hash(packData) handle := restic.Handle{Type: restic.DataFile, Name: id.String()} OK(t, b.Save(handle, packData)) verifyBlobs(t, bufs, k, restic.ReaderAt(b, handle), packSize) }
func TestUnpackReadSeeker(t *testing.T) { // create random keys k := crypto.NewRandomKey() bufs, packData, packSize := newPack(t, k, testLens) b := mem.New() id := restic.Hash(packData) handle := restic.Handle{Type: restic.DataFile, Name: id.String()} OK(t, b.Save(handle, packData)) verifyBlobs(t, bufs, k, restic.ReaderAt(b, handle), packSize) }
func TestPackerManager(t *testing.T) { rnd := newRandReader(rand.NewSource(23)) be := mem.New() pm := newPackerManager(be, crypto.NewRandomKey()) blobBuf := make([]byte, maxBlobSize) bytes := fillPacks(t, rnd, be, pm, blobBuf) bytes += flushRemainingPacks(t, rnd, be, pm) t.Logf("saved %d bytes", bytes) }
func TestUnpackReadSeeker(t *testing.T) { // create random keys k := crypto.NewRandomKey() bufs, packData, packSize := newPack(t, k) b := mem.New() id := backend.Hash(packData) handle := backend.Handle{Type: backend.Data, Name: id.String()} OK(t, b.Save(handle, packData)) rd := backend.NewReadSeeker(b, handle) verifyBlobs(t, bufs, k, rd, packSize) }
func TestCheckerModifiedData(t *testing.T) { be := mem.New() repository.TestUseLowSecurityKDFParameters(t) repo := repository.New(be) test.OK(t, repo.Init(test.TestPassword)) arch := archiver.New(repo) _, id, err := arch.Snapshot(nil, []string{"."}, nil, nil) test.OK(t, err) t.Logf("archived as %v", id.Str()) beError := &errorBackend{Backend: be} checkRepo := repository.New(beError) test.OK(t, checkRepo.SearchKey(test.TestPassword, 5)) chkr := checker.New(checkRepo) hints, errs := chkr.LoadIndex() if len(errs) > 0 { t.Fatalf("expected no errors, got %v: %v", len(errs), errs) } if len(hints) > 0 { t.Errorf("expected no hints, got %v: %v", len(hints), hints) } beError.ProduceErrors = true errFound := false for _, err := range checkPacks(chkr) { t.Logf("pack error: %v", err) } for _, err := range checkStruct(chkr) { t.Logf("struct error: %v", err) } for _, err := range checkData(chkr) { t.Logf("struct error: %v", err) errFound = true } if !errFound { t.Fatal("no error found, checker is broken") } }
func TestReadSeeker(t *testing.T) { b := mem.New() length := rand.Intn(1<<24) + 2000 data := Random(23, length) id := backend.Hash(data) handle := backend.Handle{Type: backend.Data, Name: id.String()} err := b.Save(handle, data) if err != nil { t.Fatalf("Save() error: %v", err) } for i := 0; i < 50; i++ { l := rand.Intn(length + 2000) o := rand.Intn(length + 2000) if rand.Float32() > 0.5 { o = -o } d := data if o > 0 && o < len(d) { d = d[o:] } else { o = len(d) d = d[:0] } if l > 0 && l < len(d) { d = d[:l] } rd := backend.NewReadSeeker(b, handle) loadAndCompare(t, rd, len(data), int64(o), d) } }
func init() { test.CreateFn = func() (restic.Backend, error) { if be != nil { return nil, errors.New("temporary memory backend dir already exists") } be = mem.New() return be, nil } test.OpenFn = func() (restic.Backend, error) { if be == nil { return nil, errors.New("repository not initialized") } return be, nil } test.CleanupFn = func() error { be = nil return nil } }
func TestLoadAll(t *testing.T) { b := mem.New() for i := 0; i < 20; i++ { data := Random(23+i, rand.Intn(MiB)+500*KiB) id := restic.Hash(data) err := b.Save(restic.Handle{Name: id.String(), Type: restic.DataFile}, data) OK(t, err) buf, err := backend.LoadAll(b, restic.Handle{Type: restic.DataFile, Name: id.String()}, nil) OK(t, err) if len(buf) != len(data) { t.Errorf("length of returned buffer does not match, want %d, got %d", len(data), len(buf)) continue } if !bytes.Equal(buf, data) { t.Errorf("wrong data returned") continue } } }
// TestBackend returns a fully configured in-memory backend. func TestBackend(t testing.TB) (be restic.Backend, cleanup func()) { return mem.New(), func() {} }