Files
bitchat/bitchatTests/Services/BLEPublicMessagePolicyTests.swift
T
7341696280 Expand store-and-forward: open couriers, spray-and-wait, persistent outbox, 6h public history (#1372)
Store-and-forward previously delivered to an out-of-range peer only if a
mutual favorite happened to be connected at send time and later met the
recipient directly, and everything except courier envelopes died with the
app process. This closes those gaps end to end:

- Persist the MessageRouter outbox to disk, sealed with a ChaChaPoly key
  held only in the Keychain (no plaintext at rest); queued private
  messages now survive an app kill and flush on next launch.
- Deposit retry: queued messages are re-deposited whenever a new eligible
  courier connects, tracked per message so the same courier is never
  double-burned, until 3 distinct couriers carry it or it expires.
- Tiered open couriering: signature-verified strangers can now carry mail
  (2 envelopes/depositor into a 20-slot pool) alongside mutual favorites
  (5 each); overflow evicts verified-tier mail before favorites'.
- Spray-and-wait: envelopes carry a copy budget (4, capped 8, new TLV,
  wire-compatible with old clients); couriers split half their remaining
  budget with each newly encountered courier so mail diffuses through a
  moving crowd.
- Remote handover: a verified relayed announce now floods a copy toward
  the multi-hop recipient (directed-relay treatment, 10-min per-envelope
  cooldown) while the carried original stays put for a direct encounter.
- Public history: gossip-sync window for whole public messages widened
  from 15 min to 6 h, matched on the receive-acceptance side, and the
  message store persists to disk so devices bridge partitions and
  restarts ("town crier").
- Privacy-safe local delivery counters (bare tallies, log-only) so the
  store-and-forward stack is measurable on-device.
- Panic wipe now also clears the sealed outbox, gossip archive, and
  counters.
- Rewrite WHITEPAPER.md to describe the app as implemented (Noise XX/X,
  actual flood control, courier system, gossip sync, Nostr path); the old
  document described a bloom filter, three fragment types, and a
  MessageRetryService that don't exist.

1037 macOS tests pass (17 new); iOS builds.

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 19:33:16 +02:00

128 lines
4.0 KiB
Swift

import BitFoundation
import Foundation
import Testing
@testable import bitchat
struct BLEPublicMessagePolicyTests {
@Test
func selfAuthoredNonSyncReplayIsRejected() {
let localPeerID = PeerID(str: "1122334455667788")
let packet = makePacket(sender: localPeerID, ttl: 3)
let decision = BLEPublicMessagePolicy.evaluate(
packet: packet,
from: localPeerID,
localPeerID: localPeerID,
now: Date(timeIntervalSince1970: 1_000)
)
#expect(decision == .reject(.selfEcho))
}
@Test
func selfAuthoredSyncReplayIsAccepted() {
let localPeerID = PeerID(str: "1122334455667788")
let packet = makePacket(sender: localPeerID, ttl: 0)
let decision = BLEPublicMessagePolicy.evaluate(
packet: packet,
from: localPeerID,
localPeerID: localPeerID,
now: Date(timeIntervalSince1970: 1_000)
)
#expect(decision == .accept(BLEPublicMessageAcceptance(shouldTrackForSync: true)))
}
@Test
func staleBroadcastIsRejectedWithAge() {
// The acceptance window matches the gossip public-history window.
let staleAge = TransportConfig.syncPublicMessageMaxAgeSeconds + 1
let now = Date(timeIntervalSince1970: 1_000_000)
let sender = PeerID(str: "8877665544332211")
let packet = makePacket(
sender: sender,
timestamp: UInt64((now.timeIntervalSince1970 - staleAge) * 1000),
recipientID: nil
)
let decision = BLEPublicMessagePolicy.evaluate(
packet: packet,
from: sender,
localPeerID: PeerID(str: "1122334455667788"),
now: now
)
#expect(decision == .reject(.staleBroadcast(ageSeconds: staleAge)))
}
@Test
func staleDirectedMessageIsAcceptedAndNotTrackedForSync() {
let now = Date(timeIntervalSince1970: 1_000)
let sender = PeerID(str: "8877665544332211")
let recipient = PeerID(str: "1122334455667788")
let packet = makePacket(
sender: sender,
timestamp: UInt64((now.timeIntervalSince1970 - 901) * 1000),
recipientID: Data(hexString: recipient.id)
)
let decision = BLEPublicMessagePolicy.evaluate(
packet: packet,
from: sender,
localPeerID: recipient,
now: now
)
#expect(decision == .accept(BLEPublicMessageAcceptance(shouldTrackForSync: false)))
}
@Test
func freshBroadcastMessageIsTrackedForSync() {
let sender = PeerID(str: "8877665544332211")
let packet = makePacket(sender: sender)
let decision = BLEPublicMessagePolicy.evaluate(
packet: packet,
from: sender,
localPeerID: PeerID(str: "1122334455667788"),
now: Date(timeIntervalSince1970: 1_000)
)
#expect(decision == .accept(BLEPublicMessageAcceptance(shouldTrackForSync: true)))
}
@Test
func broadcastNonMessageIsNotTrackedForSync() {
let sender = PeerID(str: "8877665544332211")
let packet = makePacket(sender: sender, type: .leave)
let decision = BLEPublicMessagePolicy.evaluate(
packet: packet,
from: sender,
localPeerID: PeerID(str: "1122334455667788"),
now: Date(timeIntervalSince1970: 1_000)
)
#expect(decision == .accept(BLEPublicMessageAcceptance(shouldTrackForSync: false)))
}
private func makePacket(
sender: PeerID,
type: MessageType = .message,
timestamp: UInt64 = 900_000,
ttl: UInt8 = 3,
recipientID: Data? = nil
) -> BitchatPacket {
BitchatPacket(
type: type.rawValue,
senderID: Data(hexString: sender.id) ?? Data(),
recipientID: recipientID,
timestamp: timestamp,
payload: Data("Hello".utf8),
signature: nil,
ttl: ttl
)
}
}