106 lines
6.9 KiB
Swift
106 lines
6.9 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import XCTest
|
|
|
|
final class BinaryTests: XCTestCase {
|
|
// Authored RAM program: LDA #$6B; STA $5000; BRA *, then transfer to $3000.
|
|
private let binary = Data([0,0,7,0x30,0,0x86,0x6b,0xb7,0x50,0,0x20,0xfe,255,0,0,0x30,0])
|
|
@MainActor private func step(_ operation: (@escaping @Sendable (VCCMachineSnapshot) -> Void) -> Void) async -> VCCMachineSnapshot {
|
|
await withCheckedContinuation { done in operation { done.resume(returning: $0) } }
|
|
}
|
|
@MainActor private func until(_ condition: () -> Bool) async throws {
|
|
for _ in 0..<500 {
|
|
if condition() { return }; try await Task.sleep(nanoseconds: 10_000_000)
|
|
}
|
|
XCTFail("Binary operation timed out"); throw NSError(domain: "TestTimeout", code: 1)
|
|
}
|
|
func testFileBoundsCasePathsAndReadOnlyInput() throws {
|
|
let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
|
|
defer { try? FileManager.default.removeItem(at: folder) }
|
|
let url = folder.appendingPathComponent("Program é.BIN")
|
|
try binary.write(to: url)
|
|
try FileManager.default.setAttributes([.posixPermissions: 0o444], ofItemAtPath: url.path)
|
|
XCTAssertEqual(try BinaryFile.read(url), binary)
|
|
let link = folder.appendingPathComponent("Linked.bin")
|
|
try FileManager.default.createSymbolicLink(at: link, withDestinationURL: url)
|
|
XCTAssertEqual(try BinaryFile.read(link), binary)
|
|
XCTAssertThrowsError(try BinaryFile.read(folder))
|
|
XCTAssertThrowsError(try BinaryFile.read(folder.appendingPathComponent("Missing.bin")))
|
|
let bad = folder.appendingPathComponent("bad.bin")
|
|
for bytes in [Data(),Data(repeating: 0,count:BinaryFile.limit+1)] {
|
|
try bytes.write(to: bad); XCTAssertThrowsError(try BinaryFile.read(bad))
|
|
}
|
|
let wrong = folder.appendingPathComponent("Program.rom"); try binary.write(to: wrong)
|
|
XCTAssertThrowsError(try BinaryFile.read(wrong))
|
|
XCTAssertEqual(try Data(contentsOf: url),binary, "Loading never writes the host binary")
|
|
}
|
|
@MainActor func testBridgeQuickLoadAndRejectionOnBothCPUs() async throws {
|
|
for cpu in [false,true] {
|
|
let controller = VCCMachineController()
|
|
var state = await step { controller.quickLoadBinary(binary, completion: $0) }
|
|
XCTAssertEqual(state.state,.empty); XCTAssertNotNil(state.errorMessage)
|
|
_ = await step { controller.setAudioEnabled(false,volume:0,completion:$0) }
|
|
state = await step { controller.loadDiagnostic(use6309:cpu,completion:$0) }
|
|
let generation = state.generation
|
|
state = await step { controller.quickLoadBinary(binary,completion:$0) }
|
|
XCTAssertNil(state.errorMessage); XCTAssertEqual(state.state,.running); XCTAssertEqual(state.programCounter,0x3000)
|
|
XCTAssertEqual(state.generation,generation)
|
|
// The callback is captured before the next timer frame. Poll until executed.
|
|
for _ in 0..<200 {
|
|
state = await step(controller.snapshot)
|
|
if state.frames>0 { break }; try await Task.sleep(nanoseconds:10_000_000)
|
|
}
|
|
XCTAssertGreaterThan(state.frames,0); XCTAssertEqual(state.programCounter,0x3005)
|
|
state = await step(controller.pause); let before = state
|
|
for bytes in [Data(),binary.dropLast(),binary+Data([0]),Data(repeating:0,count:BinaryFile.limit+1)] {
|
|
state = await step { controller.quickLoadBinary(Data(bytes),completion:$0) }
|
|
XCTAssertNotNil(state.errorMessage); XCTAssertEqual(state.state,.paused)
|
|
XCTAssertEqual(state.programCounter,before.programCounter); XCTAssertEqual(state.cycles,before.cycles)
|
|
}
|
|
_ = await step(controller.shutdown)
|
|
state = await step { controller.quickLoadBinary(binary,completion:$0) }
|
|
XCTAssertEqual(state.state,.closed); XCTAssertNotNil(state.errorMessage)
|
|
}
|
|
}
|
|
@MainActor func testModelStartsBinaryAndLeavesFailuresPaused() async throws {
|
|
let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories:true)
|
|
defer { try? FileManager.default.removeItem(at:folder) }
|
|
let url=folder.appendingPathComponent("Probe.BIN"), bad=folder.appendingPathComponent("Truncated.bin")
|
|
try binary.write(to:url); try binary.dropLast().write(to:bad)
|
|
let suite = "VCCBinaryTests-" + UUID().uuidString
|
|
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
|
|
defer { defaults.removePersistentDomain(forName: suite) }
|
|
for cpu in [false,true] {
|
|
let controller=VCCMachineController()
|
|
let model=MachineModel(diskStore:.init(url:folder.appendingPathComponent("settings.json")),controller:controller, displayDefaults:defaults)
|
|
do {
|
|
XCTAssertFalse(model.canQuickLoad)
|
|
try await until { !model.busy }; XCTAssertFalse(model.canQuickLoad)
|
|
model.loadDiagnostic(); try await until { !model.busy }
|
|
var settings = model.settings; settings.use6309 = cpu
|
|
model.applyMachineSettings(settings); try await until { !model.busy }
|
|
XCTAssertTrue(model.canQuickLoad)
|
|
let focus=model.focusRequest, epoch=model.inputEpoch
|
|
model.loadBinary(url); XCTAssertFalse(model.canQuickLoad)
|
|
try await until { !model.busy }
|
|
XCTAssertEqual(model.state,.running); XCTAssertEqual(model.binaryName,"Probe.BIN")
|
|
XCTAssertNil(model.error); XCTAssertNil(model.fileError)
|
|
XCTAssertGreaterThan(model.focusRequest,focus); XCTAssertGreaterThan(model.inputEpoch,epoch)
|
|
model.loadBinary(bad); try await until { !model.busy }
|
|
XCTAssertEqual(model.state,.paused); XCTAssertNotNil(model.error)
|
|
XCTAssertEqual(model.binaryName,"Probe.BIN"); XCTAssertTrue(model.canQuickLoad)
|
|
model.loadBinary(folder.appendingPathComponent("Missing.bin")); try await until { !model.busy }
|
|
XCTAssertEqual(model.state,.paused); XCTAssertNotNil(model.fileError)
|
|
model.loadBinary(url); try await until { !model.busy }
|
|
XCTAssertNil(model.fileError); XCTAssertNil(model.error); XCTAssertEqual(model.state,.running)
|
|
model.reset(); try await until { !model.busy }; XCTAssertNil(model.binaryName)
|
|
} catch {
|
|
await withCheckedContinuation { c in model.shutdown { c.resume() } }; throw error
|
|
}
|
|
await withCheckedContinuation { c in model.shutdown { c.resume() } }
|
|
XCTAssertFalse(model.canQuickLoad)
|
|
}
|
|
}
|
|
}
|