Run a virtual iPhone on an Apple Silicon Mac.
Swift
import Foundation
import Testing
@testable import VPhoneCoreKit
// Serialized: defaultRootHonorsEnvOverride / defaultRootIsShellSafe mutate the
// process-global VPHONE_LIBRARY_ROOT; run in parallel they race (a set/unset
// from one can land inside the other's assertion).
@Suite(.serialized)
struct LibraryTests {
private func makeRoot() throws -> URL {
let dir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
return dir
}
private func writeBundle(_ name: String, in root: URL) throws {
let dir = root.appendingPathComponent(name)
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
let manifest = VPhoneVirtualMachineManifest(
cpuCount: 8,
memorySize: 8 * 1024 * 1024 * 1024,
romImages: .init(avpBooter: "AVPBooter.vresearch1.bin", avpSEPBooter: "AVPSEPBooter.vresearch1.bin"),
)
try manifest.write(to: dir.appendingPathComponent("config.plist"))
}
@Test func `scans only dirs with manifest`() throws {
let root = try makeRoot()
defer { try? FileManager.default.removeItem(at: root) }
try writeBundle("alpha", in: root)
try writeBundle("beta", in: root)
// A stray dir without config.plist must be ignored.
try FileManager.default.createDirectory(
at: root.appendingPathComponent("junk"),
withIntermediateDirectories: true,
)
let names = try VPhoneLibrary(root: root).bundles().map(\.name)
#expect(names == ["alpha", "beta"])
}
@Test func `bundle named throws when missing`() throws {
let root = try makeRoot()
defer { try? FileManager.default.removeItem(at: root) }
#expect(throws: VPhoneLibraryError.self) {
_ = try VPhoneLibrary(root: root).bundle(named: "nope")
}
}
/// These three go through `ProcessEnvironment` rather than calling `setenv`
/// and `unsetenv` directly. `.serialized` on this suite orders these tests
/// against each other, but `ResourcesTests` drives the same two variables
/// from its own serialized suite, and nothing ordered the two suites — so
/// the bare `unsetenv` that used to open `defaultRootIsShellSafe` could
/// clear `VPHONE_ROOT` in the middle of a ResourcesTests assertion.
@Test func `default root honors env override`() {
ProcessEnvironment.withOverrides(["VPHONE_LIBRARY_ROOT": "/tmp/vphone-test-root"]) {
#expect(VPhoneLibrary.defaultRoot().path == "/tmp/vphone-test-root")
}
}
@Test func `default root honors VPHONE root`() {
ProcessEnvironment.withOverrides([
"VPHONE_LIBRARY_ROOT": nil,
"VPHONE_ROOT": "/tmp/vphone-test-root",
]) {
#expect(VPhoneLibrary.defaultRoot().path == "/tmp/vphone-test-root/machines")
}
}
@Test func `default root is shell safe`() {
// The default root feeds the shell/make firmware pipeline; a space in it
// (e.g. "Application Support") breaks unquoted expansion. Must stay space-free.
ProcessEnvironment.withOverrides([
"VPHONE_LIBRARY_ROOT": nil,
"VPHONE_ROOT": nil,
]) {
#expect(!VPhoneLibrary.defaultRoot().path.contains(" "))
}
}
@Test func `scan reports corrupt bundles instead of dropping`() throws {
let root = try makeRoot()
defer { try? FileManager.default.removeItem(at: root) }
try writeBundle("good", in: root)
// A directory WITH config.plist but corrupt contents must be reported, not silently dropped.
let bad = root.appendingPathComponent("bad")
try FileManager.default.createDirectory(at: bad, withIntermediateDirectories: true)
try Data("not a plist".utf8).write(to: bad.appendingPathComponent("config.plist"))
let result = try VPhoneLibrary(root: root).scan()
#expect(result.bundles.map(\.name) == ["good"])
#expect(result.skipped.map(\.name) == ["bad"])
}
}