// SPDX-License-Identifier: GPL-3.0-or-later import XCTest import CryptoKit final class SoftwareLibrarySourceImporterTests: XCTestCase { private var root: URL! override func setUpWithError() throws { root = FileManager.default.temporaryDirectory.appendingPathComponent("LibrarySource-" + UUID().uuidString) try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) } override func tearDownWithError() throws { try? FileManager.default.removeItem(at: root) } private func service() -> SoftwareLibrarySourceImporter { let configuration = URLSessionConfiguration.ephemeral configuration.protocolClasses = [SourceImportTestTransport.self] return SoftwareLibrarySourceImporter(cacheDirectory: root.appendingPathComponent("Cache"), configuration: configuration) } private func source(_ scenario: String = "valid", project: String = "CapitalQuest", entry: String = "main.c") -> SoftwareLibraryOwnedGame.Source { let data = SourceImportTestTransport.package(scenario) let sha = SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined() return .init(package: .init(url: URL(string: "https://vccplus.brianholman.com/dl/software/CoCoLibrary/Sources/Exclusives/Fixture/1/" + scenario + ".zip")!, sha256: sha, byteSize: Int64(data.count)), projectDirectory: project, configurationFile: project + "/vccplus-project.json", entrySource: entry, licenseFiles: ["LICENSE"]) } private func stagingNames() throws -> [String] { try FileManager.default.contentsOfDirectory(atPath: root.path).filter { $0.hasPrefix(".vcc-source-") } } func testHostedSourceURLPolicy() { XCTAssertTrue(SoftwareLibrarySourceImporter.acceptsDownloadURL(source().package.url)) for text in ["https://evil.test/source.zip", "http://vccplus.brianholman.com/dl/software/CoCoLibrary/Sources/Exclusives/a.zip", "https://vccplus.brianholman.com/dl/software/CoCoLibrary/Exclusives/a.zip", "https://vccplus.brianholman.com/dl/software/CoCoLibrary/Sources/Exclusives/a.zip?next=external", "https://user@vccplus.brianholman.com/dl/software/CoCoLibrary/Sources/Exclusives/a.zip", "https://vccplus.brianholman.com/dl/software/CoCoLibrary/Sources/Exclusives/%2e%2e/a.zip"] { XCTAssertFalse(SoftwareLibrarySourceImporter.acceptsDownloadURL(URL(string: text)!)) } } func testPreferredBuildConfigurationMustExistWithoutExecutingIt() async throws { var selected = source(); selected.buildConfiguration = "Release" let imported = try await service().importProject(selected, to: root.appendingPathComponent("Release Source")) let project = try CMOCProject.open(at: imported.projectDirectory) XCTAssertTrue(project.configuration.configurations.contains(where: { $0.name == "Release" })) selected.buildConfiguration = "Does Not Exist" let missing = root.appendingPathComponent("Missing Configuration") do { _ = try await service().importProject(selected, to: missing); XCTFail("Unknown configuration must be rejected") } catch { } XCTAssertFalse(FileManager.default.fileExists(atPath: missing.path)) } func testVerifiedImportPreservesOriginalAndDoesNotExecuteMake() async throws { let destination = root.appendingPathComponent("My new project") let result = try await service().importProject(source(), to: destination) XCTAssertEqual(result.projectDirectory.lastPathComponent, "CapitalQuest") XCTAssertEqual(result.importedDirectory.resolvingSymlinksInPath(), destination.resolvingSymlinksInPath()) XCTAssertEqual(try Data(contentsOf: result.originalPackage), SourceImportTestTransport.package("valid")) XCTAssertTrue(FileManager.default.fileExists(atPath: result.receiptURL.path)) XCTAssertEqual((try FileManager.default.attributesOfItem(atPath: result.originalPackage.path)[.posixPermissions] as? NSNumber)?.intValue, 0o444) XCTAssertFalse(FileManager.default.fileExists(atPath: result.projectDirectory.appendingPathComponent("MUST-NOT-RUN").path)) let entry = result.projectDirectory.appendingPathComponent("main.c") try Data("personal edits".utf8).write(to: entry) XCTAssertEqual(try Data(contentsOf: entry), Data("personal edits".utf8)) XCTAssertEqual(try Data(contentsOf: result.originalPackage), SourceImportTestTransport.package("valid")) XCTAssertTrue(try stagingNames().isEmpty) } func testExistingFolderAndSymlinkAreNeverOverwritten() async throws { let destination = root.appendingPathComponent("Personal") try FileManager.default.createDirectory(at: destination, withIntermediateDirectories: false) let marker = destination.appendingPathComponent("Keep.txt"); try Data("KEEP".utf8).write(to: marker) do { _ = try await service().importProject(source(), to: destination); XCTFail("Existing folder must fail") } catch { } XCTAssertEqual(try String(contentsOf: marker, encoding: .utf8), "KEEP") let link = root.appendingPathComponent("Link") try FileManager.default.createSymbolicLink(at: link, withDestinationURL: destination) do { _ = try await service().importProject(source(), to: link); XCTFail("Existing link must fail") } catch { } XCTAssertEqual(try String(contentsOf: marker, encoding: .utf8), "KEEP") XCTAssertTrue(try stagingNames().isEmpty) } func testLateDestinationIsPreservedByExclusivePromotion() async throws { let destination = root.appendingPathComponent("Late arrival") _ = try? FileManager.default.removeItem(at: destination) do { _ = try await service().importProject(source(), to: destination) { progress in if progress.stage == .checking { try? FileManager.default.createDirectory(at: destination, withIntermediateDirectories: false) try? Data("KEEP LATE".utf8).write(to: destination.appendingPathComponent("Keep.txt")) } } XCTFail("Late destination must not be replaced") } catch { } XCTAssertEqual(try String(contentsOf: destination.appendingPathComponent("Keep.txt"), encoding: .utf8), "KEEP LATE") XCTAssertFalse(FileManager.default.fileExists(atPath: destination.appendingPathComponent("CapitalQuest").path)) XCTAssertTrue(try stagingNames().isEmpty) } func testChangedTruncatedOversizedAndHTTPFailureDoNotCreateProject() async throws { for scenario in ["corrupt", "truncated", "oversized", "http-error"] { let destination = root.appendingPathComponent(scenario) do { _ = try await service().importProject(source(scenario), to: destination); XCTFail(scenario) } catch { } XCTAssertFalse(FileManager.default.fileExists(atPath: destination.path), scenario) XCTAssertTrue(try stagingNames().isEmpty) } } func testUnsafeZIPPathsLinksVCSAndMismatchedEntryAreRejected() async throws { for scenario in ["traversal", "symlink", "vcs", "mismatched-entry", "missing-license"] { let destination = root.appendingPathComponent(scenario) do { _ = try await service().importProject(source(scenario), to: destination); XCTFail(scenario) } catch { } XCTAssertFalse(FileManager.default.fileExists(atPath: destination.path), scenario) XCTAssertTrue(try stagingNames().isEmpty) } XCTAssertFalse(FileManager.default.fileExists(atPath: root.appendingPathComponent("escaped.c").path)) } func testSiblingSupportInsidePackageIsAllowedButEscapeIsRejected() async throws { let valid = try await service().importProject(source("sibling", project: "Wrapper/game"), to: root.appendingPathComponent("Sibling Project")) XCTAssertEqual(valid.projectDirectory.lastPathComponent, "game") XCTAssertTrue(FileManager.default.fileExists(atPath: valid.importedDirectory.appendingPathComponent("Wrapper/tools/pack.pl").path)) let external = root.appendingPathComponent("tools") try FileManager.default.createDirectory(at: external, withIntermediateDirectories: false) try Data("PERSONAL".utf8).write(to: external.appendingPathComponent("keep.txt")) let destination = root.appendingPathComponent("Escaping Project") do { _ = try await service().importProject(source("escaping", project: "game"), to: destination); XCTFail("Escape must fail") } catch { } XCTAssertFalse(FileManager.default.fileExists(atPath: destination.path)) XCTAssertEqual(try String(contentsOf: external.appendingPathComponent("keep.txt"), encoding: .utf8), "PERSONAL") XCTAssertTrue(try stagingNames().isEmpty) } func testCancellationLeavesNoProjectAndRetryWorks() async throws { let importer = service(), destination = root.appendingPathComponent("Cancelled") let selected = source("cancel") let task = Task { try await importer.importProject(selected, to: destination) } try await Task.sleep(nanoseconds: 100_000_000) task.cancel() do { _ = try await task.value; XCTFail("Cancellation expected") } catch is CancellationError { } catch { XCTFail("Unexpected cancellation: \(error)") } XCTAssertFalse(FileManager.default.fileExists(atPath: destination.path)) XCTAssertTrue(try stagingNames().isEmpty) _ = try await importer.importProject(source(), to: destination) } } private final class SourceImportTestTransport: URLProtocol, @unchecked Sendable { private let lock = NSLock() private var cancelled = false override class func canInit(with request: URLRequest) -> Bool { true } override class func canonicalRequest(for request: URLRequest) -> URLRequest { request } static func package(_ scenario: String) -> Data { let project = scenario == "sibling" ? "Wrapper/game" : scenario == "escaping" ? "game" : "CapitalQuest" let support = scenario == "escaping" ? "../../tools" : "../tools" let config = "{\"entrySource\":\"\(scenario == "mismatched-entry" ? "other.c" : "main.c")\",\"makefile\":\"Makefile\"\(scenario == "sibling" || scenario == "escaping" ? ",\"buildSupportDirectories\":[\"\(support)\"]" : "")}" var entries: [(String, Data, UInt32)] = [ (project + "/vccplus-project.json", Data(config.utf8), 0o100644), (project + "/main.c", Data("int main(void) { return 0; }\n".utf8), 0o100644), (project + "/Makefile", Data("all:\n\t@touch MUST-NOT-RUN\n".utf8), 0o100644) ] if scenario != "missing-license" { entries.append(("LICENSE", Data("Authored fixture only".utf8), 0o100644)) } if scenario == "mismatched-entry" { entries.append((project + "/other.c", Data("int main(void) { return 1; }".utf8), 0o100644)) } if scenario == "sibling" { entries.append(("Wrapper/tools/pack.pl", Data("print 'fixture';".utf8), 0o100644)) } if scenario == "traversal" { entries.append(("../escaped.c", Data("escape".utf8), 0o100644)) } if scenario == "symlink" { entries.append((project + "/link", Data("main.c".utf8), 0o120777)) } if scenario == "vcs" { entries.append((project + "/.git/config", Data("private".utf8), 0o100644)) } return zip(entries) } override func startLoading() { guard let url = request.url else { return } let scenario = url.deletingPathExtension().lastPathComponent var bytes = Self.package(scenario) if scenario == "corrupt" { bytes[0] ^= 1 } if scenario == "truncated" { bytes.removeLast(5) } let length = scenario == "oversized" ? bytes.count * 2 : bytes.count client?.urlProtocol(self, didReceive: HTTPURLResponse(url: url, statusCode: scenario == "http-error" ? 503 : 200, httpVersion: nil, headerFields: ["Content-Length": String(length)])!, cacheStoragePolicy: .notAllowed) if scenario == "cancel" { DispatchQueue.global().asyncAfter(deadline: .now() + 1) { [weak self] in guard let self else { return } self.lock.lock(); let stopped = self.cancelled; self.lock.unlock() guard !stopped else { return } self.client?.urlProtocol(self, didLoad: Self.package("cancel")) self.client?.urlProtocolDidFinishLoading(self) } } else { client?.urlProtocol(self, didLoad: bytes) client?.urlProtocolDidFinishLoading(self) } } override func stopLoading() { lock.lock(); cancelled = true; lock.unlock() } private static func zip(_ entries: [(String, Data, UInt32)]) -> Data { var body = Data(), central = Data() for (path, bytes, mode) in entries { let name = Data(path.utf8), offset = UInt32(body.count), crc = crc32(bytes), size = UInt32(bytes.count) body.sourceLE32(0x04034b50); body.sourceLE16(20); body.sourceLE16(0x800); body.sourceLE16(0) body.sourceLE16(0); body.sourceLE16(0); body.sourceLE32(crc); body.sourceLE32(size); body.sourceLE32(size) body.sourceLE16(UInt16(name.count)); body.sourceLE16(0); body.append(name); body.append(bytes) central.sourceLE32(0x02014b50); central.sourceLE16(0x0314); central.sourceLE16(20); central.sourceLE16(0x800); central.sourceLE16(0) central.sourceLE16(0); central.sourceLE16(0); central.sourceLE32(crc); central.sourceLE32(size); central.sourceLE32(size) central.sourceLE16(UInt16(name.count)); central.sourceLE16(0); central.sourceLE16(0); central.sourceLE16(0); central.sourceLE16(0) central.sourceLE32(mode << 16); central.sourceLE32(offset); central.append(name) } let offset = UInt32(body.count); body.append(central) body.sourceLE32(0x06054b50); body.sourceLE16(0); body.sourceLE16(0); body.sourceLE16(UInt16(entries.count)); body.sourceLE16(UInt16(entries.count)) body.sourceLE32(UInt32(central.count)); body.sourceLE32(offset); body.sourceLE16(0) return body } private static func crc32(_ bytes: Data) -> UInt32 { var result: UInt32 = 0xffffffff for byte in bytes { result ^= UInt32(byte) for _ in 0..<8 { result = (result >> 1) ^ (result & 1 == 0 ? 0 : 0xedb88320) } } return result ^ 0xffffffff } } private extension Data { mutating func sourceLE16(_ value: UInt16) { append(UInt8(truncatingIfNeeded: value)); append(UInt8(truncatingIfNeeded: value >> 8)) } mutating func sourceLE32(_ value: UInt32) { sourceLE16(UInt16(truncatingIfNeeded: value)); sourceLE16(UInt16(truncatingIfNeeded: value >> 16)) } }