operations: add operations/hashsum to the rc as rclone hashsum equivalent

Fixes #7569
This commit is contained in:
Nick Craig-Wood
2024-01-17 11:47:33 +00:00
parent 0b8689dc28
commit d50572b108
2 changed files with 164 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package operations_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
@@ -14,6 +15,7 @@ import (
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/cache"
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/fs/operations"
"github.com/rclone/rclone/fs/rc"
"github.com/rclone/rclone/fstest"
@@ -779,3 +781,88 @@ deadbeefcafe00000000000000000000 subdir/file2
})
}
// operations/hashsum: hashsum a directory
func TestRcHashsum(t *testing.T) {
ctx := context.Background()
r, call := rcNewRun(t, "operations/hashsum")
r.Mkdir(ctx, r.Fremote)
file1Contents := "file1 contents"
file1 := r.WriteBoth(ctx, "hashsum-file1", file1Contents, t1)
r.CheckLocalItems(t, file1)
r.CheckRemoteItems(t, file1)
hasher := hash.NewMultiHasher()
_, err := hasher.Write([]byte(file1Contents))
require.NoError(t, err)
for _, test := range []struct {
ht hash.Type
base64 bool
download bool
}{
{
ht: r.Fremote.Hashes().GetOne(),
}, {
ht: r.Fremote.Hashes().GetOne(),
base64: true,
}, {
ht: hash.Whirlpool,
base64: false,
download: true,
}, {
ht: hash.Whirlpool,
base64: true,
download: true,
},
} {
t.Run(fmt.Sprintf("hash=%v,base64=%v,download=%v", test.ht, test.base64, test.download), func(t *testing.T) {
file1Hash, err := hasher.SumString(test.ht, test.base64)
require.NoError(t, err)
in := rc.Params{
"fs": r.FremoteName,
"hashType": test.ht.String(),
"base64": test.base64,
"download": test.download,
}
out, err := call.Fn(ctx, in)
require.NoError(t, err)
assert.Equal(t, test.ht.String(), out["hashType"])
want := []string{
fmt.Sprintf("%s hashsum-file1", file1Hash),
}
assert.Equal(t, want, out["hashsum"])
})
}
}
// operations/hashsum: hashsum a single file
func TestRcHashsumFile(t *testing.T) {
ctx := context.Background()
r, call := rcNewRun(t, "operations/hashsum")
r.Mkdir(ctx, r.Fremote)
file1Contents := "file1 contents"
file1 := r.WriteBoth(ctx, "hashsum-file1", file1Contents, t1)
file2Contents := "file2 contents"
file2 := r.WriteBoth(ctx, "hashsum-file2", file2Contents, t1)
r.CheckLocalItems(t, file1, file2)
r.CheckRemoteItems(t, file1, file2)
// Make an fs pointing to just the file
fsString := path.Join(r.FremoteName, file1.Path)
in := rc.Params{
"fs": fsString,
"hashType": "MD5",
"download": true,
}
out, err := call.Fn(ctx, in)
require.NoError(t, err)
assert.Equal(t, "md5", out["hashType"])
assert.Equal(t, []string{"0ef726ce9b1a7692357ff70dd321d595 hashsum-file1"}, out["hashsum"])
}