43 lines
2.3 KiB
Swift
43 lines
2.3 KiB
Swift
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) }))
|
|
}
|
|
}
|