Files
VirtualBuddy/Packages/MacDebugProtocol/Tests/MacDebugProtocolTests/DebugTransportTests.swift
Guilherme Rambo aabca5342d Xcode app provisioning (#823)
* Automatic provisioning of Mac apps launched via Xcode

* Improve Xcode integration sheet UI

* Security: harden local debugging socket and Xcode tool usage

* Soften rule around Xcode MCP use and xcodebuild prohibition

* Security: harden App Store Connect API key storage and access

* Fix debugged app incorrectly detected as not provisioned due to wildcard matching in provisioning profile

* Add previews for provisioning and debugging UI elements

* Disambiguate virtual machine vs. host Mac in provisioning and signing team management UIs

* Make Xcode debugging setup accessible from the Guest menu

* Completely disable debugging socket when developer features disabled, fix socket dying on accept failure

* Add versioning to scheme bindings, refer to VirtualBuddy components by a stable symlinked location, store scheme URLs so that app can potentially migrate schemes in the future

* Delay showing provisioning sheet so that an app that's already provisioned for the VM doesn't trigger it
2026-11-24 17:46:23 -03:00

65 lines
3.3 KiB
Swift

import Foundation
import Testing
@testable import MacDebugProtocol
struct DebugTransportTests {
@Test func framingAndDuplexRelayPreserveArbitraryBytes() async throws {
func pair() throws -> (DebugSocket, DebugSocket) {
var fds: [Int32] = [-1, -1]
#expect(socketpair(AF_UNIX, SOCK_STREAM, 0, &fds) == 0)
return (try DebugSocket(owning: fds[0]), try DebugSocket(owning: fds[1]))
}
let (client, a) = try pair(), (b, server) = try pair()
let relay = Task { await DebugSocket.relay(a, b) }
let payload = Data((0..<100000).map { UInt8(truncatingIfNeeded: $0) })
async let send: Void = client.send(payload)
#expect(try await server.receive(Data.self) == payload)
try await send
let token = UUID()
try await server.send(token)
#expect(try await client.receive(UUID.self) == token)
client.stop()
await relay.value
#expect(try await server.read(upTo: 1).isEmpty)
}
@Test func cancellingAnUnusedListenerWakesAccept() async throws {
let file = URL(fileURLWithPath: "/private/tmp/" + UUID().uuidString + ".sock")
let listener = try DebugSocket.unix(file, listening: true)
defer { unlink(file.path) }
let task = Task { try await listener.accept(timeout: 5000) }
try await Task.sleep(for: .milliseconds(35))
let start = ContinuousClock.now
task.cancel()
await #expect(throws: (any Error).self) { try await task.value }
#expect(start.duration(to: .now) < .seconds(2))
}
@Test func aLocalPeerIsIdentifiedByItsCodeSignature() async throws {
let file = URL(fileURLWithPath: "/private/tmp/" + UUID().uuidString + ".sock")
let listener = try DebugSocket.unix(file, listening: true)
defer { unlink(file.path) }
async let accepted = listener.accept(timeout: 5000) { _, _ in }
let client = try DebugSocket.unix(file, listening: false)
let server = try await accepted
// Both ends are this test process, which satisfies only its own requirement.
let own = try DebugPeerRequirement.currentApplication()
#expect(throws: Never.self) { try server.requirePeer(own) }
#expect(throws: Never.self) { try client.requirePeer(own) }
let other = try DebugPeerRequirement(#"identifier "com.example.another-app" and anchor apple generic"#)
#expect(throws: (any Error).self) { try server.requirePeer(other) }
#expect(throws: (any Error).self) { try DebugPeerRequirement("not a requirement") }
}
@Test func guestControlledCacheNamesAreConfined() {
let files = [DebugSystemImages.File(name: "dyld_shared_cache_arm64e", bytes: 200), .init(name: "dyld", bytes: 100)]
func check(_ build: String, _ extra: [DebugSystemImages.File]) throws {
try DebugSystemImages(version: "27.0", build: build, architecture: "arm64e", uuid: UUID(), files: files + extra).validate()
}
#expect(throws: Never.self) { try check("26A12", [.init(name: "dyld_shared_cache_arm64e.01", bytes: 64)]) }
#expect(throws: (any Error).self) { try check("../escape", []) }
#expect(throws: (any Error).self) { try check("26A12", [.init(name: "dyld_shared_cache_arm64e.../escape", bytes: 1)]) }
#expect(throws: (any Error).self) { try check("26A12", files) }
}
}