mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 09:25:19 +00:00
* Add capability bits to announce TLV Announces now carry an optional capabilities TLV (0x05): a little-endian bitfield with named bits for upcoming features (prekeys, wifiBulk, gateway, groups, board, vouch, meshDiagnostics). Old clients skip the unknown TLV; peers without it decode as nil so features can distinguish "legacy peer" from "advertises nothing". PeerCapabilities lives in BitFoundation with a minimal-length encoding that preserves unknown bits for forward compatibility. Peer capabilities are stored in the BLE peer registry on verified announce and exposed via BLEService.peerCapabilities(_:). The local advertisement set is empty until each feature ships its bit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Transitive verification: vouch for verified peers over Noise When a Noise session establishes with a peer I verified and that peer advertises the .vouch capability, send signed attestations (up to 16, most recently verified first, at most once per peer per 24h) for the OTHER fingerprints I verified. Receivers accept vouches only from senders they verified themselves, verify the Ed25519 signature against the sender's announce-bound signing key, and surface the result as a new derived trust tier: vouched (unfilled seal) between casual and trusted. Protocol: - NoisePayloadType.vouch = 0x12 carries a batch of TLV attestations: voucheeFingerprint (32B), voucheeSigningKey (32B), timestamp (uint64 ms BE), Ed25519 signature over "bitchat-vouch-v1" | fingerprint | signingKey | timestamp. The voucher is implicit in the authenticated session. - PeerCapabilities.localSupported now advertises .vouch. Storage (SecureIdentityStateManager / IdentityCache): - vouches keyed by vouchee, capped at 8 vouchers each; validity is recomputed on read (voucher still verified-by-me, < 30 days old), so unverifying a voucher retires their vouches without cascade deletes. - New IdentityCache fields are Optional so pre-existing encrypted caches decode cleanly; TrustLevel.vouched is inserted mid-ladder but raw values are strings, so persisted values are unaffected (and vouched itself is never persisted). - Panic wipe clears vouch state with the rest of the identity cache. UI: unfilled checkmark.seal badge in the mesh peer list (filled seal stays exclusive to verified) and a "vouched for by N people you verified" section with voucher names in FingerprintView; VoiceOver labels and xcstrings entries included. Tests: attestation encode/decode + signature (forged/tampered/expired), accept-policy gates, batch cap, trust-level derivation incl. voucher invalidation, persistence compat, and coordinator exchange/accept policies. Full macOS suite: 1088 tests passing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Fix CI deadlock in vouch tests and live-refresh the fingerprint sheet on vouch acceptance Two fixes for PR #1380 review findings: 1. CI "Run Swift Tests (app)" hang (exit 137): the new SecureIdentityStateManagerVouchTests suite was nonisolated, so Swift Testing ran its tests in parallel on the Swift Concurrency cooperative pool. Each test enqueues a queue.async(.barrier) write (setVerified) and immediately blocks in queue.sync / queue.sync(.barrier) (recordVouch / effectiveTrustLevel). On CI's few-core runners every cooperative-pool thread ended up parked behind a pending barrier that never got a dispatch worker, deadlocking the whole test process until the watchdog SIGKILLed it. The suite is now @MainActor, matching the production isolation of the vouch API (ChatVouchCoordinator is @MainActor) and keeping blocking syncs off the cooperative pool. 2. Codex P2: an open fingerprint sheet did not refresh its vouched badge when a vouch batch was accepted - VerificationModel.bind() never observed the trust-change signal. It now subscribes to the "peerStatusUpdated" notification that ChatVouchCoordinator.notifyPeerTrustChanged() posts (same source PeerListModel uses) and forwards it to objectWillChange. Added a regression test that pins VerificationModel's own subscription (verified to fail without the fix). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Skip media-wipe detached tasks under tests (shared-filesystem race) panicClearAllData and clearCurrentPublicTimeline delete the real ~/Library/Application Support/files tree in detached utility-priority tasks. The SPM test process shares that tree and ChatViewModelTests invoke both methods, so under parallel scheduling the wipe lands at a nondeterministic time — deleting media a concurrently running test just wrote (and the developer's real app data with it). Guard both with the existing TestEnvironment.isRunningTests pattern, mirroring the same fix on feat/mesh-diagnostics (#1377). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Port vouch capability-race fix to feat/vouching (ports b8adcbe9) Ports the on-device-confirmed fix from the integration test branch (commit b8adcbe9) onto feat/vouching so PR #1380 is actually correct. On-device testing confirmed the transitive vouch propagated once the send was triggered on verify / announce arrival rather than auth alone. Vouch attestations only ever sent from peerAuthenticated, gated on the peer's .vouch capability. That capability arrives via the peer's announce, processed independently of the Noise handshake, so at auth time the set was usually empty -> gate failed -> vouch silently skipped and never retried. - Refactor the send path into a reusable attemptVouch(to:fingerprint:now:). - Trigger on peer-list updates (peersUpdated): fired after every verified announce, so the batch goes out once the .vouch bit actually arrives. - Trigger on local verification (vouchToConnectedVerifiedPeers): verifying a peer runs a vouch pass over connected verified peers, covering the verify-while-connected case and propagating the new identity onward. - Relax the capability gate: treat an empty/unknown set as eligible (the Noise 0x12 payload is ignored by non-supporting peers); only skip when a non-empty set explicitly lacks .vouch. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
325 lines
14 KiB
Swift
325 lines
14 KiB
Swift
//
|
|
// ChatVerificationCoordinatorContextTests.swift
|
|
// bitchatTests
|
|
//
|
|
// Exercises `ChatVerificationCoordinator` against a mock
|
|
// `ChatVerificationContext` — proving the coordinator works without a
|
|
// `ChatViewModel`, following the `ChatDeliveryCoordinatorContextTests` /
|
|
// `ChatPrivateConversationCoordinatorContextTests` exemplars.
|
|
//
|
|
// Scope note: `handleVerifyResponsePayload` requires a real Ed25519
|
|
// signature; it remains covered by the full view-model/integration tests.
|
|
// Challenge handling, QR kickoff, fingerprint verification, verified-set
|
|
// loading, and the mutual-verification notification (posted through the
|
|
// injected context) are covered here (`VerificationService.shared` is only
|
|
// used for pure payload build/parse).
|
|
//
|
|
|
|
import Testing
|
|
import Foundation
|
|
import BitFoundation
|
|
@testable import bitchat
|
|
|
|
// MARK: - Mock Context
|
|
|
|
/// Lightweight stand-in for `ChatVerificationContext` proving that
|
|
/// `ChatVerificationCoordinator` is testable without a `ChatViewModel`.
|
|
@MainActor
|
|
private final class MockChatVerificationContext: ChatVerificationContext {
|
|
// Fingerprints & verification state
|
|
var fingerprintsByPeerID: [PeerID: String] = [:]
|
|
var verifiedFingerprints: Set<String> = []
|
|
var persistedFingerprints: Set<String> = []
|
|
private(set) var identityVerifiedCalls: [(fingerprint: String, verified: Bool)] = []
|
|
private(set) var storedVerifiedCalls: [(fingerprint: String, verified: Bool)] = []
|
|
private(set) var saveIdentityStateCount = 0
|
|
|
|
func getFingerprint(for peerID: PeerID) -> String? { fingerprintsByPeerID[peerID] }
|
|
func persistedVerifiedFingerprints() -> Set<String> { persistedFingerprints }
|
|
|
|
func setIdentityVerified(fingerprint: String, verified: Bool) {
|
|
identityVerifiedCalls.append((fingerprint, verified))
|
|
}
|
|
|
|
func setStoredVerified(_ fingerprint: String, verified: Bool) {
|
|
storedVerifiedCalls.append((fingerprint, verified))
|
|
}
|
|
|
|
func isVerifiedFingerprint(_ fingerprint: String) -> Bool {
|
|
verifiedFingerprints.contains(fingerprint)
|
|
}
|
|
|
|
func saveIdentityState() { saveIdentityStateCount += 1 }
|
|
|
|
private(set) var vouchToConnectedVerifiedPeersCount = 0
|
|
func vouchToConnectedVerifiedPeers() { vouchToConnectedVerifiedPeersCount += 1 }
|
|
|
|
// Encryption status
|
|
private(set) var encryptionStatuses: [PeerID: EncryptionStatus?] = [:]
|
|
private(set) var updatedEncryptionStatusPeers: [PeerID] = []
|
|
private(set) var invalidatedEncryptionCachePeers: [PeerID?] = []
|
|
private(set) var notifyUIChangedCount = 0
|
|
|
|
func setEncryptionStatus(_ status: EncryptionStatus?, for peerID: PeerID) {
|
|
encryptionStatuses[peerID] = status
|
|
}
|
|
|
|
func updateEncryptionStatus(for peerID: PeerID) {
|
|
updatedEncryptionStatusPeers.append(peerID)
|
|
}
|
|
|
|
func invalidateEncryptionCache(for peerID: PeerID?) {
|
|
invalidatedEncryptionCachePeers.append(peerID)
|
|
}
|
|
|
|
func notifyUIChanged() { notifyUIChangedCount += 1 }
|
|
|
|
// Peers
|
|
var unifiedPeers: [BitchatPeer] = []
|
|
var unifiedFavorites: [BitchatPeer] = []
|
|
private(set) var stablePeerIDCache: [PeerID: PeerID] = [:]
|
|
|
|
func unifiedPeer(for peerID: PeerID) -> BitchatPeer? {
|
|
unifiedPeers.first { $0.peerID == peerID }
|
|
}
|
|
|
|
func unifiedFingerprint(for peerID: PeerID) -> String? { fingerprintsByPeerID[peerID] }
|
|
func resolveNickname(for peerID: PeerID) -> String { "anon\(peerID.id.prefix(4))" }
|
|
func cachedStablePeerID(for shortPeerID: PeerID) -> PeerID? { stablePeerIDCache[shortPeerID] }
|
|
|
|
func cacheStablePeerID(_ stablePeerID: PeerID, for shortPeerID: PeerID) {
|
|
stablePeerIDCache[shortPeerID] = stablePeerID
|
|
}
|
|
|
|
// Noise sessions & verification transport
|
|
var myNoiseStaticKey = Data(repeating: 0x42, count: 32)
|
|
var establishedNoiseSessions: Set<PeerID> = []
|
|
var noiseSessionKeysByPeerID: [PeerID: Data] = [:]
|
|
private(set) var installedCallbacks: (onPeerAuthenticated: (PeerID, String) -> Void, onHandshakeRequired: (PeerID) -> Void)?
|
|
private(set) var triggeredHandshakes: [PeerID] = []
|
|
private(set) var sentChallenges: [(peerID: PeerID, noiseKeyHex: String, nonceA: Data)] = []
|
|
private(set) var sentResponses: [(peerID: PeerID, noiseKeyHex: String, nonceA: Data)] = []
|
|
|
|
func installNoiseSessionCallbacks(
|
|
onPeerAuthenticated: @escaping (PeerID, String) -> Void,
|
|
onHandshakeRequired: @escaping (PeerID) -> Void
|
|
) {
|
|
installedCallbacks = (onPeerAuthenticated, onHandshakeRequired)
|
|
}
|
|
|
|
func noiseSessionPublicKeyData(for peerID: PeerID) -> Data? { noiseSessionKeysByPeerID[peerID] }
|
|
func noiseStaticPublicKeyData() -> Data { myNoiseStaticKey }
|
|
func hasEstablishedNoiseSession(with peerID: PeerID) -> Bool {
|
|
establishedNoiseSessions.contains(peerID)
|
|
}
|
|
func triggerHandshake(with peerID: PeerID) { triggeredHandshakes.append(peerID) }
|
|
|
|
func sendVerifyChallenge(to peerID: PeerID, noiseKeyHex: String, nonceA: Data) {
|
|
sentChallenges.append((peerID, noiseKeyHex, nonceA))
|
|
}
|
|
|
|
func sendVerifyResponse(to peerID: PeerID, noiseKeyHex: String, nonceA: Data) {
|
|
sentResponses.append((peerID, noiseKeyHex, nonceA))
|
|
}
|
|
|
|
// Notifications
|
|
private(set) var postedLocalNotifications: [(title: String, body: String, identifier: String)] = []
|
|
|
|
func postLocalNotification(title: String, body: String, identifier: String) {
|
|
postedLocalNotifications.append((title, body, identifier))
|
|
}
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
/// Builds the raw verify-challenge TLV as it arrives at the coordinator
|
|
/// (i.e. with the `NoisePayload` type byte already stripped).
|
|
private func makeVerifyChallengeTLV(noiseKeyHex: String, nonceA: Data) -> Data {
|
|
var tlv = Data()
|
|
tlv.append(0x01)
|
|
tlv.append(UInt8(noiseKeyHex.count))
|
|
tlv.append(Data(noiseKeyHex.utf8))
|
|
tlv.append(0x02)
|
|
tlv.append(UInt8(nonceA.count))
|
|
tlv.append(nonceA)
|
|
return tlv
|
|
}
|
|
|
|
private func makeVerificationQR(noiseKeyHex: String) -> VerificationService.VerificationQR {
|
|
VerificationService.VerificationQR(
|
|
v: 1,
|
|
noiseKeyHex: noiseKeyHex,
|
|
signKeyHex: "00" + String(repeating: "ab", count: 31),
|
|
npub: nil,
|
|
nickname: "alice",
|
|
ts: 0,
|
|
nonceB64: "",
|
|
sigHex: ""
|
|
)
|
|
}
|
|
|
|
// MARK: - Coordinator Tests Against Mock Context
|
|
|
|
/// Exercises `ChatVerificationCoordinator` against
|
|
/// `MockChatVerificationContext` with no `ChatViewModel`.
|
|
struct ChatVerificationCoordinatorContextTests {
|
|
|
|
@Test @MainActor
|
|
func verifyAndUnverifyFingerprint_updateBothStoresAndStatus() async {
|
|
let context = MockChatVerificationContext()
|
|
let coordinator = ChatVerificationCoordinator(context: context)
|
|
let peerID = PeerID(str: "1122334455667788")
|
|
|
|
// Unknown fingerprint: nothing happens.
|
|
coordinator.verifyFingerprint(for: peerID)
|
|
#expect(context.identityVerifiedCalls.isEmpty)
|
|
|
|
context.fingerprintsByPeerID[peerID] = "fp"
|
|
coordinator.verifyFingerprint(for: peerID)
|
|
coordinator.unverifyFingerprint(for: peerID)
|
|
|
|
#expect(context.identityVerifiedCalls.map(\.fingerprint) == ["fp", "fp"])
|
|
#expect(context.identityVerifiedCalls.map(\.verified) == [true, false])
|
|
#expect(context.storedVerifiedCalls.map(\.verified) == [true, false])
|
|
#expect(context.saveIdentityStateCount == 2)
|
|
#expect(context.updatedEncryptionStatusPeers == [peerID, peerID])
|
|
}
|
|
|
|
@Test @MainActor
|
|
func beginQRVerification_sendsChallengeOrTriggersHandshake() async {
|
|
let context = MockChatVerificationContext()
|
|
let coordinator = ChatVerificationCoordinator(context: context)
|
|
let noiseKey = Data(repeating: 0xCD, count: 32)
|
|
let peerID = PeerID(str: "1122334455667788")
|
|
let qr = makeVerificationQR(noiseKeyHex: noiseKey.hexEncodedString())
|
|
|
|
// No matching peer -> not started.
|
|
#expect(!coordinator.beginQRVerification(with: qr))
|
|
|
|
// Matching peer without an established session -> handshake first.
|
|
context.unifiedPeers = [BitchatPeer(peerID: peerID, noisePublicKey: noiseKey, nickname: "alice")]
|
|
#expect(coordinator.beginQRVerification(with: qr))
|
|
#expect(context.triggeredHandshakes == [peerID])
|
|
#expect(context.sentChallenges.isEmpty)
|
|
|
|
// Already pending -> short-circuits without re-triggering.
|
|
#expect(coordinator.beginQRVerification(with: qr))
|
|
#expect(context.triggeredHandshakes == [peerID])
|
|
|
|
// Fresh coordinator with an established session -> immediate challenge.
|
|
let context2 = MockChatVerificationContext()
|
|
context2.unifiedPeers = [BitchatPeer(peerID: peerID, noisePublicKey: noiseKey, nickname: "alice")]
|
|
context2.establishedNoiseSessions = [peerID]
|
|
let coordinator2 = ChatVerificationCoordinator(context: context2)
|
|
#expect(coordinator2.beginQRVerification(with: qr))
|
|
#expect(context2.sentChallenges.count == 1)
|
|
#expect(context2.sentChallenges.first?.noiseKeyHex == qr.noiseKeyHex)
|
|
#expect(context2.triggeredHandshakes.isEmpty)
|
|
}
|
|
|
|
@Test @MainActor
|
|
func handleVerifyChallengePayload_respondsOncePerNonceForOurKeyOnly() async {
|
|
let context = MockChatVerificationContext()
|
|
let coordinator = ChatVerificationCoordinator(context: context)
|
|
let peerID = PeerID(str: "1122334455667788")
|
|
let myHex = context.myNoiseStaticKey.hexEncodedString()
|
|
let nonce = Data(repeating: 0x07, count: 16)
|
|
let payload = makeVerifyChallengeTLV(noiseKeyHex: myHex, nonceA: nonce)
|
|
|
|
coordinator.handleVerifyChallengePayload(from: peerID, payload: payload)
|
|
#expect(context.sentResponses.count == 1)
|
|
#expect(context.sentResponses.first?.noiseKeyHex.lowercased() == myHex)
|
|
#expect(context.sentResponses.first?.nonceA == nonce)
|
|
|
|
// Same nonce again: deduplicated, no second response.
|
|
coordinator.handleVerifyChallengePayload(from: peerID, payload: payload)
|
|
#expect(context.sentResponses.count == 1)
|
|
|
|
// A challenge for someone else's key is ignored.
|
|
let otherHex = Data(repeating: 0x99, count: 32).hexEncodedString()
|
|
let otherPayload = makeVerifyChallengeTLV(
|
|
noiseKeyHex: otherHex,
|
|
nonceA: Data(repeating: 0x08, count: 16)
|
|
)
|
|
coordinator.handleVerifyChallengePayload(from: peerID, payload: otherPayload)
|
|
#expect(context.sentResponses.count == 1)
|
|
}
|
|
|
|
@Test @MainActor
|
|
func loadVerifiedFingerprints_syncsPersistedSetAndRefreshesUI() async {
|
|
let context = MockChatVerificationContext()
|
|
let coordinator = ChatVerificationCoordinator(context: context)
|
|
context.persistedFingerprints = ["fp1", "fp2"]
|
|
|
|
coordinator.loadVerifiedFingerprints()
|
|
|
|
#expect(context.verifiedFingerprints == ["fp1", "fp2"])
|
|
#expect(context.invalidatedEncryptionCachePeers == [nil])
|
|
#expect(context.notifyUIChangedCount == 1)
|
|
}
|
|
|
|
@Test @MainActor
|
|
func installedNoiseCallbacks_publishStatusAndStableIDs() async {
|
|
let context = MockChatVerificationContext()
|
|
let coordinator = ChatVerificationCoordinator(context: context)
|
|
let peerID = PeerID(str: "1122334455667788")
|
|
let noiseKey = Data(repeating: 0x33, count: 32)
|
|
context.noiseSessionKeysByPeerID[peerID] = noiseKey
|
|
context.verifiedFingerprints = ["fp-verified"]
|
|
|
|
coordinator.setupNoiseCallbacks()
|
|
let callbacks = try? #require(context.installedCallbacks)
|
|
|
|
// Authenticated with a verified fingerprint -> verified status and a
|
|
// cached stable peer ID derived from the session key.
|
|
callbacks?.onPeerAuthenticated(peerID, "fp-verified")
|
|
await waitForMainQueue()
|
|
#expect(context.encryptionStatuses[peerID] == .noiseVerified)
|
|
#expect(context.stablePeerIDCache[peerID] == PeerID(hexData: noiseKey))
|
|
#expect(context.invalidatedEncryptionCachePeers.contains(peerID))
|
|
|
|
// Handshake required -> handshaking status.
|
|
callbacks?.onHandshakeRequired(peerID)
|
|
await waitForMainQueue()
|
|
#expect(context.encryptionStatuses[peerID] == .noiseHandshaking)
|
|
}
|
|
|
|
@Test @MainActor
|
|
func handleVerifyChallengePayload_postsMutualVerificationToastOncePerMinute() async {
|
|
let context = MockChatVerificationContext()
|
|
let coordinator = ChatVerificationCoordinator(context: context)
|
|
let peerID = PeerID(str: "1122334455667788")
|
|
let myHex = context.myNoiseStaticKey.hexEncodedString()
|
|
context.fingerprintsByPeerID[peerID] = "fp-mutual"
|
|
context.verifiedFingerprints = ["fp-mutual"]
|
|
|
|
coordinator.handleVerifyChallengePayload(
|
|
from: peerID,
|
|
payload: makeVerifyChallengeTLV(noiseKeyHex: myHex, nonceA: Data(repeating: 0x07, count: 16))
|
|
)
|
|
|
|
// Already-verified peer challenging us: mutual-verification toast.
|
|
#expect(context.postedLocalNotifications.count == 1)
|
|
#expect(context.postedLocalNotifications.first?.title == "Mutual verification")
|
|
#expect(context.postedLocalNotifications.first?.body.hasSuffix("verified each other") == true)
|
|
#expect(context.postedLocalNotifications.first?.identifier.hasPrefix("verify-mutual-") == true)
|
|
|
|
// A fresh nonce inside the per-fingerprint toast cooldown stays silent.
|
|
coordinator.handleVerifyChallengePayload(
|
|
from: peerID,
|
|
payload: makeVerifyChallengeTLV(noiseKeyHex: myHex, nonceA: Data(repeating: 0x08, count: 16))
|
|
)
|
|
#expect(context.postedLocalNotifications.count == 1)
|
|
#expect(context.sentResponses.count == 2)
|
|
}
|
|
}
|
|
|
|
/// The installed callbacks hop through `DispatchQueue.main.async`; tests must
|
|
/// let that queue drain before asserting.
|
|
@MainActor
|
|
private func waitForMainQueue() async {
|
|
await withCheckedContinuation { continuation in
|
|
DispatchQueue.main.async { continuation.resume() }
|
|
}
|
|
}
|