func testCompressDecompress(t *testing.T, size int64, algo AlgorithmType, useReadFrom, useWriteTo bool) { // Fake data file is written to disk, // as compression reader has to be a ReadSeeker. testutil.Remover(t, ZipFilePath) data := testutil.CreateDummyBuf(size) zipFileDest := openDest(t, ZipFilePath) defer testutil.Remover(t, ZipFilePath) // Compress. w, err := NewWriter(zipFileDest, algo) if err != nil { t.Errorf("Writer init failed %v", err) return } if _, err := testutil.DumbCopy(w, bytes.NewReader(data), useReadFrom, useWriteTo); err != nil { t.Errorf("Compress failed %v", err) return } if err := w.Close(); err != nil { t.Errorf("Compression writer close failed: %v", err) return } if err := zipFileDest.Close(); err != nil { t.Errorf("close(zipFileDest) failed: %v", err) return } // Read compressed file into buffer. dataUncomp := bytes.NewBuffer(nil) dataFromZip := openSrc(t, ZipFilePath) // Uncompress. r := NewReader(dataFromZip) if _, err := testutil.DumbCopy(dataUncomp, r, useReadFrom, useWriteTo); err != nil { t.Errorf("Decompression failed: %v", err) return } if err := dataFromZip.Close(); err != nil { t.Errorf("Zip close failed: %v", err) return } // Compare. got, want := dataUncomp.Bytes(), data if !bytes.Equal(got, want) { t.Error("Uncompressed data and input data does not match.") t.Errorf("\tGOT: %v", util.OmitBytes(got, 10)) t.Errorf("\tWANT: %v", util.OmitBytes(want, 10)) return } }
func withMount(t *testing.T, f func(mount *Mount)) { mntPath := filepath.Join(os.TempDir(), "brig_fuse_mountdir") // NOTE: This is useful for debugging. log.SetLevel(log.WarnLevel) // log.SetLevel(log.DebugLevel) if err := os.MkdirAll(mntPath, 0777); err != nil { t.Errorf("Unable to create empty mount dir: %v", err) return } defer testutil.Remover(t, mntPath) repo.WithAliceRepo(t, func(rep *repo.Repository) { mount, err := NewMount(rep.OwnStore, mntPath) if err != nil { t.Errorf("Cannot create mount: %v", err) return } f(mount) if err := mount.Close(); err != nil { t.Errorf("Closing mount failed: %v", err) } }) }
func WithIpfsRepo(t *testing.T, root string, f func(string)) { path, err := ipfsutil.InitRepo(root, 1024) if err != nil { t.Errorf("Could not create ipfs repo: %v", err) return } defer testutil.Remover(t, path) f(path) }
func withStore(t *testing.T, ID id.ID, IPFS *ipfsutil.Node, fn func(st *store.Store)) { tempDir, err := ioutil.TempDir("", "brig-store-") if err != nil { t.Fatalf("Could not create temp dir `%s`: %v", tempDir, err) return } defer testutil.Remover(t, tempDir) st, err := store.Open(tempDir, id.NewPeer(ID, "QmIMACOW"), IPFS) if err != nil { t.Fatalf("Could not create store: %v", err) return } fn(st) }
func withRemoteStore(t *testing.T, f func(rms RemoteStore)) { path := filepath.Join(os.TempDir(), "brig-test-remote.yml") defer testutil.Remover(t, path) rms, err := NewYAMLRemotes(path) if err != nil { t.Errorf("Creating yaml store failed: %v", err) return } f(rms) if err := rms.Close(); err != nil { t.Errorf("Closing yaml store failed: %v", err) return } }
// TODO: make the with functions globally available. func withStore(t *testing.T, ID id.ID, IPFS *ipfsutil.Node, fn func(st *Store)) { tempDir, err := ioutil.TempDir("", "brig-store-") if err != nil { t.Fatalf("Could not create temp dir `%s`: %v", tempDir, err) return } defer testutil.Remover(t, tempDir) st, err := Open(tempDir, id.NewPeer(ID, "QmW2jc7k5Ug987QEkUx6tJUTdZov7io39MDCiKKp2f57mD"), IPFS) if err != nil { t.Fatalf("Could not create store: %v", err) return } fn(st) }
func WithRepoAtPath(t *testing.T, path, user, pass string, f func(*Repository)) { if err := os.RemoveAll(path); err != nil { t.Errorf("previous repo exists; cannot delete it though: %v", err) return } rep, err := NewRepository(user, pass, path) if err != nil { t.Errorf("creating repo failed: %v", err) return } defer testutil.Remover(t, path) f(rep) if err := rep.Close(); err != nil { t.Errorf("closing repo failed: %v", err) return } }
func testSeek(t *testing.T, size, offset int64, algo AlgorithmType, useReadFrom, useWriteTo bool) { // Fake data file is written to disk, // as compression reader has to be a ReadSeeker. testutil.Remover(t, ZipFilePath) data := testutil.CreateDummyBuf(size) zipFileDest := openDest(t, ZipFilePath) // Compress. w, err := NewWriter(zipFileDest, algo) if err != nil { t.Errorf("Writer init failed %v", err) return } if _, err := testutil.DumbCopy(w, bytes.NewReader(data), useReadFrom, useWriteTo); err != nil { t.Errorf("Compress failed %v", err) return } defer testutil.Remover(t, ZipFilePath) if err := w.Close(); err != nil { t.Errorf("Compression writer close failed: %v", err) return } if err := zipFileDest.Close(); err != nil { t.Errorf("close(zipFileDest) failed: %v", err) return } // Read compressed file into buffer. dataUncomp := bytes.NewBuffer(nil) dataFromZip := openSrc(t, ZipFilePath) zr := NewReader(dataFromZip) // Set specific offset before read. _, err = zr.Seek(offset, os.SEEK_SET) if err == io.EOF && offset < size && offset > -1 { t.Errorf("Seek failed even with EOF: %d <= %d", offset, size) return } if err != io.EOF && err != nil { t.Errorf("Seek failed: %v", err) return } // Read starting at a specific offset. if _, err := testutil.DumbCopy(dataUncomp, zr, useReadFrom, useWriteTo); err != nil { t.Errorf("Decompression failed: %v", err) return } if err := dataFromZip.Close(); err != nil { t.Errorf("Zip close failed: %v", err) return } // Compare. maxOffset := offset if offset > size { maxOffset = size } if offset < 0 { maxOffset = 0 } got, want := dataUncomp.Bytes(), data[maxOffset:] if !bytes.Equal(got, want) { t.Error("Uncompressed data and input data does not match.") t.Errorf("\tGOT: %v", util.OmitBytes(got, 10)) t.Errorf("\tWANT: %v", util.OmitBytes(want, 10)) return } }