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>
503 lines
19 KiB
Go
503 lines
19 KiB
Go
package protontest
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"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"
|
|
)
|
|
|
|
const testClientUID = "fake-test-client"
|
|
|
|
// uploader uploads files to the root of a user's drive as the SDK
|
|
// does, step by step, for tests to spoil one step.
|
|
type uploader struct {
|
|
t *testing.T
|
|
s *Server
|
|
u *User
|
|
sess Session
|
|
volume string
|
|
}
|
|
|
|
func newUploader(t *testing.T) *uploader {
|
|
s := New(t)
|
|
u := s.AddUser("user", "pass")
|
|
return &uploader{t: t, s: s, u: u, sess: s.NewSession(u), volume: u.VolumeID()}
|
|
}
|
|
|
|
// fileDraft is a file being uploaded, with its keys.
|
|
type fileDraft struct {
|
|
req *createFileRequest
|
|
node lockedKey
|
|
ck contentKey
|
|
signer *crypto.Key
|
|
linkID string
|
|
revID string
|
|
code []byte
|
|
content []byte
|
|
}
|
|
|
|
// newFileRequest returns a create file request for name in the root
|
|
// made as the SDK's createDraft does.
|
|
func (x *uploader) newFileRequest(name string) *fileDraft {
|
|
v := x.u.view()
|
|
parentKey, parentHashKey := v.folderKeys(v.d.rootID)
|
|
node := newDriveKey(true)
|
|
x.s.rememberKey(node)
|
|
pass := sealPassphrase(node.passphrase, parentKey, v.addrKey)
|
|
ck := newContentKey(node.key, true)
|
|
uid := testClientUID
|
|
return &fileDraft{
|
|
req: &createFileRequest{
|
|
ParentLinkID: v.d.rootID,
|
|
Name: encryptArmored([]byte(name), parentKey, v.addrKey, false),
|
|
Hash: lookupHash(parentHashKey, name),
|
|
MIMEType: "text/plain",
|
|
ClientUID: &uid,
|
|
NodeKey: node.armored,
|
|
NodePassphrase: pass.armored,
|
|
NodePassphraseSignature: pass.signature,
|
|
ContentKeyPacket: ck.packet,
|
|
ContentKeyPacketSignature: &ck.signature,
|
|
SignatureAddress: &v.email,
|
|
},
|
|
node: node,
|
|
ck: ck,
|
|
signer: v.addrKey,
|
|
}
|
|
}
|
|
|
|
// send sends in with method to path as the user.
|
|
func (x *uploader) send(method, path string, in any) (int, []byte) {
|
|
return sendJSON(x.t, x.s, x.sess, method, path, in)
|
|
}
|
|
|
|
// create creates the draft of f, failing the test if it can't.
|
|
func (x *uploader) create(f *fileDraft) {
|
|
t := x.t
|
|
status, body := x.send("POST", "/drive/v2/volumes/"+x.volume+"/files", f.req)
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
var resp createFileResponse
|
|
require.NoError(t, json.Unmarshal(body, &resp))
|
|
f.linkID, f.revID = resp.File.ID, resp.File.RevisionID
|
|
x.loadCode(f)
|
|
}
|
|
|
|
// loadCode fetches the verification code of f's draft revision.
|
|
func (x *uploader) loadCode(f *fileDraft) {
|
|
var ver verificationResponse
|
|
status, body := getJSON(x.t, x.s, x.sess, "/drive/v2/volumes/"+x.volume+"/links/"+f.linkID+"/revisions/"+f.revID+"/verification", &ver)
|
|
require.Equal(x.t, http.StatusOK, status, "body: %s", body)
|
|
code, err := base64.StdEncoding.DecodeString(ver.VerificationCode)
|
|
require.NoError(x.t, err)
|
|
require.Len(x.t, code, verificationCodeSize)
|
|
assert.Equal(x.t, f.ck.packet, ver.ContentKeyPacket)
|
|
f.code = code
|
|
}
|
|
|
|
// testBlock is an encrypted block with its upload request entry.
|
|
type testBlock struct {
|
|
data []byte
|
|
in blockUploadInput
|
|
}
|
|
|
|
// encrypt encrypts content as f's blocks of blockSize.
|
|
func (f *fileDraft) encrypt(content []byte) []testBlock {
|
|
f.content = content
|
|
var blocks []testBlock
|
|
for i := 0; i < len(content); i += blockSize {
|
|
data, encSig := f.ck.encryptBlock(content[i:min(i+blockSize, len(content))], f.node.key, f.signer)
|
|
blocks = append(blocks, testBlock{data: data, in: blockUploadInput{
|
|
Index: len(blocks) + 1,
|
|
Verifier: &verifierJSON{Token: base64.StdEncoding.EncodeToString(xorPrefix(f.code, data))},
|
|
EncSignature: &encSig,
|
|
}})
|
|
}
|
|
return blocks
|
|
}
|
|
|
|
// requestURLs requests upload URLs for blocks of f.
|
|
func (x *uploader) requestURLs(f *fileDraft, blocks []testBlock) (int, []byte, []blockURLJSON) {
|
|
addressID := x.u.addresses[0].id
|
|
req := &blockUploadRequest{LinkID: f.linkID, RevisionID: f.revID, AddressID: &addressID, VolumeID: &x.volume}
|
|
for _, b := range blocks {
|
|
req.BlockList = append(req.BlockList, b.in)
|
|
}
|
|
status, body := x.send("POST", "/drive/blocks", req)
|
|
var resp blockUploadResponse
|
|
if status == http.StatusOK {
|
|
require.NoError(x.t, json.Unmarshal(body, &resp))
|
|
}
|
|
return status, body, resp.UploadLinks
|
|
}
|
|
|
|
// putBlock uploads data to link as the SDK does, with extra headers,
|
|
// and returns the HTTP status.
|
|
func (x *uploader) putBlock(link blockURLJSON, data []byte, header map[string]string) int {
|
|
t := x.t
|
|
var body bytes.Buffer
|
|
mw := multipart.NewWriter(&body)
|
|
w, err := mw.CreateFormFile("Block", "blob")
|
|
require.NoError(t, err)
|
|
_, err = w.Write(data)
|
|
require.NoError(t, err)
|
|
require.NoError(t, mw.Close())
|
|
req, err := http.NewRequest("POST", link.BareURL, &body)
|
|
require.NoError(t, err)
|
|
req.Header.Set("Content-Type", mw.FormDataContentType())
|
|
req.Header.Set("x-pm-appversion", testAppVersion)
|
|
req.Header.Set("pm-storage-token", link.Token)
|
|
for k, v := range header {
|
|
req.Header.Set(k, v)
|
|
}
|
|
resp, err := x.s.Client().Do(req)
|
|
require.NoError(t, err)
|
|
_, _ = io.Copy(io.Discard, resp.Body)
|
|
_ = resp.Body.Close()
|
|
return resp.StatusCode
|
|
}
|
|
|
|
// uploadAll requests URLs for blocks and uploads each.
|
|
func (x *uploader) uploadAll(f *fileDraft, blocks []testBlock) {
|
|
status, body, links := x.requestURLs(f, blocks)
|
|
require.Equal(x.t, http.StatusOK, status, "body: %s", body)
|
|
require.Len(x.t, links, len(blocks))
|
|
for i, link := range links {
|
|
require.Equal(x.t, blocks[i].in.Index, link.Index)
|
|
require.Equal(x.t, http.StatusOK, x.putBlock(link, blocks[i].data, nil))
|
|
}
|
|
}
|
|
|
|
// commitRequest returns the commit request of f with blocks, as the SDK
|
|
// makes it.
|
|
func (f *fileDraft) commitRequest(blocks []testBlock) *commitRevisionRequest {
|
|
var manifest []byte
|
|
var sizes []int64
|
|
for i, b := range blocks {
|
|
sum := sha256.Sum256(b.data)
|
|
manifest = append(manifest, sum[:]...)
|
|
sizes = append(sizes, int64(min(blockSize, len(f.content)-i*blockSize)))
|
|
}
|
|
xattr := encryptArmored(newFileXAttr(f.content, sizes, "2024-01-02T03:04:05.000Z"), f.node.key, f.signer, true)
|
|
return &commitRevisionRequest{
|
|
ManifestSignature: signArmored(manifest, f.signer),
|
|
SignatureAddress: f.req.SignatureAddress,
|
|
XAttr: &xattr,
|
|
}
|
|
}
|
|
|
|
// commit commits f's draft revision with req.
|
|
func (x *uploader) commit(f *fileDraft, req *commitRevisionRequest) (int, []byte) {
|
|
return x.send("PUT", "/drive/v2/volumes/"+x.volume+"/files/"+f.linkID+"/revisions/"+f.revID, req)
|
|
}
|
|
|
|
func TestCreateFileFormats(t *testing.T) {
|
|
x := newUploader(t)
|
|
post := func(f *fileDraft) (int, []byte) {
|
|
return x.send("POST", "/drive/v2/volumes/"+x.volume+"/files", f.req)
|
|
}
|
|
for variant, bad := range aeadMessages(t) {
|
|
for field, spoil := range map[string]func(req *createFileRequest){
|
|
"Name": func(req *createFileRequest) { req.Name = bad },
|
|
"NodePassphrase": func(req *createFileRequest) { req.NodePassphrase = bad },
|
|
} {
|
|
t.Run(field+" "+variant, func(t *testing.T) {
|
|
f := x.newFileRequest("f")
|
|
spoil(f.req)
|
|
status, body := post(f)
|
|
requireRejected(t, api.CodeInvalidValue, status, body, field)
|
|
})
|
|
}
|
|
}
|
|
|
|
aeadLocked := profile.Default()
|
|
aeadLocked.AeadKeyEncryption = &packet.AEADConfig{}
|
|
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)
|
|
for name, tc := range map[string]struct {
|
|
key string
|
|
want string
|
|
}{
|
|
"v6": {v6Armored, "not a v4 key"},
|
|
"AEAD locked": {lockedWith(t, aeadLocked), "locked with AEAD"},
|
|
"salted S2K": {lockedWith(t, salted), "S2K mode 1"},
|
|
} {
|
|
t.Run("NodeKey "+name, func(t *testing.T) {
|
|
f := x.newFileRequest("f")
|
|
f.req.NodeKey = tc.key
|
|
status, body := post(f)
|
|
requireRejected(t, api.CodeInvalidValue, status, body, "NodeKey", tc.want)
|
|
})
|
|
}
|
|
|
|
legacy := newContentKey(newDriveKey(false).key, false)
|
|
unrounded := int64(123_456)
|
|
for name, tc := range map[string]struct {
|
|
spoil func(req *createFileRequest)
|
|
want string
|
|
}{
|
|
"v3 content key packet": {func(req *createFileRequest) { req.ContentKeyPacket = legacy.packet }, "v6 PKESK"},
|
|
"no content key sig": {func(req *createFileRequest) { req.ContentKeyPacketSignature = nil }, "ContentKeyPacketSignature"},
|
|
"no passphrase sig": {func(req *createFileRequest) { req.NodePassphraseSignature = "" }, "NodePassphraseSignature"},
|
|
"unrounded size": {func(req *createFileRequest) { req.IntendedUploadSize = &unrounded }, "IntendedUploadSize"},
|
|
"no signer": {func(req *createFileRequest) { req.SignatureAddress = nil }, "SignatureAddress"},
|
|
"no media type": {func(req *createFileRequest) { req.MIMEType = "" }, "MIMEType"},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
f := x.newFileRequest("f")
|
|
tc.spoil(f.req)
|
|
status, body := post(f)
|
|
requireRejected(t, api.CodeInvalidValue, status, body, tc.want)
|
|
})
|
|
}
|
|
assert.Empty(t, x.u.Drafts())
|
|
|
|
// The name hash must be unique, and a conflict names the draft and
|
|
// its creator.
|
|
f := x.newFileRequest("f")
|
|
rounded := int64(200_000)
|
|
f.req.IntendedUploadSize = &rounded
|
|
x.create(f)
|
|
assert.Equal(t, []string{f.linkID}, x.u.Drafts())
|
|
status, body := post(x.newFileRequest("f"))
|
|
requireRejected(t, api.CodeAlreadyExists, status, body)
|
|
var conflict conflictResponse
|
|
require.NoError(t, json.Unmarshal(body, &conflict))
|
|
assert.Equal(t, f.linkID, conflict.Details.ConflictLinkID)
|
|
assert.Nil(t, conflict.Details.ConflictRevisionID)
|
|
require.NotNil(t, conflict.Details.ConflictDraftRevisionID)
|
|
assert.Equal(t, f.revID, *conflict.Details.ConflictDraftRevisionID)
|
|
require.NotNil(t, conflict.Details.ConflictDraftClientUID)
|
|
assert.Equal(t, testClientUID, *conflict.Details.ConflictDraftClientUID)
|
|
assert.Empty(t, x.u.Dump(), "a draft is dumped")
|
|
}
|
|
|
|
func TestCommitChecks(t *testing.T) {
|
|
x := newUploader(t)
|
|
content := patterned(blockSize + 1000)
|
|
n := 0
|
|
// prepare returns a new draft with content encrypted, its blocks
|
|
// and their commit request.
|
|
prepare := func() (*fileDraft, []testBlock, *commitRevisionRequest) {
|
|
n++
|
|
f := x.newFileRequest("f" + strconv.Itoa(n))
|
|
x.create(f)
|
|
blocks := f.encrypt(content)
|
|
return f, blocks, f.commitRequest(blocks)
|
|
}
|
|
impostor := generateKey("impostor", "impostor@proton.test")
|
|
empty := ""
|
|
aead := aeadMessages(t)["v6 PKESK and SEIPDv2"]
|
|
legacy := newContentKey(newDriveKey(false).key, false)
|
|
otherHash := base64.StdEncoding.EncodeToString(make([]byte, sha256.Size))
|
|
for name, tc := range map[string]struct {
|
|
spoil func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock
|
|
code int
|
|
want string
|
|
}{
|
|
"AEAD XAttr": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
|
|
req.XAttr = &aead
|
|
return blocks
|
|
}, api.CodeInvalidValue, "XAttr"},
|
|
"empty XAttr": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
|
|
req.XAttr = &empty
|
|
return blocks
|
|
}, api.CodeInvalidValue, "XAttr"},
|
|
"no XAttr": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
|
|
req.XAttr = nil
|
|
return blocks
|
|
}, api.CodeInvalidValue, "XAttr"},
|
|
"verifier": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
|
|
token := xorPrefix(f.code, blocks[1].data)
|
|
token[5] ^= 1
|
|
blocks[1].in.Verifier.Token = base64.StdEncoding.EncodeToString(token)
|
|
return blocks
|
|
}, api.CodeInvalidRequirements, "block 2: verifier token"},
|
|
"block not uploaded": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
|
|
_, _, links := x.requestURLs(f, blocks[1:])
|
|
require.Len(t, links, 1)
|
|
return blocks[:1]
|
|
}, api.CodeInvalidRequirements, "block 2 was not uploaded"},
|
|
"index gap": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
|
|
blocks[1].in.Index = 3
|
|
return blocks
|
|
}, api.CodeInvalidRequirements, "block 2 was not requested"},
|
|
"declared hash": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
|
|
blocks[0].in.Hash = &otherHash
|
|
return blocks
|
|
}, api.CodeInvalidRequirements, "declared hash"},
|
|
"manifest": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
|
|
req.ManifestSignature = signArmored([]byte("not the manifest"), f.signer)
|
|
return blocks
|
|
}, api.CodeInvalidValue, "manifest"},
|
|
"manifest signer": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
|
|
var manifest []byte
|
|
for _, b := range blocks {
|
|
sum := sha256.Sum256(b.data)
|
|
manifest = append(manifest, sum[:]...)
|
|
}
|
|
req.ManifestSignature = signArmored(manifest, impostor)
|
|
return blocks
|
|
}, api.CodeInvalidValue, "manifest"},
|
|
"SEIPDv1 block": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
|
|
data, _ := legacy.encryptBlock(content[blockSize:], f.node.key, f.signer)
|
|
blocks[1].data = data
|
|
blocks[1].in.Verifier.Token = base64.StdEncoding.EncodeToString(xorPrefix(f.code, data))
|
|
*req = *f.commitRequest(blocks)
|
|
return blocks
|
|
}, api.CodeInvalidValue, "block 2: packet with tag 18 version 1, want SEIPDv2"},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
f, blocks, req := prepare()
|
|
blocks = tc.spoil(f, blocks, req)
|
|
x.uploadAll(f, blocks)
|
|
status, body := x.commit(f, req)
|
|
requireRejected(t, tc.code, status, body, tc.want)
|
|
assert.Contains(t, x.u.Drafts(), f.linkID)
|
|
})
|
|
}
|
|
assert.Empty(t, x.u.Dump(), "a draft was committed")
|
|
|
|
f, blocks, req := prepare()
|
|
x.uploadAll(f, blocks)
|
|
status, body := x.commit(f, req)
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
dump := x.u.Dump()
|
|
require.Len(t, dump, 1)
|
|
assert.NoError(t, dump[0].Err)
|
|
assert.Equal(t, f.linkID, dump[0].ID)
|
|
assert.True(t, bytes.Equal(content, dump[0].Content))
|
|
assert.NotContains(t, x.u.Drafts(), f.linkID)
|
|
|
|
// A committed revision can't be committed again or deleted as a
|
|
// draft.
|
|
status, body = x.commit(f, req)
|
|
requireRejected(t, api.CodeNotExists, status, body)
|
|
status, body = x.send("POST", "/drive/v2/volumes/"+x.volume+"/delete_multiple", &linksRequest{LinkIDs: []string{f.linkID}})
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
var multi multiResponse
|
|
require.NoError(t, json.Unmarshal(body, &multi))
|
|
require.Len(t, multi.Responses, 1)
|
|
assert.Equal(t, api.CodeInvalidRequirements, multi.Responses[0].Response.Code)
|
|
status, body = x.send("DELETE", "/drive/v2/volumes/"+x.volume+"/files/"+f.linkID+"/revisions/"+f.revID, nil)
|
|
requireRejected(t, api.CodeNotExists, status, body)
|
|
require.Len(t, x.u.Dump(), 1)
|
|
}
|
|
|
|
func TestStorageUploadRules(t *testing.T) {
|
|
x := newUploader(t)
|
|
f := x.newFileRequest("f")
|
|
x.create(f)
|
|
blocks := f.encrypt([]byte("content"))
|
|
status, body, links := x.requestURLs(f, blocks)
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
require.Len(t, links, 1)
|
|
link := links[0]
|
|
|
|
assert.Equal(t, http.StatusBadRequest, x.putBlock(link, blocks[0].data, sessionHeader(x.sess)), "session credentials accepted")
|
|
wrong := link
|
|
wrong.Token = "wrong"
|
|
assert.Equal(t, http.StatusForbidden, x.putBlock(wrong, blocks[0].data, nil))
|
|
x.s.ExpireUploadTokens()
|
|
assert.Equal(t, http.StatusNotFound, x.putBlock(link, blocks[0].data, nil))
|
|
|
|
// A block requested again gets a URL which works.
|
|
_, _, links = x.requestURLs(f, blocks)
|
|
require.Len(t, links, 1)
|
|
require.Equal(t, http.StatusOK, x.putBlock(links[0], blocks[0].data, nil))
|
|
status, body = x.commit(f, f.commitRequest(blocks))
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
dump := x.u.Dump()
|
|
require.Len(t, dump, 1)
|
|
require.NoError(t, dump[0].Err)
|
|
|
|
// Uploads to a committed revision are refused.
|
|
assert.Equal(t, http.StatusNotFound, x.putBlock(links[0], blocks[0].data, nil))
|
|
|
|
// Another user's AddressID is refused.
|
|
other := x.s.AddUser("other", "pass")
|
|
g := x.newFileRequest("g")
|
|
x.create(g)
|
|
addressID := other.addresses[0].id
|
|
status, body = x.send("POST", "/drive/blocks", &blockUploadRequest{LinkID: g.linkID, RevisionID: g.revID, AddressID: &addressID, VolumeID: &x.volume, BlockList: []blockUploadInput{g.encrypt([]byte("x"))[0].in}})
|
|
requireRejected(t, api.CodeInvalidValue, status, body, "AddressID")
|
|
}
|
|
|
|
func TestRevisionRules(t *testing.T) {
|
|
x := newUploader(t)
|
|
id := x.u.SeedFile(x.u.RootID(), "f", []byte("old"), time.Now())
|
|
current := x.u.view().d.links[id].activeRevision().id
|
|
path := "/drive/v2/volumes/" + x.volume + "/files/" + id + "/revisions"
|
|
stale := "stale"
|
|
status, body := x.send("POST", path, &createRevisionRequest{CurrentRevisionID: &stale})
|
|
requireRejected(t, api.CodeInvalidRequirements, status, body, "CurrentRevisionID")
|
|
status, body = x.send("POST", path, &createRevisionRequest{})
|
|
requireRejected(t, api.CodeInvalidRequirements, status, body, "CurrentRevisionID")
|
|
|
|
uid := testClientUID
|
|
status, body = x.send("POST", path, &createRevisionRequest{CurrentRevisionID: ¤t, ClientUID: &uid})
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
var resp createRevisionResponse
|
|
require.NoError(t, json.Unmarshal(body, &resp))
|
|
assert.Equal(t, []string{resp.Revision.ID}, x.u.Drafts())
|
|
|
|
// The name conflict of a file with a draft revision names both.
|
|
status, body = x.send("POST", "/drive/v2/volumes/"+x.volume+"/files", x.newFileRequest("f").req)
|
|
requireRejected(t, api.CodeAlreadyExists, status, body)
|
|
var conflict conflictResponse
|
|
require.NoError(t, json.Unmarshal(body, &conflict))
|
|
require.NotNil(t, conflict.Details.ConflictRevisionID)
|
|
assert.Equal(t, current, *conflict.Details.ConflictRevisionID)
|
|
require.NotNil(t, conflict.Details.ConflictDraftRevisionID)
|
|
assert.Equal(t, resp.Revision.ID, *conflict.Details.ConflictDraftRevisionID)
|
|
|
|
status, body = x.send("DELETE", path+"/"+resp.Revision.ID, nil)
|
|
require.Equal(t, http.StatusOK, status, "body: %s", body)
|
|
assert.Empty(t, x.u.Drafts())
|
|
status, body = x.send("DELETE", path+"/"+current, nil)
|
|
requireRejected(t, api.CodeNotExists, status, body)
|
|
dump := x.u.Dump()
|
|
require.Len(t, dump, 1)
|
|
assert.Equal(t, []byte("old"), dump[0].Content)
|
|
}
|
|
|
|
// TestUploadWireNames checks the backend's upload request types decode
|
|
// into the fake's without unknown fields.
|
|
func TestUploadWireNames(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
client any
|
|
fake any
|
|
}{
|
|
{&api.CreateFileRequest{}, &createFileRequest{}},
|
|
{&api.CreateRevisionRequest{}, &createRevisionRequest{}},
|
|
{&api.BlockUploadRequest{BlockList: []api.BlockUploadInfo{{}}, ThumbnailList: []struct{}{}}, &blockUploadRequest{}},
|
|
{&api.CommitRevisionRequest{}, &commitRevisionRequest{}},
|
|
} {
|
|
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)
|
|
}
|
|
}
|