Files
rclone/backend/protondrive/protontest/events_test.go
StereotypicalCatandClaude Opus 5.5 c0bdbcde6b
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
protondrive: rewrite the backend as a native Go client of the Proton Drive API
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>
2026-11-23 23:22:46 +01:00

424 lines
16 KiB
Go

package protontest
import (
"encoding/json"
"net/http"
"testing"
"time"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// latestEvent returns the start cursor of u's volume as sess.
func latestEvent(t *testing.T, s *Server, sess Session, u *User) string {
t.Helper()
var resp latestEventResponse
status, body := getJSON(t, s, sess, "/drive/volumes/"+u.VolumeID()+"/events/latest", &resp)
require.Equal(t, http.StatusOK, status, "body: %s", body)
require.Equal(t, api.CodeOK, resp.Code)
require.NotEmpty(t, resp.EventID)
return resp.EventID
}
// eventsPage returns the page of u's volume events after cursor.
func eventsPage(t *testing.T, s *Server, sess Session, u *User, cursor string) eventsResponse {
t.Helper()
var resp eventsResponse
status, body := getJSON(t, s, sess, "/drive/v2/volumes/"+u.VolumeID()+"/events/"+cursor, &resp)
require.Equal(t, http.StatusOK, status, "body: %s", body)
require.Equal(t, api.CodeOK, resp.Code)
return resp
}
// eventsSince returns every event of u's volume after cursor, following
// More.
func eventsSince(t *testing.T, s *Server, sess Session, u *User, cursor string) []eventJSON {
t.Helper()
var out []eventJSON
for {
resp := eventsPage(t, s, sess, u, cursor)
require.False(t, resp.Refresh)
out = append(out, resp.Events...)
cursor = resp.EventID
if !resp.More {
return out
}
}
}
func TestEventsLatestAndPage(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
start := latestEvent(t, s, sess, u)
resp := eventsPage(t, s, sess, u, start)
assert.Empty(t, resp.Events)
assert.Equal(t, start, resp.EventID)
assert.False(t, resp.More)
assert.False(t, resp.Refresh)
status, body := sendJSON(t, s, sess, "POST", "/drive/v2/volumes/"+u.VolumeID()+"/folders", newFolderRequest(u, u.RootID(), "dir"))
require.Equal(t, http.StatusOK, status, "body: %s", body)
var created createFolderResponse
require.NoError(t, json.Unmarshal(body, &created))
resp = eventsPage(t, s, sess, u, start)
require.Len(t, resp.Events, 1)
e := resp.Events[0]
assert.Equal(t, EventCreate, e.EventType)
assert.Equal(t, created.Folder.ID, e.Link.LinkID)
require.NotNil(t, e.Link.ParentLinkID)
assert.Equal(t, u.RootID(), *e.Link.ParentLinkID)
assert.False(t, e.Link.IsShared)
assert.False(t, e.Link.IsTrashed)
assert.NotEqual(t, start, e.EventID)
assert.Equal(t, e.EventID, resp.EventID)
assert.False(t, resp.More)
assert.False(t, resp.Refresh)
assert.Equal(t, e.EventID, latestEvent(t, s, sess, u))
resp = eventsPage(t, s, sess, u, e.EventID)
assert.Empty(t, resp.Events)
assert.Equal(t, e.EventID, resp.EventID)
// Field names are Proton's.
var raw map[string]any
_, body = getJSON(t, s, sess, "/drive/v2/volumes/"+u.VolumeID()+"/events/"+start, &raw)
assert.Subset(t, keysOf(t, raw), []string{"Code", "Events", "EventID", "More", "Refresh"})
events, ok := raw["Events"].([]any)
require.True(t, ok, "body: %s", body)
require.Len(t, events, 1)
assert.ElementsMatch(t, []string{"EventID", "EventType", "Link"}, keysOf(t, events[0]))
assert.ElementsMatch(t, []string{"LinkID", "ParentLinkID", "IsShared", "IsTrashed"}, keysOf(t, events[0].(map[string]any)["Link"]))
}
func TestEventsPerMutation(t *testing.T) {
type event struct {
Type int
LinkID string
ParentID string
IsTrashed bool
}
for _, tc := range []struct {
name string
// do makes a change and returns the events it expects.
do func(t *testing.T, x *renameMoveCopy) []event
}{
{"create folder", func(t *testing.T, x *renameMoveCopy) []event {
status, body := sendJSON(t, x.s, x.sess, "POST", "/drive/v2/volumes/"+x.volume+"/folders", newFolderRequest(x.u, x.a, "new"))
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp createFolderResponse
require.NoError(t, json.Unmarshal(body, &resp))
return []event{{EventCreate, resp.Folder.ID, x.a, false}}
}},
{"upload commit", func(t *testing.T, x *renameMoveCopy) []event {
up := &uploader{t: t, s: x.s, u: x.u, sess: x.sess, volume: x.volume}
f := up.newFileRequest("up")
up.create(f)
blocks := f.encrypt([]byte("content"))
up.uploadAll(f, blocks)
status, body := up.commit(f, f.commitRequest(blocks))
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{{EventCreate, f.linkID, x.u.RootID(), false}}
}},
{"new revision", func(t *testing.T, x *renameMoveCopy) []event {
up := &uploader{t: t, s: x.s, u: x.u, sess: x.sess, volume: x.volume}
f := up.newFileRequest("up")
up.create(f)
blocks := f.encrypt([]byte("one"))
up.uploadAll(f, blocks)
status, body := up.commit(f, f.commitRequest(blocks))
require.Equal(t, http.StatusOK, status, "body: %s", body)
current := f.revID
uid := testClientUID
status, body = up.send("POST", "/drive/v2/volumes/"+x.volume+"/files/"+f.linkID+"/revisions", &createRevisionRequest{CurrentRevisionID: &current, ClientUID: &uid})
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp createRevisionResponse
require.NoError(t, json.Unmarshal(body, &resp))
f.revID = resp.Revision.ID
up.loadCode(f)
blocks = f.encrypt([]byte("two"))
up.uploadAll(f, blocks)
status, body = up.commit(f, f.commitRequest(blocks))
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{
{EventCreate, f.linkID, x.u.RootID(), false},
{EventUpdate, f.linkID, x.u.RootID(), false},
}
}},
{"rename", func(t *testing.T, x *renameMoveCopy) []event {
email, hash, original := x.u.Email(), lookupHash([]byte("k"), "new"), x.hash(x.f)
status, body := sendJSON(t, x.s, x.sess, "PUT", "/drive/v2/volumes/"+x.volume+"/links/"+x.f+"/rename",
&renameRequest{Name: x.validMsg, Hash: &hash, NameSignatureEmail: &email, OriginalHash: &original})
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{{EventUpdateMetadata, x.f, x.a, false}}
}},
{"move", func(t *testing.T, x *renameMoveCopy) []event {
email := x.u.Email()
status, body := sendJSON(t, x.s, x.sess, "PUT", "/drive/v2/volumes/"+x.volume+"/links/"+x.f+"/move", &moveRequest{
Name: x.validMsg, NodePassphrase: x.validMsg, Hash: lookupHash([]byte("k"), "new"),
ParentLinkID: x.b, OriginalHash: x.hash(x.f), NameSignatureEmail: &email,
})
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{{EventUpdateMetadata, x.f, x.b, false}}
}},
{"copy", func(t *testing.T, x *renameMoveCopy) []event {
email := x.u.Email()
status, body := sendJSON(t, x.s, x.sess, "POST", "/drive/volumes/"+x.volume+"/links/"+x.f+"/copy", &copyRequest{
Name: x.validMsg, NodePassphrase: x.validMsg, Hash: lookupHash([]byte("k"), "copy"),
TargetVolumeID: x.volume, TargetParentLinkID: x.b, NameSignatureEmail: &email,
})
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp copyResponse
require.NoError(t, json.Unmarshal(body, &resp))
return []event{{EventCreate, resp.LinkID, x.b, false}}
}},
{"trash", func(t *testing.T, x *renameMoveCopy) []event {
var resp multiResponse
status := postJSON(t, x.s, "/drive/v2/volumes/"+x.volume+"/trash_multiple", sessionHeader(x.sess), &linksRequest{LinkIDs: []string{x.f}}, &resp)
require.Equal(t, http.StatusOK, status)
return []event{{EventUpdateMetadata, x.f, x.a, true}}
}},
{"create then trash", func(t *testing.T, x *renameMoveCopy) []event {
status, body := sendJSON(t, x.s, x.sess, "POST", "/drive/v2/volumes/"+x.volume+"/folders", newFolderRequest(x.u, x.a, "new"))
require.Equal(t, http.StatusOK, status, "body: %s", body)
var created createFolderResponse
require.NoError(t, json.Unmarshal(body, &created))
var resp multiResponse
status = postJSON(t, x.s, "/drive/v2/volumes/"+x.volume+"/trash_multiple", sessionHeader(x.sess), &linksRequest{LinkIDs: []string{created.Folder.ID}}, &resp)
require.Equal(t, http.StatusOK, status)
// Both report the link as it is when read.
return []event{{EventCreate, created.Folder.ID, x.a, true}, {EventUpdateMetadata, created.Folder.ID, x.a, true}}
}},
{"empty trash", func(t *testing.T, x *renameMoveCopy) []event {
var resp multiResponse
status := postJSON(t, x.s, "/drive/v2/volumes/"+x.volume+"/trash_multiple", sessionHeader(x.sess), &linksRequest{LinkIDs: []string{x.f}}, &resp)
require.Equal(t, http.StatusOK, status)
status, body := sendJSON(t, x.s, x.sess, "DELETE", "/drive/volumes/"+x.volume+"/trash", nil)
require.Equal(t, http.StatusOK, status, "body: %s", body)
// A link deleted for good has no parent and isn't trashed.
return []event{{EventUpdateMetadata, x.f, "", false}, {EventDelete, x.f, "", false}}
}},
{"empty trash of a folder", func(t *testing.T, x *renameMoveCopy) []event {
var resp multiResponse
status := postJSON(t, x.s, "/drive/v2/volumes/"+x.volume+"/trash_multiple", sessionHeader(x.sess), &linksRequest{LinkIDs: []string{x.a}}, &resp)
require.Equal(t, http.StatusOK, status)
status, body := sendJSON(t, x.s, x.sess, "DELETE", "/drive/volumes/"+x.volume+"/trash", nil)
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{
{EventUpdateMetadata, x.a, "", false},
{EventDelete, x.a, "", false},
{EventDelete, x.f, "", false},
{EventDelete, x.anon, "", false},
}
}},
{"delete draft", func(t *testing.T, x *renameMoveCopy) []event {
up := &uploader{t: t, s: x.s, u: x.u, sess: x.sess, volume: x.volume}
f := up.newFileRequest("draft")
up.create(f)
var resp multiResponse
status := postJSON(t, x.s, "/drive/v2/volumes/"+x.volume+"/delete_multiple", sessionHeader(x.sess), &linksRequest{LinkIDs: []string{f.linkID}}, &resp)
require.Equal(t, http.StatusOK, status)
require.Equal(t, api.CodeOK, resp.Responses[0].Response.Code)
return nil
}},
{"delete draft revision", func(t *testing.T, x *renameMoveCopy) []event {
up := &uploader{t: t, s: x.s, u: x.u, sess: x.sess, volume: x.volume}
f := up.newFileRequest("up")
up.create(f)
blocks := f.encrypt([]byte("one"))
up.uploadAll(f, blocks)
status, body := up.commit(f, f.commitRequest(blocks))
require.Equal(t, http.StatusOK, status, "body: %s", body)
current := f.revID
uid := testClientUID
path := "/drive/v2/volumes/" + x.volume + "/files/" + f.linkID + "/revisions"
status, body = up.send("POST", path, &createRevisionRequest{CurrentRevisionID: &current, ClientUID: &uid})
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp createRevisionResponse
require.NoError(t, json.Unmarshal(body, &resp))
status, body = up.send("DELETE", path+"/"+resp.Revision.ID, nil)
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{{EventCreate, f.linkID, x.u.RootID(), false}}
}},
} {
t.Run(tc.name, func(t *testing.T) {
x := newRenameMoveCopy(t)
start := latestEvent(t, x.s, x.sess, x.u)
want := tc.do(t, x)
var got []event
for _, e := range eventsSince(t, x.s, x.sess, x.u, start) {
parent := ""
if e.Link.ParentLinkID != nil {
parent = *e.Link.ParentLinkID
}
got = append(got, event{e.EventType, e.Link.LinkID, parent, e.Link.IsTrashed})
}
assert.Equal(t, want, got)
})
}
}
func TestEventsCopyFolder(t *testing.T) {
x := newRenameMoveCopy(t)
start := latestEvent(t, x.s, x.sess, x.u)
email := x.u.Email()
status, body := sendJSON(t, x.s, x.sess, "POST", "/drive/volumes/"+x.volume+"/links/"+x.a+"/copy", &copyRequest{
Name: x.validMsg, NodePassphrase: x.validMsg, Hash: lookupHash([]byte("k"), "copy"),
TargetVolumeID: x.volume, TargetParentLinkID: x.b, NameSignatureEmail: &email,
})
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp copyResponse
require.NoError(t, json.Unmarshal(body, &resp))
// Every link the copy makes is created.
events := eventsSince(t, x.s, x.sess, x.u, start)
require.Len(t, events, 3)
parents := map[string]string{}
for _, e := range events {
assert.Equal(t, EventCreate, e.EventType)
parents[e.Link.LinkID] = *e.Link.ParentLinkID
}
assert.Equal(t, x.b, parents[resp.LinkID])
for id, parent := range parents {
if id != resp.LinkID {
assert.Equal(t, resp.LinkID, parent)
}
}
}
func TestEventsPaging(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
s.SetEventsPageSize(2)
cursor := latestEvent(t, s, sess, u)
var want []string
for _, name := range []string{"a", "b", "c", "d", "e"} {
want = append(want, u.SeedFolder(u.RootID(), name))
}
var got []string
seen := map[string]bool{cursor: true}
for i, wantMore := range []bool{true, true, false} {
resp := eventsPage(t, s, sess, u, cursor)
require.False(t, resp.Refresh)
assert.Equal(t, wantMore, resp.More, "call %d", i)
require.Len(t, resp.Events, min(2, len(want)-len(got)), "call %d", i)
for _, e := range resp.Events {
assert.False(t, seen[e.EventID], "duplicate event ID %s", e.EventID)
seen[e.EventID] = true
got = append(got, e.Link.LinkID)
}
assert.Equal(t, resp.Events[len(resp.Events)-1].EventID, resp.EventID)
cursor = resp.EventID
}
assert.Equal(t, want, got)
assert.Equal(t, cursor, latestEvent(t, s, sess, u))
}
// TestEventsPageSizeDefault checks a page size of 0 or less restores
// the default rather than serving empty pages with More set.
func TestEventsPageSizeDefault(t *testing.T) {
for _, n := range []int{0, -1} {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
s.SetEventsPageSize(n)
cursor := latestEvent(t, s, sess, u)
id := u.SeedFolder(u.RootID(), "a")
resp := eventsPage(t, s, sess, u, cursor)
assert.False(t, resp.More, "page size %d", n)
require.Len(t, resp.Events, 1, "page size %d", n)
assert.Equal(t, id, resp.Events[0].Link.LinkID)
}
}
func TestEventsRefresh(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
old := latestEvent(t, s, sess, u)
u.SeedFolder(u.RootID(), "a")
s.ExpireEvents(u)
newest := latestEvent(t, s, sess, u)
assert.NotEqual(t, old, newest)
resp := eventsPage(t, s, sess, u, old)
assert.True(t, resp.Refresh)
assert.Equal(t, newest, resp.EventID)
assert.Empty(t, resp.Events)
resp = eventsPage(t, s, sess, u, "unknown")
assert.True(t, resp.Refresh)
assert.Equal(t, newest, resp.EventID)
// The newest cursor still works, and so do the ones after it.
resp = eventsPage(t, s, sess, u, newest)
assert.False(t, resp.Refresh)
assert.Empty(t, resp.Events)
id := u.SeedFolder(u.RootID(), "b")
events := eventsSince(t, s, sess, u, newest)
require.Len(t, events, 1)
assert.Equal(t, id, events[0].Link.LinkID)
}
func TestEventsVolumeGone(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
cursor := latestEvent(t, s, sess, u)
for _, path := range []string{
"/drive/volumes/unknown/events/latest",
"/drive/v2/volumes/unknown/events/" + cursor,
} {
status, body := getJSON(t, s, sess, path, nil)
requireRejected(t, api.CodeNotExists, status, body)
}
}
func TestEventsSeedAppends(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
assert.Empty(t, s.Events(u))
dir := u.SeedFolder(u.RootID(), "dir")
id := u.SeedFile(u.RootID(), "f", []byte("f"), time.Now())
assert.Equal(t, []Event{
{Type: EventCreate, LinkID: dir, ParentLinkID: u.RootID()},
{Type: EventCreate, LinkID: id, ParentLinkID: u.RootID()},
}, s.Events(u))
u.SeedRevision(id, []byte("g"), time.Now())
u.ClaimSize(id, 10)
u.Corrupt(id, "name")
u.SetParent(id, dir)
// Every event reports the link as it is now.
assert.Equal(t, []Event{
{Type: EventCreate, LinkID: id, ParentLinkID: dir},
{Type: EventUpdate, LinkID: id, ParentLinkID: dir},
{Type: EventUpdate, LinkID: id, ParentLinkID: dir},
{Type: EventUpdate, LinkID: id, ParentLinkID: dir},
{Type: EventUpdateMetadata, LinkID: id, ParentLinkID: dir},
}, s.Events(u)[1:])
u.ForgeRootSigner(u, u.Email())
assert.Equal(t, Event{Type: EventUpdate, LinkID: u.RootID()}, s.Events(u)[6])
// An injected event is served as given, not with its link's state.
sess := s.NewSession(u)
cursor := latestEvent(t, s, sess, u)
s.InjectEvent(u, Event{Type: EventUpdateMetadata, LinkID: dir, IsShared: true, IsTrashed: true})
events := eventsSince(t, s, sess, u, cursor)
require.Len(t, events, 1)
assert.Equal(t, EventUpdateMetadata, events[0].EventType)
assert.Equal(t, dir, events[0].Link.LinkID)
assert.Nil(t, events[0].Link.ParentLinkID)
assert.True(t, events[0].Link.IsShared)
assert.True(t, events[0].Link.IsTrashed)
assert.Equal(t, Event{Type: EventUpdateMetadata, LinkID: dir, IsShared: true, IsTrashed: true}, s.Events(u)[len(s.Events(u))-1])
}