mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 01:05:19 +00:00
Fix warnings and improve autocomplete positioning, add unit tests
- Remove unused variables (beforeCount, beforeFavCount) - Remove Tx Power Level from advertising (not allowed) - Fix autocomplete popup to appear near cursor position - Calculate position based on nickname width and @ location - Add comprehensive unit tests for: - Binary protocol encoding/decoding - Message padding for privacy - Bloom filter duplicate detection - BitchatMessage serialization - Add test target to project.yml
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
import XCTest
|
||||
@testable import bitchat
|
||||
|
||||
class BinaryProtocolTests: XCTestCase {
|
||||
|
||||
func testPacketEncodingDecoding() {
|
||||
// Test basic packet
|
||||
let packet = BitchatPacket(
|
||||
version: 1,
|
||||
type: MessageType.message.rawValue,
|
||||
senderID: Data("testuser".utf8),
|
||||
recipientID: Data("recipient".utf8),
|
||||
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
||||
payload: Data("Hello, World!".utf8),
|
||||
signature: nil,
|
||||
ttl: 5
|
||||
)
|
||||
|
||||
// Encode
|
||||
guard let encoded = packet.toBinaryData() else {
|
||||
XCTFail("Failed to encode packet")
|
||||
return
|
||||
}
|
||||
|
||||
// Decode
|
||||
guard let decoded = BitchatPacket.from(encoded) else {
|
||||
XCTFail("Failed to decode packet")
|
||||
return
|
||||
}
|
||||
|
||||
// Verify
|
||||
XCTAssertEqual(decoded.version, packet.version)
|
||||
XCTAssertEqual(decoded.type, packet.type)
|
||||
XCTAssertEqual(decoded.ttl, packet.ttl)
|
||||
XCTAssertEqual(decoded.timestamp, packet.timestamp)
|
||||
XCTAssertEqual(decoded.payload, packet.payload)
|
||||
}
|
||||
|
||||
func testBroadcastPacket() {
|
||||
let packet = BitchatPacket(
|
||||
type: MessageType.message.rawValue,
|
||||
senderID: Data("sender".utf8),
|
||||
recipientID: SpecialRecipients.broadcast,
|
||||
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
||||
payload: Data("Broadcast message".utf8),
|
||||
signature: nil,
|
||||
ttl: 3
|
||||
)
|
||||
|
||||
guard let encoded = packet.toBinaryData() else {
|
||||
XCTFail("Failed to encode broadcast packet")
|
||||
return
|
||||
}
|
||||
|
||||
guard let decoded = BitchatPacket.from(encoded) else {
|
||||
XCTFail("Failed to decode broadcast packet")
|
||||
return
|
||||
}
|
||||
|
||||
// Verify broadcast recipient
|
||||
XCTAssertEqual(decoded.recipientID, SpecialRecipients.broadcast)
|
||||
}
|
||||
|
||||
func testPacketWithSignature() {
|
||||
let signature = Data(repeating: 0xAB, count: 64)
|
||||
let packet = BitchatPacket(
|
||||
type: MessageType.message.rawValue,
|
||||
senderID: Data("sender".utf8),
|
||||
recipientID: Data("recipient".utf8),
|
||||
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
||||
payload: Data("Signed message".utf8),
|
||||
signature: signature,
|
||||
ttl: 5
|
||||
)
|
||||
|
||||
guard let encoded = packet.toBinaryData() else {
|
||||
XCTFail("Failed to encode signed packet")
|
||||
return
|
||||
}
|
||||
|
||||
guard let decoded = BitchatPacket.from(encoded) else {
|
||||
XCTFail("Failed to decode signed packet")
|
||||
return
|
||||
}
|
||||
|
||||
XCTAssertNotNil(decoded.signature)
|
||||
XCTAssertEqual(decoded.signature, signature)
|
||||
}
|
||||
|
||||
func testInvalidPacketHandling() {
|
||||
// Test empty data
|
||||
XCTAssertNil(BitchatPacket.from(Data()))
|
||||
|
||||
// Test truncated data
|
||||
let truncated = Data(repeating: 0, count: 10)
|
||||
XCTAssertNil(BitchatPacket.from(truncated))
|
||||
|
||||
// Test invalid version
|
||||
var invalidVersion = Data(repeating: 0, count: 100)
|
||||
invalidVersion[0] = 99 // Invalid version
|
||||
XCTAssertNil(BitchatPacket.from(invalidVersion))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user