diff --git a/bitchat/Noise/NoiseSessionManager.swift b/bitchat/Noise/NoiseSessionManager.swift index 80b3cc42..1e378f77 100644 --- a/bitchat/Noise/NoiseSessionManager.swift +++ b/bitchat/Noise/NoiseSessionManager.swift @@ -1046,6 +1046,55 @@ final class NoiseSessionManager { requestHandshakeRecovery(for: peerID) } } + + /// Fires a pending ordinary initiator handshake timeout immediately + /// instead of waiting out the real timer. A test stepping a handshake + /// through consecutive synchronous calls loses to any short timeout the + /// moment a starved runner stalls between two of them, so tests inject a + /// timeout no run can outlive and expire the handshake here. + /// + /// Returns whether a timeout was armed, so a test can also assert that + /// none is. + func _test_fireOrdinaryInitiatorTimeout(for peerID: PeerID) -> Bool { + managerQueue.sync(flags: .barrier) { + firePendingTimeoutLocked(ordinaryInitiatorTimeouts[peerID]) + } + } + + /// Responder counterpart of `_test_fireOrdinaryInitiatorTimeout`. Also + /// expires the rollback quarantine, which shares the responder deadline. + func _test_fireOrdinaryResponderTimeout(for peerID: PeerID) -> Bool { + managerQueue.sync(flags: .barrier) { + firePendingTimeoutLocked(ordinaryResponderTimeouts[peerID]) + } + } + + /// The deadline the pending ordinary responder timeout is scheduled for, + /// so tests can assert it is preserved rather than wait it out. + func _test_ordinaryResponderDeadline(for peerID: PeerID) -> DispatchTime? { + managerQueue.sync { + ordinaryResponderDeadlines[peerID] + } + } + + /// Whether a recovery has been requested and not yet prepared or + /// cancelled. The request is recorded before its callback is dispatched, + /// so a test can assert "no further retry" without waiting for one. + func _test_hasPendingHandshakeRecovery(for peerID: PeerID) -> Bool { + managerQueue.sync { + pendingHandshakeRecoveryIDs[peerID] != nil + } + } + + /// Runs the scheduled work item itself, so the hook exercises exactly + /// what the timer would have, then cancels it so the real deadline + /// cannot run it a second time. + private func firePendingTimeoutLocked(_ pending: DispatchWorkItem?) -> Bool { + guard let pending, !pending.isCancelled else { return false } + pending.perform() + pending.cancel() + return true + } #endif private func requestHandshakeRecovery( diff --git a/bitchat/Services/BLE/BLEService.swift b/bitchat/Services/BLE/BLEService.swift index a07e5d31..dfad0459 100644 --- a/bitchat/Services/BLE/BLEService.swift +++ b/bitchat/Services/BLE/BLEService.swift @@ -3251,6 +3251,14 @@ extension BLEService { try noiseService.processHandshakeMessage(from: peerID, message: message) } + /// Expires the pending Noise responder handshake (and its rollback + /// quarantine) now, so a test can inject a deadline no run can outlive + /// instead of racing a short one through its setup handshake. Returns + /// whether a timeout was armed. + func _test_fireNoiseResponderTimeout(for peerID: PeerID) -> Bool { + noiseService._test_fireOrdinaryResponderTimeout(for: peerID) + } + func _test_enqueuePendingPrivateMessage( content: String, messageID: String, diff --git a/bitchat/Services/NoiseEncryptionService.swift b/bitchat/Services/NoiseEncryptionService.swift index 5ee7608d..014786ab 100644 --- a/bitchat/Services/NoiseEncryptionService.swift +++ b/bitchat/Services/NoiseEncryptionService.swift @@ -1093,6 +1093,22 @@ final class NoiseEncryptionService { func _test_fireSuppressedInitiationRecovery(for peerID: PeerID) { sessionManager._test_fireSuppressedInitiationRecovery(for: peerID) } + + func _test_fireOrdinaryInitiatorTimeout(for peerID: PeerID) -> Bool { + sessionManager._test_fireOrdinaryInitiatorTimeout(for: peerID) + } + + func _test_fireOrdinaryResponderTimeout(for peerID: PeerID) -> Bool { + sessionManager._test_fireOrdinaryResponderTimeout(for: peerID) + } + + func _test_ordinaryResponderDeadline(for peerID: PeerID) -> DispatchTime? { + sessionManager._test_ordinaryResponderDeadline(for: peerID) + } + + func _test_hasPendingHandshakeRecovery(for peerID: PeerID) -> Bool { + sessionManager._test_hasPendingHandshakeRecovery(for: peerID) + } #endif deinit { diff --git a/bitchatTests/BLEServiceCoreTests.swift b/bitchatTests/BLEServiceCoreTests.swift index 13f2bb25..c9cad525 100644 --- a/bitchatTests/BLEServiceCoreTests.swift +++ b/bitchatTests/BLEServiceCoreTests.swift @@ -855,7 +855,20 @@ struct BLEServiceCoreTests { /// has to wait for the convergence handshake and use its new session. @Test func timeoutRestoredSessionDefersQueueDrainUntilConvergence() async throws { - let ble = makeService(noiseResponderHandshakeTimeout: 0.3) + // The responder deadline also arms during the setup handshake below, + // where a stalled runner would let a short one tear the half-open + // responder down. It is unlosable here and fired explicitly. + // + // The engine clock is injected for the same reason. With an unlosable + // deadline the quarantine lasts until this test fires it, and on a + // slow runner the real 5s capability-proof watchdog armed at the + // setup authentication can expire inside it. Its drain cannot + // encrypt under a quarantined session and drops the parked payload, + // so the final drain comes up one packet short. + let ble = makeService( + noiseResponderHandshakeTimeout: TestConstants.unlosableInterval, + engineScheduler: BLEEngineManualScheduler() + ) let alice = NoiseEncryptionService(keychain: MockKeychain()) let mallory = NoiseEncryptionService(keychain: MockKeychain()) let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) @@ -929,10 +942,9 @@ struct BLEServiceCoreTests { ttl: 7 ) ble._test_handlePacket(reconnectPacket, fromPeerID: alicePeerID) - // The responder's message 2 is a monotonic quarantine signal; the - // secure-delivery dip itself only lasts until the responder deadline, - // which parallel test load can outrun. (The recovery gate keeps the - // convergence retry's message 1 out of the tap until released.) + // The responder's message 2 signals that the quarantine is in place + // and its deadline armed. (The recovery gate keeps the convergence + // retry's message 1 out of the tap until released.) let responderReady = await TestHelpers.waitUntil( { outbound.count(ofType: .noiseHandshake) >= 1 }, timeout: TestConstants.longTimeout @@ -942,6 +954,7 @@ struct BLEServiceCoreTests { // The responder timeout restores the quarantined generation; the // gate guarantees its handler runs before the convergence retry. + #expect(ble._test_fireNoiseResponderTimeout(for: alicePeerID)) let restoreRan = await TestHelpers.waitUntil( { reconciled.count(for: alicePeerID) == 1 }, timeout: TestConstants.longTimeout @@ -954,8 +967,8 @@ struct BLEServiceCoreTests { #expect(outbound.count(ofType: .noiseEncrypted) == 0) // The capability-proof watchdog armed at the original authentication - // is still live and can genuinely reach its real 5s deadline here on - // a stalled CI runner. Fire it deterministically: its drain must + // is still live, and on the injected clock it expires only when + // fired. Fire it here, while the restore is current: its drain must // respect the deferred-until-convergence state instead of encrypting // the parked queues under the restored keys (the exact silent loss // the defer path exists to prevent). The retry below then still diff --git a/bitchatTests/Services/NoiseEncryptionServiceTests.swift b/bitchatTests/Services/NoiseEncryptionServiceTests.swift index c0cd293f..aa4ca375 100644 --- a/bitchatTests/Services/NoiseEncryptionServiceTests.swift +++ b/bitchatTests/Services/NoiseEncryptionServiceTests.swift @@ -29,7 +29,7 @@ struct NoiseEncryptionServiceTests { @Test("Announce and packet signatures round-trip and detect tampering") func announceAndPacketSignaturesRoundTrip() throws { - let service = NoiseEncryptionService(keychain: MockKeychain()) + let service = makeNoiseService() let signingPublicKey = service.getSigningPublicKeyData() let noisePublicKey = service.getStaticPublicKeyData() @@ -89,8 +89,8 @@ struct NoiseEncryptionServiceTests { @Test("Service-level handshake, encryption, and fingerprint lifecycle work") func handshakeEncryptionAndFingerprintLifecycle() async throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let bob = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let bob = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) let recorder = AuthenticationRecorder() @@ -138,9 +138,9 @@ struct NoiseEncryptionServiceTests { @Test("Handshake rejects a claimed peer ID that does not match the authenticated static key") func handshakeRejectsClaimedPeerIDStaticKeyMismatch() async throws { - let receiver = NoiseEncryptionService(keychain: MockKeychain()) - let claimedAlice = NoiseEncryptionService(keychain: MockKeychain()) - let mallory = NoiseEncryptionService(keychain: MockKeychain()) + let receiver = makeNoiseService() + let claimedAlice = makeNoiseService() + let mallory = makeNoiseService() let receiverPeerID = PeerID(publicKey: receiver.getStaticPublicKeyData()) let claimedAlicePeerID = PeerID(publicKey: claimedAlice.getStaticPublicKeyData()) let recorder = AuthenticationRecorder() @@ -173,9 +173,9 @@ struct NoiseEncryptionServiceTests { @Test("Failed forged reconnect restores the established peer session") func forgedReconnectRestoresEstablishedSession() async throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let receiver = NoiseEncryptionService(keychain: MockKeychain()) - let mallory = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let receiver = makeNoiseService() + let mallory = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let receiverPeerID = PeerID(publicKey: receiver.getStaticPublicKeyData()) let recorder = AuthenticationRecorder() @@ -223,8 +223,8 @@ struct NoiseEncryptionServiceTests { @Test("Valid ordinary rehandshake atomically replaces the established session") func validOrdinaryRehandshakeReplacesEstablishedSession() throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let receiver = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let receiver = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let receiverPeerID = PeerID(publicKey: receiver.getStaticPublicKeyData()) @@ -249,8 +249,8 @@ struct NoiseEncryptionServiceTests { @Test("Automatic rekey exposes and completes its exact handshake bytes") func automaticRekeyHandshakeIsNotStranded() throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let bob = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let bob = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) try establishSessions(alice: alice, bob: bob) @@ -309,8 +309,8 @@ struct NoiseEncryptionServiceTests { @Test("Large private-file payloads use the bounded Noise extension") func largePrivateFileNoiseRoundTrip() throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let bob = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let bob = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) try establishSessions(alice: alice, bob: bob) @@ -358,8 +358,8 @@ struct NoiseEncryptionServiceTests { @Test("Concurrent BLE starts preserve one ordinary attempt") func duplicateHandshakeIfNeededPreservesFirstAttempt() throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let bob = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let bob = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) let starts = HandshakeInitiationRecorder() @@ -401,13 +401,13 @@ struct NoiseEncryptionServiceTests { @Test("Restarted peer establishes against a retained ordinary session") func restartedPeerCompletesRetainedRemoteRehandshake() throws { let aliceKeychain = MockKeychain() - let alice = NoiseEncryptionService(keychain: aliceKeychain) - let bob = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService(keychain: aliceKeychain) + let bob = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) try establishSessions(alice: alice, bob: bob) - let restartedAlice = NoiseEncryptionService(keychain: aliceKeychain) + let restartedAlice = makeNoiseService(keychain: aliceKeychain) let attempt = try #require( try restartedAlice.initiateHandshakeIfNeeded(with: bobPeerID) ) @@ -621,10 +621,7 @@ struct NoiseEncryptionServiceTests { @Test("Ordinary timeout produces exactly one bounded retry") func ordinaryInitiationTimeoutIsBounded() async throws { - let service = NoiseEncryptionService( - keychain: MockKeychain(), - ordinaryHandshakeTimeout: 0.03 - ) + let service = makeNoiseService() let peerID = PeerID(str: "1021324354657687") let recorder = HandshakeStartRecorder() service.onHandshakeRecoveryRequired = { [weak service] request in @@ -650,16 +647,21 @@ struct NoiseEncryptionServiceTests { #expect( service.claimHandshakeInitiation(first, for: peerID) != nil ) + // The unanswered attempt expires and hands over its one retry. The + // recovery callback is dispatched, so wait for it to claim the retry. + #expect(service._test_fireOrdinaryInitiatorTimeout(for: peerID)) let retried = await TestHelpers.waitUntil( { recorder.messages.count == 1 }, - timeout: TestConstants.longTimeout + timeout: TestConstants.settleTimeout ) - #expect(retried) - let retryExpired = await TestHelpers.waitUntil( - { !service.hasSession(with: peerID) }, - timeout: TestConstants.longTimeout - ) - #expect(retryExpired) + try #require(retried) + // The retry expires too, and must not mint another. A request is + // recorded before its callback is dispatched; with nothing left to + // answer it, one minted here would stay on record. + service.onHandshakeRecoveryRequired = nil + #expect(service._test_fireOrdinaryInitiatorTimeout(for: peerID)) + #expect(!service.hasSession(with: peerID)) + #expect(!service._test_hasPendingHandshakeRecovery(for: peerID)) #expect(recorder.timeoutCount == 1) #expect(recorder.errorCount == 0) } @@ -667,10 +669,11 @@ struct NoiseEncryptionServiceTests { @Test("Claim gives an attempt a full on-wire timeout window") func handshakeClaimRearmsDeadline() async throws { let timeoutInterval: TimeInterval = 1 - let service = NoiseEncryptionService( - keychain: MockKeychain(), - ordinaryHandshakeTimeout: timeoutInterval - ) + // test-timing-ok: the real deadline is what this test measures, and + // it is where the initiator timer is seen to fire unprompted. Every + // other test expires it by hook, which would still pass if the timer + // were never scheduled. + let service = NoiseEncryptionService(keychain: MockKeychain(), ordinaryHandshakeTimeout: timeoutInterval) let peerID = PeerID(str: "1021324354657687") let recorder = HandshakeStartRecorder() service.onHandshakeRecoveryRequired = { [weak service] request in @@ -686,14 +689,24 @@ struct NoiseEncryptionServiceTests { ) ) try? await Task.sleep(nanoseconds: 500_000_000) - let claimed = service.claimHandshakeInitiation(attempt, for: peerID) let claimedAt = DispatchTime.now().uptimeNanoseconds - #expect(claimed == attempt.payload) + let claimed = service.claimHandshakeInitiation(attempt, for: peerID) + // A stall that outlasts the first deadline leaves nothing to claim. + // Such a run shows that the timer fires and nothing about re-arming, + // so it is inconclusive below instead of red. + let outrunByFirstDeadline = + claimed == nil && !service.hasSession(with: peerID) + let expired = await TestHelpers.waitUntil( { recorder.timeoutCount == 1 }, - timeout: 5 + timeout: TestConstants.settleTimeout ) #expect(expired) + #expect(!service.hasSession(with: peerID)) + #expect(recorder.timeoutCount == 1) + guard !outrunByFirstDeadline else { return } + + #expect(claimed == attempt.payload) let firedAt = try #require(recorder.firstTimeoutUptimeNanoseconds) try #require(firedAt >= claimedAt) let elapsed = TimeInterval(firedAt - claimedAt) / 1_000_000_000 @@ -701,24 +714,13 @@ struct NoiseEncryptionServiceTests { // claim. Measure on the timeout queue instead of relying on a task to // resume inside a narrow pre-deadline window under parallel CI load. #expect(elapsed >= timeoutInterval * 0.75) - #expect(!service.hasSession(with: peerID)) - #expect(recorder.timeoutCount == 1) } @Test("Duplicate spoofed message one cannot extend rollback or repause during cooldown") func pacedMessageOneCannotHoldOutboundPaused() async throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let bob = NoiseEncryptionService( - keychain: MockKeychain(), - // Generous for the same reason as the quarantine-restore test - // (#1483): this timeout also arms during the `establishSessions` - // setup handshake below, where bob is the responder. At 0.06 a - // preempted runner could fire it mid-setup, tear down the half-open - // responder, and make message 3 be answered as a fresh initiation. - ordinaryResponderHandshakeTimeout: 1.0, - ordinaryReconnectRollbackCooldown: 0.3 - ) - let mallory = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let bob = makeNoiseService() + let mallory = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) let recovery = HandshakeStartRecorder() @@ -735,23 +737,27 @@ struct NoiseEncryptionServiceTests { message: spoofedMessage1 ) ) - // Exercise replacement before yielding: the test runner may resume a - // short sleep after the fixed responder deadline under parallel load. + let rollbackDeadline = try #require( + bob._test_ordinaryResponderDeadline(for: alicePeerID) + ) + // The duplicate replaces the incomplete responder but must keep the + // rollback deadline the first one set. _ = try #require( try bob.processHandshakeMessage( from: alicePeerID, message: spoofedMessage1 ) ) - - let restored = await TestHelpers.waitUntil( - { bob.hasEstablishedSession(with: alicePeerID) }, - timeout: TestConstants.longTimeout + #expect( + bob._test_ordinaryResponderDeadline(for: alicePeerID) + == rollbackDeadline ) - #expect(restored) + + #expect(bob._test_fireOrdinaryResponderTimeout(for: alicePeerID)) + #expect(bob.hasEstablishedSession(with: alicePeerID)) let callbackArrived = await TestHelpers.waitUntil( { recovery.timeoutCount == 1 }, - timeout: TestConstants.longTimeout + timeout: TestConstants.settleTimeout ) #expect(callbackArrived) @@ -778,21 +784,8 @@ struct NoiseEncryptionServiceTests { @Test("Lost reconnect message three restores then retries once") func lostReconnectCompletionGetsOneLocalRetry() async throws { - let alice = NoiseEncryptionService( - keychain: MockKeychain(), - ordinaryHandshakeTimeout: 0.04 - ) - let bob = NoiseEncryptionService( - keychain: MockKeychain(), - ordinaryHandshakeTimeout: 0.04, - // Also arms during the `establishSessions` setup handshake below, - // where bob is the responder. Observed failing on a loaded CI - // runner with exactly the signature #1483 documented: the setup's - // `#expect(finalMessage == nil)` saw a 96-byte message 2, because - // the half-open responder had already been torn down and message 3 - // was answered as a fresh initiation. - ordinaryResponderHandshakeTimeout: 1.0 - ) + let alice = makeNoiseService() + let bob = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) try establishSessions(alice: alice, bob: bob) @@ -823,27 +816,26 @@ struct NoiseEncryptionServiceTests { ) // Drop message 3. Bob restores its old receive-only transport and // initiates one bounded convergence retry; drop that message 1 too. + #expect(bob._test_fireOrdinaryResponderTimeout(for: alicePeerID)) let retryPrepared = await TestHelpers.waitUntil( { recovery.messages.count == 1 }, - timeout: TestConstants.longTimeout + timeout: TestConstants.settleTimeout ) - #expect(retryPrepared) - let retryExpired = await TestHelpers.waitUntil( - { !bob.hasSession(with: alicePeerID) }, - timeout: TestConstants.longTimeout - ) - #expect(retryExpired) + try #require(retryPrepared) + // "Once": a request is recorded before its callback is dispatched; + // with nothing left to answer it, one minted by the expiring retry + // would stay on record. + bob.onHandshakeRecoveryRequired = nil + #expect(bob._test_fireOrdinaryInitiatorTimeout(for: alicePeerID)) + #expect(!bob.hasSession(with: alicePeerID)) + #expect(!bob._test_hasPendingHandshakeRecovery(for: alicePeerID)) #expect(recovery.timeoutCount == 1) #expect(recovery.errorCount == 0) } @Test("Deterministic responder recovers once from an always-yield peer") func yieldedResponderRecoversFromLegacyDoubleYield() async throws { - let timeoutInterval: TimeInterval = 1 - let endpoints = orderedServices( - ordinaryHandshakeTimeout: timeoutInterval, - ordinaryResponderHandshakeTimeout: timeoutInterval - ) + let endpoints = orderedServices() let modern = endpoints.higher let legacy = endpoints.lower let recovery = HandshakeStartRecorder() @@ -918,7 +910,7 @@ struct NoiseEncryptionServiceTests { let recoveryRequested = await TestHelpers.waitUntil( { recovery.requests.count == 1 }, - timeout: 5 + timeout: TestConstants.settleTimeout ) #expect(recoveryRequested) let recoveryRequest = try #require(recovery.requests.first) @@ -944,8 +936,22 @@ struct NoiseEncryptionServiceTests { from: endpoints.higherPeerID, message: retryMessage3 ) - try? await Task.sleep( - nanoseconds: UInt64(timeoutInterval * 1_200_000_000) + // Convergence leaves no handshake timer armed and no recovery + // requested, so nothing remains that could spend a second one. + #expect( + !modern._test_fireOrdinaryInitiatorTimeout( + for: endpoints.lowerPeerID + ) + ) + #expect( + !modern._test_fireOrdinaryResponderTimeout( + for: endpoints.lowerPeerID + ) + ) + #expect( + !modern._test_hasPendingHandshakeRecovery( + for: endpoints.lowerPeerID + ) ) #expect(recovery.timeoutCount == 1) let ciphertext = try modern.encrypt( @@ -964,19 +970,13 @@ struct NoiseEncryptionServiceTests { func immediateLegacyRestartDuringCompletionGrace() async throws { // The grace period must still be open when the restart initiation // arrives below. A small value races the wall clock on a starved - // runner, so inject one no test run can outlive; the recovery half - // is then fired explicitly instead of waiting out the timer. - let unlosableGracePeriod: TimeInterval = 600 + // runner, so the factory's is one no test run can outlive; the + // recovery half is then fired explicitly instead of waiting out the + // timer. let firstKeychain = MockKeychain() let secondKeychain = MockKeychain() - let first = NoiseEncryptionService( - keychain: firstKeychain, - recentInitiatorCompletionGracePeriod: unlosableGracePeriod - ) - let second = NoiseEncryptionService( - keychain: secondKeychain, - recentInitiatorCompletionGracePeriod: unlosableGracePeriod - ) + let first = makeNoiseService(keychain: firstKeychain) + let second = makeNoiseService(keychain: secondKeychain) let firstPeerID = PeerID(publicKey: first.getStaticPublicKeyData()) let secondPeerID = PeerID(publicKey: second.getStaticPublicKeyData()) @@ -1000,7 +1000,7 @@ struct NoiseEncryptionServiceTests { } try establishSessions(alice: lower, bob: higher) - let restartedHigher = NoiseEncryptionService(keychain: higherKeychain) + let restartedHigher = makeNoiseService(keychain: higherKeychain) let recovery = HandshakeStartRecorder() lower.onHandshakeRecoveryRequired = { [weak lower] request in guard let lower else { return } @@ -1078,8 +1078,8 @@ struct NoiseEncryptionServiceTests { @Test("Atomic reconnect retires old sending keys before message one") func atomicReconnectQueuesUntilOrdinaryHandshakeCompletes() throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let bob = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let bob = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) @@ -1117,8 +1117,8 @@ struct NoiseEncryptionServiceTests { @Test("Inbound reconnect quarantines old sending keys until identity proof") func inboundReconnectQuarantinesOldTransport() throws { - let restarted = NoiseEncryptionService(keychain: MockKeychain()) - let retained = NoiseEncryptionService(keychain: MockKeychain()) + let restarted = makeNoiseService() + let retained = makeNoiseService() let restartedPeerID = PeerID(publicKey: restarted.getStaticPublicKeyData()) let retainedPeerID = PeerID(publicKey: retained.getStaticPublicKeyData()) @@ -1178,8 +1178,8 @@ struct NoiseEncryptionServiceTests { @Test("Malformed handshake bytes cannot tear down an established session") func establishedSessionIgnoresNonInitialHandshakeGarbage() throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let bob = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let bob = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) try establishSessions(alice: alice, bob: bob) @@ -1203,9 +1203,9 @@ struct NoiseEncryptionServiceTests { @Test("Forged reconnect restores the quarantined transport") func forgedReconnectRestoresQuarantinedTransport() throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let bob = NoiseEncryptionService(keychain: MockKeychain()) - let mallory = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let bob = makeNoiseService() + let mallory = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) @@ -1250,21 +1250,10 @@ struct NoiseEncryptionServiceTests { } @Test("Lost reconnect completion restores the quarantined transport") - func timedOutReconnectRestoresQuarantinedTransport() async throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - // The injected responder timeout also arms during the ordinary setup - // handshake below (bob is its responder), where the only work between - // message 1 and message 3 is two consecutive synchronous statements. - // It must be generous enough that a preempted runner cannot let the - // timeout fire mid-setup and tear down the half-open responder — at - // 20ms a loaded 2-core CI runner did exactly that, so message 3 was - // answered as a fresh initiation (96-byte message 2) and nothing was - // ever quarantined. - let bob = NoiseEncryptionService( - keychain: MockKeychain(), - ordinaryResponderHandshakeTimeout: 1.0 - ) - let mallory = NoiseEncryptionService(keychain: MockKeychain()) + func timedOutReconnectRestoresQuarantinedTransport() throws { + let alice = makeNoiseService() + let bob = makeNoiseService() + let mallory = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) @@ -1278,15 +1267,10 @@ struct NoiseEncryptionServiceTests { ) #expect(!bob.hasEstablishedSession(with: alicePeerID)) - // Poll instead of sleeping a fixed interval: the responder timeout - // fires on bob's manager queue at the quarantine deadline, and a - // starved runner can delay that work item well past the deadline. - let restored = await TestHelpers.waitUntil( - { bob.hasEstablishedSession(with: alicePeerID) }, - timeout: TestConstants.longTimeout - ) + // Message 3 never arrives; the responder deadline expires. + #expect(bob._test_fireOrdinaryResponderTimeout(for: alicePeerID)) try #require( - restored, + bob.hasEstablishedSession(with: alicePeerID), "Responder timeout should restore the quarantined transport" ) let oldTransport = try alice.encrypt( @@ -1299,10 +1283,33 @@ struct NoiseEncryptionServiceTests { ) } + @Test("Responder timeout expires an unanswered handshake unprompted") + func ordinaryResponderTimeoutFiresOnItsOwn() async throws { + let alice = makeNoiseService() + // test-timing-ok: every other test expires the responder by hook, + // which would still pass if the timer were never scheduled. Nothing + // below has to happen before this deadline, so a stall only delays + // the result. + let bob = NoiseEncryptionService(keychain: MockKeychain(), ordinaryResponderHandshakeTimeout: 0.05) + let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) + let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) + + let message1 = try alice.initiateHandshake(with: bobPeerID) + _ = try #require( + try bob.processHandshakeMessage(from: alicePeerID, message: message1) + ) + + let expired = await TestHelpers.waitUntil( + { !bob.hasSession(with: alicePeerID) }, + timeout: TestConstants.settleTimeout + ) + #expect(expired) + } + @Test("Failed reconnect authorization preserves the established transport") func failedReconnectAuthorizationPreservesSession() throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let bob = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let bob = makeNoiseService() let alicePeerID = PeerID(publicKey: alice.getStaticPublicKeyData()) let bobPeerID = PeerID(publicKey: bob.getStaticPublicKeyData()) @@ -1339,7 +1346,7 @@ struct NoiseEncryptionServiceTests { @Test("Encrypt without a session requests handshake and decrypt without session fails") func handshakeRequiredAndSessionNotEstablishedErrors() throws { - let service = NoiseEncryptionService(keychain: MockKeychain()) + let service = makeNoiseService() let peerID = PeerID(str: "1021324354657687") var requestedPeerID: PeerID? @@ -1367,7 +1374,7 @@ struct NoiseEncryptionServiceTests { @Test("Clearing persistent identity removes saved keys") func clearPersistentIdentityRemovesSavedKeys() { let keychain = MockKeychain() - let service = NoiseEncryptionService(keychain: keychain) + let service = makeNoiseService(keychain: keychain) #expect(service.getStaticPublicKeyData().count == 32) #expect(service.getSigningPublicKeyData().count == 32) @@ -1387,8 +1394,8 @@ struct NoiseEncryptionServiceTests { @Test("Responder completion state spans ordinary XX message three") func responderCompletionStateTracksOrdinaryHandshake() throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let bob = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let bob = makeNoiseService() let alicePeerID = PeerID( publicKey: alice.getStaticPublicKeyData() ) @@ -1430,8 +1437,8 @@ struct NoiseEncryptionServiceTests { @Test("Transport readiness rejection spends no message budget") func transportReadinessRejectionSpendsNoMessageBudget() throws { - let alice = NoiseEncryptionService(keychain: MockKeychain()) - let bob = NoiseEncryptionService(keychain: MockKeychain()) + let alice = makeNoiseService() + let bob = makeNoiseService() let alicePeerID = PeerID( publicKey: alice.getStaticPublicKeyData() ) @@ -1508,29 +1515,41 @@ struct NoiseEncryptionServiceTests { } } -private func orderedServices( - ordinaryHandshakeTimeout: TimeInterval = - NoiseSecurityConstants.ordinaryHandshakeTimeout, - ordinaryResponderHandshakeTimeout: TimeInterval = - NoiseSecurityConstants.ordinaryResponderHandshakeTimeout -) -> ( +/// Builds a service in which no time window can close while a test is still +/// stepping through the state it guards. Tests drive a handshake as +/// consecutive synchronous calls; a starved runner that stalls between two of +/// them lets any window sized to the work close first. A handshake timeout +/// then tears the half-open session down, so message 3 is answered as a fresh +/// initiation. Raising the value (20 ms to 1 s in #1483/#1491) only scaled +/// with the stall, and the production defaults were outrun too (#1737). The +/// completion grace and the rollback cooldown are windows of the same kind. +/// +/// It takes no timing parameters on purpose. A test whose subject is expiry +/// fires it with `_test_fireOrdinaryInitiatorTimeout` / +/// `_test_fireOrdinaryResponderTimeout`. A test that has to watch a real +/// timer builds its own service, which `TestTimingHygieneTests` flags unless +/// it carries a waiver saying why. +private func makeNoiseService( + keychain: KeychainManagerProtocol = MockKeychain() +) -> NoiseEncryptionService { + // test-timing-ok: the one sanctioned construction site. + NoiseEncryptionService( + keychain: keychain, + ordinaryHandshakeTimeout: TestConstants.unlosableInterval, + ordinaryResponderHandshakeTimeout: TestConstants.unlosableInterval, + recentInitiatorCompletionGracePeriod: TestConstants.unlosableInterval, + ordinaryReconnectRollbackCooldown: TestConstants.unlosableInterval + ) +} + +private func orderedServices() -> ( lower: NoiseEncryptionService, lowerPeerID: PeerID, higher: NoiseEncryptionService, higherPeerID: PeerID ) { - let first = NoiseEncryptionService( - keychain: MockKeychain(), - ordinaryHandshakeTimeout: ordinaryHandshakeTimeout, - ordinaryResponderHandshakeTimeout: - ordinaryResponderHandshakeTimeout - ) - let second = NoiseEncryptionService( - keychain: MockKeychain(), - ordinaryHandshakeTimeout: ordinaryHandshakeTimeout, - ordinaryResponderHandshakeTimeout: - ordinaryResponderHandshakeTimeout - ) + let first = makeNoiseService() + let second = makeNoiseService() let firstPeerID = PeerID(publicKey: first.getStaticPublicKeyData()) let secondPeerID = PeerID(publicKey: second.getStaticPublicKeyData()) if firstPeerID < secondPeerID { diff --git a/bitchatTests/TestUtilities/TestConstants.swift b/bitchatTests/TestUtilities/TestConstants.swift index fb0cb65c..b8863779 100644 --- a/bitchatTests/TestUtilities/TestConstants.swift +++ b/bitchatTests/TestUtilities/TestConstants.swift @@ -58,6 +58,21 @@ struct TestConstants { /// expect to succeed reintroduces exactly the flake class it sits next to. static let negativeWaitWindow: TimeInterval = 1.0 + /// For an injected **production timer or time window** that must not + /// elapse while a test is still stepping through the state it guards. + /// + /// A test drives a handshake as consecutive synchronous calls, and the + /// product arms a teardown timer on the first of them. A starved runner + /// can stall between any two calls for longer than any value sized to the + /// work — 1 s was outrun, and so was the 20 s production default — so the + /// timer wins, the half-open state is torn down, and the next call fails + /// in a way that reads like a product bug. Raising the value only scales + /// with the stall. Inject this instead, and where expiry is the behaviour + /// under test, fire it through the product's DEBUG hook. + /// + /// Longer than any CI job is allowed to run, so no test run can outlive it. + static let unlosableInterval: TimeInterval = 3600 + static let testNickname1 = "Alice" static let testNickname2 = "Bob" diff --git a/bitchatTests/TestUtilities/TestTimingHygieneTests.swift b/bitchatTests/TestUtilities/TestTimingHygieneTests.swift index c6a15147..fb469b3c 100644 --- a/bitchatTests/TestUtilities/TestTimingHygieneTests.swift +++ b/bitchatTests/TestUtilities/TestTimingHygieneTests.swift @@ -11,7 +11,7 @@ import Testing /// into a red build that reads like a product bug, and the debugging cost lands /// on whoever opened an unrelated PR. /// -/// Two rules, both enforced below: +/// Three rules, all enforced below: /// /// 1. A wait helper's default deadline must be at least /// `TestConstants.minimumSettleTimeout`. Waits return as soon as their @@ -20,8 +20,11 @@ import Testing /// assertion cannot distinguish the behaviour under test from a slow /// machine, so it can only be flaky. Assert the property somewhere it is /// computable — with an injected clock, on the pure logic — instead. +/// 3. A Noise handshake timeout is never sized to the work. The timer arms +/// on the first handshake message and a stalled runner lets it fire before +/// the next, so it is made unlosable and expiry is fired by hook. /// -/// Both rules can be waived per line with `\(Self.waiver)` plus a reason, for +/// All rules can be waived per line with `\(Self.waiver)` plus a reason, for /// the rare case where the timing itself is genuinely the thing under test. struct TestTimingHygieneTests { /// Opt-out marker. Reviewers should expect a reason next to it. @@ -85,8 +88,7 @@ struct TestTimingHygieneTests { // // Deliberately NOT matched: a bare `timeout:` label on something that is // not a wait, such as the injected production handshake timeouts in the - // Noise tests. Those are the behaviour under test, and a short value is - // correct there. + // Noise tests. A floor is the wrong rule for those; rule 3 covers them. let patterns = [ #"(?:timeout|deadline)\s*:\s*TimeInterval\s*=\s*([0-9]+(?:\.[0-9]+)?)"#, #"(?:wait|waitUntil|waitFor|fulfillment)\s*\([^)]*\btimeout:\s*([0-9]+(?:\.[0-9]+)?)"# @@ -168,6 +170,73 @@ struct TestTimingHygieneTests { ) } + /// Rule 3: a Noise handshake timeout is never sized to the work. + /// + /// Rule 1 deliberately leaves injected production timeouts alone, and + /// that was the blind spot: the ordinary initiator and responder + /// timeouts arm on the first handshake message, and tests step a + /// handshake as consecutive synchronous calls. A runner that stalls + /// between two of them lets the timer tear the half-open session down, + /// and message 3 is then answered as a fresh initiation. It kept + /// recurring while the injected value was raised from 20 ms to 1 s + /// (#1483, #1491), and then outran the 20 s production default (#1737). + /// + /// Services are built with `TestConstants.unlosableInterval` instead, and + /// a test whose subject is expiry fires it through a `_test_fire…Timeout` + /// hook. Two checks hold that in place: + /// + /// - In `NoiseEncryptionServiceTests`, which drives more handshakes than + /// any other suite, a service may only come from the factory. The + /// factory takes no timing parameters, so there a real timer needs a + /// waived construction. + /// - In every test file, a numeric handshake timeout written next to its + /// label is flagged. A value passed through a variable, or wrapped onto + /// the next line, is not seen; outside the file above this check is a + /// tripwire and not a guarantee. + @Test func noiseHandshakeTimeoutsAreNotSizedToTheWork() throws { + let lines = try Self.swiftLines() + + let factoryOnlyFile = "NoiseEncryptionServiceTests.swift" + #expect( + lines.contains { $0.file == factoryOnlyFile }, + "hygiene scan did not find \(factoryOnlyFile) — was it renamed?" + ) + let directConstruction = try NSRegularExpression( + pattern: #"\bNoiseEncryptionService\s*\("# + ) + let literalTimeout = try NSRegularExpression( + pattern: #"[Hh]andshakeTimeout\s*:\s*[0-9]"# + ) + + var offenders: [String] = [] + for line in lines where !Self.isWaived(line) { + let range = NSRange(line.text.startIndex..., in: line.text) + let isOffender = + literalTimeout.firstMatch(in: line.text, range: range) != nil + || (line.file == factoryOnlyFile + && directConstruction.firstMatch( + in: line.text, + range: range + ) != nil) + guard isOffender else { continue } + offenders.append("\(line.file):\(line.number) — \(line.text.trimmingCharacters(in: .whitespaces))") + } + + #expect( + offenders.isEmpty, + """ + A Noise handshake timeout sized to the work will fire mid-handshake \ + on a stalled runner. Build the service with \ + TestConstants.unlosableInterval (in \(factoryOnlyFile), through \ + makeNoiseService) and fire expiry with a _test_fire…Timeout hook, \ + or add "\(Self.waiver) " if the real deadline is what the \ + test measures. + + \(offenders.joined(separator: "\n")) + """ + ) + } + /// The floor must stay meaningfully above the operations being waited on, /// and the default must satisfy the rule this file enforces. @Test func settleTimeoutsAreSelfConsistent() {