Add camera QR pairing, Bonjour discovery and pinned address recovery
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import XCTest
|
||||
import Darwin
|
||||
@testable import MusicBridge
|
||||
|
||||
final class DiscoveryTests: XCTestCase {
|
||||
let pin = String(repeating: "A", count: 64)
|
||||
func testWindowsAdvertisementAndTrustPreservingRelocation() throws {
|
||||
let txt = ["v": Data("1".utf8), "id": Data(pin.lowercased().utf8)]
|
||||
let pc = try XCTUnwrap(DiscoveredComputer.parse(name: "MusicBridge-AAAAAAAAAAAA", host: "192.168.1.23", port: 8765, txt: txt))
|
||||
let saved = try Connection(endpoint: "https://192.168.1.2:8765", fingerprint: pin, token: "secret")
|
||||
let moved = try XCTUnwrap(pc.reconnect(saved))
|
||||
XCTAssertEqual(moved.endpoint.absoluteString, "https://192.168.1.23:8765")
|
||||
XCTAssertEqual(moved.fingerprint, saved.fingerprint)
|
||||
XCTAssertEqual(moved.token, saved.token)
|
||||
let other = try Connection(endpoint: saved.endpoint.absoluteString, fingerprint: String(repeating: "B", count: 64))
|
||||
XCTAssertNil(pc.reconnect(other))
|
||||
}
|
||||
|
||||
func testRejectsPublicAddressesUnsupportedVersionAndBadPins() {
|
||||
let txt = ["v": Data("1".utf8), "id": Data(pin.utf8)]
|
||||
for host in ["8.8.8.8", "127.0.0.1", "localhost", "192.168.1.2/path"] {
|
||||
XCTAssertNil(DiscoveredComputer.parse(name: "PC", host: host, port: 8765, txt: txt))
|
||||
}
|
||||
for port in [0, -1, 65536] {
|
||||
XCTAssertNil(DiscoveredComputer.parse(name: "PC", host: "192.168.1.2", port: port, txt: txt))
|
||||
}
|
||||
XCTAssertNil(DiscoveredComputer.parse(name: "PC", host: "192.168.1.2", port: 8765, txt: ["v": Data("2".utf8), "id": Data(pin.utf8)]))
|
||||
XCTAssertNil(DiscoveredComputer.parse(name: "PC", host: "192.168.1.2", port: 8765, txt: ["v": Data("1".utf8), "id": Data("bad".utf8)]))
|
||||
}
|
||||
|
||||
func testParsesBonjourSocketAddressWithoutAcceptingTruncatedOrIPv6Data() {
|
||||
var address = sockaddr_in()
|
||||
address.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
|
||||
address.sin_family = sa_family_t(AF_INET)
|
||||
_ = "192.168.1.42".withCString { inet_pton(AF_INET, $0, &address.sin_addr) }
|
||||
let data = withUnsafeBytes(of: &address) { Data($0) }
|
||||
XCTAssertEqual(DiscoveryStore.ipv4(data), "192.168.1.42")
|
||||
XCTAssertNil(DiscoveryStore.ipv4(Data(data.prefix(4))))
|
||||
address.sin_family = sa_family_t(AF_INET6)
|
||||
XCTAssertNil(DiscoveryStore.ipv4(withUnsafeBytes(of: &address) { Data($0) }))
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,24 @@ final class ProtocolTests: XCTestCase {
|
||||
XCTAssertFalse(Invitation.validCode("12345678"))
|
||||
}
|
||||
|
||||
func testWindowsInvitationTimestampAndMalformedJSON() throws {
|
||||
// System.Text.Json emits up to seven fractional digits and a numeric UTC offset.
|
||||
let json = """
|
||||
{"version":1,"endpoint":"https://192.168.1.23:8765","certificateSha256":"\(fingerprint)","code":"01234567","expiresAt":"2026-09-12T15:30:00.1234567+03:00"}
|
||||
"""
|
||||
func payload(_ json: String) -> String {
|
||||
"musicbridge://pair?data=" + Data(json.utf8).base64EncodedString()
|
||||
.replacingOccurrences(of: "+", with: "-").replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "=", with: "")
|
||||
}
|
||||
let before = try XCTUnwrap(ISO8601DateFormatter().date(from: "2026-09-12T12:29:00Z"))
|
||||
XCTAssertEqual(try Invitation.parse(payload(json), now: before).code, "01234567")
|
||||
XCTAssertThrowsError(try Invitation.parse(payload(json), now: before.addingTimeInterval(120)))
|
||||
XCTAssertThrowsError(try Invitation.parse(payload("{}"), now: before)) { error in
|
||||
XCTAssertTrue(error is BridgeError)
|
||||
}
|
||||
XCTAssertThrowsError(try Invitation.parse("https://example.com"))
|
||||
}
|
||||
|
||||
func testWindowsSnapshotAndTimeline() throws {
|
||||
let url = try XCTUnwrap(Bundle(for: Self.self).url(forResource: "state-v1", withExtension: "json"))
|
||||
let state = try JSONDecoder().decode(MediaState.self, from: Data(contentsOf: url))
|
||||
|
||||
Reference in New Issue
Block a user