diff options
-rw-r--r-- | librsync/common_test.go | 24 | ||||
-rw-r--r-- | librsync/helpers.go | 73 | ||||
-rw-r--r-- | librsync/helpers_test.go | 38 | ||||
-rw-r--r-- | librsync/librsync_test.go | 17 |
4 files changed, 135 insertions, 17 deletions
diff --git a/librsync/common_test.go b/librsync/common_test.go new file mode 100644 index 0000000..35d59b1 --- /dev/null +++ b/librsync/common_test.go @@ -0,0 +1,24 @@ +package librsync + +import ( + "fmt" + "io" + "os" +) + +// Some functions to support testing + +func dump(r io.Reader) (string, error) { + path := fmt.Sprintf("%s%cgolibrsync_test", os.TempDir(), os.PathSeparator) + file, err := os.Create(path) + if err != nil { + return "", err + } + defer file.Close() + + if _, err = io.Copy(file, r); err != nil { + return "", err + } + + return path, nil +} diff --git a/librsync/helpers.go b/librsync/helpers.go new file mode 100644 index 0000000..e560275 --- /dev/null +++ b/librsync/helpers.go @@ -0,0 +1,73 @@ +package librsync + +import ( + "io" +) + +// Some helper functions to make things more convenient. + +// CreateSignature wraps around a signature generation job and copies the result to the signature writer. +func CreateSignature(basis io.Reader, signature io.Writer) error { + siggen, err := NewDefaultSignatureGen(basis) + if err != nil { + return err + } + defer siggen.Close() + + _, err = io.Copy(signature, siggen) + return err +} + +// CreateDelta wraps around a delta generation job and copies the result to the delta writer. +func CreateDelta(signature, newfile io.Reader, delta io.Writer) error { + sig, err := LoadSignature(signature) + if err != nil { + return err + } + defer sig.Close() + + deltagen, err := NewDeltaGen(sig, newfile) + if err != nil { + return err + } + defer deltagen.Close() + + _, err = io.Copy(delta, deltagen) + return err +} + +// InstantDelta creates a delta file without the extra step of creating a signature. +func InstantDelta(basis, newfile io.Reader, delta io.Writer) error { + siggen, err := NewDefaultSignatureGen(basis) + if err != nil { + return err + } + defer siggen.Close() + + sig, err := LoadSignature(siggen) + if err != nil { + return err + } + defer sig.Close() + + deltagen, err := NewDeltaGen(sig, newfile) + if err != nil { + return err + } + defer deltagen.Close() + + _, err = io.Copy(delta, deltagen) + return err +} + +// Patch wraps around a Patcher job and copies the result to newfile. +func Patch(basis io.ReaderAt, delta io.Reader, newfile io.Writer) error { + patcher, err := NewPatcher(delta, basis) + if err != nil { + return err + } + defer patcher.Close() + + _, err = io.Copy(newfile, patcher) + return err +} diff --git a/librsync/helpers_test.go b/librsync/helpers_test.go new file mode 100644 index 0000000..c12992f --- /dev/null +++ b/librsync/helpers_test.go @@ -0,0 +1,38 @@ +package librsync + +import ( + "bytes" + "github.com/kch42/golibrsync/librsync/testdata" + "testing" +) + +func TestHelpers(t *testing.T) { + basis := bytes.NewReader(testdata.RandomData()) + mutation := bytes.NewReader(testdata.Mutation()) + + delta := new(bytes.Buffer) + if err := InstantDelta(basis, mutation, delta); err != nil { + t.Fatalf("InstantDelta failed: %s", err) + } + + if !bytes.Equal(delta.Bytes(), testdata.Delta()) { + if path, err := dump(delta); err == nil { + t.Fatalf("Deltas do not match. Generated delta dumped to %s", path) + } else { + t.Fatalf("Deltas do not match. Could not dump delta: %s", err) + } + } + + newfile := new(bytes.Buffer) + if err := Patch(basis, delta, newfile); err != nil { + t.Fatalf("Patch failed: %s", err) + } + + if !bytes.Equal(newfile.Bytes(), testdata.Mutation()) { + if path, err := dump(newfile); err == nil { + t.Fatalf("patch result and mutation are not equal. Result dumped to %s", path) + } else { + t.Fatalf("patch result and mutation are not equal. Could not dump result: %s", err) + } + } +} diff --git a/librsync/librsync_test.go b/librsync/librsync_test.go index e0fd50f..256e344 100644 --- a/librsync/librsync_test.go +++ b/librsync/librsync_test.go @@ -2,28 +2,11 @@ package librsync import ( "bytes" - "fmt" "github.com/kch42/golibrsync/librsync/testdata" "io" - "os" "testing" ) -func dump(r io.Reader) (string, error) { - path := fmt.Sprintf("%s%cgolibrsync_test", os.TempDir(), os.PathSeparator) - file, err := os.Create(path) - if err != nil { - return "", err - } - defer file.Close() - - if _, err = io.Copy(file, r); err != nil { - return "", err - } - - return path, nil -} - func TestSignatureDeltaPatch(t *testing.T) { // Generate signature orig := bytes.NewReader(testdata.RandomData()) |