2018-01-12 17:30:54 +01:00
|
|
|
package hash_test
|
2016-01-11 13:39:33 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/hash"
|
2018-01-26 15:25:21 +01:00
|
|
|
"github.com/spf13/pflag"
|
2016-06-29 18:59:31 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-01-11 13:39:33 +01:00
|
|
|
)
|
|
|
|
|
2018-01-26 15:25:21 +01:00
|
|
|
// Check it satisfies the interface
|
|
|
|
var _ pflag.Value = (*hash.Type)(nil)
|
|
|
|
|
2016-01-11 13:39:33 +01:00
|
|
|
func TestHashSet(t *testing.T) {
|
2018-01-12 17:30:54 +01:00
|
|
|
var h hash.Set
|
2016-01-11 13:39:33 +01:00
|
|
|
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, 0, h.Count())
|
|
|
|
|
2016-01-11 13:39:33 +01:00
|
|
|
a := h.Array()
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Len(t, a, 0)
|
2016-01-11 13:39:33 +01:00
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
h = h.Add(hash.MD5)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, 1, h.Count())
|
2018-01-18 21:27:52 +01:00
|
|
|
assert.Equal(t, hash.MD5, h.GetOne())
|
2016-01-11 13:39:33 +01:00
|
|
|
a = h.Array()
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Len(t, a, 1)
|
2018-01-18 21:27:52 +01:00
|
|
|
assert.Equal(t, a[0], hash.MD5)
|
2016-01-11 13:39:33 +01:00
|
|
|
|
|
|
|
// Test overlap, with all hashes
|
2018-01-18 21:27:52 +01:00
|
|
|
h = h.Overlap(hash.Supported)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, 1, h.Count())
|
2018-01-18 21:27:52 +01:00
|
|
|
assert.Equal(t, hash.MD5, h.GetOne())
|
|
|
|
assert.True(t, h.SubsetOf(hash.Supported))
|
|
|
|
assert.True(t, h.SubsetOf(hash.NewHashSet(hash.MD5)))
|
2016-01-11 13:39:33 +01:00
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
h = h.Add(hash.SHA1)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, 2, h.Count())
|
2016-01-11 13:39:33 +01:00
|
|
|
one := h.GetOne()
|
2018-01-18 21:27:52 +01:00
|
|
|
if !(one == hash.MD5 || one == hash.SHA1) {
|
2016-01-11 13:39:33 +01:00
|
|
|
t.Fatalf("expected to be either MD5 or SHA1, got %v", one)
|
|
|
|
}
|
2018-01-18 21:27:52 +01:00
|
|
|
assert.True(t, h.SubsetOf(hash.Supported))
|
|
|
|
assert.False(t, h.SubsetOf(hash.NewHashSet(hash.MD5)))
|
|
|
|
assert.False(t, h.SubsetOf(hash.NewHashSet(hash.SHA1)))
|
|
|
|
assert.True(t, h.SubsetOf(hash.NewHashSet(hash.MD5, hash.SHA1)))
|
2016-01-11 13:39:33 +01:00
|
|
|
a = h.Array()
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Len(t, a, 2)
|
2016-01-11 13:39:33 +01:00
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
ol := h.Overlap(hash.NewHashSet(hash.MD5))
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, 1, ol.Count())
|
2018-01-18 21:27:52 +01:00
|
|
|
assert.True(t, ol.Contains(hash.MD5))
|
|
|
|
assert.False(t, ol.Contains(hash.SHA1))
|
2016-01-11 13:39:33 +01:00
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
ol = h.Overlap(hash.NewHashSet(hash.MD5, hash.SHA1))
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, 2, ol.Count())
|
2018-01-18 21:27:52 +01:00
|
|
|
assert.True(t, ol.Contains(hash.MD5))
|
|
|
|
assert.True(t, ol.Contains(hash.SHA1))
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type hashTest struct {
|
|
|
|
input []byte
|
2018-01-12 17:30:54 +01:00
|
|
|
output map[hash.Type]string
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var hashTestSet = []hashTest{
|
2016-02-29 17:57:23 +01:00
|
|
|
{
|
2016-01-11 13:39:33 +01:00
|
|
|
input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
|
2018-01-12 17:30:54 +01:00
|
|
|
output: map[hash.Type]string{
|
2018-04-20 12:42:46 +02:00
|
|
|
hash.MD5: "bf13fc19e5151ac57d4252e0e0f87abe",
|
|
|
|
hash.SHA1: "3ab6543c08a75f292a5ecedac87ec41642d12166",
|
|
|
|
hash.Dropbox: "214d2fcf3566e94c99ad2f59bd993daca46d8521a0c447adf4b324f53fddc0c7",
|
|
|
|
hash.QuickXorHash: "0110c000085000031c0001095ec00218d0000700",
|
2019-06-26 20:39:01 +02:00
|
|
|
hash.Whirlpool: "eddf52133d4566d763f716e853d6e4efbabd29e2c2e63f56747b1596172851d34c2df9944beb6640dbdbe3d9b4eb61180720a79e3d15baff31c91e43d63869a4",
|
2019-08-06 14:39:29 +02:00
|
|
|
hash.CRC32: "a6041d7e",
|
2016-01-11 13:39:33 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// Empty data set
|
2016-02-29 17:57:23 +01:00
|
|
|
{
|
2016-01-11 13:39:33 +01:00
|
|
|
input: []byte{},
|
2018-01-12 17:30:54 +01:00
|
|
|
output: map[hash.Type]string{
|
2018-04-20 12:42:46 +02:00
|
|
|
hash.MD5: "d41d8cd98f00b204e9800998ecf8427e",
|
|
|
|
hash.SHA1: "da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
|
|
|
hash.Dropbox: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
|
|
hash.QuickXorHash: "0000000000000000000000000000000000000000",
|
2019-06-26 20:39:01 +02:00
|
|
|
hash.Whirlpool: "19fa61d75522a4669b44e39c1d2e1726c530232130d407f89afee0964997f7a73e83be698b288febcf88e3e03c4f0757ea8964e59b63d93708b138cc42a66eb3",
|
2019-08-06 14:39:29 +02:00
|
|
|
hash.CRC32: "00000000",
|
2016-01-11 13:39:33 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiHasher(t *testing.T) {
|
|
|
|
for _, test := range hashTestSet {
|
2018-01-12 17:30:54 +01:00
|
|
|
mh := hash.NewMultiHasher()
|
2016-01-11 13:39:33 +01:00
|
|
|
n, err := io.Copy(mh, bytes.NewBuffer(test.input))
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, test.input, int(n))
|
2016-01-11 13:39:33 +01:00
|
|
|
sums := mh.Sums()
|
|
|
|
for k, v := range sums {
|
|
|
|
expect, ok := test.output[k]
|
2017-05-26 16:09:31 +02:00
|
|
|
require.True(t, ok, "test output for hash not found")
|
2018-04-20 12:42:46 +02:00
|
|
|
assert.Equal(t, expect, v)
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
// Test that all are present
|
|
|
|
for k, v := range test.output {
|
|
|
|
expect, ok := sums[k]
|
2017-05-26 16:09:31 +02:00
|
|
|
require.True(t, ok, "test output for hash not found")
|
2018-04-20 12:42:46 +02:00
|
|
|
assert.Equal(t, expect, v)
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiHasherTypes(t *testing.T) {
|
2018-01-18 21:27:52 +01:00
|
|
|
h := hash.SHA1
|
2016-01-11 13:39:33 +01:00
|
|
|
for _, test := range hashTestSet {
|
2018-01-12 17:30:54 +01:00
|
|
|
mh, err := hash.NewMultiHasherTypes(hash.NewHashSet(h))
|
2016-01-11 13:39:33 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
n, err := io.Copy(mh, bytes.NewBuffer(test.input))
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, test.input, int(n))
|
2016-01-11 13:39:33 +01:00
|
|
|
sums := mh.Sums()
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Len(t, sums, 1)
|
|
|
|
assert.Equal(t, sums[h], test.output[h])
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashStream(t *testing.T) {
|
|
|
|
for _, test := range hashTestSet {
|
2018-01-12 17:30:54 +01:00
|
|
|
sums, err := hash.Stream(bytes.NewBuffer(test.input))
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
2016-01-11 13:39:33 +01:00
|
|
|
for k, v := range sums {
|
|
|
|
expect, ok := test.output[k]
|
2016-06-29 18:59:31 +02:00
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, v, expect)
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
// Test that all are present
|
|
|
|
for k, v := range test.output {
|
|
|
|
expect, ok := sums[k]
|
2016-06-29 18:59:31 +02:00
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, v, expect)
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashStreamTypes(t *testing.T) {
|
2018-01-18 21:27:52 +01:00
|
|
|
h := hash.SHA1
|
2016-01-11 13:39:33 +01:00
|
|
|
for _, test := range hashTestSet {
|
2018-01-12 17:30:54 +01:00
|
|
|
sums, err := hash.StreamTypes(bytes.NewBuffer(test.input), hash.NewHashSet(h))
|
2016-06-29 18:59:31 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, sums, 1)
|
|
|
|
assert.Equal(t, sums[h], test.output[h])
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashSetStringer(t *testing.T) {
|
2018-04-20 12:42:46 +02:00
|
|
|
h := hash.NewHashSet(hash.SHA1, hash.MD5, hash.Dropbox, hash.QuickXorHash)
|
|
|
|
assert.Equal(t, h.String(), "[MD5, SHA-1, DropboxHash, QuickXorHash]")
|
2018-01-18 21:27:52 +01:00
|
|
|
h = hash.NewHashSet(hash.SHA1)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, h.String(), "[SHA-1]")
|
2018-01-12 17:30:54 +01:00
|
|
|
h = hash.NewHashSet()
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, h.String(), "[]")
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashStringer(t *testing.T) {
|
2018-01-18 21:27:52 +01:00
|
|
|
h := hash.MD5
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, h.String(), "MD5")
|
2018-01-18 21:27:52 +01:00
|
|
|
h = hash.None
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, h.String(), "None")
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|