wake-alarm/phone_app/test/e2e_support.dart
Krzysztof kuhy Rudnicki 9250db4cc1
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,free-days-interop) (push) Has been cancelled
Tests / test (push) Has been cancelled
Tests / free-days-interop (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
E2E-encrypt the published alarm record
The phone pushes alarm.json sealed under wake-alarm-sync/devices-e2e
(Argon2id key from the shared crdt-sync passphrase, keystore + lock
icon in Settings). The PC reads it through open_encrypted_client on
both Firebase and the GitHub mirror (now via GitHubSyncClient); no or
wrong passphrase reads as nothing published, never plaintext. The old
plaintext prefix is copied once per process, never deleted.

crdt-sync 0.11.0, crdt_sync_dart 0.14.1, crdt_sync_flutter 0.8.1,
sync_settings_ui 0.8.2, freedays 0.4.2, sickdays 0.5.3. install.sh
now fails if crdt_sync/pynacl/argon2 do not import.

Todo: fda71ed1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 10:30:27 +01:00

93 lines
2.8 KiB
Dart

/// Shared end-to-end encryption plumbing for the sync tests.
library;
import 'dart:convert';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart' as http_testing;
import 'package:wake_alarm_sync/services/alarm_sync_e2e.dart';
import 'fake_secure_storage.dart';
/// The passphrase every test device shares.
const testPassphrase = 'test-passphrase';
/// A keystore holding [testPassphrase], cheap Argon2id, a fresh copy memo.
void installE2eTestStorage({Map<String, String>? initial}) {
e2eArgon2Params = const Argon2Params(
memoryKiB: 8,
iterations: 1,
parallelism: 1,
);
resetE2eCopyMemo();
installFakeSecureStorage(
initial: {'crdt_sync_e2e_passphrase': testPassphrase, ...?initial},
);
}
/// A GitHub Contents API stand-in that stores what is PUT and lists
/// directories from it, so the encryption header reads back.
class MemGitHub {
/// Starts holding [files] (repo path -> text).
MemGitHub([Map<String, String>? files]) : files = {...?files};
/// Repo path -> text.
final Map<String, String> files;
/// Every PUT path, in order.
final List<String> putPaths = [];
/// The mock HTTP client.
late final http.Client httpClient = http_testing.MockClient(_handle);
/// A [GitHubClient] over [httpClient].
GitHubClient client() =>
GitHubClient(owner: 'o', repo: 'r', token: 't', httpClient: httpClient);
/// The decrypted text at [path], read like another device would.
Future<String?> opened(String path) async {
final state = await openEncryptedStore(
store: client(),
oldPrefix: legacyAlarmPrefix,
readPassphrase: () async => testPassphrase,
);
return await (state as E2eReady).store.getFileText(path);
}
Future<http.Response> _handle(http.Request request) async {
final path = request.url.path;
if (!path.contains('/contents/')) return http.Response('{}', 200);
final key = path.split('/contents/').last;
if (request.method == 'PUT') {
final body = jsonDecode(request.body) as Map<String, dynamic>;
files[key] = utf8.decode(base64.decode(body['content'] as String));
putPaths.add(key);
return http.Response('{}', 200);
}
final text = files[key];
if (text != null) {
return http.Response(
jsonEncode({'content': base64.encode(utf8.encode(text)), 'sha': 's'}),
200,
);
}
final names = {
for (final k in files.keys)
if (k.startsWith('$key/')) k.substring(key.length + 1).split('/')[0],
};
if (names.isEmpty) return http.Response('{}', 404);
return http.Response(
jsonEncode([
for (final n in names)
{
'type': files.containsKey('$key/$n') ? 'file' : 'dir',
'name': n,
'path': '$key/$n',
},
]),
200,
);
}
}