build / windows (push) Skipped
build / other_os (push) Skipped
build / mac_amd64 (push) Skipped
build / mac_arm64 (push) Skipped
build / linux (push) Skipped
build / go1.26 (push) Skipped
build / linux_386 (push) Skipped
build / lint (push) Skipped
build / android-all (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/386 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/amd64 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm/v6 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm/v7 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm64 (push) Skipped
Build & Push Docker Images / Merge & Push Final Docker Image (push) Waiting for needed jobs
Replace Proton-API-Bridge and go-proton-api with a client in backend/protondrive written against Proton's current Drive API, following the behaviour of Proton's official Drive SDK: - volume-based and v2 routes only - upload block verification, signed manifests and extended attributes - current content encryption (v4 node keys, v6 PKESK, SEIPDv2) - browser (session fork) sign-in, with username and password still supported - the volume's change events, used to keep the metadata cache current and for ChangeNotify, polled at most every 30 s - an in-memory fake Proton server (protontest) so the tests run without an account Uses ProtonMail/gopenpgp/v3 and ProtonMail/go-srp directly; no cgo. Written with heavy AI assistance (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
543 lines
22 KiB
Go
543 lines
22 KiB
Go
package protontest
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ProtonMail/go-crypto/openpgp/armor"
|
|
"github.com/ProtonMail/go-crypto/openpgp/packet"
|
|
"github.com/ProtonMail/go-crypto/openpgp/s2k"
|
|
"github.com/ProtonMail/gopenpgp/v3/crypto"
|
|
"github.com/ProtonMail/gopenpgp/v3/profile"
|
|
"github.com/rclone/rclone/backend/protondrive/api"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// sendJSON sends in as JSON with method to path as sess and returns
|
|
// the HTTP status and body.
|
|
func sendJSON(t *testing.T, s *Server, sess Session, method, path string, in any) (int, []byte) {
|
|
t.Helper()
|
|
body, err := json.Marshal(in)
|
|
require.NoError(t, err)
|
|
req, err := http.NewRequest(method, s.URL()+path, bytes.NewReader(body))
|
|
require.NoError(t, err)
|
|
for k, v := range sessionHeader(sess) {
|
|
req.Header.Set(k, v)
|
|
}
|
|
resp, err := s.Client().Do(req)
|
|
require.NoError(t, err)
|
|
defer func() { _ = resp.Body.Close() }()
|
|
out, err := io.ReadAll(resp.Body)
|
|
require.NoError(t, err)
|
|
return resp.StatusCode, out
|
|
}
|
|
|
|
// requireRejected fails the test unless the response is HTTP 422 with
|
|
// code, and its message contains each of want.
|
|
func requireRejected(t *testing.T, code, status int, body []byte, want ...string) {
|
|
t.Helper()
|
|
require.Equal(t, http.StatusUnprocessableEntity, status, "body: %s", body)
|
|
e := decodeError(t, body)
|
|
assert.Equal(t, code, e.Code, "body: %s", body)
|
|
for _, w := range want {
|
|
assert.Contains(t, e.Message, w)
|
|
}
|
|
}
|
|
|
|
// newFolderRequest returns a create folder request for name in
|
|
// parentID made as the SDK's createFolder does.
|
|
func newFolderRequest(u *User, parentID, name string) *createFolderRequest {
|
|
v := u.view()
|
|
parentKey, parentHashKey := v.folderKeys(parentID)
|
|
node := newDriveKey(false)
|
|
pass := sealPassphrase(node.passphrase, parentKey, v.addrKey)
|
|
_, hashKey := newHashKey(node.key)
|
|
xattr := encryptArmored([]byte(`{"Common":{"ModificationTime":"2024-01-02T03:04:05.000Z"}}`), node.key, v.addrKey, true)
|
|
return &createFolderRequest{
|
|
ParentLinkID: parentID,
|
|
NodeKey: node.armored,
|
|
NodeHashKey: hashKey,
|
|
NodePassphrase: pass.armored,
|
|
NodePassphraseSignature: pass.signature,
|
|
SignatureEmail: &v.email,
|
|
Name: encryptArmored([]byte(name), parentKey, v.addrKey, false),
|
|
Hash: lookupHash(parentHashKey, name),
|
|
XAttr: &xattr,
|
|
}
|
|
}
|
|
|
|
// messagePackets returns the packets of an armored message.
|
|
func messagePackets(t *testing.T, armored string) []*packet.OpaquePacket {
|
|
t.Helper()
|
|
block, err := armor.Decode(strings.NewReader(armored))
|
|
require.NoError(t, err)
|
|
var out []*packet.OpaquePacket
|
|
r := packet.NewOpaqueReader(block.Body)
|
|
for {
|
|
op, err := r.Next()
|
|
if err == io.EOF {
|
|
return out
|
|
}
|
|
require.NoError(t, err)
|
|
out = append(out, op)
|
|
}
|
|
}
|
|
|
|
// armorPackets returns packets as an armored message.
|
|
func armorPackets(t *testing.T, packets ...*packet.OpaquePacket) string {
|
|
t.Helper()
|
|
var out bytes.Buffer
|
|
w, err := armor.Encode(&out, "PGP MESSAGE", nil)
|
|
require.NoError(t, err)
|
|
for _, op := range packets {
|
|
require.NoError(t, op.Serialize(w))
|
|
}
|
|
require.NoError(t, w.Close())
|
|
return out.String()
|
|
}
|
|
|
|
// aeadMessages returns messages in the formats clients must not write
|
|
// for names, passphrases, hash keys and extended attributes, by name.
|
|
func aeadMessages(t *testing.T) map[string]string {
|
|
t.Helper()
|
|
// Encrypting to a file key, which advertises AEAD, gives a v6 PKESK
|
|
// and SEIPDv2, as gopenpgp does unless told otherwise.
|
|
eh, err := aeadPGP().Encryption().Recipient(newDriveKey(true).key).New()
|
|
require.NoError(t, err)
|
|
msg, err := eh.Encrypt([]byte("value"))
|
|
require.NoError(t, err)
|
|
armored, err := msg.Armor()
|
|
require.NoError(t, err)
|
|
v6 := messagePackets(t, armored)
|
|
require.Len(t, v6, 2)
|
|
require.Equal(t, []byte{6, 2}, []byte{v6[0].Contents[0], v6[1].Contents[0]})
|
|
v3 := messagePackets(t, encryptArmored([]byte("value"), newDriveKey(false).key, nil, false))
|
|
require.Len(t, v3, 2)
|
|
require.Equal(t, []byte{3, 1}, []byte{v3[0].Contents[0], v3[1].Contents[0]})
|
|
return map[string]string{
|
|
"v6 PKESK and SEIPDv2": armored,
|
|
"v3 PKESK and SEIPDv2": armorPackets(t, v3[0], v6[1]),
|
|
"v6 PKESK and SEIPDv1": armorPackets(t, v6[0], v3[1]),
|
|
}
|
|
}
|
|
|
|
// lockedWith returns a new v4 node key locked by a handle of p.
|
|
func lockedWith(t *testing.T, p *profile.Custom) string {
|
|
t.Helper()
|
|
key, err := crypto.PGP().KeyGeneration().AddUserId(driveKeyName, "").New().GenerateKey()
|
|
require.NoError(t, err)
|
|
locked, err := crypto.PGPWithProfile(p).LockKey(key, newPassphrase())
|
|
require.NoError(t, err)
|
|
armored, err := locked.Armor()
|
|
require.NoError(t, err)
|
|
return armored
|
|
}
|
|
|
|
func TestCreateFolderFormats(t *testing.T) {
|
|
s := New(t)
|
|
u := s.AddUser("user", "pass")
|
|
sess := s.NewSession(u)
|
|
path := "/drive/v2/volumes/" + u.VolumeID() + "/folders"
|
|
post := func(req *createFolderRequest) (int, []byte) {
|
|
return sendJSON(t, s, sess, "POST", path, req)
|
|
}
|
|
|
|
for variant, bad := range aeadMessages(t) {
|
|
for field, spoil := range map[string]func(req *createFolderRequest){
|
|
"Name": func(req *createFolderRequest) { req.Name = bad },
|
|
"NodePassphrase": func(req *createFolderRequest) { req.NodePassphrase = bad },
|
|
"NodeHashKey": func(req *createFolderRequest) { req.NodeHashKey = bad },
|
|
"XAttr": func(req *createFolderRequest) { req.XAttr = &bad },
|
|
} {
|
|
t.Run(field+" "+variant, func(t *testing.T) {
|
|
req := newFolderRequest(u, u.RootID(), "dir")
|
|
spoil(req)
|
|
status, body := post(req)
|
|
requireRejected(t, api.CodeInvalidValue, status, body, field)
|
|
})
|
|
}
|
|
}
|
|
|
|
aeadLocked := profile.Default()
|
|
aeadLocked.AeadKeyEncryption = &packet.AEADConfig{}
|
|
argon2 := profile.Default()
|
|
argon2.AeadKeyEncryption = &packet.AEADConfig{}
|
|
argon2.S2kKeyEncryption = &s2k.Config{S2KMode: s2k.Argon2S2K, Argon2Config: &s2k.Argon2Config{}}
|
|
salted := profile.Default()
|
|
salted.S2kKeyEncryption = &s2k.Config{S2KMode: s2k.SaltedS2K, PassphraseIsHighEntropy: true}
|
|
v6Key, err := crypto.PGPWithProfile(profile.RFC9580()).KeyGeneration().AddUserId(driveKeyName, "").New().GenerateKey()
|
|
require.NoError(t, err)
|
|
v6Locked, err := crypto.PGPWithProfile(profile.RFC9580()).LockKey(v6Key, newPassphrase())
|
|
require.NoError(t, err)
|
|
v6Armored, err := v6Locked.Armor()
|
|
require.NoError(t, err)
|
|
unlocked, err := generateKey(driveKeyName, "").Armor()
|
|
require.NoError(t, err)
|
|
for name, tc := range map[string]struct {
|
|
key string
|
|
want string
|
|
}{
|
|
"advertises AEAD": {newDriveKey(true).armored, "advertises AEAD"},
|
|
"v6": {v6Armored, "not a v4 key"},
|
|
"AEAD locked": {lockedWith(t, aeadLocked), "locked with AEAD"},
|
|
"Argon2": {lockedWith(t, argon2), "locked with AEAD"},
|
|
"salted S2K": {lockedWith(t, salted), "S2K mode 1"},
|
|
"not locked": {unlocked, "not locked"},
|
|
} {
|
|
t.Run("NodeKey "+name, func(t *testing.T) {
|
|
req := newFolderRequest(u, u.RootID(), "dir")
|
|
req.NodeKey = tc.key
|
|
status, body := post(req)
|
|
requireRejected(t, api.CodeInvalidValue, status, body, "NodeKey", tc.want)
|
|
})
|
|
}
|
|
|
|
for name, spoil := range map[string]func(req *createFolderRequest){
|
|
"no hash key": func(req *createFolderRequest) { req.NodeHashKey = "" },
|
|
"no signature": func(req *createFolderRequest) { req.NodePassphraseSignature = "" },
|
|
"bad hash": func(req *createFolderRequest) { req.Hash = "ABC" },
|
|
"no signature email": func(req *createFolderRequest) { req.SignatureEmail = nil },
|
|
"someone else's email": func(req *createFolderRequest) { other := "other@proton.test"; req.SignatureEmail = &other },
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
req := newFolderRequest(u, u.RootID(), "dir")
|
|
spoil(req)
|
|
status, body := post(req)
|
|
requireRejected(t, api.CodeInvalidValue, status, body)
|
|
})
|
|
}
|
|
assert.Empty(t, u.Dump())
|
|
|
|
req := newFolderRequest(u, u.RootID(), "dir")
|
|
req.ParentLinkID = "unknown"
|
|
status, body := post(req)
|
|
requireRejected(t, api.CodeNotExists, status, body)
|
|
|
|
var created createFolderResponse
|
|
status, body = post(newFolderRequest(u, u.RootID(), "dir"))
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
require.NoError(t, json.Unmarshal(body, &created))
|
|
dump := u.Dump()
|
|
require.Len(t, dump, 1)
|
|
assert.Equal(t, created.Folder.ID, dump[0].ID)
|
|
assert.Equal(t, "dir", dump[0].Path)
|
|
assert.NoError(t, dump[0].Err)
|
|
assert.True(t, time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC).Equal(dump[0].ModTime))
|
|
|
|
// The name hash must be unique among the folder's untrashed nodes.
|
|
fileID := u.SeedFile(u.RootID(), "file", []byte("x"), time.Now())
|
|
status, body = post(newFolderRequest(u, u.RootID(), "file"))
|
|
requireRejected(t, api.CodeAlreadyExists, status, body)
|
|
var conflict conflictResponse
|
|
require.NoError(t, json.Unmarshal(body, &conflict))
|
|
assert.Equal(t, fileID, conflict.Details.ConflictLinkID)
|
|
require.NotNil(t, conflict.Details.ConflictRevisionID)
|
|
assert.Equal(t, u.view().d.links[fileID].activeRevision().id, *conflict.Details.ConflictRevisionID)
|
|
}
|
|
|
|
// renameMoveCopy holds a user with folders a and b, a signed file f in
|
|
// a and an anonymous file anon in a, and a session.
|
|
type renameMoveCopy struct {
|
|
s *Server
|
|
u *User
|
|
sess Session
|
|
a, b, f, anon string
|
|
volume string
|
|
validMsg, aeadMsg string
|
|
}
|
|
|
|
func newRenameMoveCopy(t *testing.T) *renameMoveCopy {
|
|
s := New(t)
|
|
u := s.AddUser("user", "pass")
|
|
x := &renameMoveCopy{s: s, u: u, sess: s.NewSession(u), volume: u.VolumeID()}
|
|
x.a = u.SeedFolder(u.RootID(), "a")
|
|
x.b = u.SeedFolder(u.RootID(), "b")
|
|
x.f = u.SeedFile(x.a, "f", []byte("f"), time.Now())
|
|
x.anon = u.SeedAnonymousFile(x.a, "anon", []byte("anon"), time.Now())
|
|
// The fake doesn't decrypt what it is sent, so any message in the
|
|
// right format will do.
|
|
x.validMsg = encryptArmored([]byte("value"), newDriveKey(false).key, nil, false)
|
|
x.aeadMsg = aeadMessages(t)["v6 PKESK and SEIPDv2"]
|
|
return x
|
|
}
|
|
|
|
// hash returns the stored name hash of link id.
|
|
func (x *renameMoveCopy) hash(id string) string {
|
|
return x.u.view().d.links[id].nameHash
|
|
}
|
|
|
|
func TestRenameRules(t *testing.T) {
|
|
x := newRenameMoveCopy(t)
|
|
path := func(id string) string { return "/drive/v2/volumes/" + x.volume + "/links/" + id + "/rename" }
|
|
email := x.u.Email()
|
|
newHash := lookupHash([]byte("k"), "new")
|
|
req := func(original string) *renameRequest {
|
|
return &renameRequest{Name: x.validMsg, Hash: &newHash, NameSignatureEmail: &email, OriginalHash: &original}
|
|
}
|
|
|
|
status, body := sendJSON(t, x.s, x.sess, "PUT", path("unknown"), req(""))
|
|
requireRejected(t, api.CodeNotExists, status, body)
|
|
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.f), req("stale"))
|
|
requireRejected(t, api.CodeInvalidRequirements, status, body, "OriginalHash")
|
|
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.u.RootID()), req(""))
|
|
requireRejected(t, api.CodeInvalidRequirements, status, body)
|
|
bad := req(x.hash(x.f))
|
|
bad.Name = x.aeadMsg
|
|
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.f), bad)
|
|
requireRejected(t, api.CodeInvalidValue, status, body, "Name")
|
|
taken := req(x.hash(x.f))
|
|
anonHash := x.hash(x.anon)
|
|
taken.Hash = &anonHash
|
|
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.f), taken)
|
|
requireRejected(t, api.CodeAlreadyExists, status, body)
|
|
|
|
// Renaming to its own name is no conflict.
|
|
same := req(x.hash(x.f))
|
|
ownHash := x.hash(x.f)
|
|
same.Hash = &ownHash
|
|
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.f), same)
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.f), req(x.hash(x.f)))
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
assert.Equal(t, newHash, x.hash(x.f))
|
|
}
|
|
|
|
func TestMoveRules(t *testing.T) {
|
|
x := newRenameMoveCopy(t)
|
|
path := func(id string) string { return "/drive/v2/volumes/" + x.volume + "/links/" + id + "/move" }
|
|
email := x.u.Email()
|
|
other := "other@proton.test"
|
|
sig := "signature"
|
|
req := func(id, parentID string) *moveRequest {
|
|
return &moveRequest{
|
|
Name: x.validMsg,
|
|
NodePassphrase: x.validMsg,
|
|
Hash: lookupHash([]byte("k"), "new"),
|
|
ParentLinkID: parentID,
|
|
OriginalHash: x.hash(id),
|
|
NameSignatureEmail: &email,
|
|
}
|
|
}
|
|
sub := x.u.SeedFolder(x.a, "sub")
|
|
|
|
for name, tc := range map[string]struct {
|
|
id string
|
|
req *moveRequest
|
|
code int
|
|
reason string
|
|
}{
|
|
"unknown link": {"unknown", req(x.f, x.b), api.CodeNotExists, ""},
|
|
"unknown parent": {x.f, req(x.f, "unknown"), api.CodeNotExists, ""},
|
|
"parent is a file": {x.anon, req(x.anon, x.f), api.CodeNotExists, ""},
|
|
"into itself": {x.a, req(x.a, x.a), api.CodeInvalidRequirements, "into itself"},
|
|
"into a descendant": {x.a, req(x.a, sub), api.CodeInvalidRequirements, "into itself"},
|
|
"the root": {x.u.RootID(), req(x.u.RootID(), x.b), api.CodeInvalidRequirements, ""},
|
|
"stale original hash": {x.f, func() *moveRequest { r := req(x.f, x.b); r.OriginalHash = "stale"; return r }(), api.CodeInvalidRequirements, "OriginalHash"},
|
|
"AEAD name": {x.f, func() *moveRequest { r := req(x.f, x.b); r.Name = x.aeadMsg; return r }(), api.CodeInvalidValue, "Name"},
|
|
"AEAD passphrase": {x.f, func() *moveRequest { r := req(x.f, x.b); r.NodePassphrase = x.aeadMsg; return r }(), api.CodeInvalidValue, "NodePassphrase"},
|
|
"name signed by another": {x.f, func() *moveRequest { r := req(x.f, x.b); r.NameSignatureEmail = &other; return r }(), api.CodeInvalidValue, "NameSignatureEmail"},
|
|
"signed node re-signed": {x.f, func() *moveRequest {
|
|
r := req(x.f, x.b)
|
|
r.NodePassphraseSignature, r.SignatureEmail = &sig, &email
|
|
return r
|
|
}(), api.CodeInvalidValue, "must not be given"},
|
|
"anonymous node unsigned": {x.anon, req(x.anon, x.b), api.CodeInvalidValue, "anonymous"},
|
|
"anonymous node signed by another": {x.anon, func() *moveRequest {
|
|
r := req(x.anon, x.b)
|
|
r.NodePassphraseSignature, r.SignatureEmail = &sig, &other
|
|
return r
|
|
}(), api.CodeInvalidValue, "SignatureEmail"},
|
|
"name taken": {x.f, func() *moveRequest { r := req(x.f, x.a); r.Hash = x.hash(x.anon); return r }(), api.CodeAlreadyExists, ""},
|
|
} {
|
|
status, body := sendJSON(t, x.s, x.sess, "PUT", path(tc.id), tc.req)
|
|
t.Run(name, func(t *testing.T) {
|
|
requireRejected(t, tc.code, status, body, tc.reason)
|
|
})
|
|
}
|
|
|
|
// A signed node keeps its passphrase signature; an anonymous one
|
|
// takes the one given.
|
|
before := *x.u.view().d.links[x.f]
|
|
status, body := sendJSON(t, x.s, x.sess, "PUT", path(x.f), req(x.f, x.b))
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
after := x.u.view().d.links[x.f]
|
|
assert.Equal(t, x.b, after.parentID)
|
|
assert.Equal(t, before.nodePass.signature, after.nodePass.signature)
|
|
assert.Equal(t, before.signatureEmail, after.signatureEmail)
|
|
assert.Equal(t, x.validMsg, after.nodePass.armored)
|
|
|
|
r := req(x.anon, x.b)
|
|
r.Hash = lookupHash([]byte("k"), "anon")
|
|
r.NodePassphraseSignature, r.SignatureEmail = &sig, &email
|
|
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.anon), r)
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
anon := x.u.view().d.links[x.anon]
|
|
assert.Equal(t, sig, anon.nodePass.signature)
|
|
assert.Equal(t, email, anon.signatureEmail)
|
|
assert.Equal(t, email, anon.nameSignatureEmail)
|
|
}
|
|
|
|
func TestCopyRules(t *testing.T) {
|
|
x := newRenameMoveCopy(t)
|
|
path := func(id string) string { return "/drive/volumes/" + x.volume + "/links/" + id + "/copy" }
|
|
email := x.u.Email()
|
|
req := func(parentID string) *copyRequest {
|
|
return ©Request{
|
|
Name: x.validMsg,
|
|
NodePassphrase: x.validMsg,
|
|
Hash: lookupHash([]byte("k"), "copy"),
|
|
TargetVolumeID: x.volume,
|
|
TargetParentLinkID: parentID,
|
|
NameSignatureEmail: &email,
|
|
}
|
|
}
|
|
status, body := sendJSON(t, x.s, x.sess, "POST", path("unknown"), req(x.b))
|
|
requireRejected(t, api.CodeNotExists, status, body)
|
|
wrongVolume := req(x.b)
|
|
wrongVolume.TargetVolumeID = "unknown"
|
|
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.f), wrongVolume)
|
|
requireRejected(t, api.CodeNotExists, status, body)
|
|
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.a), req(x.a))
|
|
requireRejected(t, api.CodeInvalidRequirements, status, body, "into itself")
|
|
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.anon), req(x.b))
|
|
requireRejected(t, api.CodeInvalidValue, status, body, "anonymous")
|
|
aead := req(x.b)
|
|
aead.NodePassphrase = x.aeadMsg
|
|
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.f), aead)
|
|
requireRejected(t, api.CodeInvalidValue, status, body, "NodePassphrase")
|
|
taken := req(x.a)
|
|
taken.Hash = x.hash(x.f)
|
|
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.f), taken)
|
|
requireRejected(t, api.CodeAlreadyExists, status, body)
|
|
|
|
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.f), req(x.b))
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
var resp copyResponse
|
|
require.NoError(t, json.Unmarshal(body, &resp))
|
|
v := x.u.view()
|
|
src, cp := v.d.links[x.f], v.d.links[resp.LinkID]
|
|
require.NotNil(t, cp)
|
|
assert.Equal(t, x.b, cp.parentID)
|
|
assert.Equal(t, src.nodeKey, cp.nodeKey)
|
|
assert.Equal(t, src.contentKeyPacket, cp.contentKeyPacket)
|
|
assert.Equal(t, src.nodePass.signature, cp.nodePass.signature)
|
|
assert.NotEqual(t, src.activeRevision().id, cp.activeRevision().id)
|
|
assert.Equal(t, src.activeRevision().blocks, cp.activeRevision().blocks)
|
|
}
|
|
|
|
func TestTrashAndEmptyTrash(t *testing.T) {
|
|
s := New(t)
|
|
u := s.AddUser("user", "pass")
|
|
sess := s.NewSession(u)
|
|
dir := u.SeedFolder(u.RootID(), "dir")
|
|
child := u.SeedFile(dir, "child", []byte("c"), time.Now())
|
|
kept := u.SeedFile(u.RootID(), "kept", []byte("k"), time.Now())
|
|
|
|
var resp multiResponse
|
|
status := postJSON(t, s, "/drive/v2/volumes/"+u.VolumeID()+"/trash_multiple", sessionHeader(sess),
|
|
&linksRequest{LinkIDs: []string{dir, "unknown", u.RootID()}}, &resp)
|
|
require.Equal(t, http.StatusOK, status)
|
|
assert.Equal(t, api.CodeOKMany, resp.Code)
|
|
require.Len(t, resp.Responses, 3)
|
|
assert.Equal(t, linkResultJSON{LinkID: dir, Response: codeJSON{Code: api.CodeOK}}, resp.Responses[0])
|
|
assert.Equal(t, api.CodeNotExists, resp.Responses[1].Response.Code)
|
|
assert.NotEmpty(t, resp.Responses[1].Response.Error)
|
|
assert.Equal(t, api.CodeInvalidRequirements, resp.Responses[2].Response.Code)
|
|
|
|
states := map[string]int{}
|
|
for _, n := range u.Dump() {
|
|
require.NoError(t, n.Err)
|
|
states[n.ID] = n.State
|
|
}
|
|
assert.Equal(t, map[string]int{dir: linkStateTrashed, child: linkStateActive, kept: linkStateActive}, states)
|
|
|
|
status, body := sendJSON(t, s, sess, "DELETE", "/drive/volumes/"+u.VolumeID()+"/trash", nil)
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
dump := u.Dump()
|
|
require.Len(t, dump, 1)
|
|
assert.Equal(t, kept, dump[0].ID)
|
|
}
|
|
|
|
func TestCheckAvailableHashes(t *testing.T) {
|
|
s := New(t)
|
|
u := s.AddUser("user", "pass")
|
|
sess := s.NewSession(u)
|
|
root := u.RootID()
|
|
u.SeedFile(root, "taken", []byte("x"), time.Now())
|
|
trashed := u.SeedFile(root, "trashed", []byte("x"), time.Now())
|
|
_, hashKey := u.view().folderKeys(root)
|
|
draftID, draftRev := randomID(), randomID()
|
|
s.mu.Lock()
|
|
d := u.mustDriveLocked()
|
|
d.links[trashed].state = linkStateTrashed
|
|
d.addLink(&link{
|
|
id: draftID,
|
|
parentID: root,
|
|
typ: linkTypeFile,
|
|
state: linkStateDraft,
|
|
nameHash: lookupHash(hashKey, "draft"),
|
|
revisions: []*revision{{id: draftRev, state: revisionStateDraft, clientUID: "mine"}},
|
|
})
|
|
s.mu.Unlock()
|
|
|
|
hashes := []string{lookupHash(hashKey, "free"), lookupHash(hashKey, "taken"), lookupHash(hashKey, "trashed"), lookupHash(hashKey, "draft")}
|
|
path := "/drive/v2/volumes/" + u.VolumeID() + "/links/" + root + "/checkAvailableHashes"
|
|
var resp availableHashesResponse
|
|
require.Equal(t, http.StatusOK, postJSON(t, s, path, sessionHeader(sess), &checkHashesRequest{Hashes: hashes}, &resp))
|
|
assert.Equal(t, []string{hashes[0], hashes[2]}, resp.AvailableHashes)
|
|
mine := "mine"
|
|
assert.Equal(t, []pendingHashJSON{{Hash: hashes[3], RevisionID: draftRev, LinkID: draftID, ClientUID: &mine}}, resp.PendingHashes)
|
|
|
|
resp = availableHashesResponse{}
|
|
require.Equal(t, http.StatusOK, postJSON(t, s, path, sessionHeader(sess), &checkHashesRequest{Hashes: hashes, ClientUID: []string{"mine"}}, &resp))
|
|
assert.Equal(t, []string{hashes[0], hashes[2]}, resp.AvailableHashes)
|
|
assert.Empty(t, resp.PendingHashes)
|
|
|
|
var apiErr api.Error
|
|
assert.Equal(t, http.StatusUnprocessableEntity, postJSON(t, s, "/drive/v2/volumes/"+u.VolumeID()+"/links/unknown/checkAvailableHashes", sessionHeader(sess), &checkHashesRequest{}, &apiErr))
|
|
assert.Equal(t, api.CodeNotExists, apiErr.Code)
|
|
}
|
|
|
|
func TestDumpChecksNameHash(t *testing.T) {
|
|
s := New(t)
|
|
u := s.AddUser("user", "pass")
|
|
id := u.SeedFile(u.RootID(), "file", []byte("x"), time.Now())
|
|
require.NoError(t, u.Dump()[0].Err)
|
|
s.mu.Lock()
|
|
u.mustDriveLocked().links[id].nameHash = lookupHash([]byte("other key"), "file")
|
|
s.mu.Unlock()
|
|
err := u.Dump()[0].Err
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "name hash")
|
|
}
|
|
|
|
// TestMutationWireNames checks the backend's request types decode into
|
|
// the fake's without unknown fields.
|
|
func TestMutationWireNames(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
client any
|
|
fake any
|
|
}{
|
|
{&api.CreateFolderRequest{XAttr: "x"}, &createFolderRequest{}},
|
|
{&api.RenameRequest{}, &renameRequest{}},
|
|
{&api.MoveRequest{NodePassphraseSignature: "x", SignatureEmail: "x"}, &moveRequest{}},
|
|
{&api.CopyRequest{NodePassphraseSignature: "x", SignatureEmail: "x"}, ©Request{}},
|
|
{&api.LinksRequest{}, &linksRequest{}},
|
|
{&api.MultiLinkResponse{Responses: []api.LinkResult{{}}}, &multiResponse{}},
|
|
{&api.CreateFolderResponse{}, &createFolderResponse{}},
|
|
{&api.CopyResponse{}, ©Response{}},
|
|
} {
|
|
data, err := json.Marshal(tc.client)
|
|
require.NoError(t, err)
|
|
dec := json.NewDecoder(bytes.NewReader(data))
|
|
dec.DisallowUnknownFields()
|
|
assert.NoError(t, dec.Decode(tc.fake), "%T", tc.client)
|
|
}
|
|
}
|