package protondrive import ( "bytes" "context" "fmt" "io" "testing" "github.com/ProtonMail/gopenpgp/v3/crypto" "github.com/rclone/rclone/backend/protondrive/protontest" "github.com/stretchr/testify/require" ) // newTestClient returns a client with caching enabled for a new user // of a new fake server, signed in without a login round trip. func newTestClient(t testing.TB) (*client, *protontest.Server, *protontest.User) { t.Helper() return newTestClientOpt(t, clientOptions{EnableCaching: true}) } // newTestClientOpt is newTestClient with the options opt. func newTestClientOpt(t testing.TB, opt clientOptions) (*client, *protontest.Server, *protontest.User) { t.Helper() srv := protontest.New(t) u := srv.AddUser("user", "pass") return newTestClientFor(t, srv, u, opt), srv, u } // newTestClientFor returns a client for a new session of u. func newTestClientFor(t testing.TB, srv *protontest.Server, u *protontest.User, opt clientOptions) *client { t.Helper() c, err := newClient(context.Background(), newAccountSession(srv, u), opt) require.NoError(t, err) t.Cleanup(c.close) return c } // decryptBlock returns the encrypted block enc (SEIPDv1 or v2) // decrypted with the content session key sk. func decryptBlock(enc []byte, sk *crypto.SessionKey) ([]byte, error) { r, err := blockDecrypter(bytes.NewReader(enc), sk) if err != nil { return nil, err } plain, err := io.ReadAll(r) if err != nil { return nil, fmt.Errorf("decrypt block: %w", err) } return plain, nil }