E2E-encrypt todo sync
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run

Todo: fda71ed1

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-11-12 08:39:05 +01:00
parent c9eb4d6cff
commit f4913daaa8
31 changed files with 389 additions and 217 deletions

View File

@ -46,7 +46,16 @@ Set<String> claudeNodeIds(Map<String, String> env) {
class CliSession {
/// Wraps already-open parts; [open] is the normal way in. Public so a test
/// can subclass the session and script [sync]'s outcome.
CliSession(this.dataDir, this.repository, this._firebase);
CliSession(
this.dataDir,
this.repository,
this._firebase, {
this.passphraseFile,
});
/// Where the passphrase is read from besides the environment: next to the
/// seeded Firebase session, e.g. `~/.config/todo/e2e_passphrase`.
final File? passphraseFile;
/// File holding this install's persisted CRDT node id.
static const nodeIdFileName = 'node_id';
@ -105,7 +114,14 @@ class CliSession {
auth: auth,
httpClient: httpClient,
);
return CliSession(dataDir, repository, firebase);
return CliSession(
dataDir,
repository,
firebase,
passphraseFile: File(
p.join(p.dirname(seedCredentials), e2ePassphraseFileName),
),
);
}
/// Pull every peer's log and merge it; with [push], also publish this
@ -113,15 +129,53 @@ class CliSession {
///
/// Without [push] nothing is written anywhere remote, and the revision cache
/// is bypassed so a read always sees every peer's current log.
Future<SyncResult> sync({required bool push}) {
if (!push) {
return const SyncService().sync(repository, PullOnlyStore(_firebase));
Future<SyncResult> sync({required bool push}) async {
final plainService = SyncService(
stateStore: push
? PersistedSyncStateStore(
FileLogPersistence(File(p.join(dataDir, syncStateFileName))),
)
: null,
);
final e2e = await openEncryptedStore(
store: _remote(push: push),
oldPrefix: plainService.notesDir,
readPassphrase: _readPassphrase,
);
if (e2e is! E2eReady && !push) {
// Read-only: nothing encrypted yet (or no key) falls back to the old
// plaintext prefix, which still holds the pre-migration data.
return await const SyncService().sync(repository, _remote(push: false));
}
return SyncService(
stateStore: PersistedSyncStateStore(
FileLogPersistence(File(p.join(dataDir, syncStateFileName))),
),
).sync(repository, _firebase);
if (e2e is! E2eReady) {
throw StateError(
'sync is locked: put the crdt-sync passphrase in '
'$e2ePassphraseEnv or ${passphraseFile?.path ?? e2ePassphraseFileName}',
);
}
final layout = E2eLayout.of(plainService.notesDir);
return await SyncService(
notesDir: layout.prefix,
revsPath: layout.revsPath,
stateStore: plainService.stateStore,
).sync(repository, e2e.store);
}
RemoteStore _remote({required bool push}) =>
push ? _firebase : PullOnlyStore(_firebase);
/// Environment variable holding the shared crdt-sync passphrase.
static const e2ePassphraseEnv = 'TODO_E2E_PASSPHRASE';
/// File name of the passphrase, beside the seeded Firebase session.
static const e2ePassphraseFileName = 'e2e_passphrase';
Future<String?> _readPassphrase() async {
final fromEnv = Platform.environment[e2ePassphraseEnv];
if (fromEnv != null && fromEnv.isNotEmpty) return fromEnv;
final file = passphraseFile;
if (file == null || !file.existsSync()) return null;
return (await file.readAsString()).trim();
}
/// Releases the log and the HTTP client.

View File

@ -73,11 +73,7 @@ class LegacySqliteMigration {
}
// coverage:ignore-end
static Future<void> _onUpgrade(
CrdtTableExecutor db,
int from,
int to,
) async {
static Future<void> _onUpgrade(CrdtTableExecutor db, int from, int to) async {
if (from < 2) {
await db.execute(
'ALTER TABLE notes ADD COLUMN status INTEGER NOT NULL DEFAULT 0',

View File

@ -7,6 +7,9 @@
library;
import 'package:crdt_sync/crdt_sync.dart';
import 'package:crdt_sync_flutter/crdt_sync_flutter.dart'
show kE2ePassphraseKey, kSecureStorage;
import 'package:flutter/foundation.dart' show visibleForTesting;
import 'package:http/http.dart' as http;
import 'package:todo/analytics/analytics_service.dart';
import 'package:todo/data/app_settings.dart';
@ -16,6 +19,10 @@ import 'package:todo/sync/sync_service.dart';
import 'package:todo/sync/sync_settings.dart';
import 'package:todo/sync/sync_state_factory.dart';
/// Argon2id cost for new headers; only tests lower it.
@visibleForTesting
Argon2Params e2eArgon2Params = Argon2Params.recommended;
/// Bundles a [SyncResult] with the [AppSettings] as reconciled against the
/// same Firebase session — so a settings change made on another device is
/// picked up without a second authenticated round trip.
@ -44,8 +51,12 @@ class SyncRunResult {
/// rather than each concern (notes, settings, analytics) opening and
/// closing its own.
///
/// [firebaseFactory] and [stateStore] exist for tests, which must not reach
/// the OS keystore or an application-support directory.
/// [firebaseFactory], [stateStore] and [readPassphrase] exist for tests, which
/// must not reach the OS keystore or an application-support directory.
///
/// Everything pushed is end-to-end encrypted under the shared crdt-sync
/// passphrase; without a usable key the run is a no-op flagged
/// [SyncResult.locked] rather than a plaintext sync.
Future<SyncRunResult> runSync(
NoteRepository repository,
SyncSettings settings, {
@ -54,6 +65,7 @@ Future<SyncRunResult> runSync(
http.Client? httpClient,
Future<FirebaseRestClient?> Function()? firebaseFactory,
SyncStateStore? stateStore,
Future<String?> Function()? readPassphrase,
}) async {
final github = GitHubClient(
owner: settings.owner,
@ -72,12 +84,27 @@ Future<SyncRunResult> runSync(
stateStore: stateStore ?? await openSyncStateStore(),
// coverage:ignore-end
);
final result = await service.sync(
repository,
firebase == null
? github
: MirrorStore(primary: firebase, mirror: github),
final backend = firebase == null
? github
: MirrorStore(primary: firebase, mirror: github);
final e2e = await openEncryptedStore(
store: backend,
oldPrefix: service.notesDir,
readPassphrase:
readPassphrase ?? () => kSecureStorage.read(key: kE2ePassphraseKey),
params: e2eArgon2Params,
);
if (e2e is! E2eReady) {
return const SyncRunResult(
syncResult: SyncResult(mergedDevices: 0, pushed: false, locked: true),
);
}
final layout = E2eLayout.of(service.notesDir);
final result = await SyncService(
notesDir: layout.prefix,
revsPath: layout.revsPath,
stateStore: service.stateStore,
).sync(repository, e2e.store);
await analytics?.flush(firebase);
return SyncRunResult(
syncResult: result.withFirebaseConnected(value: firebase != null),

View File

@ -11,6 +11,7 @@ class SyncResult {
this.skippedFiles = const [],
this.skippedUnchanged = 0,
this.firebaseConnected = false,
this.locked = false,
});
/// How many other devices' logs were pulled and merged.
@ -41,6 +42,10 @@ class SyncResult {
/// device dropping out.
final int skippedUnchanged;
/// True when end-to-end encryption has no usable key on this device, so
/// nothing was pulled, merged or pushed.
final bool locked;
/// Returns a copy with [firebaseConnected] set. [SyncService] never sets
/// it -- only [runSync], which is the layer that knows which [RemoteStore]
/// it built -- so this is the seam that lets it do that without
@ -51,6 +56,7 @@ class SyncResult {
skippedFiles: skippedFiles,
skippedUnchanged: skippedUnchanged,
firebaseConnected: value,
locked: locked,
);
@override
@ -73,7 +79,11 @@ class SyncResult {
/// two never share a path and an old client can't misread a new file.
class SyncService {
/// Creates a [SyncService], optionally overriding [notesDir].
const SyncService({this.notesDir = 'todo-sync/notes', this.stateStore});
const SyncService({
this.notesDir = 'todo-sync/notes',
this.stateStore,
this.revsPath,
});
/// Directory in the repo under which per-device note-log files live.
final String notesDir;
@ -87,7 +97,11 @@ class SyncService {
final SyncStateStore? stateStore;
/// Where revisions live, as a sibling of [notesDir].
String get revsDir => defaultRevsPath(notesDir);
/// Revision cache directory; null derives it from [notesDir].
final String? revsPath;
/// Where peers publish their revisions.
String get revsDir => revsPath ?? defaultRevsPath(notesDir);
/// Runs a full pull-merge-push cycle. Safe to call repeatedly.
///
@ -123,7 +137,15 @@ class SyncService {
skippedUnchanged++;
continue;
}
final text = await remote.getFileText('$notesDir/$name');
final String? text;
try {
text = await remote.getFileText('$notesDir/$name');
} on E2eDecryptError {
// Wrong key, tampered blob or a newer format: skipped and reported,
// never merged and never fatal.
skipped.add(name);
continue;
}
final remoteLog = text == null ? null : _decodeLog(text);
if (remoteLog == null) {
// A vanished or undecodable peer file must not abort the sync, but

View File

@ -98,6 +98,7 @@ class SyncStatusStore {
/// precisely how a desktop stuck syncing over the GitHub mirror alone
/// went unnoticed for days.
String describeSyncResult(SyncResult result) {
if (result.locked) return '🔒';
final skipped = result.skippedFiles.length;
final backend = result.firebaseConnected
? ''

View File

@ -100,7 +100,10 @@ class CaptureSyncRunner {
ok: true,
detail: detail,
reconciled: result.appSettings,
degraded: !synced.firebaseConnected || synced.skippedFiles.isNotEmpty,
degraded:
synced.locked ||
!synced.firebaseConnected ||
synced.skippedFiles.isNotEmpty,
);
} on Exception catch (e) {
await status.record(ok: false, detail: '$e', live: live());

View File

@ -1,6 +1,8 @@
import 'dart:async';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:crdt_sync_flutter/crdt_sync_flutter.dart'
show E2ePassphraseButton;
import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
@ -223,6 +225,10 @@ class _SettingsScreenState extends State<SettingsScreen> {
const Icon(Icons.sync, semanticLabel: 'Sync settings: Firebase'),
_openSyncSettings,
),
const ListTile(
contentPadding: EdgeInsets.zero,
leading: E2ePassphraseButton(locked: true),
),
CloudImagesTile(httpClient: widget.httpClient),
_link(
Icon(

View File

@ -109,11 +109,20 @@ packages:
dependency: "direct main"
description:
path: crdt_sync_dart
ref: "crdt_sync_dart-v0.12.1"
resolved-ref: "6641d85f0550ca60433fc8e0d13a3dafdbb664d6"
ref: "crdt_sync_dart-v0.13.1"
resolved-ref: eebeb0b51053f160f163140141aba10a447ba1e8
url: "https://github.com/kuhyx/utils"
source: git
version: "0.12.1"
version: "0.13.1"
crdt_sync_flutter:
dependency: "direct main"
description:
path: crdt_sync_flutter
ref: "crdt_sync_flutter-v0.7.0"
resolved-ref: "307248b60bbb4e71f8625047a379c5aa275b147e"
url: "https://github.com/kuhyx/utils"
source: git
version: "0.7.0"
cross_file:
dependency: transitive
description:
@ -130,6 +139,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.8"
cryptography:
dependency: transitive
description:
name: cryptography
sha256: "3eda3029d34ec9095a27a198ac9785630fe525c0eb6a49f3d575272f8e792ef0"
url: "https://pub.dev"
source: hosted
version: "2.9.0"
cupertino_icons:
dependency: "direct main"
description:
@ -1006,11 +1023,11 @@ packages:
dependency: "direct main"
description:
path: sync_settings_ui
ref: "sync_settings_ui-v0.7.0"
resolved-ref: "3084080d744c348a217727e26a6be838e7d5a4ff"
ref: "sync_settings_ui-v0.7.1"
resolved-ref: eb9bdc5796755c4e02b7a54d83efcdeb4db91af1
url: "https://github.com/kuhyx/utils"
source: git
version: "0.7.0"
version: "0.7.1"
synchronized:
dependency: transitive
description:

View File

@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.83+1
version: 1.0.84+1
environment:
sdk: ^3.12.2
@ -33,8 +33,13 @@ dependencies:
crdt_sync:
git:
url: https://github.com/kuhyx/utils
ref: crdt_sync_dart-v0.12.1
ref: crdt_sync_dart-v0.13.1
path: crdt_sync_dart
crdt_sync_flutter:
git:
url: https://github.com/kuhyx/utils
ref: crdt_sync_flutter-v0.7.0
path: crdt_sync_flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
@ -97,7 +102,7 @@ dependencies:
sync_settings_ui:
git:
url: https://github.com/kuhyx/utils
ref: sync_settings_ui-v0.7.0
ref: sync_settings_ui-v0.7.1
path: sync_settings_ui
url_launcher: 6.3.3
uuid: 4.6.0

View File

@ -47,8 +47,9 @@ void main() {
expect(pubspec, contains('google_account_token-v0.1.0'));
expect(pubspec, isNot(contains('google_sign_in')));
expect(
File('android/app/src/main/kotlin/dev/kuhy/todo/AccountTokenChannel.kt')
.existsSync(),
File(
'android/app/src/main/kotlin/dev/kuhy/todo/AccountTokenChannel.kt',
).existsSync(),
isFalse,
);
expect(File('lib/sync/account_token.dart').existsSync(), isFalse);

View File

@ -95,10 +95,7 @@ void main() {
const service = AnalyticsService(nodeId: 'device-a');
for (var i = 0; i < AnalyticsService.maxBufferedEvents + 5; i++) {
await service.logEvent(
AnalyticsEvent(
name: 'event_$i',
timestamp: DateTime.now(),
),
AnalyticsEvent(name: 'event_$i', timestamp: DateTime.now()),
);
}

View File

@ -135,42 +135,39 @@ void main() {
expect((remaining.single as Map)['name'], 'mid_flush_event');
});
test(
'restores the claimed batch ahead of events logged mid-flush on '
'failure',
() async {
// The HTTP handler runs mid-`putFileText`, strictly after flush()
// has already claimed (cleared) the buffer, so writing here is a
// genuine race rather than something that could instead land before
// or after `flush()` depending on scheduling.
final client = _fakeClient((_) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(
'analytics.buffer',
jsonEncode([
AnalyticsEvent(
name: 'mid_flush_event',
timestamp: DateTime(2026),
).toJson(),
]),
);
return http.Response('error', 500);
});
const service = AnalyticsService(nodeId: 'device-a');
await service.logEvent(
AnalyticsEvent(name: 'app_open', timestamp: DateTime(2026)),
);
await service.flush(client);
test('restores the claimed batch ahead of events logged mid-flush on '
'failure', () async {
// The HTTP handler runs mid-`putFileText`, strictly after flush()
// has already claimed (cleared) the buffer, so writing here is a
// genuine race rather than something that could instead land before
// or after `flush()` depending on scheduling.
final client = _fakeClient((_) async {
final prefs = await SharedPreferences.getInstance();
final remaining =
jsonDecode(prefs.getString('analytics.buffer')!) as List;
expect(remaining, hasLength(2));
expect((remaining[0] as Map)['name'], 'app_open');
expect((remaining[1] as Map)['name'], 'mid_flush_event');
},
);
await prefs.setString(
'analytics.buffer',
jsonEncode([
AnalyticsEvent(
name: 'mid_flush_event',
timestamp: DateTime(2026),
).toJson(),
]),
);
return http.Response('error', 500);
});
const service = AnalyticsService(nodeId: 'device-a');
await service.logEvent(
AnalyticsEvent(name: 'app_open', timestamp: DateTime(2026)),
);
await service.flush(client);
final prefs = await SharedPreferences.getInstance();
final remaining =
jsonDecode(prefs.getString('analytics.buffer')!) as List;
expect(remaining, hasLength(2));
expect((remaining[0] as Map)['name'], 'app_open');
expect((remaining[1] as Map)['name'], 'mid_flush_event');
});
test(
'a RemoteSyncError from an expired session restores the buffer',

View File

@ -91,9 +91,7 @@ void main() {
});
test('swallows a push failure and keeps the local write', () async {
final client = _fakeClient(
(_) async => http.Response('error', 500),
);
final client = _fakeClient((_) async => http.Response('error', 500));
const initial = AppSettings(advancedMode: false);
final updated = await initial.withAdvancedMode(
@ -107,22 +105,19 @@ void main() {
expect(reloaded.advancedMode, isTrue);
});
test(
'swallows a FirebaseAuthError (expired session) and keeps the local '
'write',
() async {
const initial = AppSettings(advancedMode: false);
test('swallows a FirebaseAuthError (expired session) and keeps the local '
'write', () async {
const initial = AppSettings(advancedMode: false);
final updated = await initial.withAdvancedMode(
value: true,
client: _authFailingClient(),
);
final updated = await initial.withAdvancedMode(
value: true,
client: _authFailingClient(),
);
expect(updated.advancedMode, isTrue);
final reloaded = await AppSettings.load();
expect(reloaded.advancedMode, isTrue);
},
);
expect(updated.advancedMode, isTrue);
final reloaded = await AppSettings.load();
expect(reloaded.advancedMode, isTrue);
});
});
group('reconcileWithRemote', () {
@ -220,9 +215,7 @@ void main() {
() async {
const initial = AppSettings(advancedMode: false);
final result = await initial.reconcileWithRemote(
_authFailingClient(),
);
final result = await initial.reconcileWithRemote(_authFailingClient());
expect(identical(result, initial), isTrue);
},

View File

@ -20,61 +20,59 @@ import 'fake_note_repository.dart';
import 'fake_secure_storage.dart';
import 'capture_screen_harness.dart';
import 'e2e_github_header.dart';
import 'text_free_guard.dart';
void main() {
testWidgets(
'auto-sync adopts a newer advancedMode value from Firebase',
(tester) async {
final remoteTime = DateTime(2026, 6);
final firebase = FirebaseRestClient(
databaseUrl: 'https://x-rtdb.europe-west1.firebasedatabase.app',
auth: FirebaseTokenProvider(
apiKey: 'AIzaKey',
store: InMemoryCredentialStore(
FirebaseCredentials(
idToken: 'id',
refreshToken: 'refresh',
expiresAt: DateTime.now().add(const Duration(hours: 1)),
),
testWidgets('auto-sync adopts a newer advancedMode value from Firebase', (
tester,
) async {
final remoteTime = DateTime(2026, 6);
final firebase = FirebaseRestClient(
databaseUrl: 'https://x-rtdb.europe-west1.firebasedatabase.app',
auth: FirebaseTokenProvider(
apiKey: 'AIzaKey',
store: InMemoryCredentialStore(
FirebaseCredentials(
idToken: 'id',
refreshToken: 'refresh',
expiresAt: DateTime.now().add(const Duration(hours: 1)),
),
),
httpClient: MockClient((request) async {
if (request.url.path == '/settings/advancedMode.json') {
return http.Response(
'{"value": "true", '
'"updatedAtMillis": "${remoteTime.millisecondsSinceEpoch}"}',
200,
);
}
if (request.method == 'PUT') return http.Response(request.body, 200);
return http.Response('null', 200);
}),
);
final appSettings = ValueNotifier(
AppSettings(
advancedMode: false,
// Older than remoteTime, and non-null, so the reconciliation
// guard's `currentAt != null && !reconciledAt.isAfter(currentAt)`
// check actually evaluates both operands.
advancedModeUpdatedAt: remoteTime.subtract(
const Duration(days: 1),
),
),
);
),
httpClient: e2eMock((request) async {
if (request.url.path == '/settings/advancedMode.json') {
return http.Response(
'{"value": "true", '
'"updatedAtMillis": "${remoteTime.millisecondsSinceEpoch}"}',
200,
);
}
if (request.method == 'PUT') return http.Response(request.body, 200);
return http.Response('null', 200);
}),
);
final appSettings = ValueNotifier(
AppSettings(
advancedMode: false,
// Older than remoteTime, and non-null, so the reconciliation
// guard's `currentAt != null && !reconciledAt.isAfter(currentAt)`
// check actually evaluates both operands.
advancedModeUpdatedAt: remoteTime.subtract(const Duration(days: 1)),
),
);
await pumpCapture(
tester,
prefs: configuredPrefs,
firebaseFactory: () async => firebase,
appSettings: appSettings,
);
await tester.pump(); // settings load → auto-sync (pull) …
await tester.pump(); // … then push, then reconcile
await pumpCapture(
tester,
prefs: configuredPrefs,
firebaseFactory: () async => firebase,
appSettings: appSettings,
);
await tester.pump(); // settings load → auto-sync (pull) …
await tester.pump(); // … then push, then reconcile
expect(appSettings.value.advancedMode, isTrue);
},
);
expect(appSettings.value.advancedMode, isTrue);
});
testWidgets('logs app_open when analytics is injected', (tester) async {
const analytics = AnalyticsService(nodeId: 'device-a');

View File

@ -31,6 +31,7 @@ import 'package:todo/ui/settings_screen.dart';
import 'package:todo/ui/text_free_localizations.dart';
import 'fake_note_repository.dart';
import 'e2e_github_header.dart';
import 'fake_secure_storage.dart';
// A real CRDT DB schedules sqflite timers that never drain under the
@ -108,7 +109,7 @@ const configuredPrefs = {
// A MockClient that records request methods and answers the sync flow:
// repo-exists GET (not /contents/) → 200, the (empty) log listing → 404,
// and the device's own PUT → 200.
MockClient recordingMock(List<String> methods) => MockClient((req) async {
MockClient recordingMock(List<String> methods) => e2eMock((req) async {
methods.add(req.method);
if (req.method == 'PUT') return http.Response('{}', 200);
if (!req.url.path.contains('/contents/')) return http.Response('{}', 200);

View File

@ -17,6 +17,7 @@ import 'package:todo/ui/github_mirror_screen.dart';
import 'package:todo/ui/settings_screen.dart';
import 'fake_note_repository.dart';
import 'e2e_github_header.dart';
import 'fake_secure_storage.dart';
import 'capture_screen_harness.dart';
@ -29,7 +30,7 @@ void main() {
// Empty remote directory → the service has nothing to merge and pushes
// this device's own log (PUT). The bare repo GET (not /contents/) returns
// 200 so crdt_sync's client treats the 404 dir as "empty", not missing.
final mock = MockClient((req) async {
final mock = e2eMock((req) async {
if (req.method == 'PUT') return http.Response('{}', 200);
if (!req.url.path.contains('/contents/')) return http.Response('{}', 200);
return http.Response('', 404);
@ -70,10 +71,10 @@ void main() {
tester,
) async {
// One peer file exists but holds garbage → merged 0, skipped 1.
final mock = MockClient((req) async {
final mock = e2eMock((req) async {
if (req.method == 'PUT') return http.Response('{}', 200);
if (!req.url.path.contains('/contents/')) return http.Response('{}', 200);
if (req.url.path.endsWith('/todo-sync/notes')) {
if (req.url.path.endsWith('/todo-sync/notes-e2e')) {
return http.Response('[{"name": "bad.json"}]', 200);
}
if (req.url.path.endsWith('/bad.json')) {
@ -132,7 +133,7 @@ void main() {
// A MockClient that records request methods and answers the sync flow:
// repo-exists GET (not /contents/) → 200, the (empty) log listing → 404,
// and the device's own PUT → 200.
MockClient recordingMock(List<String> methods) => MockClient((req) async {
MockClient recordingMock(List<String> methods) => e2eMock((req) async {
methods.add(req.method);
if (req.method == 'PUT') return http.Response('{}', 200);
if (!req.url.path.contains('/contents/')) return http.Response('{}', 200);

View File

@ -19,6 +19,9 @@ class CliHarness {
writeSeedCredentials(
tmp,
).copySync('${home.path}/.config/todo/firebase_auth.json');
File(
'${tmp.path}/e2e_passphrase',
).copySync('${home.path}/.config/todo/e2e_passphrase');
env = {'HOME': home.path, 'XDG_DATA_HOME': '${tmp.path}/xdg'};
}

View File

@ -22,6 +22,9 @@ void main() {
writeSeedCredentials(
tmp,
).copySync('${home.path}/.config/todo/firebase_auth.json');
File(
'${tmp.path}/e2e_passphrase',
).copySync('${home.path}/.config/todo/e2e_passphrase');
env = {'HOME': home.path, 'XDG_DATA_HOME': '${tmp.path}/xdg'};
out = StringBuffer();
err = StringBuffer();
@ -86,7 +89,10 @@ void main() {
test('pushes an ordinary CRDT log and prints the new id', () async {
final id = await add('a', 'Title\n\nbody', ['--priority', 'high']);
expect(id, matches(RegExp(r'^[0-9a-f-]{36}$')));
expect(db.files.keys.where((k) => k.contains('notes/')), hasLength(1));
expect(
db.files.keys.where((k) => k.contains('notes-e2e/')),
hasLength(1),
);
expect(err.toString(), isEmpty);
});

View File

@ -117,8 +117,8 @@ void main() {
final first = await session.sync(push: true);
expect(first.pushed, isTrue);
final node = session.repository.nodeId;
expect(db.files.keys, contains('todo-sync/notes/$node~2Ejson'));
expect(db.files.keys, contains('todo-sync/revs/$node'));
expect(db.files.keys, contains('todo-sync/notes-e2e/$node~2Ejson'));
expect(db.files.keys, contains('todo-sync/revs-e2e/$node'));
expect(
File('${tmp.path}/push/${CliSession.syncStateFileName}').existsSync(),
isTrue,

View File

@ -64,6 +64,8 @@ class FakeRtdb {
/// Writes a Firebase session that does not expire during a test run.
File writeSeedCredentials(Directory dir, {String name = 'seed.json'}) {
// The CLI reads its crdt-sync passphrase from beside the seed file.
File('${dir.path}/e2e_passphrase').writeAsStringSync('test-passphrase');
return File('${dir.path}/$name')..writeAsStringSync(
jsonEncode({
'id_token': 'seed-id-token',

View File

@ -0,0 +1,35 @@
/// A GitHub stand-in that persists the end-to-end encryption header, which
/// the sync service writes and re-reads; every other request goes to [inner].
library;
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
/// Wraps [inner] so `…/e2e` PUTs are stored and served back on GET.
MockClient e2eMock(Future<http.Response> Function(http.Request) inner) {
String? header;
return MockClient((request) async {
if (request.url.path.endsWith('/e2e.json')) {
// Firebase REST: the value is a JSON-encoded string.
if (request.method == 'PUT') {
header = jsonDecode(request.body) as String;
return http.Response(request.body, 200);
}
return http.Response(jsonEncode(header), 200);
}
if (!request.url.path.endsWith('/e2e')) return inner(request);
if (request.method == 'PUT') {
final body = jsonDecode(request.body) as Map<String, dynamic>;
header = utf8.decode(base64.decode(body['content'] as String));
return http.Response(jsonEncode({'content': {}, 'sha': 'h'}), 200);
}
final text = header;
if (text == null) return http.Response('', 404);
return http.Response(
jsonEncode({'content': base64.encode(utf8.encode(text)), 'sha': 'h'}),
200,
);
});
}

View File

@ -1,4 +1,6 @@
import 'package:crdt_sync/crdt_sync.dart' show Argon2Params;
import 'package:flutter/services.dart';
import 'package:todo/sync/run_sync.dart' show e2eArgon2Params;
import 'package:flutter_test/flutter_test.dart';
/// In-memory fake for the `flutter_secure_storage` platform channel so tests
@ -13,7 +15,17 @@ void installFakeSecureStorage({
bool throwing = false,
}) {
const channel = MethodChannel('plugins.it_nomads.com/flutter_secure_storage');
final store = <String, String>{...?initial};
e2eArgon2Params = const Argon2Params(
memoryKiB: 8,
iterations: 1,
parallelism: 1,
);
// Sync is end-to-end encrypted, so a device with no passphrase is locked;
// tests that sync start with one unless they pass their own `initial`.
final store = <String, String>{
'crdt_sync_e2e_passphrase': 'test-passphrase',
...?initial,
};
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;

View File

@ -6,10 +6,7 @@ void main() {
test('returns the token the sign-in function yields', () async {
// The injected path: proves the closure is what feeds crdt_sync, and
// that no platform channel is touched to get there.
expect(
await googleIdToken(signInFn: () async => 'a-token'),
'a-token',
);
expect(await googleIdToken(signInFn: () async => 'a-token'), 'a-token');
});
test('passes a null cancellation straight through', () async {

View File

@ -62,8 +62,8 @@ void main() {
final stop = await start();
expect(stop, isNotNull);
expect(err.toString(), contains('serving http://127.0.0.1:'));
expect(db.files.keys, contains(startsWith('todo-sync/notes/')));
expect(db.files.keys, contains(startsWith('todo-sync/revs/')));
expect(db.files.keys, contains(startsWith('todo-sync/notes-e2e/')));
expect(db.files.keys, contains(startsWith('todo-sync/revs-e2e/')));
await stop!();
});
}

View File

@ -61,7 +61,7 @@ void main() {
});
test('an unmergeable peer log is reported, not hidden', () async {
h.db.files['todo-sync/notes/broken~2Ejson'] = 'not a log';
h.db.files['todo-sync/notes-e2e/broken~2Ejson'] = 'not a log';
expect(await h.refused(c, 'list_tasks', {}), contains('unmergeable'));
});
}

View File

@ -109,10 +109,7 @@ void main() {
});
test('accepts a binary replaced by an upgrade', () {
expect(
isTodoWrapper('/opt/todo/bin/todo_desktop (deleted)', []),
isTrue,
);
expect(isTodoWrapper('/opt/todo/bin/todo_desktop (deleted)', []), isTrue);
});
test('accepts a development run out of the repo', () {
@ -155,48 +152,36 @@ void main() {
test('matches a normally separated argument vector', () {
expect(
hasWindowAttached(
[
['thorium', '--user-data-dir=$_profile', '--class=todo'],
],
_profile,
),
hasWindowAttached([
['thorium', '--user-data-dir=$_profile', '--class=todo'],
], _profile),
isTrue,
);
});
test('matches when the flag is the final argument', () {
expect(
hasWindowAttached(
[
['thorium', '--user-data-dir=$_profile'],
],
_profile,
),
hasWindowAttached([
['thorium', '--user-data-dir=$_profile'],
], _profile),
isTrue,
);
});
test('does not match a sibling profile directory', () {
expect(
hasWindowAttached(
[
['thorium', '--user-data-dir=${_profile}2'],
],
_profile,
),
hasWindowAttached([
['thorium', '--user-data-dir=${_profile}2'],
], _profile),
isFalse,
);
});
test('does not match when no process mentions the profile', () {
expect(
hasWindowAttached(
[
['bash', '-c', 'sleep 1'],
],
_profile,
),
hasWindowAttached([
['bash', '-c', 'sleep 1'],
], _profile),
isFalse,
);
});

View File

@ -91,17 +91,11 @@ void main() {
);
final action = await guard.resolve(8730);
expect(action, isA<AbortWithOwner>());
expect(
(action as AbortWithOwner).description,
contains('unidentified'),
);
expect((action as AbortWithOwner).description, contains('unidentified'));
});
test('retries when the owner exited mid-probe', () async {
final guard = PortGuard(
profileDir: _profile,
probe: FakeProbe(pid: 42),
);
final guard = PortGuard(profileDir: _profile, probe: FakeProbe(pid: 42));
expect(await guard.resolve(8730), isA<BindNow>());
});
@ -202,20 +196,14 @@ void main() {
final probe = FakeProbe(freeAfter: ProcessSignal.sigkill);
final guard = PortGuard(profileDir: _profile, probe: probe);
expect(await _reap(guard, 42, 8730), isTrue);
expect(probe.signalsSent, [
ProcessSignal.sigterm,
ProcessSignal.sigkill,
]);
expect(probe.signalsSent, [ProcessSignal.sigterm, ProcessSignal.sigkill]);
});
test('reports failure when the port never comes free', () async {
final probe = FakeProbe(inUse: [true]);
final guard = PortGuard(profileDir: _profile, probe: probe);
expect(await _reap(guard, 42, 8730), isFalse);
expect(probe.signalsSent, [
ProcessSignal.sigterm,
ProcessSignal.sigkill,
]);
expect(probe.signalsSent, [ProcessSignal.sigterm, ProcessSignal.sigkill]);
});
});
}

View File

@ -19,12 +19,36 @@ import 'package:todo/data/note_repository.dart';
import 'package:todo/sync/run_sync.dart';
import 'package:todo/sync/sync_settings.dart';
import 'fake_secure_storage.dart';
final _headers = <String, String>{};
/// Stateful GitHub stand-in for the encryption header, the one file the
/// sync service itself does not own: PUT stores it, GET serves it back.
http.Response? _headerReply(http.Request request) {
if (!request.url.path.endsWith('/e2e')) return null;
if (request.method == 'PUT') {
final body = jsonDecode(request.body) as Map<String, dynamic>;
_headers['e2e'] = utf8.decode(base64.decode(body['content'] as String));
return http.Response(jsonEncode({'content': {}, 'sha': 'h'}), 200);
}
final text = _headers['e2e'];
if (text == null) return http.Response('', 404);
return http.Response(
jsonEncode({'content': base64.encode(utf8.encode(text)), 'sha': 'h'}),
200,
);
}
const _settings = SyncSettings(owner: 'kuhyx', repo: 'syncs', token: 'tok');
void main() {
late NoteRepository repo;
setUp(() async {
TestWidgetsFlutterBinding.ensureInitialized();
installFakeSecureStorage();
_headers.clear();
SharedPreferences.setMockInitialValues({});
repo = await NoteRepository.openInMemory(nodeId: 'me');
});
@ -39,6 +63,8 @@ void main() {
_settings,
httpClient: http_testing.MockClient((request) async {
paths.add('${request.method} ${request.url.host}');
final header = _headerReply(request);
if (header != null) return header;
return http.Response(jsonEncode({'content': '', 'sha': 'x'}), 200);
}),
firebaseFactory: () async => null,
@ -83,6 +109,8 @@ void main() {
_settings,
httpClient: http_testing.MockClient((request) async {
githubPaths.add(request.method);
final header = _headerReply(request);
if (header != null) return header;
return http.Response(jsonEncode({'content': '', 'sha': 'x'}), 200);
}),
firebaseFactory: () async => firebase,
@ -108,6 +136,8 @@ void main() {
repo,
_settings,
httpClient: http_testing.MockClient((request) async {
final header = _headerReply(request);
if (header != null) return header;
return http.Response(jsonEncode({'content': '', 'sha': 'x'}), 200);
}),
firebaseFactory: () async => null,
@ -158,6 +188,8 @@ void main() {
appSettings: const AppSettings(advancedMode: false),
analytics: analytics,
httpClient: http_testing.MockClient((request) async {
final header = _headerReply(request);
if (header != null) return header;
return http.Response(jsonEncode({'content': '', 'sha': 'x'}), 200);
}),
firebaseFactory: () async => firebase,

View File

@ -64,10 +64,7 @@ void main() {
final first = await SyncService(stateStore: store).sync(repo, remote);
expect(first.pushed, isTrue);
expect(remote.writes, [
'todo-sync/notes/me.json',
'todo-sync/revs/me',
]);
expect(remote.writes, ['todo-sync/notes/me.json', 'todo-sync/revs/me']);
remote.writes.clear();
final second = await SyncService(stateStore: store).sync(repo, remote);

View File

@ -27,9 +27,9 @@ void main() {
test('persists revisions across a restart', () async {
// A fresh store instance stands in for the next app launch: an
// in-memory cache would forget every peer and re-download everything.
await openSyncStateStoreIn(dir.path).save(
const SyncState(pushedRev: 'mine', peerRevs: {'phone': 'theirs'}),
);
await openSyncStateStoreIn(
dir.path,
).save(const SyncState(pushedRev: 'mine', peerRevs: {'phone': 'theirs'}));
final reloaded = await openSyncStateStoreIn(dir.path).load();
@ -38,9 +38,7 @@ void main() {
});
test('writes beside the log it describes', () async {
await openSyncStateStoreIn(
dir.path,
).save(const SyncState(pushedRev: 'x'));
await openSyncStateStoreIn(dir.path).save(const SyncState(pushedRev: 'x'));
expect(File(p.join(dir.path, kSyncStateFileName)).existsSync(), isTrue);
});

View File

@ -69,9 +69,7 @@ void main() {
expect(response.statusCode, 500);
// Still alive afterwards.
final after = await http.get(
Uri.parse('http://localhost:${wedged.port}/'),
);
final after = await http.get(Uri.parse('http://localhost:${wedged.port}/'));
expect(after.statusCode, 200);
});