mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 02:25:20 +00:00
Add comprehensive test suite for bitchat
- Created test utilities and helpers for common test operations - Implemented Binary Protocol tests covering encoding/decoding, compression, and padding - Added Noise Protocol tests for handshake, encryption, and session management - Created Public Chat E2E tests for broadcasting, routing, TTL, and mesh topologies - Implemented Private Chat E2E tests for direct messaging, delivery ACKs, and retry logic - Added Integration tests for multi-peer scenarios, network resilience, and mixed traffic patterns - Created mock implementations for BluetoothMeshService and NoiseSession Test coverage includes: - Protocol layer (binary encoding, message serialization) - Security layer (Noise handshake, encryption/decryption) - Application layer (public/private messaging, delivery tracking) - Network scenarios (mesh topology, partitions, churn) - Performance and stress testing
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
//
|
||||
// MockBluetoothMeshService.swift
|
||||
// bitchatTests
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain.
|
||||
// For more information, see <https://unlicense.org>
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import MultipeerConnectivity
|
||||
@testable import bitchat
|
||||
|
||||
class MockBluetoothMeshService: BluetoothMeshService {
|
||||
var sentMessages: [(message: BitchatMessage, packet: BitchatPacket)] = []
|
||||
var sentPackets: [BitchatPacket] = []
|
||||
var connectedPeers: Set<String> = []
|
||||
var messageDeliveryHandler: ((BitchatMessage) -> Void)?
|
||||
var packetDeliveryHandler: ((BitchatPacket) -> Void)?
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
}
|
||||
|
||||
func simulateConnectedPeer(_ peerID: String) {
|
||||
connectedPeers.insert(peerID)
|
||||
delegate?.bluetoothMeshService(self, didConnectToPeer: peerID, peerInfo: PeerInfo(
|
||||
mcPeerID: MCPeerID(displayName: peerID),
|
||||
peerID: peerID,
|
||||
nickname: "Test User",
|
||||
publicKey: nil,
|
||||
capabilities: PeerCapabilities(supportedProtocolVersions: [1])
|
||||
))
|
||||
}
|
||||
|
||||
func simulateDisconnectedPeer(_ peerID: String) {
|
||||
connectedPeers.remove(peerID)
|
||||
delegate?.bluetoothMeshService(self, didDisconnectFromPeer: peerID)
|
||||
}
|
||||
|
||||
override func sendMessage(_ content: String, mentions: [String], to room: String? = nil, messageID: String? = nil, timestamp: Date? = nil) {
|
||||
let message = BitchatMessage(
|
||||
id: messageID ?? UUID().uuidString,
|
||||
sender: nickname,
|
||||
content: content,
|
||||
timestamp: timestamp ?? Date(),
|
||||
isRelay: false,
|
||||
originalSender: nil,
|
||||
isPrivate: false,
|
||||
recipientNickname: nil,
|
||||
senderPeerID: peerID,
|
||||
mentions: mentions.isEmpty ? nil : mentions
|
||||
)
|
||||
|
||||
if let payload = message.toBinaryPayload() {
|
||||
let packet = BitchatPacket(
|
||||
type: 0x01,
|
||||
senderID: peerID.data(using: .utf8)!,
|
||||
recipientID: nil,
|
||||
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
||||
payload: payload,
|
||||
signature: nil,
|
||||
ttl: 3
|
||||
)
|
||||
|
||||
sentMessages.append((message, packet))
|
||||
sentPackets.append(packet)
|
||||
|
||||
// Simulate local echo
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
self?.delegate?.bluetoothMeshService(self!, didReceiveMessage: message)
|
||||
}
|
||||
|
||||
// Call delivery handler if set
|
||||
messageDeliveryHandler?(message)
|
||||
}
|
||||
}
|
||||
|
||||
override func sendPrivateMessage(_ content: String, to recipientPeerID: String, recipientNickname: String, messageID: String? = nil) {
|
||||
let message = BitchatMessage(
|
||||
id: messageID ?? UUID().uuidString,
|
||||
sender: nickname,
|
||||
content: content,
|
||||
timestamp: Date(),
|
||||
isRelay: false,
|
||||
originalSender: nil,
|
||||
isPrivate: true,
|
||||
recipientNickname: recipientNickname,
|
||||
senderPeerID: peerID,
|
||||
mentions: nil
|
||||
)
|
||||
|
||||
if let payload = message.toBinaryPayload() {
|
||||
let packet = BitchatPacket(
|
||||
type: 0x01,
|
||||
senderID: peerID.data(using: .utf8)!,
|
||||
recipientID: recipientPeerID.data(using: .utf8)!,
|
||||
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
||||
payload: payload,
|
||||
signature: nil,
|
||||
ttl: 3
|
||||
)
|
||||
|
||||
sentMessages.append((message, packet))
|
||||
sentPackets.append(packet)
|
||||
|
||||
// Simulate local echo
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
self?.delegate?.bluetoothMeshService(self!, didReceiveMessage: message)
|
||||
}
|
||||
|
||||
// Call delivery handler if set
|
||||
messageDeliveryHandler?(message)
|
||||
}
|
||||
}
|
||||
|
||||
func simulateIncomingMessage(_ message: BitchatMessage) {
|
||||
delegate?.bluetoothMeshService(self, didReceiveMessage: message)
|
||||
}
|
||||
|
||||
func simulateIncomingPacket(_ packet: BitchatPacket) {
|
||||
// Process through the actual handling logic
|
||||
if let message = BitchatMessage.fromBinaryPayload(packet.payload) {
|
||||
delegate?.bluetoothMeshService(self, didReceiveMessage: message)
|
||||
}
|
||||
packetDeliveryHandler?(packet)
|
||||
}
|
||||
|
||||
override func getConnectedPeers() -> [String] {
|
||||
return Array(connectedPeers)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
//
|
||||
// MockNoiseSession.swift
|
||||
// bitchatTests
|
||||
//
|
||||
// This is free and unencumbered software released into the public domain.
|
||||
// For more information, see <https://unlicense.org>
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CryptoKit
|
||||
@testable import bitchat
|
||||
|
||||
class MockNoiseSession: NoiseSession {
|
||||
var mockState: NoiseSessionState = .uninitialized
|
||||
var shouldFailHandshake = false
|
||||
var shouldFailEncryption = false
|
||||
var handshakeMessages: [Data] = []
|
||||
var encryptedData: [Data] = []
|
||||
var decryptedData: [Data] = []
|
||||
|
||||
override func getState() -> NoiseSessionState {
|
||||
return mockState
|
||||
}
|
||||
|
||||
override func isEstablished() -> Bool {
|
||||
return mockState == .established
|
||||
}
|
||||
|
||||
override func startHandshake() throws -> Data {
|
||||
if shouldFailHandshake {
|
||||
mockState = .failed(NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure")))
|
||||
throw NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure"))
|
||||
}
|
||||
|
||||
mockState = .handshaking
|
||||
let handshakeData = TestHelpers.generateRandomData(length: 32)
|
||||
handshakeMessages.append(handshakeData)
|
||||
return handshakeData
|
||||
}
|
||||
|
||||
override func processHandshakeMessage(_ message: Data) throws -> Data? {
|
||||
if shouldFailHandshake {
|
||||
mockState = .failed(NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure")))
|
||||
throw NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure"))
|
||||
}
|
||||
|
||||
handshakeMessages.append(message)
|
||||
|
||||
// Simulate handshake completion after 2 messages
|
||||
if handshakeMessages.count >= 2 {
|
||||
mockState = .established
|
||||
return nil
|
||||
} else {
|
||||
let response = TestHelpers.generateRandomData(length: 48)
|
||||
handshakeMessages.append(response)
|
||||
return response
|
||||
}
|
||||
}
|
||||
|
||||
override func encrypt(_ plaintext: Data) throws -> Data {
|
||||
if shouldFailEncryption {
|
||||
throw NoiseSessionError.notEstablished
|
||||
}
|
||||
|
||||
guard mockState == .established else {
|
||||
throw NoiseSessionError.notEstablished
|
||||
}
|
||||
|
||||
// Simple mock encryption: prepend magic bytes and append the data
|
||||
var encrypted = Data([0xDE, 0xAD, 0xBE, 0xEF])
|
||||
encrypted.append(plaintext)
|
||||
encryptedData.append(encrypted)
|
||||
return encrypted
|
||||
}
|
||||
|
||||
override func decrypt(_ ciphertext: Data) throws -> Data {
|
||||
if shouldFailEncryption {
|
||||
throw NoiseSessionError.notEstablished
|
||||
}
|
||||
|
||||
guard mockState == .established else {
|
||||
throw NoiseSessionError.notEstablished
|
||||
}
|
||||
|
||||
// Simple mock decryption: remove magic bytes
|
||||
guard ciphertext.count > 4 else {
|
||||
throw TestError.testFailure("Invalid ciphertext")
|
||||
}
|
||||
|
||||
let plaintext = ciphertext.dropFirst(4)
|
||||
decryptedData.append(plaintext)
|
||||
return plaintext
|
||||
}
|
||||
|
||||
override func reset() {
|
||||
mockState = .uninitialized
|
||||
handshakeMessages.removeAll()
|
||||
encryptedData.removeAll()
|
||||
decryptedData.removeAll()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user