aboutsummaryrefslogtreecommitdiff
path: root/repo_test.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-08-23 21:12:55 +0200
committern-peugnet <n.peugnet@free.fr>2021-08-23 21:12:55 +0200
commitb070eae35c1e7a4996b90208153d01f2be08d588 (patch)
tree67069c772a376fcd7da199c6bb821cc7261cd523 /repo_test.go
parenta67dfdd993dee93950c4b60e99ab6ca92d842072 (diff)
downloaddna-backup-b070eae35c1e7a4996b90208153d01f2be08d588.tar.gz
dna-backup-b070eae35c1e7a4996b90208153d01f2be08d588.zip
try bsdiff and rework some of the API
Diffstat (limited to 'repo_test.go')
-rw-r--r--repo_test.go56
1 files changed, 46 insertions, 10 deletions
diff --git a/repo_test.go b/repo_test.go
index 6475fc2..bde2a63 100644
--- a/repo_test.go
+++ b/repo_test.go
@@ -2,10 +2,13 @@ package main
import (
"bytes"
+ "io/ioutil"
"log"
"os"
"path"
"testing"
+
+ "github.com/gabstv/go-bsdiff/pkg/bsdiff"
)
func TestMain(m *testing.M) {
@@ -31,8 +34,8 @@ func prepareResult() string {
func chunkCompare(t *testing.T, dataDir string, testFiles []string, chunkCount int) {
chunks := make(chan []byte)
- files := ListFiles(dataDir)
- go ReadFiles(files, chunks)
+ files := listFiles(dataDir)
+ go readFiles(files, chunks)
offset := 0
buff := make([]byte, chunkSize*chunkCount)
@@ -100,12 +103,12 @@ func TestLoadChunks(t *testing.T) {
chunks1 := make(chan []byte, 16)
chunks2 := make(chan []byte, 16)
chunks3 := make(chan Chunk, 16)
- files := ListFiles(dataDir)
- go ReadFiles(files, chunks1)
- go ReadFiles(files, chunks2)
- StoreChunks(resultChunks, chunks1)
+ files := listFiles(dataDir)
+ go readFiles(files, chunks1)
+ go readFiles(files, chunks2)
+ storeChunks(resultChunks, chunks1)
versions := []string{resultVersion}
- go LoadChunks(versions, chunks3)
+ go loadChunks(versions, chunks3)
i := 0
for c2 := range chunks2 {
@@ -123,9 +126,9 @@ func TestStoreLoadFiles(t *testing.T) {
resultDir := prepareResult()
dataDir := path.Join("test", "data")
resultFiles := path.Join(resultDir, "files")
- files1 := ListFiles(dataDir)
- StoreFiles(resultFiles, files1)
- files2 := LoadFiles(resultFiles)
+ files1 := listFiles(dataDir)
+ storeFiles(resultFiles, files1)
+ files2 := loadFiles(resultFiles)
for i, f := range files1 {
if f != files2[i] {
t.Errorf("Loaded file data %d does not match stored one", i)
@@ -134,3 +137,36 @@ func TestStoreLoadFiles(t *testing.T) {
}
}
}
+
+func TestBsdiff(t *testing.T) {
+ resultDir := prepareResult()
+ dataDir := path.Join("test", "data")
+ addedFile := path.Join(dataDir, "logs.2", "slogTest.log")
+ resultVersion := path.Join(resultDir, "00000")
+ resultChunks := path.Join(resultVersion, "chunks")
+ os.MkdirAll(resultChunks, 0775)
+ chunks := make(chan []byte, 16)
+ files := listFiles(dataDir)
+ go readFiles(files, chunks)
+ storeChunks(resultChunks, chunks)
+
+ input, _ := ioutil.ReadFile(path.Join(dataDir, "logs.1", "logTest.log"))
+ ioutil.WriteFile(addedFile, input, 0664)
+
+ newChunks := make(chan []byte, 16)
+ oldChunks := make(chan Chunk, 16)
+ files = listFiles(dataDir)
+ repo := NewRepo(resultDir)
+ versions := repo.loadVersions()
+ go loadChunks(versions, oldChunks)
+ go readFiles(files, newChunks)
+ hashes := hashChunks(oldChunks)
+ recipe := repo.matchChunks(newChunks, hashes)
+ buff := new(bytes.Buffer)
+ bsdiff.Reader(recipe[2], recipe[0], buff)
+ if len(buff.Bytes()) >= chunkSize {
+ t.Errorf("Bsdiff of chunk is too large: %d", len(buff.Bytes()))
+ }
+
+ os.Remove(addedFile)
+}