mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-11-29 08:29:22 +00:00
The capability-proof watchdog drains the peer's parked Noise payloads whenever the timeout-restore deferral is off, whether or not a session exists. The drain encrypts under the current session and never re-queues, so with the handshake still in flight past the 5 s deadline every encryption fails: media with a transfer ID gets a visible rejection, and a delivery ack, read receipt, verify, vouch or group payload is dropped with a log line. No attacker is needed. Sending a photo to a private-media peer with no session arms the watchdog and starts a handshake; anything parked behind that handshake is lost if it outlives 5 s, which is ordinary over a multi-hop mesh. noteNoiseSessionCleared re-arms the watchdog as a session goes away, so a slow reconnect with a pending media decision takes the same path. Of the drain's four production callers the watchdog was the only one without a session precondition; the timeout-restore case was already held back by deferredOutbound. It now drains only under an established session and otherwise leaves the payloads for the drain that runs when the session authenticates. The policy resolution is unchanged. proofTimeoutBeforeHandshakeLeavesParkedAckForTheSession drives it through the public path -- resolvePrivateMediaSendPolicy, then sendDeliveryAck -- and forces the watchdog as the neighbouring proof-timeout tests do. Without the guard it fails on its last expectation: the parked count goes from 1 to 0. It needs one DEBUG-only count hook, because _test_privateMediaTransferState only sees payloads that carry a transfer ID. Encryption fails closed throughout; nothing is sent unencrypted. The drain's error path still drops what it cannot encrypt; whether it should re-queue payloads without a transfer ID is left for review rather than changed here. Full SwiftPM suite green: 2068 tests in 222 suites.
Test Harness Guide
This test suite uses an in-memory networking harness to make end-to-end and integration tests deterministic, fast, and race-free without touching production code.
In-Memory Bus
- File:
bitchatTests/Mocks/MockBLEService.swift - Registry/Adjacency: Global
registrymapspeerIDto aMockBLEServiceinstance;adjacencyrecords simulated links between peers. - Setup: Call
MockBLEService.resetTestBus()insetUp()to clear state between tests. - Topology: Use
simulateConnectedPeer(_:)andsimulateDisconnectedPeer(_:)to add/remove links.connectFullMesh()helpers in tests build larger topologies. - Handlers: Tests can observe data via
messageDeliveryHandler(decodedBitchatMessage) andpacketDeliveryHandler(rawBitchatPacket). - De‑duplication: A thread-safe
seenMessageIDsprevents duplicate deliveries during flooding/relays.
Broadcast Flooding
- Flag:
MockBLEService.autoFloodEnabled - Intent: When
true, public broadcasts propagate across the entire connected component (ignores TTL for reach) while still de‑duping to prevent loops. - Usage: Enabled in Integration tests (
setUp) to simulate large-network broadcast; disabled in E2E tests to keep routing explicit and verify TTL behavior (seePublicChatE2ETests.testZeroTTLNotRelayed).
Rehandshake Flow (Noise)
- Why: The legacy NACK recovery path was removed; recovery now relies on Noise session rehandshake after decrypt failure or desync.
- Manager:
NoiseSessionManagermanages per-peer sessions. - Pattern: On decrypt failure, proactively clear the local session and re-initiate a handshake. The peer accepts and replaces their session.
- Test:
IntegrationTests.testRehandshakeAfterDecryptionFailure- Corrupts ciphertext to induce a decrypt error.
- Calls
removeSession(for:)on the initiator’s manager beforeinitiateHandshake(with:)to avoidalreadyEstablished. - Verifies encrypt/decrypt succeeds post-rehandshake.
Tips
- Determinism: Add small async delays only where handler installation/topology changes could race the first send.
- Scoping: Keep
autoFloodEnabledtoggled only within Integration tests; always reset intearDown()to avoid cross-test contamination. - Direct vs Relay: Private messages target a specific peer when adjacent; otherwise they are surfaced to neighbors for relay and, if known, also delivered to the target.
Quick Start
- Create nodes and connect them:
let svc = MockBLEService(); svc.myPeerID = "PEER1"svc.simulateConnectedPeer("PEER2")
- Observe messages:
svc.messageDeliveryHandler = { msg in /* asserts */ }
- Enable broadcast flooding for Integration suites only:
MockBLEService.autoFloodEnabled = true