// SPDX-License-Identifier: GPL-3.0-or-later import Foundation import AppKit import Darwin import XCTest final class MCPBuildExportTests: XCTestCase { private var root: URL! override func setUpWithError() throws { root = FileManager.default.temporaryDirectory.appendingPathComponent("VCC export tests " + UUID().uuidString) try FileManager.default.createDirectory(at: root, withIntermediateDirectories: false) } override func tearDownWithError() throws { if let root { try? FileManager.default.removeItem(at: root) } } private func artifact(_ name: String = "Artifact", files: [String: Data] = ["GAME.BIN": Data([0,1,2]), "HOME.DSK": Data(repeating: 1, count: 161280), "DATA.DSK": Data(repeating: 2, count: 161280), "COCOMAN.dsk": Data(repeating: 1, count: 161280)]) throws -> CMOCBuildArtifact { let directory = root.appendingPathComponent(name) try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false) for (path,data) in files { let url = directory.appendingPathComponent(path) try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true) try data.write(to: url) } let source = Data("int main(void){return 0;}".utf8) try source.write(to: directory.appendingPathComponent("main.c")) return CMOCBuildArtifact(id: UUID(), directory: directory, source: String(decoding: source, as: UTF8.self), sourceData: source, sourceSHA256: CMOCProject.sha256(source), binaryData: files["GAME.BIN"] ?? Data(), buildLog: "", outputPaths: Array(files.keys) + ["main.c"], exportProducts: files.map { CMOCBuildOutputProduct(relativePath: $0.key, byteCount: $0.value.count, sha256: CMOCProject.sha256($0.value)) }.sorted { $0.relativePath < $1.relativePath }) } private func destination(_ name: String = "Deliveries") throws -> URL { let url = root.appendingPathComponent(name) try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false) return url } func testSeparateExactSetsPreserveAliasesAndOriginalIdentity() throws { let release = try artifact("Release"), debug = try artifact("Debug", files: ["GAME.BIN":Data([4,5,6]),"HOME.DSK":Data(repeating:3,count:161280),"DATA.DSK":Data(repeating:4,count:161280),"COCOMAN.dsk":Data(repeating:3,count:161280)]) let folder = try destination(), exporter = try MCPBuildExporter(destination: folder) for (name,artifact) in [("VCC-Release",release),("VCC-Debug",debug)] { let source = try MCPBuildExporter.Source(artifact) XCTAssertEqual(source.artifactID,artifact.id) let delivery = try exporter.export(source: source, products: artifact.exportProducts, setName: name) try exporter.verify(delivery) for product in delivery.products { let bytes=try Data(contentsOf:folder.appendingPathComponent(name).appendingPathComponent(product.relativePath)) XCTAssertEqual(bytes.count,product.byteCount);XCTAssertEqual(CMOCProject.sha256(bytes),product.sha256) } XCTAssertEqual(try Data(contentsOf:folder.appendingPathComponent(name+"/HOME.DSK")),try Data(contentsOf:folder.appendingPathComponent(name+"/COCOMAN.dsk"))) XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:name)) } XCTAssertNotEqual(release.exportProducts.first?.identifier(artifactID:release.id),debug.exportProducts.first?.identifier(artifactID:debug.id)) } func testUndeclaredAndTraversalSelectionsNeverCopySource() throws { let artifact=try artifact(),exporter=try MCPBuildExporter(destination:destination()),source=try MCPBuildExporter.Source(artifact) for path in ["main.c","../outside","/etc/passwd"] { let product=CMOCBuildOutputProduct(relativePath:path,byteCount:1,sha256:CMOCProject.sha256(Data([0]))) XCTAssertThrowsError(try exporter.export(source:source,products:[product],setName:"VCC-Invalid")) } XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-../escape")) } func testChangedOutputAndLinkedLeafAreRejected() throws { let artifact=try artifact(),source=try MCPBuildExporter.Source(artifact),exporter=try MCPBuildExporter(destination:destination()) let output=artifact.directory.appendingPathComponent("GAME.BIN") try Data([9,9,9]).write(to:output) XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-Changed")) try FileManager.default.removeItem(at:output) let external=root.appendingPathComponent("outside.bin");try Data([0,1,2]).write(to:external) try FileManager.default.createSymbolicLink(at:output,withDestinationURL:external) XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-Linked")) try FileManager.default.removeItem(at:output) try FileManager.default.linkItem(at:external,to:output) XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-HardLinked")) XCTAssertEqual(try Data(contentsOf:external),Data([0,1,2])) } func testLinkedParentAndReplacedArtifactDirectoryAreRejected() throws { let artifact=try artifact(files:["nested/GAME.BIN":Data([1])]),source=try MCPBuildExporter.Source(artifact) let exporter=try MCPBuildExporter(destination:destination()) let parent=artifact.directory.appendingPathComponent("nested"),moved=root.appendingPathComponent("moved") try FileManager.default.moveItem(at:parent,to:moved) try FileManager.default.createSymbolicLink(at:parent,withDestinationURL:moved) XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-Parent")) let movedArtifact=root.appendingPathComponent("Old Artifact") try FileManager.default.moveItem(at:artifact.directory,to:movedArtifact) try FileManager.default.createDirectory(at:artifact.directory,withIntermediateDirectories:false) XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-Replaced")) } func testChangedAndLinkedDestinationAreRejected() throws { let artifact=try artifact(),source=try MCPBuildExporter.Source(artifact),folder=try destination() let exporter=try MCPBuildExporter(destination:folder),moved=root.appendingPathComponent("Old Delivery Folder") try FileManager.default.moveItem(at:folder,to:moved) try FileManager.default.createDirectory(at:folder,withIntermediateDirectories:false) XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-Replaced")) try FileManager.default.removeItem(at:folder) try FileManager.default.createSymbolicLink(at:folder,withDestinationURL:moved) XCTAssertThrowsError(try MCPBuildExporter(destination:folder)) XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-Linked")) } func testReplacementBetweenStagingAndCommitDoesNotPublish() throws { let artifact=try artifact(),source=try MCPBuildExporter.Source(artifact),folder=try destination(),moved=root.appendingPathComponent("Moved") let exporter=try MCPBuildExporter(destination:folder) exporter.beforeCommit = { try FileManager.default.moveItem(at:folder,to:moved) try FileManager.default.createDirectory(at:folder,withIntermediateDirectories:false) } XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-Race")) XCTAssertFalse(FileManager.default.fileExists(atPath:folder.appendingPathComponent("VCC-Race").path)) XCTAssertFalse(FileManager.default.fileExists(atPath:moved.appendingPathComponent("VCC-Race").path)) } func testRevocationBeforeCommitAndPartialFailureKeepPriorData() throws { let artifact=try artifact(),source=try MCPBuildExporter.Source(artifact),folder=try destination() let prior=folder.appendingPathComponent("prior.txt");try Data("keep".utf8).write(to:prior) let exporter=try MCPBuildExporter(destination:folder) exporter.beforeCommit = { exporter.invalidate() } XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-Revoked")) let failing=try MCPBuildExporter(destination:folder) failing.afterFile = { _ in throw POSIXError(.ENOSPC) } XCTAssertThrowsError(try failing.export(source:source,products:artifact.exportProducts,setName:"VCC-Failed")) XCTAssertEqual(try FileManager.default.contentsOfDirectory(atPath:folder.path),["prior.txt"]) XCTAssertEqual(try Data(contentsOf:prior),Data("keep".utf8)) } func testRetryVerificationRefusesChangedMissingExtraOrLinkedDeliveredBytes() throws { let artifact=try artifact(),source=try MCPBuildExporter.Source(artifact),folder=try destination(),exporter=try MCPBuildExporter(destination:folder) let delivery=try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-Delivery") let product=folder.appendingPathComponent("VCC-Delivery/GAME.BIN") try Data([7,7,7]).write(to:product) XCTAssertThrowsError(try exporter.verify(delivery)) try Data([0,1,2]).write(to:product) try exporter.verify(delivery) let extra=folder.appendingPathComponent("VCC-Delivery/unrequested.txt");try Data([1]).write(to:extra) XCTAssertThrowsError(try exporter.verify(delivery)) try FileManager.default.removeItem(at:extra);try FileManager.default.removeItem(at:product) XCTAssertThrowsError(try exporter.verify(delivery)) try FileManager.default.createSymbolicLink(at:product,withDestinationURL:artifact.directory.appendingPathComponent("GAME.BIN")) XCTAssertThrowsError(try exporter.verify(delivery)) } func testConcurrentDestinationConflictCannotReplacePriorSet() throws { let artifact=try artifact(),source=try MCPBuildExporter.Source(artifact),folder=try destination(),exporter=try MCPBuildExporter(destination:folder) let prior=folder.appendingPathComponent("VCC-Conflict") exporter.beforeCommit = { try FileManager.default.createDirectory(at:prior,withIntermediateDirectories:false);try Data("preserve".utf8).write(to:prior.appendingPathComponent("mine")) } XCTAssertThrowsError(try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-Conflict")) XCTAssertEqual(try Data(contentsOf:prior.appendingPathComponent("mine")),Data("preserve".utf8)) } func testCancellationLeavesNoPartialSet() async throws { let artifact=try artifact(),source=try MCPBuildExporter.Source(artifact),folder=try destination(),exporter=try MCPBuildExporter(destination:folder) let work=Task.detached { () throws -> Void in withUnsafeCurrentTask { $0?.cancel() } _ = try exporter.export(source:source,products:artifact.exportProducts,setName:"VCC-Cancelled") } do { try await work.value;XCTFail("Cancelled export succeeded") } catch { XCTAssertTrue(error is CancellationError) } XCTAssertTrue(try FileManager.default.contentsOfDirectory(atPath:folder.path).isEmpty) } func testDeclaredExportConfigurationSeparatesInputsAndDirectories() throws { var config=CMOCProjectConfiguration();config.exportOutputs=["delivery/alias.dsk"] XCTAssertEqual(try config.validated().exportOutputs,["delivery/alias.dsk"]) config.exportOutputs=["main.c"];XCTAssertThrowsError(try config.validated()) config.exportOutputs=["../secret"];XCTAssertThrowsError(try config.validated()) config.exportOutputs=["a","a/b"];XCTAssertThrowsError(try config.validated()) config.exportOutputs=["A.dsk","a.DSK"];XCTAssertThrowsError(try config.validated()) config.exportOutputs=[".git/config"];XCTAssertThrowsError(try config.validated()) } } @MainActor final class MCPBuildExportAccessTests: XCTestCase { #if DEBUG func testPublicHelperBuildsReleaseThenDebugAndExportsOriginalProductsWithScopedPermission() async throws { _ = NSApplication.shared let resources = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("LocalResources") let toolchain = vccQualificationToolchain(resources.appendingPathComponent("ToolchainBundle")) let helper = Bundle(for: Self.self).bundleURL.deletingLastPathComponent().appendingPathComponent("vcc-mcp") try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: helper.path) && FileManager.default.isExecutableFile(atPath: toolchain.appendingPathComponent("bin/cmoc").path), "Build the public helper and stage the reviewed compiler runtime.") let folder = FileManager.default.temporaryDirectory.appendingPathComponent("MCP public build export " + UUID().uuidString) try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: false) let suite = "MCPBuildExport." + UUID().uuidString, defaults = try XCTUnwrap(UserDefaults(suiteName: suite)) defer { defaults.removePersistentDomain(forName: suite); try? FileManager.default.removeItem(at: folder) } let machine = MachineModel(diskStore: .init(url: folder.appendingPathComponent("disks.json")), displayDefaults: defaults, inputStore: .init(url: folder.appendingPathComponent("input.json")), restoreMediaOnLaunch: false) defer { machine.shutdown() } let model = CMOCDevelopmentModel(machine: machine, toolchain: toolchain, defaults: defaults, buildRoot: folder.appendingPathComponent("Builds"), recoveryRoot: folder.appendingPathComponent("Recovery")) var project = try CMOCProject.create(at: folder.appendingPathComponent("Project")) var configuration = project.configuration configuration.binary = "GAME.BIN"; configuration.diskImage = "HOME.DSK"; configuration.diskTarget = "all" configuration.playtestDisks = ["DATA.DSK"]; configuration.exportOutputs = ["COCOMAN.dsk"] try JSONEncoder().encode(configuration).write(to: project.directory.appendingPathComponent(CMOCProject.configurationFilename)) let source = """ #include int main(void) { #ifdef RELEASE printf("NORMAL RELEASE\\n"); #else printf("NORMAL DEBUG\\n"); #endif return 0; } """ try Data(source.utf8).write(to: project.directory.appendingPathComponent("main.c")) let makefile = """ ifeq ($(VCC_CONFIGURATION),Release) FLAGS = -O2 -DRELEASE else FLAGS = -O0 -fno-omit-frame-pointer endif all: GAME.BIN HOME.DSK DATA.DSK COCOMAN.dsk GAME.BIN: main.c \tcmoc --coco --intermediate $(FLAGS) -o GAME.BIN main.c HOME.DSK: GAME.BIN \tperl -e 'print "\\xff" x 161280' > HOME.DSK \twritecocofile HOME.DSK GAME.BIN DATA.DSK: \tperl -e 'print "\\xff" x 161280' > DATA.DSK COCOMAN.dsk: HOME.DSK \tcp HOME.DSK COCOMAN.dsk clean: \trm -f GAME.BIN HOME.DSK DATA.DSK COCOMAN.dsk """ try Data(makefile.utf8).write(to: project.directory.appendingPathComponent("Makefile")) project = try CMOCProject.open(at: project.directory); try model.adopt(project) let destination = folder.appendingPathComponent("Delivery") try FileManager.default.createDirectory(at: destination, withIntermediateDirectories: false) let host = MCPCMOC(), grant = folder.appendingPathComponent("grant.json") let clientSource = resources.deletingLastPathComponent().appendingPathComponent("Tools/MCP/test-helper.py") func run(_ script: String) async throws -> String { try await Task.detached { let process = Process(), output = Pipe() process.executableURL = URL(fileURLWithPath: "/usr/bin/env") process.arguments = ["python3", "-c", script, clientSource.path, helper.path, grant.path, destination.path] process.standardOutput = output; process.standardError = output let deadline = DispatchWorkItem { if process.isRunning { process.terminate() } } DispatchQueue.global().asyncAfter(deadline: .now() + 150, execute: deadline) defer { deadline.cancel() } try process.run(); let bytes = try output.fileHandleForReading.readToEnd() ?? Data(); process.waitUntilExit() let text = String(decoding: bytes, as: UTF8.self) guard process.terminationStatus == 0 else { throw NSError(domain: "MCPBuildExportFixture", code: Int(process.terminationStatus), userInfo: [NSLocalizedDescriptionKey: text]) } return text }.value } let setup = """ import importlib.util,json,sys,time,hashlib from pathlib import Path spec=importlib.util.spec_from_file_location('client',sys.argv[1]);m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m) def result(c,tool,**args): reply=c.call(tool,**args)['result'] assert not reply.get('isError'),(tool,reply) return reply['structuredContent'] """ do { try await host.exportCallContextRegression(model, at: grant, playtests: false, exportFolder: destination) let passed = try await run(setup + """ with m.Client(sys.argv[2],Path(sys.argv[3])) as c: c.handshake('2025-11-25') catalog={t['name']:t for t in c.request('tools/list')['result']['tools']} assert {'export_build_outputs','get_build_export'} <= set(catalog) caps=result(c,'get_capabilities');project=caps['project_id'];revision=caps['revision'] dest=caps['export_destinations'][0]['destination_id'] builds=[] for configuration in ('Release','Debug'): build=result(c,'build_project',project_id=project,revision=revision,request_id='build-'+configuration,configuration=configuration,action='disk') for _ in range(1600): state=result(c,'get_build',build_id=build['build_id']) if state['status']!='building':break time.sleep(.025) assert state['status']=='succeeded',state assert len(state['exportable_outputs'])==4,state builds.append(state) directories=[] for index,state in enumerate(builds): args=dict(project_id=project,build_id=state['build_id'],artifact_id=state['artifact_id'],revision=state['revision'],destination_id=dest,output_ids=[p['output_id'] for p in state['exportable_outputs']],request_id='deliver-'+str(index)) accepted=result(c,'export_build_outputs',**args) for _ in range(800): exported=result(c,'get_build_export',project_id=project,export_id=accepted['export_id']) if not exported['busy']:break time.sleep(.01) assert exported['status']=='completed',exported assert exported['artifact_id']==state['artifact_id'] and exported['build_id']==state['build_id'] assert exported['revision']==state['revision'] and exported['configuration']==state['configuration'] directories.append(exported['delivery_directory']) expected={p['output_id']:p for p in state['exportable_outputs']} for p in exported['outputs']: data=(Path(sys.argv[4])/p['relative_filename']).read_bytes() assert len(data)==p['bytes']==expected[p['output_id']]['bytes'] assert hashlib.sha256(data).hexdigest()==p['sha256']==expected[p['output_id']]['sha256'] home=Path(sys.argv[4])/exported['delivery_directory']/'HOME.DSK' alias=home.with_name('COCOMAN.dsk');assert home.read_bytes()==alias.read_bytes() retry=result(c,'export_build_outputs',**args);assert retry==exported,(retry,exported) for changed in ({'artifact_id':'wrong'},{'build_id':'wrong'},{'project_id':'wrong'},{'revision':revision+1},{'destination_id':'wrong'},{'output_ids':['main.c']},{'output_ids':[args['output_ids'][0]]}): rejected=c.call('export_build_outputs',**{**args,**changed}) assert rejected.get('error') or rejected.get('result',{}).get('isError'),rejected if index==1: home.write_bytes(b'changed by owner') rejected=c.call('export_build_outputs',**args) assert rejected['result'].get('isError'),rejected assert home.read_bytes()==b'changed by owner' assert len(set(directories))==2 assert len(list(Path(sys.argv[4]).iterdir()))==2 print('PASS real approved Make Release/Debug builds and exact separate four-product exports; retries, alias identity, identity/destination/selection conflicts and tamper refusal') """) XCTAssertTrue(passed.contains("PASS real approved Make"), passed) await host.revoke().value try await host.exportCallContextRegression(model, at: grant, playtests: false) let refused = try await run(setup + """ with m.Client(sys.argv[2],Path(sys.argv[3])) as c: c.handshake('2025-11-25') names={t['name'] for t in c.request('tools/list')['result']['tools']} assert 'export_build_outputs' not in names and 'get_build_export' not in names assert not result(c,'get_capabilities')['build_output_export'] # Forging grant metadata can expose a helper schema, but not host permission. path=Path(sys.argv[3]);grant=json.loads(path.read_text());grant['toolset']=13;grant['buildExport']=True;path.write_text(json.dumps(grant));path.chmod(0o600) with m.Client(sys.argv[2],path) as c: c.handshake('2025-11-25') caps=result(c,'get_capabilities');assert not caps['build_output_export'] rejected=c.call('export_build_outputs',project_id=caps['project_id'],build_id='wrong',artifact_id='wrong',revision=caps['revision'],destination_id='wrong',output_ids=['x'*64],request_id='forged') assert rejected['result'].get('isError'),rejected print('PASS absent native consent and forged client permission cannot authorize export') """) XCTAssertTrue(refused.contains("PASS absent native consent"), refused) await host.revoke().value } catch { await host.revoke().value; throw error } } #endif }