44 lines
2 KiB
Swift
44 lines
2 KiB
Swift
import CryptoKit
|
|
import XCTest
|
|
@testable import HoloLakeMobile
|
|
|
|
final class HoloLakeMobileTests: XCTestCase {
|
|
func testPairingCoordinateParsesOnlyCompleteLink() throws {
|
|
let secret = Data(repeating: 7, count: 32).base64URL
|
|
let id = UUID().uuidString.lowercased()
|
|
let url = try XCTUnwrap(URL(string: "hololake://pair?host=192.168.1.8&port=37421&id=\(id)&secret=\(secret)"))
|
|
let value = try PairingDescriptor(url: url)
|
|
XCTAssertEqual(value.host, "192.168.1.8")
|
|
XCTAssertEqual(value.port, 37421)
|
|
XCTAssertEqual(value.pairingID, id)
|
|
XCTAssertEqual(value.secret, Data(repeating: 7, count: 32))
|
|
}
|
|
|
|
func testPairingCoordinateRejectsMissingSecret() throws {
|
|
let url = try XCTUnwrap(URL(string: "hololake://pair?host=127.0.0.1&port=37421&id=\(UUID().uuidString)"))
|
|
XCTAssertThrowsError(try PairingDescriptor(url: url))
|
|
}
|
|
|
|
func testPairingCoordinateRejectsDuplicateKeys() throws {
|
|
let secret = Data(repeating: 7, count: 32).base64URL
|
|
let id = UUID().uuidString.lowercased()
|
|
let url = try XCTUnwrap(URL(string: "hololake://pair?host=192.168.1.8&host=127.0.0.1&port=37421&id=\(id)&secret=\(secret)"))
|
|
XCTAssertThrowsError(try PairingDescriptor(url: url))
|
|
}
|
|
|
|
func testCryptoKitPayloadMatchesServerWireShape() throws {
|
|
let key = SymmetricKey(data: Data(repeating: 3, count: 32))
|
|
let nonce = try ChaChaPoly.Nonce(data: Data(repeating: 9, count: 12))
|
|
let aad = Data("hololake.mobile.pair/v1".utf8)
|
|
let clear = Data("hello".utf8)
|
|
let sealed = try ChaChaPoly.seal(clear, using: key, nonce: nonce, authenticating: aad)
|
|
let wire = sealed.ciphertext + sealed.tag
|
|
XCTAssertEqual(wire.count, clear.count + 16)
|
|
let restored = try ChaChaPoly.open(
|
|
ChaChaPoly.SealedBox(nonce: nonce, ciphertext: wire.dropLast(16), tag: wire.suffix(16)),
|
|
using: key,
|
|
authenticating: aad
|
|
)
|
|
XCTAssertEqual(restored, clear)
|
|
}
|
|
}
|