mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-11-29 08:29:22 +00:00
* Require Noise-authenticated signing keys and valid clock window for prekey bundles * Capture voice note destination at hold start and stop recording immediately on release * Fail private messages exceeding the 255-byte TLV wire limit * Flush 64-hex outbox aliases on reconnect and normalize BLE receipt peer IDs * Defer unbound BLE link binding until announce verification succeeds --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com>
159 lines
6.7 KiB
Swift
159 lines
6.7 KiB
Swift
import BitFoundation
|
|
import Foundation
|
|
import Testing
|
|
@testable import bitchat
|
|
|
|
@Suite(.serialized)
|
|
struct BLEReceiptRoutingTests {
|
|
@Test(arguments: [false, true], [false, true])
|
|
func establishedSessionDeliversReceipt(fullKey: Bool, readReceipt: Bool) async throws {
|
|
let (mesh, a, b) = await onReceiptMeshQueue {
|
|
let mesh = makeSerializedReceiptMesh()
|
|
let a = mesh.addNode(nickname: "receipt-alpha")
|
|
let b = mesh.addNode(nickname: "receipt-beta")
|
|
mesh.connect(0, 1)
|
|
mesh.announceAll()
|
|
mesh.settleUntil {
|
|
a.service.canDeliverSecurely(to: b.service.myPeerID)
|
|
&& b.service.canDeliverSecurely(to: a.service.myPeerID)
|
|
}
|
|
return (mesh, a, b)
|
|
}
|
|
let full = PeerID(hexData: b.service.noiseStaticPublicKeyData())
|
|
#expect(full.toShort() == b.service.myPeerID)
|
|
let destination = fullKey ? full : b.service.myPeerID
|
|
#expect(a.service.isPeerReachable(destination))
|
|
#expect(a.service.canDeliverSecurely(to: destination))
|
|
let capture = BLEReceiptCapture()
|
|
b.service.eventDelegate = capture
|
|
let id = "synthetic-established-receipt"
|
|
if readReceipt {
|
|
let handedOff = await MainActor.run {
|
|
let router = MessageRouter(
|
|
transports: [a.service],
|
|
courierDirectory: CourierDirectory(noiseKey: { _ in nil }, isTrustedCourier: { _ in false })
|
|
)
|
|
return router.sendReadReceipt(
|
|
ReadReceipt(originalMessageID: id, readerID: a.service.myPeerID, readerNickname: "receipt-alpha"),
|
|
to: destination
|
|
)
|
|
}
|
|
#expect(handedOff)
|
|
} else {
|
|
a.service.sendDeliveryAck(for: id, to: destination)
|
|
}
|
|
let received = await pumpReceiptMesh(mesh) { capture.contains(id, read: readReceipt, from: a.service.myPeerID) }
|
|
#expect(received, "Receipt must reach the other service through its Noise receive handler")
|
|
}
|
|
|
|
@Test(arguments: [false, true], [false, true])
|
|
func queuedReceiptDrainsAfterHandshake(fullKey: Bool, readReceipt: Bool) async throws {
|
|
let (mesh, a, b) = await onReceiptMeshQueue {
|
|
let mesh = makeSerializedReceiptMesh()
|
|
let a = mesh.addNode(nickname: "queued-alpha")
|
|
let b = mesh.addNode(nickname: "queued-beta")
|
|
return (mesh, a, b)
|
|
}
|
|
let full = PeerID(hexData: b.service.noiseStaticPublicKeyData())
|
|
#expect(full.toShort() == b.service.myPeerID)
|
|
let destination = fullKey ? full : b.service.myPeerID
|
|
#expect(!a.service.canDeliverSecurely(to: destination))
|
|
let capture = BLEReceiptCapture()
|
|
b.service.eventDelegate = capture
|
|
let id = "synthetic-queued-receipt"
|
|
if readReceipt {
|
|
a.service.sendReadReceipt(
|
|
ReadReceipt(originalMessageID: id, readerID: a.service.myPeerID, readerNickname: "queued-alpha"),
|
|
to: destination
|
|
)
|
|
} else {
|
|
a.service.sendDeliveryAck(for: id, to: destination)
|
|
}
|
|
await onReceiptMeshQueue { mesh.pump() }
|
|
#expect(!capture.contains(id, read: readReceipt, from: a.service.myPeerID))
|
|
await onReceiptMeshQueue {
|
|
mesh.connect(0, 1)
|
|
mesh.announceAll()
|
|
mesh.settleUntil {
|
|
a.service.canDeliverSecurely(to: b.service.myPeerID)
|
|
&& b.service.canDeliverSecurely(to: a.service.myPeerID)
|
|
}
|
|
}
|
|
#expect(a.service.canDeliverSecurely(to: destination))
|
|
#expect(b.service.canDeliverSecurely(to: a.service.myPeerID))
|
|
let received = await pumpReceiptMesh(mesh) { capture.contains(id, read: readReceipt, from: a.service.myPeerID) }
|
|
#expect(received, "Queued receipt must drain when the real peer session establishes")
|
|
}
|
|
|
|
@Test @MainActor
|
|
func unreachableBLEPreservesFullKeyForAlternateTransport() {
|
|
let keychain = MockKeychain()
|
|
let ble = BLEService(
|
|
keychain: keychain,
|
|
idBridge: NostrIdentityBridge(keychain: MockKeychainHelper()),
|
|
identityManager: MockIdentityManager(keychain),
|
|
initializeBluetoothManagers: false
|
|
)
|
|
let fullKey = PeerID(hexData: Data(repeating: 0x42, count: 32))
|
|
let alternate = MockTransport()
|
|
alternate.reachablePeers.insert(fullKey)
|
|
let router = MessageRouter(transports: [ble, alternate])
|
|
let receipt = ReadReceipt(originalMessageID: "alternate-receipt", readerID: ble.myPeerID, readerNickname: "reader")
|
|
#expect(!ble.isPeerReachable(fullKey))
|
|
#expect(router.sendReadReceipt(receipt, to: fullKey))
|
|
#expect(alternate.sentReadReceipts.count == 1)
|
|
#expect(alternate.sentReadReceipts.first?.peerID == fullKey)
|
|
}
|
|
}
|
|
|
|
private final class BLEReceiptCapture: TransportEventDelegate, @unchecked Sendable {
|
|
private let lock = NSLock()
|
|
private var receipts: [(String, Bool, PeerID)] = []
|
|
func didReceiveTransportEvent(_ event: TransportEvent) {
|
|
guard case let .noisePayloadReceived(peerID, type, payload, _) = event,
|
|
type == .readReceipt || type == .delivered,
|
|
let id = String(data: payload, encoding: .utf8) else { return }
|
|
lock.lock()
|
|
receipts.append((id, type == .readReceipt, peerID))
|
|
lock.unlock()
|
|
}
|
|
func contains(_ id: String, read: Bool, from peerID: PeerID) -> Bool {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
return receipts.contains { $0.0 == id && $0.1 == read && $0.2 == peerID }
|
|
}
|
|
}
|
|
|
|
private func onReceiptMeshQueue<T>(_ operation: @escaping () -> T) async -> T {
|
|
await withCheckedContinuation { continuation in
|
|
DispatchQueue.global(qos: .userInitiated).async {
|
|
continuation.resume(returning: operation())
|
|
}
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
private func pumpReceiptMesh(_ mesh: SimulatedMesh, until condition: () -> Bool) async -> Bool {
|
|
let deadline = Date().addingTimeInterval(TestConstants.settleTimeout)
|
|
repeat {
|
|
await onReceiptMeshQueue { mesh.pump() }
|
|
if condition() { return true }
|
|
try? await Task.sleep(nanoseconds: 10_000_000)
|
|
} while Date() < deadline
|
|
return condition()
|
|
}
|
|
|
|
private func makeSerializedReceiptMesh() -> SimulatedMesh {
|
|
SimulatedMesh(packetTransform: { packet in
|
|
do {
|
|
let data = try #require(packet.toBinaryData(
|
|
padding: BLEOutboundPacketPolicy.padsBLEFrame(for: packet.type)
|
|
))
|
|
let decoded: BitchatPacket = try #require(BitchatPacket.from(data))
|
|
return decoded
|
|
} catch {
|
|
return nil
|
|
}
|
|
})
|
|
}
|