utils/rtdb_relay/dart/test/e2e_gate_test.dart
Krzysztof kuhy Rudnicki d36826e2b5
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file length / file-length (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
rtdb_relay dart tests / test (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
rtdb_relay 0.2.1: recover the e2e passphrase
E2eGate takes an optional recover callback. When the key stays locked
for want of a passphrase (needsPassphrase or wrongKey), the gate asks
it once, saves what it returns into the keystore and opens again. A
bad header never asks; a wrong answer leaves the lock button.

fetchWrapperPassphrase reads <app>/sync-passphrase from the desktop
app's PC wrapper (null on 404, a malformed body, or no wrapper).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWY91NZkk1AH6ugi6ya
2026-11-18 20:52:20 +01:00

220 lines
7.6 KiB
Dart

import 'dart:convert';
import 'package:crdt_sync/crdt_sync.dart' show E2eLayout, E2eLockReason;
import 'package:crdt_sync_flutter/crdt_sync_flutter.dart'
show kE2ePassphraseKey, kSecureStorage;
import 'package:crdt_sync_flutter/testing/fake_secure_storage.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:rtdb_relay/src/e2e_gate.dart';
import 'package:rtdb_relay/src/json_tree.dart';
import 'package:rtdb_relay/src/layout.dart';
import 'package:rtdb_relay/src/rtdb_client.dart';
import 'support/e2e_fixture.dart';
import 'support/fake_rtdb.dart';
import 'support/tutor_layout.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('a client that needs a key sends nothing without one', () async {
final db = FakeRtdb();
final client = RtdbClient(
layout: tutorLayout,
databaseUrl: 'https://fake',
idToken: () async => 'tok',
httpClient: db.client().httpClient,
namespace: '',
sealRequired: true,
);
await expectLater(
client.put('tutor/v1-e2e/requests/$testRid/req', {'kind': 'x'}),
throwsA(isA<RtdbError>()),
);
await expectLater(
client.get('tutor/v1-e2e/live'),
throwsA(isA<RtdbError>()),
);
await expectLater(
client.delete('tutor/v1-e2e/live'),
throwsA(isA<RtdbError>()),
);
expect(db.log, isEmpty);
expect(E2eGate(client: client).locked, isTrue);
expect(E2eGate(client: db.client()).locked, isFalse);
expect(await E2eGate(client: db.client()).unlock(), isTrue);
});
group('E2eGate', () {
RtdbClient needy(FakeRtdb db) => RtdbClient(
layout: tutorLayout,
databaseUrl: 'https://fake',
idToken: () async => 'tok',
httpClient: db.client().httpClient,
namespace: '',
sealRequired: true,
);
test('unlocks from the keystore and makes the header once', () async {
installFakeSecureStorage(initial: {kE2ePassphraseKey: 'test-passphrase'});
final db = FakeRtdb();
final client = needy(db);
final gate = E2eGate(client: client, params: fastArgon);
expect(await gate.unlock(), isTrue);
expect(gate.locked, isFalse);
expect(await gate.unlock(), isTrue); // already open
final header = getAt(db.root, E2eLayout.of(tutorLayout.base).e2ePath);
expect(header, isA<String>());
await client.put('tutor/v1-e2e/live', {'session': 'x'});
expect(
jsonEncode(getAt(db.root, 'tutor/v1-e2e/live')),
contains(kSealedKey),
);
// A second device with the same passphrase reads the same key.
final other = needy(db);
expect(await E2eGate(client: other, params: fastArgon).unlock(), isTrue);
expect(await other.get('tutor/v1-e2e/live'), {'session': 'x'});
});
test('stays locked without, or with a wrong, passphrase', () async {
installFakeSecureStorage();
final db = FakeRtdb();
final gate = E2eGate(client: needy(db), params: fastArgon);
expect(await gate.unlock(), isFalse);
expect(gate.reason, E2eLockReason.needsPassphrase.name);
db.root = setAt(db.root, E2eLayout.of(tutorLayout.base).e2ePath, {
'not': 'a header',
});
installFakeSecureStorage(initial: {kE2ePassphraseKey: 'x'});
final bad = E2eGate(client: needy(db), params: fastArgon);
expect(await bad.unlock(), isFalse);
expect(bad.reason, E2eLockReason.badHeader.name);
});
test('opens a header and a unit the Python worker made', () async {
final h = e2eVectors['header'] as Map<String, dynamic>;
installFakeSecureStorage(
initial: {kE2ePassphraseKey: h['passphrase'] as String},
);
final db = FakeRtdb()..root = setAt(null, h['path'] as String, h['text']);
final unit = h['unit'] as Map<String, dynamic>;
db.root = setAt(db.root, 'tutor/v1-e2e/${unit['path']}', unit['stored']);
final client = needy(db);
expect(await E2eGate(client: client, params: fastArgon).unlock(), isTrue);
expect(await client.get('tutor/v1-e2e/${unit['path']}'), unit['value']);
installFakeSecureStorage(initial: {kE2ePassphraseKey: 'wrong'});
final wrong = E2eGate(client: needy(db), params: fastArgon);
expect(await wrong.unlock(), isFalse);
expect(wrong.reason, E2eLockReason.wrongKey.name);
});
});
group('E2eGate recovery', () {
RtdbClient needy(FakeRtdb db) => RtdbClient(
layout: tutorLayout,
databaseUrl: 'https://fake',
idToken: () async => 'tok',
httpClient: db.client().httpClient,
namespace: '',
sealRequired: true,
);
Future<FakeRtdb> headerFor(String passphrase) async {
installFakeSecureStorage(initial: {kE2ePassphraseKey: passphrase});
final db = FakeRtdb();
expect(
await E2eGate(client: needy(db), params: fastArgon).unlock(),
isTrue,
);
return db;
}
test('a missing passphrase is fetched, kept and opens the key', () async {
final db = await headerFor('right');
installFakeSecureStorage();
var asked = 0;
final gate = E2eGate(
client: needy(db),
params: fastArgon,
recover: () async {
asked++;
return 'right';
},
);
expect(await gate.unlock(), isTrue);
expect(asked, 1);
expect(await kSecureStorage.read(key: kE2ePassphraseKey), 'right');
});
test('a wrong stored passphrase is replaced by the fetched one', () async {
final db = await headerFor('right');
installFakeSecureStorage(initial: {kE2ePassphraseKey: 'wrong'});
final gate = E2eGate(
client: needy(db),
params: fastArgon,
recover: () async => 'right',
);
expect(await gate.unlock(), isTrue);
});
test(
'nothing fetched, or fetched wrong, stays locked; asks once',
() async {
final db = await headerFor('right');
installFakeSecureStorage();
final none = E2eGate(
client: needy(db),
params: fastArgon,
recover: () async => null,
);
expect(await none.unlock(), isFalse);
expect(none.reason, E2eLockReason.needsPassphrase.name);
var asked = 0;
final wrong = E2eGate(
client: needy(db),
params: fastArgon,
recover: () async {
asked++;
return 'also-wrong';
},
);
expect(await wrong.unlock(), isFalse);
expect(wrong.reason, E2eLockReason.wrongKey.name);
expect(await wrong.unlock(), isFalse);
expect(asked, 1);
},
);
test('a bad header is not a passphrase problem: never asks', () async {
installFakeSecureStorage(initial: {kE2ePassphraseKey: 'x'});
final db = FakeRtdb();
db.root = setAt(db.root, E2eLayout.of(tutorLayout.base).e2ePath, {
'not': 'a header',
});
var asked = 0;
final gate = E2eGate(
client: needy(db),
params: fastArgon,
recover: () async {
asked++;
return 'x';
},
);
expect(await gate.unlock(), isFalse);
expect(asked, 0);
});
});
test('the header store reads strings only and never deletes', () async {
final db = FakeRtdb()..root = setAt(null, 'tutor/e2e', {'x': 1});
final store = E2eHeaderStore(db.client());
expect(await store.getFileText('tutor/e2e'), '{x: 1}');
expect(await store.getFileText('tutor/none'), isNull);
expect(await store.listDirectory('tutor'), isEmpty);
expect(await store.canAccessRemote(), isTrue);
expect(() => store.deleteFile('tutor/e2e'), throwsA(anything));
store.close();
expect(const E2eLayoutError('x').toString(), 'E2eLayoutError: x');
});
}