2016-01-11 13:39:33 +01:00
|
|
|
package fs_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func TestHashSet(t *testing.T) {
|
|
|
|
var h fs.HashSet
|
|
|
|
|
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
|
|
|
|
|
|
|
h = h.Add(fs.HashMD5)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, 1, h.Count())
|
|
|
|
assert.Equal(t, fs.HashMD5, 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)
|
|
|
|
assert.Equal(t, a[0], fs.HashMD5)
|
2016-01-11 13:39:33 +01:00
|
|
|
|
|
|
|
// Test overlap, with all hashes
|
|
|
|
h = h.Overlap(fs.SupportedHashes)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, 1, h.Count())
|
|
|
|
assert.Equal(t, fs.HashMD5, h.GetOne())
|
|
|
|
assert.True(t, h.SubsetOf(fs.SupportedHashes))
|
|
|
|
assert.True(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5)))
|
2016-01-11 13:39:33 +01:00
|
|
|
|
|
|
|
h = h.Add(fs.HashSHA1)
|
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()
|
|
|
|
if !(one == fs.HashMD5 || one == fs.HashSHA1) {
|
|
|
|
t.Fatalf("expected to be either MD5 or SHA1, got %v", one)
|
|
|
|
}
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.True(t, h.SubsetOf(fs.SupportedHashes))
|
|
|
|
assert.False(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5)))
|
|
|
|
assert.False(t, h.SubsetOf(fs.NewHashSet(fs.HashSHA1)))
|
|
|
|
assert.True(t, h.SubsetOf(fs.NewHashSet(fs.HashMD5, fs.HashSHA1)))
|
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
|
|
|
|
|
|
|
ol := h.Overlap(fs.NewHashSet(fs.HashMD5))
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, 1, ol.Count())
|
|
|
|
assert.True(t, ol.Contains(fs.HashMD5))
|
|
|
|
assert.False(t, ol.Contains(fs.HashSHA1))
|
2016-01-11 13:39:33 +01:00
|
|
|
|
|
|
|
ol = h.Overlap(fs.NewHashSet(fs.HashMD5, fs.HashSHA1))
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, 2, ol.Count())
|
|
|
|
assert.True(t, ol.Contains(fs.HashMD5))
|
|
|
|
assert.True(t, ol.Contains(fs.HashSHA1))
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type hashTest struct {
|
|
|
|
input []byte
|
|
|
|
output map[fs.HashType]string
|
|
|
|
}
|
|
|
|
|
|
|
|
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},
|
|
|
|
output: map[fs.HashType]string{
|
|
|
|
fs.HashMD5: "bf13fc19e5151ac57d4252e0e0f87abe",
|
|
|
|
fs.HashSHA1: "3ab6543c08a75f292a5ecedac87ec41642d12166",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Empty data set
|
2016-02-29 17:57:23 +01:00
|
|
|
{
|
2016-01-11 13:39:33 +01:00
|
|
|
input: []byte{},
|
|
|
|
output: map[fs.HashType]string{
|
|
|
|
fs.HashMD5: "d41d8cd98f00b204e9800998ecf8427e",
|
|
|
|
fs.HashSHA1: "da39a3ee5e6b4b0d3255bfef95601890afd80709",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiHasher(t *testing.T) {
|
|
|
|
for _, test := range hashTestSet {
|
|
|
|
mh := fs.NewMultiHasher()
|
|
|
|
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]
|
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 TestMultiHasherTypes(t *testing.T) {
|
|
|
|
h := fs.HashSHA1
|
|
|
|
for _, test := range hashTestSet {
|
|
|
|
mh, err := fs.NewMultiHasherTypes(fs.NewHashSet(h))
|
|
|
|
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 {
|
|
|
|
sums, err := fs.HashStream(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) {
|
|
|
|
h := fs.HashSHA1
|
|
|
|
for _, test := range hashTestSet {
|
|
|
|
sums, err := fs.HashStreamTypes(bytes.NewBuffer(test.input), fs.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) {
|
|
|
|
h := fs.NewHashSet(fs.HashSHA1, fs.HashMD5)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, h.String(), "[MD5, SHA-1]")
|
2016-01-11 13:39:33 +01:00
|
|
|
h = fs.NewHashSet(fs.HashSHA1)
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, h.String(), "[SHA-1]")
|
2016-01-11 13:39:33 +01:00
|
|
|
h = fs.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) {
|
|
|
|
h := fs.HashMD5
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, h.String(), "MD5")
|
2016-01-11 13:39:33 +01:00
|
|
|
h = fs.HashNone
|
2016-06-29 18:59:31 +02:00
|
|
|
assert.Equal(t, h.String(), "None")
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|