Proton's generic "requirements not met" code 2000, which it returns for a node changed since it was read (as when a lost rename response is resent), aborted the whole run with "upgrade rclone". It is now an ordinary error that isn't retried. Code 2028 (a feature disabled, rate-limited or blocked) is retried, and the codes Proton actually uses for a rejected or missing app version, 5003 and 5001, now give the upgrade message. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
679 lines
24 KiB
Go
679 lines
24 KiB
Go
package protondrive
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"maps"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/pquerna/otp/totp"
|
|
"github.com/rclone/rclone/backend/protondrive/api"
|
|
"github.com/rclone/rclone/backend/protondrive/protontest"
|
|
"github.com/rclone/rclone/fs"
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
|
"github.com/rclone/rclone/fs/fserrors"
|
|
"github.com/rclone/rclone/fs/object"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestTranslateError(t *testing.T) {
|
|
apiErr := func(code int) error {
|
|
return fmt.Errorf("call: %w", &api.Error{Status: 422, Code: code, Message: "proton says no"})
|
|
}
|
|
notFound := fmt.Errorf("get node: %w", errNotFound)
|
|
exists := fmt.Errorf("create: %w", errExists)
|
|
other := errors.New("other")
|
|
|
|
for _, test := range []struct {
|
|
name string
|
|
err error
|
|
op errorOp
|
|
want error // compared with ==, if set
|
|
check func(t *testing.T, got error)
|
|
}{
|
|
{name: "nil", err: nil, op: opObject, want: nil},
|
|
{name: "not found object", err: notFound, op: opObject, want: fs.ErrorObjectNotFound},
|
|
{name: "not found dir", err: notFound, op: opDir, want: fs.ErrorDirNotFound},
|
|
{name: "code 2501 object", err: apiErr(api.CodeNotExists), op: opObject, want: fs.ErrorObjectNotFound},
|
|
{name: "code 2501 dir", err: apiErr(api.CodeNotExists), op: opDir, want: fs.ErrorDirNotFound},
|
|
{name: "exists mkdir", err: exists, op: opMkdir, want: nil},
|
|
{name: "exists move", err: exists, op: opMove, want: fs.ErrorCantMove},
|
|
{name: "exists copy", err: exists, op: opCopy, want: fs.ErrorCantCopy},
|
|
{name: "exists dirmove", err: exists, op: opDirMove, want: fs.ErrorDirExists},
|
|
{name: "exists object", err: exists, op: opObject, check: func(t *testing.T, got error) {
|
|
assert.ErrorIs(t, got, errExists)
|
|
}},
|
|
{name: "other unchanged", err: other, op: opObject, want: other},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
got := translateError(test.err, test.op)
|
|
if test.check != nil {
|
|
test.check(t, got)
|
|
return
|
|
}
|
|
assert.Equal(t, test.want, got)
|
|
})
|
|
}
|
|
|
|
for _, code := range []int{
|
|
api.CodeInsufficientQuota, api.CodeInsufficientSpace, api.CodeMaxFileSizeForFreeUser,
|
|
api.CodeMaxPublicEditModeForFreeUser, api.CodeTooManyChildren, api.CodeNestingTooDeep,
|
|
} {
|
|
t.Run(fmt.Sprintf("code %d no retry", code), func(t *testing.T) {
|
|
got := translateError(apiErr(code), opObject)
|
|
assert.True(t, fserrors.IsNoRetryError(got))
|
|
assert.False(t, fserrors.IsFatalError(got))
|
|
assert.Contains(t, got.Error(), "proton says no")
|
|
})
|
|
}
|
|
for _, code := range []int{api.CodeAppVersionMissing, api.CodeAppVersionBad} {
|
|
t.Run(fmt.Sprintf("code %d fatal", code), func(t *testing.T) {
|
|
got := translateError(apiErr(code), opObject)
|
|
assert.True(t, fserrors.IsFatalError(got))
|
|
assert.Contains(t, got.Error(), "upgrade rclone")
|
|
assert.Contains(t, got.Error(), "proton says no")
|
|
})
|
|
}
|
|
t.Run("code 2000 no retry", func(t *testing.T) {
|
|
got := translateError(apiErr(api.CodeInvalidRequirements), opMove)
|
|
assert.True(t, fserrors.IsNoRetryError(got))
|
|
assert.False(t, fserrors.IsFatalError(got))
|
|
assert.NotContains(t, got.Error(), "upgrade rclone")
|
|
assert.Contains(t, got.Error(), "proton says no")
|
|
})
|
|
t.Run("code 2028 not fatal", func(t *testing.T) {
|
|
got := translateError(apiErr(api.CodeFeatureBlocked), opObject)
|
|
assert.False(t, fserrors.IsFatalError(got))
|
|
assert.False(t, fserrors.IsNoRetryError(got))
|
|
assert.NotContains(t, got.Error(), "upgrade rclone")
|
|
assert.Contains(t, got.Error(), "proton says no")
|
|
})
|
|
for _, err := range []error{errIntegrity, errUploadMismatch, errUploadSuperseded} {
|
|
t.Run(err.Error()+" no retry", func(t *testing.T) {
|
|
got := translateError(fmt.Errorf("op: %w", err), opObject)
|
|
assert.True(t, fserrors.IsNoRetryError(got))
|
|
assert.ErrorIs(t, got, err)
|
|
})
|
|
}
|
|
for _, err := range []error{errUploadOutcomeUnknown, errUploadUnconfirmed} {
|
|
t.Run(err.Error()+" no low level retry", func(t *testing.T) {
|
|
got := translateError(fmt.Errorf("op: %w", err), opObject)
|
|
assert.True(t, fserrors.IsNoLowLevelRetryError(got))
|
|
assert.False(t, fserrors.IsNoRetryError(got))
|
|
assert.ErrorIs(t, got, err)
|
|
})
|
|
}
|
|
}
|
|
|
|
// sessionConfig returns the config keys of a v1.76 remote holding a new
|
|
// saved session of u, pointed at srv.
|
|
func sessionConfig(srv *protontest.Server, u *protontest.User) configmap.Simple {
|
|
sess := srv.NewSession(u)
|
|
return configmap.Simple{
|
|
"type": "protondrive",
|
|
"username": "",
|
|
"password": "",
|
|
"mailbox_password": "",
|
|
"2fa": "",
|
|
"otp_secret_key": "",
|
|
"client_uid": sess.UID,
|
|
"client_access_token": sess.AccessToken,
|
|
"client_refresh_token": sess.RefreshToken,
|
|
"client_salted_key_pass": base64.StdEncoding.EncodeToString([]byte(u.KeyPassword())),
|
|
"encoding": "Slash,LeftSpace,RightSpace,InvalidUtf8,Dot",
|
|
"original_file_size": "true",
|
|
"app_version": "",
|
|
"replace_existing_draft": "false",
|
|
"enable_caching": "true",
|
|
// Not a v1.76 key, but the only way to reach the fake.
|
|
"endpoint": srv.URL(),
|
|
}
|
|
}
|
|
|
|
// newTestFs returns an Fs for the config m, shut down when the test ends.
|
|
func newTestFs(t *testing.T, m configmap.Mapper) *Fs {
|
|
t.Helper()
|
|
f, err := NewFs(context.Background(), "test", "", m)
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { _ = f.(*Fs).Shutdown(context.Background()) })
|
|
return f.(*Fs)
|
|
}
|
|
|
|
// authRequests returns the auth requests made to srv.
|
|
func authRequests(srv *protontest.Server) []string {
|
|
var out []string
|
|
for route := range srv.Requests() {
|
|
if strings.Contains(route, "/auth") {
|
|
out = append(out, route)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestOptionsBackwardsCompatible(t *testing.T) {
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
m := sessionConfig(srv, u)
|
|
want := m["client_access_token"]
|
|
f := newTestFs(t, m)
|
|
|
|
_, err := f.List(context.Background(), "")
|
|
require.NoError(t, err)
|
|
assert.Empty(t, authRequests(srv), "a saved session must be used without signing in")
|
|
assert.Equal(t, want, m["client_access_token"])
|
|
assert.Len(t, m["draft_client_uid"], 32, "draft_client_uid is generated and saved")
|
|
assert.True(t, f.opt.ReportOriginalSize)
|
|
assert.True(t, f.opt.EnableCaching)
|
|
}
|
|
|
|
// unsavedMap is a config which saves nothing, as one from environment
|
|
// variables or a connection string.
|
|
type unsavedMap struct{ configmap.Simple }
|
|
|
|
// Set does nothing.
|
|
func (unsavedMap) Set(key, value string) {}
|
|
|
|
func TestDraftClientUIDStableWhenUnsaved(t *testing.T) {
|
|
ctx := context.Background()
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
uid := func(name string) string {
|
|
m := sessionConfig(srv, u)
|
|
f, err := NewFs(ctx, name, "", unsavedMap{m})
|
|
require.NoError(t, err)
|
|
t.Cleanup(func() { _ = f.(*Fs).Shutdown(ctx) })
|
|
got := f.(*Fs).c.opt.DraftClientUID
|
|
assert.Len(t, got, 32)
|
|
return got
|
|
}
|
|
first := uid("env")
|
|
assert.Equal(t, first, uid("env"), "a later run of the same remote has another draft client ID")
|
|
assert.NotEqual(t, first, uid("other"), "another remote of the account has the same draft client ID")
|
|
}
|
|
|
|
func TestNewFsPasswordFallback(t *testing.T) {
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
m := sessionConfig(srv, u)
|
|
m["username"] = "user"
|
|
m["password"] = obscure.MustObscure("pass")
|
|
oldUID := m["client_uid"]
|
|
srv.RevokeSession(oldUID)
|
|
|
|
f := newTestFs(t, m)
|
|
_, err := f.List(context.Background(), "")
|
|
require.NoError(t, err)
|
|
assert.NotEqual(t, oldUID, m["client_uid"])
|
|
assert.NotEmpty(t, m["client_access_token"])
|
|
assert.NotEmpty(t, m["client_refresh_token"])
|
|
keyPass, err := base64.StdEncoding.DecodeString(m["client_salted_key_pass"])
|
|
require.NoError(t, err)
|
|
assert.Equal(t, u.KeyPassword(), string(keyPass))
|
|
assert.Equal(t, 1, srv.Requests()["POST /core/v4/auth"])
|
|
}
|
|
|
|
func TestNewFsPasswordFallbackTOTP(t *testing.T) {
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
secret := u.EnableTOTP()
|
|
m := sessionConfig(srv, u)
|
|
for _, k := range []string{"client_uid", "client_access_token", "client_refresh_token", "client_salted_key_pass"} {
|
|
m[k] = ""
|
|
}
|
|
m["username"] = "user"
|
|
m["password"] = obscure.MustObscure("pass")
|
|
m["otp_secret_key"] = obscure.MustObscure(secret)
|
|
|
|
newTestFs(t, m)
|
|
assert.NotEmpty(t, m["client_uid"])
|
|
assert.Equal(t, 1, srv.Requests()["POST /core/v4/auth/2fa"])
|
|
}
|
|
|
|
func TestNewFsNoCredentials(t *testing.T) {
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
|
|
t.Run("none saved", func(t *testing.T) {
|
|
m := configmap.Simple{"endpoint": srv.URL()}
|
|
_, err := NewFs(context.Background(), "myremote", "", m)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), `rclone config reconnect myremote:`)
|
|
})
|
|
t.Run("revoked", func(t *testing.T) {
|
|
m := sessionConfig(srv, u)
|
|
srv.RevokeSession(m["client_uid"])
|
|
_, err := NewFs(context.Background(), "myremote", "", m)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), `rclone config reconnect myremote:`)
|
|
})
|
|
t.Run("undecodable key password", func(t *testing.T) {
|
|
m := sessionConfig(srv, u)
|
|
m["client_salted_key_pass"] = "not base64!"
|
|
_, err := NewFs(context.Background(), "myremote", "", m)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), `rclone config reconnect myremote:`)
|
|
})
|
|
}
|
|
|
|
func TestTwoRemotesIsolated(t *testing.T) {
|
|
ctx := context.Background()
|
|
srv := protontest.New(t)
|
|
u1 := srv.AddUser("user1", "pass1")
|
|
u2 := srv.AddUser("user2", "pass2")
|
|
m1, m2 := sessionConfig(srv, u1), sessionConfig(srv, u2)
|
|
uid1, uid2 := m1["client_uid"], m2["client_uid"]
|
|
f1, f2 := newTestFs(t, m1), newTestFs(t, m2)
|
|
require.NoError(t, f1.Mkdir(ctx, "one"))
|
|
require.NoError(t, f2.Mkdir(ctx, "two"))
|
|
|
|
// Make both sessions refresh, saving new tokens.
|
|
access1, access2 := m1["client_access_token"], m2["client_access_token"]
|
|
srv.ResetRequests()
|
|
srv.ExpireAccessToken(uid1)
|
|
srv.ExpireAccessToken(uid2)
|
|
f1.DirCacheFlush()
|
|
f2.DirCacheFlush()
|
|
entries1, err := f1.List(ctx, "")
|
|
require.NoError(t, err)
|
|
entries2, err := f2.List(ctx, "")
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries1, 1)
|
|
assert.Equal(t, "one", entries1[0].Remote())
|
|
require.Len(t, entries2, 1)
|
|
assert.Equal(t, "two", entries2[0].Remote())
|
|
assert.Equal(t, 2, srv.Requests()["POST /auth/v4/refresh"])
|
|
assert.NotEqual(t, access1, m1["client_access_token"], "remote 1 saved its refreshed token")
|
|
assert.NotEqual(t, access2, m2["client_access_token"], "remote 2 saved its refreshed token")
|
|
assert.Equal(t, uid1, m1["client_uid"])
|
|
assert.Equal(t, uid2, m2["client_uid"])
|
|
for _, k := range []string{"client_uid", "client_access_token", "client_refresh_token", "client_salted_key_pass", "draft_client_uid"} {
|
|
assert.NotEqual(t, m1[k], m2[k], k)
|
|
}
|
|
}
|
|
|
|
// configAnswers drives configure through its states, answering each
|
|
// question by its state from answers, and returns the questions asked.
|
|
func configAnswers(t *testing.T, m configmap.Mapper, signIn signInOptions, answers map[string]string) []*fs.ConfigOut {
|
|
t.Helper()
|
|
var asked []*fs.ConfigOut
|
|
in := fs.ConfigIn{}
|
|
for range 10 {
|
|
out, err := configure(context.Background(), "test", m, in, signIn)
|
|
require.NoError(t, err)
|
|
if out == nil {
|
|
return asked
|
|
}
|
|
require.Empty(t, out.Error)
|
|
in = fs.ConfigIn{State: out.State}
|
|
if out.Option != nil {
|
|
asked = append(asked, out)
|
|
answer, ok := answers[out.State]
|
|
require.True(t, ok, "unexpected question in state %q: %s", out.State, out.Option.Help)
|
|
in.Result = answer
|
|
}
|
|
}
|
|
t.Fatal("config didn't finish")
|
|
return nil
|
|
}
|
|
|
|
// requireSession checks m holds a working session of u on srv.
|
|
func requireSession(t *testing.T, srv *protontest.Server, m configmap.Simple, u *protontest.User) {
|
|
t.Helper()
|
|
keyPass, err := base64.StdEncoding.DecodeString(m["client_salted_key_pass"])
|
|
require.NoError(t, err)
|
|
assert.Equal(t, u.KeyPassword(), string(keyPass))
|
|
srv.ResetRequests()
|
|
newTestFs(t, m)
|
|
assert.Empty(t, authRequests(srv))
|
|
}
|
|
|
|
func TestConfigBrowser(t *testing.T) {
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
m := configmap.Simple{"endpoint": srv.URL()}
|
|
var shown string
|
|
signIn := signInOptions{
|
|
pollInterval: 10 * time.Millisecond,
|
|
pollTimeout: 10 * time.Second,
|
|
showURL: func(signInURL string) {
|
|
shown = signInURL
|
|
require.NoError(t, srv.ApproveFork(signInURL, u))
|
|
},
|
|
}
|
|
|
|
asked := configAnswers(t, m, signIn, map[string]string{"choose": "true"})
|
|
require.Len(t, asked, 1)
|
|
assert.Contains(t, asked[0].Option.Help, "rclone is a third-party application not officially supported by Proton.")
|
|
assert.Equal(t, true, asked[0].Option.Default)
|
|
assert.True(t, strings.HasPrefix(shown, forkSignInURL), shown)
|
|
requireSession(t, srv, m, u)
|
|
}
|
|
|
|
func TestConfigPassword(t *testing.T) {
|
|
srv := protontest.New(t)
|
|
|
|
t.Run("no 2fa", func(t *testing.T) {
|
|
u := srv.AddUser("user", "pass")
|
|
m := configmap.Simple{"endpoint": srv.URL(), "username": "user", "password": obscure.MustObscure("pass")}
|
|
asked := configAnswers(t, m, signInOptions{}, map[string]string{"choose": "false"})
|
|
require.Len(t, asked, 1)
|
|
assert.Equal(t, false, asked[0].Option.Default, "password sign-in is the default when a password is set")
|
|
requireSession(t, srv, m, u)
|
|
})
|
|
t.Run("2fa asked", func(t *testing.T) {
|
|
u := srv.AddUser("user2fa", "pass")
|
|
secret := u.EnableTOTP()
|
|
m := configmap.Simple{"endpoint": srv.URL(), "username": "user2fa", "password": obscure.MustObscure("pass")}
|
|
code, err := totp.GenerateCode(secret, time.Now())
|
|
require.NoError(t, err)
|
|
asked := configAnswers(t, m, signInOptions{}, map[string]string{"choose": "false", "password_2fa": code})
|
|
require.Len(t, asked, 2)
|
|
requireSession(t, srv, m, u)
|
|
assert.Empty(t, m["2fa"], "the 2FA code is not saved")
|
|
})
|
|
t.Run("captcha", func(t *testing.T) {
|
|
u := srv.AddUser("usercaptcha", "pass")
|
|
srv.RequireCaptcha(u)
|
|
m := configmap.Simple{"endpoint": srv.URL(), "username": "usercaptcha", "password": obscure.MustObscure("pass")}
|
|
out, err := configure(context.Background(), "test", m, fs.ConfigIn{State: "password"}, signInOptions{})
|
|
require.NoError(t, err)
|
|
require.NotNil(t, out)
|
|
assert.Contains(t, out.Error, "web browser")
|
|
assert.Equal(t, "method", out.State, "goes back to the choice of sign-in")
|
|
assert.Empty(t, m["client_uid"])
|
|
})
|
|
t.Run("no password", func(t *testing.T) {
|
|
m := configmap.Simple{"endpoint": srv.URL()}
|
|
out, err := configure(context.Background(), "test", m, fs.ConfigIn{State: "password"}, signInOptions{})
|
|
require.NoError(t, err)
|
|
require.NotNil(t, out)
|
|
assert.NotEmpty(t, out.Error)
|
|
})
|
|
}
|
|
|
|
func TestConfigStore(t *testing.T) {
|
|
creds := credentials{UID: "uid", AccessToken: "access", RefreshToken: "refresh", KeyPassword: "key\x00pass"}
|
|
|
|
t.Run("round trip", func(t *testing.T) {
|
|
m := configmap.Simple{}
|
|
s := &configStore{m: m}
|
|
s.Save(creds)
|
|
assert.Equal(t, base64.StdEncoding.EncodeToString([]byte(creds.KeyPassword)), m[clientSaltedKeyPassKey])
|
|
assert.Equal(t, creds, s.Load())
|
|
// Another user of the config saves newer tokens.
|
|
m[clientRefreshTokenKey] = "newer"
|
|
assert.Equal(t, "newer", s.Load().RefreshToken)
|
|
})
|
|
t.Run("overridden", func(t *testing.T) {
|
|
// Values from the environment or the command line take
|
|
// precedence over the config file the store saves to.
|
|
env := configmap.Simple{clientUIDKey: "old uid", clientAccessTokenKey: "old access", clientRefreshTokenKey: "old refresh"}
|
|
file := configmap.Simple{}
|
|
m := configmap.New().AddGetter(env, configmap.PriorityNormal).AddGetter(file, configmap.PriorityConfig).AddSetter(file)
|
|
s := &configStore{m: m}
|
|
assert.Equal(t, "old refresh", s.Load().RefreshToken)
|
|
s.Save(creds)
|
|
assert.Equal(t, creds, s.Load(), "saved credentials must not be shadowed by spent ones")
|
|
})
|
|
}
|
|
|
|
// putFile uploads content to f at remote and returns the object.
|
|
func putFile(t *testing.T, f fs.Fs, remote string, content []byte) fs.Object {
|
|
t.Helper()
|
|
src := object.NewStaticObjectInfo(remote, time.Now(), int64(len(content)), true, nil, nil)
|
|
o, err := f.Put(context.Background(), bytes.NewReader(content), src)
|
|
require.NoError(t, err)
|
|
return o
|
|
}
|
|
|
|
// readObject returns the content of o read with options.
|
|
func readObject(t *testing.T, o fs.Object, options ...fs.OpenOption) ([]byte, error) {
|
|
t.Helper()
|
|
rc, err := o.Open(context.Background(), options...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { require.NoError(t, rc.Close()) }()
|
|
return io.ReadAll(rc)
|
|
}
|
|
|
|
func TestRmdirRefusesHiddenChildren(t *testing.T) {
|
|
ctx := context.Background()
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
f := newTestFs(t, sessionConfig(srv, u))
|
|
|
|
require.NoError(t, f.Mkdir(ctx, "docs"))
|
|
docsID, err := f.dirCache.FindDir(ctx, "docs", false)
|
|
require.NoError(t, err)
|
|
u.SeedDoc(docsID, "notes")
|
|
assert.Equal(t, fs.ErrorDirectoryNotEmpty, f.Rmdir(ctx, "docs"))
|
|
|
|
// Filled by another client after an empty listing was cached.
|
|
require.NoError(t, f.Mkdir(ctx, "later"))
|
|
entries, err := f.List(ctx, "later")
|
|
require.NoError(t, err)
|
|
require.Empty(t, entries)
|
|
laterID, err := f.dirCache.FindDir(ctx, "later", false)
|
|
require.NoError(t, err)
|
|
u.SeedFile(laterID, "new.txt", []byte("x"), time.Now())
|
|
assert.Equal(t, fs.ErrorDirectoryNotEmpty, f.Rmdir(ctx, "later"))
|
|
|
|
require.NoError(t, f.Mkdir(ctx, "empty"))
|
|
require.NoError(t, f.Rmdir(ctx, "empty"))
|
|
paths := map[string]bool{}
|
|
for _, d := range u.Dump() {
|
|
paths[d.Path] = d.State == api.LinkStateActive
|
|
}
|
|
assert.True(t, paths["docs/notes"])
|
|
assert.True(t, paths["later/new.txt"])
|
|
}
|
|
|
|
func TestConfigKeepsSession(t *testing.T) {
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
ri, err := fs.Find("protondrive")
|
|
require.NoError(t, err)
|
|
|
|
t.Run("default", func(t *testing.T) {
|
|
m := sessionConfig(srv, u)
|
|
want := maps.Clone(m)
|
|
srv.ResetRequests()
|
|
asked := configAnswers(t, m, signInOptions{}, map[string]string{"reauth": "false"})
|
|
require.Len(t, asked, 1)
|
|
assert.Equal(t, false, asked[0].Option.Default)
|
|
assert.Equal(t, "config_refresh_token", asked[0].Option.Name)
|
|
assert.Empty(t, srv.Requests())
|
|
assert.Equal(t, want, m)
|
|
})
|
|
t.Run("config update", func(t *testing.T) {
|
|
// As "rclone config update remote enable_caching=false" runs it.
|
|
m := sessionConfig(srv, u)
|
|
srv.ResetRequests()
|
|
out, err := fs.BackendConfig(context.Background(), "test", m, ri, configmap.Simple{"config_refresh_token": "false"}, fs.ConfigIn{})
|
|
require.NoError(t, err)
|
|
assert.Nil(t, out)
|
|
assert.Empty(t, srv.Requests())
|
|
})
|
|
}
|
|
|
|
func TestConfigSignInAgain(t *testing.T) {
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
m := sessionConfig(srv, u)
|
|
m["username"] = "user"
|
|
m["password"] = obscure.MustObscure("pass")
|
|
old := maps.Clone(m)
|
|
|
|
configAnswers(t, m, signInOptions{}, map[string]string{"reauth": "true", "choose": "false"})
|
|
assert.NotEqual(t, old["client_uid"], m["client_uid"])
|
|
assert.Equal(t, 1, srv.Requests()["DELETE /core/v4/auth"], "the old session is signed out")
|
|
requireSession(t, srv, m, u)
|
|
|
|
// The old session no longer works.
|
|
old["username"], old["password"] = "", ""
|
|
_, err := NewFs(context.Background(), "test", "", old)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestShutdownKeepsFsUsable(t *testing.T) {
|
|
ctx := context.Background()
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
f := newTestFs(t, sessionConfig(srv, u))
|
|
o := putFile(t, f, "dir/a.txt", []byte("hello"))
|
|
|
|
require.NoError(t, f.Shutdown(ctx))
|
|
require.NoError(t, f.Shutdown(ctx))
|
|
got, err := readObject(t, o)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "hello", string(got))
|
|
putFile(t, f, "dir/b.txt", []byte("world"))
|
|
entries, err := f.List(ctx, "dir")
|
|
require.NoError(t, err)
|
|
assert.Len(t, entries, 2)
|
|
o, err = f.NewObject(ctx, "dir/b.txt")
|
|
require.NoError(t, err)
|
|
got, err = readObject(t, o)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "world", string(got))
|
|
}
|
|
|
|
func TestDisconnect(t *testing.T) {
|
|
ctx := context.Background()
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
m := sessionConfig(srv, u)
|
|
f := newTestFs(t, m)
|
|
o := putFile(t, f, "a.txt", []byte("hello"))
|
|
|
|
require.NoError(t, f.Disconnect(ctx))
|
|
assert.Empty(t, m["client_access_token"])
|
|
assert.Empty(t, m["client_refresh_token"])
|
|
|
|
_, err := f.List(ctx, "")
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), `rclone config reconnect test:`)
|
|
_, err = readObject(t, o)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), `rclone config reconnect test:`)
|
|
_, err = f.Put(ctx, bytes.NewReader([]byte("x")), object.NewStaticObjectInfo("b.txt", time.Now(), 1, true, nil, nil))
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), `rclone config reconnect test:`)
|
|
}
|
|
|
|
func TestSessionExpiredNamesRemote(t *testing.T) {
|
|
ctx := context.Background()
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
m := sessionConfig(srv, u)
|
|
f := newTestFs(t, m)
|
|
f.c.s.adoptWait = 0
|
|
srv.RevokeSession(m["client_uid"])
|
|
|
|
_, err := f.List(ctx, "")
|
|
require.ErrorIs(t, err, errSessionExpired)
|
|
assert.Contains(t, err.Error(), `rclone config reconnect test:`)
|
|
assert.NotContains(t, err.Error(), "remote:")
|
|
}
|
|
|
|
func TestTrashedBehindTheFsBack(t *testing.T) {
|
|
ctx := context.Background()
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
f := newTestFs(t, sessionConfig(srv, u))
|
|
other := newTestFs(t, sessionConfig(srv, u))
|
|
|
|
o := putFile(t, f, "a.txt", []byte("hello"))
|
|
require.NoError(t, f.Mkdir(ctx, "dir"))
|
|
_, err := f.List(ctx, "")
|
|
require.NoError(t, err)
|
|
trash := func(remote string) {
|
|
otherObj, err := other.NewObject(ctx, remote)
|
|
if err == nil {
|
|
require.NoError(t, otherObj.Remove(ctx))
|
|
return
|
|
}
|
|
require.NoError(t, other.Purge(ctx, remote))
|
|
}
|
|
trash("a.txt")
|
|
trash("dir")
|
|
|
|
src := object.NewStaticObjectInfo("a.txt", time.Now(), 1, true, nil, nil)
|
|
assert.Equal(t, fs.ErrorObjectNotFound, o.Update(ctx, bytes.NewReader([]byte("x")), src))
|
|
_, err = f.Move(ctx, o, "b.txt")
|
|
assert.Equal(t, fs.ErrorObjectNotFound, err)
|
|
_, err = f.Copy(ctx, o, "c.txt")
|
|
assert.Equal(t, fs.ErrorObjectNotFound, err)
|
|
assert.Equal(t, fs.ErrorDirNotFound, f.DirMove(ctx, f, "dir", "dir2"))
|
|
for _, d := range u.Dump() {
|
|
assert.NotContains(t, []string{"b.txt", "c.txt", "dir2"}, d.Path)
|
|
}
|
|
}
|
|
|
|
func TestOpenRange(t *testing.T) {
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
content := []byte("0123456789abcdefghijklmnopqrstuvwxyz")
|
|
|
|
// A file without extended attributes, whose plaintext size only
|
|
// its content tells.
|
|
seeded := u.SeedFile(u.RootID(), "tmp", content, time.Now())
|
|
stored := u.StoredFile(seeded)
|
|
stored.Revision.XAttr = ""
|
|
m := sessionConfig(srv, u)
|
|
cleanup := newTestFs(t, m)
|
|
o, err := cleanup.NewObject(context.Background(), "tmp")
|
|
require.NoError(t, err)
|
|
require.NoError(t, o.Remove(context.Background()))
|
|
u.SeedStoredFile(u.RootID(), stored)
|
|
|
|
for _, original := range []bool{true, false} {
|
|
t.Run(fmt.Sprint("original_file_size=", original), func(t *testing.T) {
|
|
m := sessionConfig(srv, u)
|
|
m["original_file_size"] = fmt.Sprint(original)
|
|
f := newTestFs(t, m)
|
|
put := putFile(t, f, fmt.Sprintf("put-%v.txt", original), content)
|
|
bare, err := f.NewObject(context.Background(), "tmp")
|
|
require.NoError(t, err)
|
|
for _, o := range []fs.Object{put, bare} {
|
|
for _, test := range []struct {
|
|
opt fs.OpenOption
|
|
want string
|
|
}{
|
|
{&fs.RangeOption{Start: -1, End: 5}, "vwxyz"},
|
|
{&fs.RangeOption{Start: 30, End: -1}, "uvwxyz"},
|
|
{&fs.RangeOption{Start: 30, End: 1000}, "uvwxyz"},
|
|
{&fs.RangeOption{Start: 2, End: 4}, "234"},
|
|
{&fs.SeekOption{Offset: 33}, "xyz"},
|
|
{&fs.RangeOption{Start: -1, End: 1000}, string(content)},
|
|
} {
|
|
got, err := readObject(t, o, test.opt)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, test.want, string(got), "%s %v", o.Remote(), test.opt)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestPutNestedReadBackLag puts a file into new nested directories
|
|
// while the server takes a moment to show new nodes.
|
|
func TestPutNestedReadBackLag(t *testing.T) {
|
|
srv := protontest.New(t)
|
|
u := srv.AddUser("user", "pass")
|
|
f := newTestFs(t, sessionConfig(srv, u))
|
|
srv.SetReadLag(2, protontest.LagMissing)
|
|
o := putFile(t, f, "A1/B1/C1/one", []byte("aaa"))
|
|
assert.Equal(t, int64(3), o.Size())
|
|
}
|