2018-08-20 22:38:21 +08:00
|
|
|
package jottacloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/lib/readers"
|
2018-08-20 22:38:21 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadMD5(t *testing.T) {
|
|
|
|
// Check readMD5 for different size and threshold
|
|
|
|
for _, size := range []int64{0, 1024, 10 * 1024, 100 * 1024} {
|
|
|
|
t.Run(fmt.Sprintf("%d", size), func(t *testing.T) {
|
|
|
|
hasher := md5.New()
|
2018-10-08 22:25:57 +08:00
|
|
|
n, err := io.Copy(hasher, readers.NewPatternReader(size))
|
2018-08-20 22:38:21 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, n, size)
|
|
|
|
wantMD5 := fmt.Sprintf("%x", hasher.Sum(nil))
|
|
|
|
for _, threshold := range []int64{512, 1024, 10 * 1024, 20 * 1024} {
|
|
|
|
t.Run(fmt.Sprintf("%d", threshold), func(t *testing.T) {
|
2018-10-08 22:25:57 +08:00
|
|
|
in := readers.NewPatternReader(size)
|
2018-08-20 22:38:21 +08:00
|
|
|
gotMD5, out, cleanup, err := readMD5(in, size, threshold)
|
|
|
|
defer cleanup()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, wantMD5, gotMD5)
|
|
|
|
|
|
|
|
// check md5hash of out
|
|
|
|
hasher := md5.New()
|
|
|
|
n, err := io.Copy(hasher, out)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, n, size)
|
|
|
|
outMD5 := fmt.Sprintf("%x", hasher.Sum(nil))
|
|
|
|
assert.Equal(t, wantMD5, outMD5)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|