Files
bitchat/bitchatTests/GCSFilterTests.swift
T
jackandClaude Fable 5 6bda919dd4 Fix transport reliability gaps: Tor stalls, weak-signal sends, GCS input validation
NostrRelayManager no longer strands work when Tor is slow to bootstrap:
failed readiness waits retry (bounded by nostrTorReadyMaxWaitAttempts)
instead of dropping queued relay connections, parked EOSE callbacks fire
after exhaustion so callers never hang, and sends made before Tor is
ready are queued locally (capped) instead of being dropped on a failed
wait - still strictly fail-closed.

MessageRouter now prefers a connected transport over a merely
window-reachable one, and sends made on a weak reachability signal are
retained in the outbox until a delivery/read ack confirms receipt
(receivers dedup by message ID), with resends bounded by attempt count.

GCS sync filters from the wire are bounds-checked (p in 1...32, m > 1)
at both the packet decode and filter decode layers; oversized Golomb
parameters previously decoded to garbage via silent shift overflow.

BLELinkStateStore is now explicitly pinned to bleQueue: debug builds
trap any access from another queue, enforcing the ownership discipline
the surrounding code already relied on by convention.

CI gains an iOS simulator build job (arm64 only; the vendored Arti
xcframework has no x86_64 simulator slice) so iOS-conditional code
paths are compile-checked - SPM tests only cover the macOS slice.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-10 16:22:35 +01:00

52 lines
2.3 KiB
Swift

import Testing
import struct Foundation.Data
@testable import bitchat
struct GCSFilterTests {
@Test func buildFilterWithDuplicateIdsProducesStableEncoding() {
let id = Data(repeating: 0xAB, count: 16)
let ids = Array(repeating: id, count: 64)
let params = GCSFilter.buildFilter(ids: ids, maxBytes: 128, targetFpr: 0.01)
#expect(params.m >= 1)
let decoded = GCSFilter.decodeToSortedSet(p: params.p, m: params.m, data: params.data)
#expect(decoded.count <= 1)
}
@Test func bucketAvoidsZeroCandidate() {
let id = Data(repeating: 0x01, count: 16)
let bucket = GCSFilter.bucket(for: id, modulus: 2)
#expect(bucket != 0)
#expect(bucket < 2)
}
@Test func decodeRejectsOutOfRangeParameters() {
let junk = Data(repeating: 0xFF, count: 64)
#expect(GCSFilter.decodeToSortedSet(p: 0, m: 1000, data: junk).isEmpty)
#expect(GCSFilter.decodeToSortedSet(p: -1, m: 1000, data: junk).isEmpty)
#expect(GCSFilter.decodeToSortedSet(p: GCSFilter.maxP + 1, m: 1000, data: junk).isEmpty)
#expect(GCSFilter.decodeToSortedSet(p: 255, m: UInt32.max, data: junk).isEmpty)
#expect(GCSFilter.decodeToSortedSet(p: 8, m: 0, data: junk).isEmpty)
#expect(GCSFilter.decodeToSortedSet(p: 8, m: 1, data: junk).isEmpty)
}
@Test func decodeOfTruncatedDataReturnsOnlyCompleteValues() {
let ids = (0..<32).map { i in Data(repeating: UInt8(i), count: 16) }
let params = GCSFilter.buildFilter(ids: ids, maxBytes: 128, targetFpr: 0.01)
let full = GCSFilter.decodeToSortedSet(p: params.p, m: params.m, data: params.data)
let truncated = GCSFilter.decodeToSortedSet(p: params.p, m: params.m, data: params.data.prefix(params.data.count / 2))
#expect(truncated.count <= full.count)
// Truncation must not invent values that were not in the full set.
#expect(truncated.allSatisfy { full.contains($0) })
}
@Test func requestSyncPacketDecodeRejectsOversizedP() {
let valid = RequestSyncPacket(p: 8, m: 4096, data: Data([0x01, 0x02]))
#expect(RequestSyncPacket.decode(from: valid.encode()) != nil)
let oversized = RequestSyncPacket(p: 200, m: 4096, data: Data([0x01, 0x02]))
#expect(RequestSyncPacket.decode(from: oversized.encode()) == nil)
}
}