Files
bitchat/localPackages/BitFoundation/Tests/BitFoundationTests/PeerCapabilitiesTests.swift
T
jackandGitHub 78a81e5b57 Make ordinary Noise reconnects atomic and race-safe (#1463)
Replaces destroy-then-rebuild Noise re-handshakes with an atomic reconnect protocol: prepared XX message-1 handoff tokens (claim-once, invalidated by crossed inbound initiations), receive-only quarantine of the established transport while an inbound replacement proves identity (promote on success, rollback+cooldown on failure/timeout), deterministic lower-peerID-wins crossed-initiator resolution, and a per-link-epoch BLE revalidation policy that re-proves cached sessions inside the same bleQueue critical section as the link rebind. On main, a fresh msg1 simply destroys an established session and rekey spans two non-atomic barriers.

Includes the review fix: timeout-restores defer outbound queue draining until the convergence retry completes (restore reason plumbed end-to-end), so DMs are never drained under keys a restarted peer already discarded — with a deterministic interleaving test. Identity-mismatch restores drain immediately. Full local suite 1768 tests green.
2026-07-26 13:04:56 +02:00

49 lines
1.8 KiB
Swift

//
// PeerCapabilitiesTests.swift
// bitchatTests
//
// This is free and unencumbered software released into the public domain.
// For more information, see <https://unlicense.org>
//
import Testing
import Foundation
@testable import BitFoundation
struct PeerCapabilitiesTests {
@Test
func encodingIsMinimalAndRoundTrips() {
#expect(PeerCapabilities([]).encoded() == Data([0x00]))
#expect(PeerCapabilities.prekeys.encoded() == Data([0x01]))
#expect(PeerCapabilities.meshDiagnostics.encoded() == Data([0x40]))
#expect(PeerCapabilities.privateMedia.encoded() == Data([0x00, 0x01]))
let high = PeerCapabilities(rawValue: 1 << 9)
#expect(high.encoded() == Data([0x00, 0x02]))
#expect(
PeerCapabilities.nonDestructiveNoiseReplacement.encoded()
== Data([0x00, 0x04])
)
let all: PeerCapabilities = [.prekeys, .wifiBulk, .gateway, .groups, .board, .vouch, .meshDiagnostics, .privateMedia]
#expect(PeerCapabilities(encoded: all.encoded()) == all)
#expect(PeerCapabilities(encoded: high.encoded()) == high)
#expect(PeerCapabilities(encoded: PeerCapabilities([]).encoded()) == [])
}
@Test
func decodingToleratesUnknownBitsAndOversizedFields() {
// Unknown bits survive a round-trip untouched.
let unknown = PeerCapabilities(encoded: Data([0xFF, 0xFF]))
#expect(unknown.rawValue == 0xFFFF)
#expect(unknown.contains(.gateway))
// Fields longer than 8 bytes keep the low 64 bits and ignore the rest.
let oversized = Data([0x01] + [UInt8](repeating: 0x00, count: 7) + [0xAA, 0xBB])
#expect(PeerCapabilities(encoded: oversized) == .prekeys)
// Empty value decodes to no capabilities.
#expect(PeerCapabilities(encoded: Data()) == [])
}
}