From bcdb7719c6993eeebc66df22d28ca329646260de Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 20 Apr 2018 11:42:46 +0100 Subject: [PATCH] fs/hash: install QuickXorHash as a supported rclone hash type #2262 --- fs/hash/hash.go | 20 ++++++++++++++++---- fs/hash/hash_test.go | 22 ++++++++++++---------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/fs/hash/hash.go b/fs/hash/hash.go index 83d72cf24..57b206bfd 100644 --- a/fs/hash/hash.go +++ b/fs/hash/hash.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/ncw/rclone/backend/dropbox/dbhash" + "github.com/ncw/rclone/backend/onedrive/quickxorhash" "github.com/pkg/errors" ) @@ -31,19 +32,24 @@ const ( // https://www.dropbox.com/developers/reference/content-hash Dropbox + // QuickXorHash indicates Microsoft onedrive hash + // https://docs.microsoft.com/en-us/onedrive/developer/code-snippets/quickxorhash + QuickXorHash + // None indicates no hashes are supported None Type = 0 ) // Supported returns a set of all the supported hashes by // HashStream and MultiHasher. -var Supported = NewHashSet(MD5, SHA1, Dropbox) +var Supported = NewHashSet(MD5, SHA1, Dropbox, QuickXorHash) // Width returns the width in characters for any HashType var Width = map[Type]int{ - MD5: 32, - SHA1: 40, - Dropbox: 64, + MD5: 32, + SHA1: 40, + Dropbox: 64, + QuickXorHash: 40, } // Stream will calculate hashes of all supported hash types. @@ -81,6 +87,8 @@ func (h Type) String() string { return "SHA-1" case Dropbox: return "DropboxHash" + case QuickXorHash: + return "QuickXorHash" default: err := fmt.Sprintf("internal error: unknown hash type: 0x%x", int(h)) panic(err) @@ -98,6 +106,8 @@ func (h *Type) Set(s string) error { *h = SHA1 case "DropboxHash": *h = Dropbox + case "QuickXorHash": + *h = QuickXorHash default: return errors.Errorf("Unknown hash type %q", s) } @@ -126,6 +136,8 @@ func fromTypes(set Set) (map[Type]hash.Hash, error) { hashers[t] = sha1.New() case Dropbox: hashers[t] = dbhash.New() + case QuickXorHash: + hashers[t] = quickxorhash.New() default: err := fmt.Sprintf("internal error: Unsupported hash type %v", t) panic(err) diff --git a/fs/hash/hash_test.go b/fs/hash/hash_test.go index ba41621f3..92ef91ab1 100644 --- a/fs/hash/hash_test.go +++ b/fs/hash/hash_test.go @@ -69,18 +69,20 @@ var hashTestSet = []hashTest{ { input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, output: map[hash.Type]string{ - hash.MD5: "bf13fc19e5151ac57d4252e0e0f87abe", - hash.SHA1: "3ab6543c08a75f292a5ecedac87ec41642d12166", - hash.Dropbox: "214d2fcf3566e94c99ad2f59bd993daca46d8521a0c447adf4b324f53fddc0c7", + hash.MD5: "bf13fc19e5151ac57d4252e0e0f87abe", + hash.SHA1: "3ab6543c08a75f292a5ecedac87ec41642d12166", + hash.Dropbox: "214d2fcf3566e94c99ad2f59bd993daca46d8521a0c447adf4b324f53fddc0c7", + hash.QuickXorHash: "0110c000085000031c0001095ec00218d0000700", }, }, // Empty data set { input: []byte{}, output: map[hash.Type]string{ - hash.MD5: "d41d8cd98f00b204e9800998ecf8427e", - hash.SHA1: "da39a3ee5e6b4b0d3255bfef95601890afd80709", - hash.Dropbox: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + hash.MD5: "d41d8cd98f00b204e9800998ecf8427e", + hash.SHA1: "da39a3ee5e6b4b0d3255bfef95601890afd80709", + hash.Dropbox: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + hash.QuickXorHash: "0000000000000000000000000000000000000000", }, }, } @@ -95,13 +97,13 @@ func TestMultiHasher(t *testing.T) { for k, v := range sums { expect, ok := test.output[k] require.True(t, ok, "test output for hash not found") - assert.Equal(t, v, expect) + assert.Equal(t, expect, v) } // Test that all are present for k, v := range test.output { expect, ok := sums[k] require.True(t, ok, "test output for hash not found") - assert.Equal(t, v, expect) + assert.Equal(t, expect, v) } } } @@ -151,8 +153,8 @@ func TestHashStreamTypes(t *testing.T) { } func TestHashSetStringer(t *testing.T) { - h := hash.NewHashSet(hash.SHA1, hash.MD5, hash.Dropbox) - assert.Equal(t, h.String(), "[MD5, SHA-1, DropboxHash]") + h := hash.NewHashSet(hash.SHA1, hash.MD5, hash.Dropbox, hash.QuickXorHash) + assert.Equal(t, h.String(), "[MD5, SHA-1, DropboxHash, QuickXorHash]") h = hash.NewHashSet(hash.SHA1) assert.Equal(t, h.String(), "[SHA-1]") h = hash.NewHashSet()