From ccba8598123e01b0933e103f01f7526331812b67 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sun, 17 Jan 2016 11:27:31 +0000 Subject: [PATCH] Test all available hashes for each remote --- amazonclouddrive/amazonclouddrive_test.go | 2 +- b2/b2_test.go | 2 +- drive/drive_test.go | 2 +- dropbox/dropbox_test.go | 2 +- fstest/fstest.go | 17 ++++---- fstest/fstests/fstests.go | 40 +++++++++---------- googlecloudstorage/googlecloudstorage_test.go | 2 +- hubic/hubic_test.go | 2 +- local/local_test.go | 2 +- onedrive/onedrive_test.go | 2 +- s3/s3_test.go | 2 +- swift/swift_test.go | 2 +- yandex/yandex_test.go | 2 +- 13 files changed, 39 insertions(+), 40 deletions(-) diff --git a/amazonclouddrive/amazonclouddrive_test.go b/amazonclouddrive/amazonclouddrive_test.go index 3357fde1d..2eab8b1e2 100644 --- a/amazonclouddrive/amazonclouddrive_test.go +++ b/amazonclouddrive/amazonclouddrive_test.go @@ -42,7 +42,7 @@ func TestFsPrecision(t *testing.T) { fstests.TestFsPrecision(t) } func TestObjectString(t *testing.T) { fstests.TestObjectString(t) } func TestObjectFs(t *testing.T) { fstests.TestObjectFs(t) } func TestObjectRemote(t *testing.T) { fstests.TestObjectRemote(t) } -func TestObjectMd5sum(t *testing.T) { fstests.TestObjectMd5sum(t) } +func TestObjectHashes(t *testing.T) { fstests.TestObjectHashes(t) } func TestObjectModTime(t *testing.T) { fstests.TestObjectModTime(t) } func TestObjectSetModTime(t *testing.T) { fstests.TestObjectSetModTime(t) } func TestObjectSize(t *testing.T) { fstests.TestObjectSize(t) } diff --git a/b2/b2_test.go b/b2/b2_test.go index 7d09b35e5..33a81b1b2 100644 --- a/b2/b2_test.go +++ b/b2/b2_test.go @@ -42,7 +42,7 @@ func TestFsPrecision(t *testing.T) { fstests.TestFsPrecision(t) } func TestObjectString(t *testing.T) { fstests.TestObjectString(t) } func TestObjectFs(t *testing.T) { fstests.TestObjectFs(t) } func TestObjectRemote(t *testing.T) { fstests.TestObjectRemote(t) } -func TestObjectMd5sum(t *testing.T) { fstests.TestObjectMd5sum(t) } +func TestObjectHashes(t *testing.T) { fstests.TestObjectHashes(t) } func TestObjectModTime(t *testing.T) { fstests.TestObjectModTime(t) } func TestObjectSetModTime(t *testing.T) { fstests.TestObjectSetModTime(t) } func TestObjectSize(t *testing.T) { fstests.TestObjectSize(t) } diff --git a/drive/drive_test.go b/drive/drive_test.go index 94af9b933..2af9b50e4 100644 --- a/drive/drive_test.go +++ b/drive/drive_test.go @@ -42,7 +42,7 @@ func TestFsPrecision(t *testing.T) { fstests.TestFsPrecision(t) } func TestObjectString(t *testing.T) { fstests.TestObjectString(t) } func TestObjectFs(t *testing.T) { fstests.TestObjectFs(t) } func TestObjectRemote(t *testing.T) { fstests.TestObjectRemote(t) } -func TestObjectMd5sum(t *testing.T) { fstests.TestObjectMd5sum(t) } +func TestObjectHashes(t *testing.T) { fstests.TestObjectHashes(t) } func TestObjectModTime(t *testing.T) { fstests.TestObjectModTime(t) } func TestObjectSetModTime(t *testing.T) { fstests.TestObjectSetModTime(t) } func TestObjectSize(t *testing.T) { fstests.TestObjectSize(t) } diff --git a/dropbox/dropbox_test.go b/dropbox/dropbox_test.go index 66b05672e..0868fac07 100644 --- a/dropbox/dropbox_test.go +++ b/dropbox/dropbox_test.go @@ -42,7 +42,7 @@ func TestFsPrecision(t *testing.T) { fstests.TestFsPrecision(t) } func TestObjectString(t *testing.T) { fstests.TestObjectString(t) } func TestObjectFs(t *testing.T) { fstests.TestObjectFs(t) } func TestObjectRemote(t *testing.T) { fstests.TestObjectRemote(t) } -func TestObjectMd5sum(t *testing.T) { fstests.TestObjectMd5sum(t) } +func TestObjectHashes(t *testing.T) { fstests.TestObjectHashes(t) } func TestObjectModTime(t *testing.T) { fstests.TestObjectModTime(t) } func TestObjectSetModTime(t *testing.T) { fstests.TestObjectSetModTime(t) } func TestObjectSize(t *testing.T) { fstests.TestObjectSize(t) } diff --git a/fstest/fstest.go b/fstest/fstest.go index 26a020803..0591fe4cf 100644 --- a/fstest/fstest.go +++ b/fstest/fstest.go @@ -25,7 +25,7 @@ func init() { // Item represents an item for checking type Item struct { Path string - Md5sum string + Hashes map[fs.HashType]string ModTime time.Time Size int64 WinPath string @@ -49,8 +49,8 @@ func (i *Item) CheckModTime(t *testing.T, obj fs.Object, modTime time.Time, prec } } -// Check checks all the attributes of the object are correct -func (i *Item) Check(t *testing.T, obj fs.Object, precision time.Duration) { +// CheckHashes checks all the hashes the object supports are correct +func (i *Item) CheckHashes(t *testing.T, obj fs.Object) { if obj == nil { t.Fatalf("Object is nil") } @@ -61,12 +61,15 @@ func (i *Item) Check(t *testing.T, obj fs.Object, precision time.Duration) { if err != nil { t.Fatalf("%s: Failed to read hash %v for %q: %v", obj.Fs().String(), hash, obj.Remote(), err) } - if hash == fs.HashMD5 { - if !fs.HashEquals(i.Md5sum, sum) { - t.Errorf("%s/%s: md5 hash incorrect - expecting %q got %q", obj.Fs().String(), obj.Remote(), i.Md5sum, sum) - } + if !fs.HashEquals(i.Hashes[hash], sum) { + t.Errorf("%s/%s: %v hash incorrect - expecting %q got %q", obj.Fs().String(), obj.Remote(), hash, i.Hashes[hash], sum) } } +} + +// Check checks all the attributes of the object are correct +func (i *Item) Check(t *testing.T, obj fs.Object, precision time.Duration) { + i.CheckHashes(t, obj) if i.Size != obj.Size() { t.Errorf("%s/%s: Size incorrect - expecting %d got %d", obj.Fs().String(), obj.Remote(), i.Size, obj.Size()) } diff --git a/fstest/fstests/fstests.go b/fstest/fstests/fstests.go index 3fa06fed6..c2b5ef045 100644 --- a/fstest/fstests/fstests.go +++ b/fstest/fstests/fstests.go @@ -7,8 +7,6 @@ package fstests import ( "bytes" - "crypto/md5" - "encoding/hex" "flag" "io" "log" @@ -157,7 +155,7 @@ func findObject(t *testing.T, Name string) fs.Object { func testPut(t *testing.T, file *fstest.Item) { buf := bytes.NewBufferString(fstest.RandomString(100)) - hash := md5.New() + hash := fs.NewMultiHasher() in := io.TeeReader(buf, hash) file.Size = int64(buf.Len()) @@ -165,7 +163,7 @@ func testPut(t *testing.T, file *fstest.Item) { if err != nil { t.Fatal("Put error", err) } - file.Md5sum = hex.EncodeToString(hash.Sum(nil)) + file.Hashes = hash.Sums() file.Check(t, obj, remote.Precision()) // Re-read the object and check again obj = findObject(t, file.Path) @@ -307,7 +305,7 @@ func TestFsCopy(t *testing.T) { // check file exists in new listing fstest.CheckListing(t, remote, []fstest.Item{file1, file2, file1Copy}) - // Check dst lightly - list above has checked ModTime/Md5sum + // Check dst lightly - list above has checked ModTime/Hashes if dst.Remote() != file1Copy.Path { t.Errorf("object path: want %q got %q", file1Copy.Path, dst.Remote()) } @@ -343,7 +341,7 @@ func TestFsMove(t *testing.T) { // check file exists in new listing fstest.CheckListing(t, remote, []fstest.Item{file2, file1Move}) - // Check dst lightly - list above has checked ModTime/Md5sum + // Check dst lightly - list above has checked ModTime/Hashes if dst.Remote() != file1Move.Path { t.Errorf("object path: want %q got %q", file1Move.Path, dst.Remote()) } @@ -474,17 +472,11 @@ func TestObjectRemote(t *testing.T) { } } -// TestObjectMd5sum tests the MD5SUM of the object is correct -func TestObjectMd5sum(t *testing.T) { +// TestObjectHashes checks all the hashes the object supports +func TestObjectHashes(t *testing.T) { skipIfNotOk(t) obj := findObject(t, file1.Path) - Md5sum, err := obj.Hash(fs.HashMD5) - if err != nil && err != fs.ErrHashUnsupported { - t.Errorf("Error in Md5sum: %v", err) - } - if !fs.HashEquals(Md5sum, file1.Md5sum) { - t.Errorf("Md5sum is wrong %v != %v", Md5sum, file1.Md5sum) - } + file1.CheckHashes(t, obj) } // TestObjectModTime tests the ModTime of the object is correct @@ -523,8 +515,8 @@ func TestObjectOpen(t *testing.T) { if err != nil { t.Fatalf("Open() return error: %v", err) } - hash := md5.New() - n, err := io.Copy(hash, in) + hasher := fs.NewMultiHasher() + n, err := io.Copy(hasher, in) if err != nil { t.Fatalf("io.Copy() return error: %v", err) } @@ -535,17 +527,21 @@ func TestObjectOpen(t *testing.T) { if err != nil { t.Fatalf("in.Close() return error: %v", err) } - Md5sum := hex.EncodeToString(hash.Sum(nil)) - if !fs.HashEquals(Md5sum, file1.Md5sum) { - t.Errorf("Md5sum is wrong %v != %v", Md5sum, file1.Md5sum) + // Check content of file by comparing the calculated hashes + for hashType, got := range hasher.Sums() { + want := file1.Hashes[hashType] + if want != got { + t.Errorf("%v is wrong %v != %v", hashType, want, got) + } } + } // TestObjectUpdate tests that Update works func TestObjectUpdate(t *testing.T) { skipIfNotOk(t) buf := bytes.NewBufferString(fstest.RandomString(200)) - hash := md5.New() + hash := fs.NewMultiHasher() in := io.TeeReader(buf, hash) file1.Size = int64(buf.Len()) @@ -554,7 +550,7 @@ func TestObjectUpdate(t *testing.T) { if err != nil { t.Fatal("Update error", err) } - file1.Md5sum = hex.EncodeToString(hash.Sum(nil)) + file1.Hashes = hash.Sums() file1.Check(t, obj, remote.Precision()) // Re-read the object and check again obj = findObject(t, file1.Path) diff --git a/googlecloudstorage/googlecloudstorage_test.go b/googlecloudstorage/googlecloudstorage_test.go index 2d0f45e59..59e20e843 100644 --- a/googlecloudstorage/googlecloudstorage_test.go +++ b/googlecloudstorage/googlecloudstorage_test.go @@ -42,7 +42,7 @@ func TestFsPrecision(t *testing.T) { fstests.TestFsPrecision(t) } func TestObjectString(t *testing.T) { fstests.TestObjectString(t) } func TestObjectFs(t *testing.T) { fstests.TestObjectFs(t) } func TestObjectRemote(t *testing.T) { fstests.TestObjectRemote(t) } -func TestObjectMd5sum(t *testing.T) { fstests.TestObjectMd5sum(t) } +func TestObjectHashes(t *testing.T) { fstests.TestObjectHashes(t) } func TestObjectModTime(t *testing.T) { fstests.TestObjectModTime(t) } func TestObjectSetModTime(t *testing.T) { fstests.TestObjectSetModTime(t) } func TestObjectSize(t *testing.T) { fstests.TestObjectSize(t) } diff --git a/hubic/hubic_test.go b/hubic/hubic_test.go index 84e344dc4..e91842159 100644 --- a/hubic/hubic_test.go +++ b/hubic/hubic_test.go @@ -42,7 +42,7 @@ func TestFsPrecision(t *testing.T) { fstests.TestFsPrecision(t) } func TestObjectString(t *testing.T) { fstests.TestObjectString(t) } func TestObjectFs(t *testing.T) { fstests.TestObjectFs(t) } func TestObjectRemote(t *testing.T) { fstests.TestObjectRemote(t) } -func TestObjectMd5sum(t *testing.T) { fstests.TestObjectMd5sum(t) } +func TestObjectHashes(t *testing.T) { fstests.TestObjectHashes(t) } func TestObjectModTime(t *testing.T) { fstests.TestObjectModTime(t) } func TestObjectSetModTime(t *testing.T) { fstests.TestObjectSetModTime(t) } func TestObjectSize(t *testing.T) { fstests.TestObjectSize(t) } diff --git a/local/local_test.go b/local/local_test.go index 68f176f90..22a2e5279 100644 --- a/local/local_test.go +++ b/local/local_test.go @@ -42,7 +42,7 @@ func TestFsPrecision(t *testing.T) { fstests.TestFsPrecision(t) } func TestObjectString(t *testing.T) { fstests.TestObjectString(t) } func TestObjectFs(t *testing.T) { fstests.TestObjectFs(t) } func TestObjectRemote(t *testing.T) { fstests.TestObjectRemote(t) } -func TestObjectMd5sum(t *testing.T) { fstests.TestObjectMd5sum(t) } +func TestObjectHashes(t *testing.T) { fstests.TestObjectHashes(t) } func TestObjectModTime(t *testing.T) { fstests.TestObjectModTime(t) } func TestObjectSetModTime(t *testing.T) { fstests.TestObjectSetModTime(t) } func TestObjectSize(t *testing.T) { fstests.TestObjectSize(t) } diff --git a/onedrive/onedrive_test.go b/onedrive/onedrive_test.go index 392997367..73958ec08 100644 --- a/onedrive/onedrive_test.go +++ b/onedrive/onedrive_test.go @@ -42,7 +42,7 @@ func TestFsPrecision(t *testing.T) { fstests.TestFsPrecision(t) } func TestObjectString(t *testing.T) { fstests.TestObjectString(t) } func TestObjectFs(t *testing.T) { fstests.TestObjectFs(t) } func TestObjectRemote(t *testing.T) { fstests.TestObjectRemote(t) } -func TestObjectMd5sum(t *testing.T) { fstests.TestObjectMd5sum(t) } +func TestObjectHashes(t *testing.T) { fstests.TestObjectHashes(t) } func TestObjectModTime(t *testing.T) { fstests.TestObjectModTime(t) } func TestObjectSetModTime(t *testing.T) { fstests.TestObjectSetModTime(t) } func TestObjectSize(t *testing.T) { fstests.TestObjectSize(t) } diff --git a/s3/s3_test.go b/s3/s3_test.go index f73440e03..6c4596ca5 100644 --- a/s3/s3_test.go +++ b/s3/s3_test.go @@ -42,7 +42,7 @@ func TestFsPrecision(t *testing.T) { fstests.TestFsPrecision(t) } func TestObjectString(t *testing.T) { fstests.TestObjectString(t) } func TestObjectFs(t *testing.T) { fstests.TestObjectFs(t) } func TestObjectRemote(t *testing.T) { fstests.TestObjectRemote(t) } -func TestObjectMd5sum(t *testing.T) { fstests.TestObjectMd5sum(t) } +func TestObjectHashes(t *testing.T) { fstests.TestObjectHashes(t) } func TestObjectModTime(t *testing.T) { fstests.TestObjectModTime(t) } func TestObjectSetModTime(t *testing.T) { fstests.TestObjectSetModTime(t) } func TestObjectSize(t *testing.T) { fstests.TestObjectSize(t) } diff --git a/swift/swift_test.go b/swift/swift_test.go index 5351449ca..ec6a39498 100644 --- a/swift/swift_test.go +++ b/swift/swift_test.go @@ -42,7 +42,7 @@ func TestFsPrecision(t *testing.T) { fstests.TestFsPrecision(t) } func TestObjectString(t *testing.T) { fstests.TestObjectString(t) } func TestObjectFs(t *testing.T) { fstests.TestObjectFs(t) } func TestObjectRemote(t *testing.T) { fstests.TestObjectRemote(t) } -func TestObjectMd5sum(t *testing.T) { fstests.TestObjectMd5sum(t) } +func TestObjectHashes(t *testing.T) { fstests.TestObjectHashes(t) } func TestObjectModTime(t *testing.T) { fstests.TestObjectModTime(t) } func TestObjectSetModTime(t *testing.T) { fstests.TestObjectSetModTime(t) } func TestObjectSize(t *testing.T) { fstests.TestObjectSize(t) } diff --git a/yandex/yandex_test.go b/yandex/yandex_test.go index c407ac746..10c270a44 100644 --- a/yandex/yandex_test.go +++ b/yandex/yandex_test.go @@ -42,7 +42,7 @@ func TestFsPrecision(t *testing.T) { fstests.TestFsPrecision(t) } func TestObjectString(t *testing.T) { fstests.TestObjectString(t) } func TestObjectFs(t *testing.T) { fstests.TestObjectFs(t) } func TestObjectRemote(t *testing.T) { fstests.TestObjectRemote(t) } -func TestObjectMd5sum(t *testing.T) { fstests.TestObjectMd5sum(t) } +func TestObjectHashes(t *testing.T) { fstests.TestObjectHashes(t) } func TestObjectModTime(t *testing.T) { fstests.TestObjectModTime(t) } func TestObjectSetModTime(t *testing.T) { fstests.TestObjectSetModTime(t) } func TestObjectSize(t *testing.T) { fstests.TestObjectSize(t) }