// End-to-end against a live reference server: register an address on a // locally running smolmaild, send sealed messages to ourselves, fetch them // back, exercise the accept-token round trip (§5.8) between the requests and // main tiers, and check the server is drained afterwards. Skips when nothing // listens on 127.0.0.1:1961, so `devbox run test` does not depend on a server. // // To run it: (cd ../smolmail && uv run smolmaild.py keygen --key server.key && // uv run smolmaild.py serve --key server.key --db mail.db) // then `devbox run test`. import "dart:io"; import "dart:typed_data"; import "package:flutter_test/flutter_test.dart"; import "package:hive_flutter/hive_flutter.dart"; import "package:smol_mail/smol/client.dart"; import "package:smol_mail/smol/crypto.dart"; import "package:smol_mail/smol/errors.dart"; import "package:smol_mail/smol/proto.dart"; import "package:smol_mail/smol/store.dart"; const host = "127.0.0.1"; const port = 1961; Future serverUp() async { try { final probe = await Socket.connect(host, port, timeout: const Duration(seconds: 2)); probe.destroy(); return true; } catch (_) { return false; } } void main() { test("register, send to self, fetch, unseal, drain", () async { if (!await serverUp()) { markTestSkipped("no smolmaild on $host:$port"); return; } final dir = await Directory.systemTemp.createTemp("smol-e2e"); Hive.init(dir.path); final store = await SmolStore.open(); final client = SmolClient(store); // First contact is trust-on-first-use: learn the key the handshake reveals, // then pin it — the flow a user with an operator-supplied key skips. final warnings = []; client.onWarning = warnings.add; final master = client.createIdentity(); final me = client.identity!; final user = "e2e${hex(randomBytes(4))}"; final address = parseAddress("$user@$host"); final learned = await client.connect(address, requirePin: false); // §8: the first, unpinned session must be announced as unverified. expect(warnings, isNotEmpty); expect(warnings.single, contains("not pinned")); store.pinServer(host, learned.serverStatic); learned.session.wire.close(); await client.registerAccount(address.short); expect(store.account()?.user, user); await client.send(address.short, "hello e2e", "sealed and signed"); await client.send(address.short, "second", "another sealed envelope"); final summary = await client.fetch(); expect(summary.stored, 2); expect(summary.rejected, isEmpty); // §5.8: we have not accepted ourselves as a correspondent yet, so this // unsolicited self-mail lands in the requests tier, not the main one. expect(store.listMessages("inbox"), isEmpty); final requests = store.listMessages("requests"); expect(requests.length, 2); // receivedAt has second granularity, so the order of the two is not // guaranteed; assert on the pair, then open the one we care about. final subjects = requests.map((m) => client.describe(m).subject).toSet(); expect(subjects, {"hello e2e", "second"}); final hello = requests.firstWhere( (m) => client.describe(m).subject == "hello e2e"); final opened = client.describe(hello); expect(opened.error, isNull); expect(opened.body, "sealed and signed\n"); expect(hex(opened.sender!), hex(me.publicKey)); // The server must be drained: everything that verified was acknowledged. final again = await client.fetch(); expect(again.stored, 0); // Accept ourselves as a correspondent (§5.8): the change is pushed to the // server right away. This next message carries our own Accept field, but // no MAC yet — we cannot know our own token before receiving and parsing // a message that carries it — so it still lands in requests. await client.acceptContact(address.short); await client.send(address.short, "third", "still unsolicited"); expect((await client.fetch()).stored, 1); expect(store.listMessages("requests").length, 3); expect(store.listMessages("inbox"), isEmpty); // Having now learned our own token from that message's Accept field, the // next one carries a matching MAC and reaches the main tier. await client.send(address.short, "fourth", "now accepted"); expect((await client.fetch()).stored, 1); final mainTier = store.listMessages("inbox"); expect(mainTier.length, 1); expect(client.describe(mainTier.single).subject, "fourth"); // The sent copy is sealed to ourselves and readable (§5.6). final sent = store.listMessages("sent"); expect(sent.length, 4); expect(client.describe(sent.first).error, isNull); // A restored seed can rebind the address without REGISTER; the address // must resolve to this identity's key. final recalled = await client.recallAccount(address.short); expect(recalled.short, address.short); expect( () => client.recallAccount("nobody@$host"), throwsA(isA())); // Restore on a second device: same master, fresh store, no pin. Unpinned // recall is refused; re-registering a taken name is refused; recall with // the operator-supplied key then binds the account without REGISTER. final restored = await SmolStore.open( stateBox: "e2e-restore-state", mailBox: "e2e-restore-mail"); final secondDevice = SmolClient(restored); restored.setMaster(master); expect( secondDevice.recallAccount(address.short), throwsA(isA())); restored.pinServer(host, learned.serverStatic); await expectLater( secondDevice.registerAccount(address.short), throwsA(isA())); final bound = await secondDevice.recallAccount(address.short); expect(bound.short, address.short); expect(restored.account()!.user, user); // Re-resolving our own address finds the same key and says so quietly. final outcome = await client.refreshContact(address.short); expect(outcome.warn, isFalse); expect(outcome.message, contains("key unchanged")); expect(store.contact(address.short)!.history, isEmpty); }, timeout: const Timeout(Duration(minutes: 2))); test("leave mail on server keeps mail until deleted, with dedupe on refetch", () async { if (!await serverUp()) { markTestSkipped("no smolmaild on $host:$port"); return; } final dir = await Directory.systemTemp.createTemp("smol-e2e-keep"); Hive.init(dir.path); final store = await SmolStore.open(stateBox: "e2e-keep-state", mailBox: "e2e-keep-mail"); final client = SmolClient(store); client.createIdentity(); final user = "e2ekeep${hex(randomBytes(4))}"; final address = parseAddress("$user@$host"); final learned = await client.connect(address, requirePin: false); store.pinServer(host, learned.serverStatic); learned.session.wire.close(); await client.registerAccount(address.short); store.setLeaveOnServer(true); await client.send(address.short, "kept", "stays on the server until deleted"); final first = await client.fetch(); expect(first.stored, 1); final record = store.listMessages("requests").single; expect(record.keptOnServer, isTrue); expect(client.describe(record).subject, "kept"); // Re-fetching from scratch must not duplicate it locally (storeIfNew's // dedupe), even though the server still has it (nothing was deleted). store.setCursor(0, Uint8List(idLen)); final second = await client.fetch(); expect(second.stored, 0); expect(store.listMessages("requests").length, 1); // Deleting removes it from the server too: a further full re-page after // deletion must come back empty rather than resurrecting it. await client.deleteMessage("requests", record); expect(store.listMessages("requests"), isEmpty); store.setCursor(0, Uint8List(idLen)); final third = await client.fetch(); expect(third.stored, 0); expect(store.listMessages("requests"), isEmpty); }, timeout: const Timeout(Duration(minutes: 2))); }