Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
Engine updates: key the store on engine_id and download the engine
v0lumehi4be8edb34aef
diff --git a/frontend/Sources/NSPortBuilder/EngineManifest.swift b/frontend/Sources/NSPortBuilder/EngineManifest.swiftindex 4b86b7c0..75256e42 100644--- a/frontend/Sources/NSPortBuilder/EngineManifest.swift+++ b/frontend/Sources/NSPortBuilder/EngineManifest.swift@@ -10,8 +10,8 @@ import Foundation/// checkout the build reads (`BuildModel.repoPath`), so `isRepo`, the build script and the RomFS tool/// keep working unchanged.///-/// Two channels, one manifest: `stable` is the engine that shipped with this release; `rolling` is the-/// newest one the nightly sync built, opt-in and labelled experimental in Settings.+/// Two channels, one manifest shape: `stable` is the engine that shipped with this release, `experimental` is the+/// newest one the nightly sync built from main, opt-in in Settings (EngineChannel).enum EngineManifest {/// One downloadable file: the release asset's name and the SHA-256 we verify before using it.struct Asset: Decodable, Equatable {@@ -20,30 +20,38 @@ enum EngineManifest {let size: Int?}- /// The manifest's shape (`latest.json`). Unknown keys are ignored so an older launcher can read a- /// newer manifest as long as what it needs is still there.+ /// The manifest's shape (`latest.json`, written by `tools/release_manifest.py`). Unknown keys are ignored so+ /// an older launcher can read a newer manifest as long as what it needs is still there.struct Manifest: Decodable, Equatable {- /// The release version this manifest describes, e.g. "0.8.0".+ /// The release version this manifest describes, e.g. "0.8.0" (or "0.8.0-experimental").let version: String- /// The seedds/nxnative commit the engine tree was built from (what `.upstream-ref` holds).+ /// The seedds/nxnative commit the engine tree was built from (what `.upstream-ref` holds). Information only:+ /// the patch series changes without the pin moving, so the pin does not name a tree.let pin: String+ /// `<pin>-<12 hex of the patch series digest>`: the tree's identity, and its folder name in the store.+ let engineID: Stringlet engine: Asset/// The app itself, so one manifest drives both updates.let app: Asset?/// Where the files live, in the order they are tried. Empty: the release page on GitHub.var sources: [String] = []- /// When the nightly sync built this engine, for the rolling channel ("2026-10-31T02:00:00Z").+ /// When the engine was built ("2026-10-31T02:00:00Z").var built: String?+ /// "stable" or "experimental".var channel: String?// Written out because a default value does not make a key optional to the synthesized decoder:// a manifest without `sources` is valid and means "the release page".- private enum CodingKeys: String, CodingKey { case version, pin, engine, app, sources, built, channel }+ private enum CodingKeys: String, CodingKey {+ case version, pin, engine, app, sources, built, channel+ case engineID = "engine_id"+ }init(from decoder: Decoder) throws {let c = try decoder.container(keyedBy: CodingKeys.self)version = try c.decode(String.self, forKey: .version)pin = try c.decode(String.self, forKey: .pin)+ engineID = try c.decodeIfPresent(String.self, forKey: .engineID) ?? ""engine = try c.decode(Asset.self, forKey: .engine)app = try c.decodeIfPresent(Asset.self, forKey: .app)sources = try c.decodeIfPresent([String].self, forKey: .sources) ?? []@@ -51,8 +59,9 @@ enum EngineManifest {channel = try c.decodeIfPresent(String.self, forKey: .channel)}- init(version: String, pin: String, engine: Asset, app: Asset?, sources: [String] = [], built: String? = nil, channel: String? = nil) {- self.version = version; self.pin = pin; self.engine = engine; self.app = app+ init(version: String, pin: String, engineID: String, engine: Asset, app: Asset?, sources: [String] = [],+ built: String? = nil, channel: String? = nil) {+ self.version = version; self.pin = pin; self.engineID = engineID; self.engine = engine; self.app = appself.sources = sources; self.built = built; self.channel = channel}}@@ -60,20 +69,30 @@ enum EngineManifest {enum Problem: Error, Equatable {case notJSONcase missingVersion- case missingPin+ case missingPin // not 7 to 64 lowercase hex characters+ case badEngineID(String) // not `<pin>-<12 lowercase hex>`case badDigest(String) // not 64 lowercase hex characters- case noSources+ case badAsset(String) // not a plain file name+ case badSource(String) // not an https URL}/// Decode and validate. A manifest the launcher cannot act on fails here rather than half-way- /// through a download.+ /// through a download. Every value that later becomes part of a path or a URL is checked here, because+ /// an unsigned manifest is accepted while no key is built in.static func decode(_ data: Data) throws -> Manifest {let m: Manifestdo { m = try JSONDecoder().decode(Manifest.self, from: data) }catch { throw Problem.notJSON }guard !m.version.trimmingCharacters(in: .whitespaces).isEmpty else { throw Problem.missingVersion }- guard m.pin.count >= 7 else { throw Problem.missingPin }- for a in [m.engine, m.app].compactMap({ $0 }) where !isDigest(a.sha256) { throw Problem.badDigest(a.sha256) }+ guard isPin(m.pin) else { throw Problem.missingPin }+ guard EngineStore.isEngineID(m.engineID), m.engineID.hasPrefix(m.pin + "-") else { throw Problem.badEngineID(m.engineID) }+ for a in [m.engine, m.app].compactMap({ $0 }) {+ guard isDigest(a.sha256) else { throw Problem.badDigest(a.sha256) }+ guard isAssetName(a.asset) else { throw Problem.badAsset(a.asset) }+ }+ for s in m.sources {+ guard let u = URL(string: s), u.scheme == "https", u.host?.isEmpty == false else { throw Problem.badSource(s) }+ }return m}@@ -81,8 +100,24 @@ enum EngineManifest {s.count == 64 && s.allSatisfy { $0.isHexDigit && !$0.isUppercase }}+ static func isPin(_ s: String) -> Bool {+ (7...64).contains(s.count) && s.allSatisfy { ("0"..."9").contains($0) || ("a"..."f").contains($0) }+ }++ /// A release asset's name: it is appended to a source URL and names a downloaded file, so no folders and no dots+ /// at the start.+ static func isAssetName(_ s: String) -> Bool {+ guard (1...128).contains(s.count), !s.hasPrefix(".") else { return false }+ return s.unicodeScalars.allSatisfy { CharacterSet.alphanumerics.contains($0) && $0.isASCII || "._-".unicodeScalars.contains($0) }+ }+// ---- Authenticity ----+ /// The public half of the key that signs `latest.json`: base64 of the 32 raw Ed25519 bytes, the same line as+ /// `frontend/engine-signing.pub`. Empty until the key pair exists, and then every manifest is accepted unsigned+ /// (`.noKey`). Once it is set, only a manifest with a valid signature installs.+ static let publicKey = ""+/// What we can say about a manifest's signature. Integrity (a SHA-256 that matches) only proves the/// download is what the manifest lists — it says nothing when whoever served the manifest also served/// the file. With a mirror in the picture that matters, so a release may carry a detached Ed25519@@ -118,14 +153,14 @@ enum EngineManifest {}}-/// Where engine trees live on disk: one folder per pin, beside the app's other state, so an app update-/// and an engine update cannot half-replace each other, and going back is just selecting the older pin.+/// Where engine trees live on disk: one folder per engine id, beside the app's other state, so an app update+/// and an engine update cannot half-replace each other, and going back is just selecting the older tree.struct EngineStore {let root: URLinit(root: URL = EngineStore.defaultRoot) { self.root = root }- /// `~/Library/Application Support/NX2Apple/engine/<pin>/`. The folder is a full checkout for the+ /// `~/Library/Application Support/NX2Apple/engine/<engine_id>/`. The folder is a full checkout for the/// build (`dm/`, `scripts/`, ...), so `repoPath` can point at it.static var defaultRoot: URL {let base = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first@@ -133,29 +168,57 @@ struct EngineStore {return base.appendingPathComponent("NX2Apple/engine", isDirectory: true)}- func directory(for pin: String) -> URL { root.appendingPathComponent(pin, isDirectory: true) }+ /// The file in a tree that names it (`tools/engine.py tree` writes it, with a newline).+ static let idFile = ".engine-id"+ /// What the launcher runs by path, and the pin. A tree without one of them is not used.+ static let requiredFiles = ["scripts/build_ipa_from_nsp.py", "dm/tools/romfs_image.py", ".upstream-ref", idFile]++ /// `<pin>-<12 hex>`: lowercase hex and dashes only, so an id can never leave the store as a path+ /// (no `/`, no `..`, no dot at the start).+ static func isEngineID(_ s: String) -> Bool {+ let parts = s.split(separator: "-", omittingEmptySubsequences: false)+ guard parts.count == 2, EngineManifest.isPin(String(parts[0])), parts[1].count == 12 else { return false }+ return parts[1].allSatisfy { ("0"..."9").contains($0) || ("a"..."f").contains($0) }+ }- /// A tree is usable when it holds what the launcher runs by path (`BuildModel.requiredTools`).- /// Unpacking into a temporary folder and renaming makes a half-written tree unreadable rather than- /// silently incomplete, and this is the check that backs it up.- func isUsable(_ pin: String) -> Bool {- let dir = directory(for: pin)- for f in ["scripts/build_ipa_from_nsp.py", "dm/tools/romfs_image.py", ".upstream-ref"] {- guard FileManager.default.fileExists(atPath: dir.appendingPathComponent(f).path) else { return false }- }- return true+ /// The tree's folder, or nil for anything that is not an engine id.+ func directory(for id: String) -> URL? {+ Self.isEngineID(id) ? root.appendingPathComponent(id, isDirectory: true) : nil+ }++ /// The id a tree names itself with (its `.engine-id`), nil when it has none.+ static func identity(of tree: URL) -> String? {+ guard let text = try? String(contentsOf: tree.appendingPathComponent(idFile), encoding: .utf8) else { return nil }+ let id = text.trimmingCharacters(in: .whitespacesAndNewlines)+ return isEngineID(id) ? id : nil+ }++ /// Whether `tree` holds everything the launcher runs by path and calls itself `id`.+ static func isComplete(_ tree: URL, id: String) -> Bool {+ requiredFiles.allSatisfy { FileManager.default.fileExists(atPath: tree.appendingPathComponent($0).path) }+ && identity(of: tree) == id+ }++ /// A tree is usable when it is complete and is the engine its folder is named after. Unpacking into a+ /// temporary folder and renaming makes a half-written tree unreadable rather than silently incomplete,+ /// and this is the check that backs it up.+ func isUsable(_ id: String) -> Bool {+ guard let dir = directory(for: id) else { return false }+ return Self.isComplete(dir, id: id)}- /// Remove every other pin's tree. Called after a successful switch, so a player keeps the one they- /// are on; `keeping` also holds the previous one while a build might still need it.+ /// Remove the trees not in `ids`, and what an interrupted install left behind. Only direct children this store+ /// creates are touched: folders named like an engine id, and the `.tmp-` and `.download-` entries. Anything else+ /// in the folder stays.@discardableResult- func prune(keeping pins: Set<String>) -> [String] {+ func prune(keeping ids: Set<String>) -> [String] {let fm = FileManager.default- guard let entries = try? fm.contentsOfDirectory(at: root, includingPropertiesForKeys: [.isDirectoryKey]) else { return [] }+ guard let names = try? fm.contentsOfDirectory(atPath: root.path) else { return [] }var removed: [String] = []- for url in entries where (try? url.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) == true {- guard !pins.contains(url.lastPathComponent) else { continue }- if (try? fm.removeItem(at: url)) != nil { removed.append(url.lastPathComponent) }+ for name in names {+ let ours = Self.isEngineID(name) || name.hasPrefix(".tmp-") || name.hasPrefix(".download-")+ guard ours, !ids.contains(name) else { continue }+ if (try? fm.removeItem(at: root.appendingPathComponent(name))) != nil { removed.append(name) }}return removed.sorted()}diff --git a/frontend/Sources/NSPortBuilder/EngineUpdater.swift b/frontend/Sources/NSPortBuilder/EngineUpdater.swiftindex 05e79449..67426068 100644--- a/frontend/Sources/NSPortBuilder/EngineUpdater.swift+++ b/frontend/Sources/NSPortBuilder/EngineUpdater.swift@@ -3,14 +3,14 @@ import Foundation/// Which engine the launcher follows, and where its manifest lives.////// - `stable` is the engine that shipped with the running app's release: the tested one, and the default.-/// - `experimental` ("实验版") is the tree the nightly sync built from the newest pin, published as a-/// pre-release under a fixed tag. It is opt-in, it never carries the app, and it is only checked by CI-/// (compiled, patches applied, host tests) — not played on hardware.+/// - `experimental` ("实验版") is the tree built from the newest upstream pin. The release workflow publishes it+/// from main after an upstream bump passed CI, as a pre-release under a fixed tag. It is opt-in and never+/// carries the app. CI compiles it, applies the patches and runs the host tests. Nobody has played it.enum EngineChannel: String, CaseIterable {case stable, experimental- /// The pre-release tag the nightly job replaces the experimental engine under. A fixed tag, so the- /// launcher's URL never changes; the assets inside are re-uploaded.+ /// The pre-release tag the experimental engine is published under. The tag is fixed, so the launcher's URL+ /// never changes. The assets inside are uploaded again with each publish.static let experimentalTag = "engine-experimental"/// Where this repository's release assets live.static let releaseBase = "https://github.com/LNPANDY/NX2Apple/releases/download/"@@ -24,38 +24,69 @@ enum EngineChannel: String, CaseIterable {}}+ /// The detached signature next to the manifest (`latest.json.sig`, base64 of the Ed25519 signature).+ func signatureURL(version: String) -> URL? {+ manifestURL(version: version).flatMap { URL(string: $0.absoluteString + ".sig") }+ }++ /// The localization key of this channel's name in Settings.+ var labelKey: String { "engine.channel." + rawValue }++ /// The localization key of the warning shown before switching from `self` to `other`, nil when nothing changes.+ /// Each switch can bring another shader chain, and then every game compiles its shaders again on its next build.+ func switchWarningKey(to other: EngineChannel) -> String? {+ guard other != self else { return nil }+ return other == .experimental ? "engine.switch.toExperimental" : "engine.switch.toStable"+ }+/// Where an asset of this channel lives when the manifest names no source. A manifest that lists/// sources wins: that is how a mirror is introduced without a new launcher.func assetURL(_ name: String, version: String, sources: [String]) -> URL? {- if let first = sources.first {- let base = first.hasSuffix("/") ? first : first + "/"- return URL(string: base + name)- }+ assetURLs(name, version: version, sources: sources).first+ }++ /// Every place an asset is tried, in order: the manifest's sources, then this channel's release on GitHub+ /// when no source already is it.+ func assetURLs(_ name: String, version: String, sources: [String]) -> [URL] {let tag = self == .stable ? "v" + version : Self.experimentalTag- return URL(string: Self.releaseBase + tag + "/" + name)+ var bases = sources.map { $0.hasSuffix("/") ? $0 : $0 + "/" }+ let github = Self.releaseBase + tag + "/"+ if !bases.contains(github) { bases.append(github) }+ return bases.compactMap { URL(string: $0 + name) }+ }+}++/// The engine tree the release app carries in `Contents/Resources/engine` (the release workflow puts it there).+/// A launcher built from the repository has none, and keeps working on its checkout with git.+enum EngineBundle {+ /// The bundled tree, when it is complete and names itself. nil: no engine in this app.+ static func tree(in resources: URL?) -> URL? {+ guard let dir = resources?.appendingPathComponent("engine", isDirectory: true),+ let id = EngineStore.identity(of: dir), EngineStore.isComplete(dir, id: id) else { return nil }+ return dir}}-/// Installing an engine tree: unpack it beside the others, check it is complete, switch to it, and keep-/// the previous one until the next successful switch (a build started moments ago may still be running).+/// Installing an engine tree into the store. Every tree is unpacked or copied into a hidden folder beside the others+/// and renamed into place only when it is complete, so an interrupted download never leaves a tree that looks usable.+/// A tree is never changed once it is in place: the same id is the same files.enum EngineInstaller {enum Failure: Error, Equatable {case unpack // ditto failed: not a zip, or out of spacecase incomplete(String) // the tree lacks what the launcher runs by path+ case wrongEngine(String) // the tree's .engine-id is not the one the manifest names+ case badID(String) // not an engine idcase move // could not put it in place}- /// Unpack `zip` into `<store>/<pin>/`. The archive's root is the tree itself- /// (`scripts/`, `dm/`, `.upstream-ref`, …), as the release workflow builds it.- ///- /// Unpacking goes to a hidden folder first and is renamed only when complete, so an interrupted- /// download never leaves a tree that looks usable.+ /// Unpack `zip` into `<store>/<engineID>/`. The archive's root is the tree itself+ /// (`scripts/`, `dm/`, `.upstream-ref`, `.engine-id`, ...), as `tools/engine.py tree` builds it.@discardableResult- static func install(zip: URL, pin: String, store: EngineStore, keeping previous: String? = nil) throws -> URL {+ static func install(zip: URL, engineID: String, store: EngineStore) throws -> URL {+ guard store.directory(for: engineID) != nil else { throw Failure.badID(engineID) }+ if store.isUsable(engineID), let dest = store.directory(for: engineID) { return dest }let fm = FileManager.default- let dest = store.directory(for: pin)- let tmp = store.root.appendingPathComponent(".tmp-" + UUID().uuidString, isDirectory: true)- try? fm.createDirectory(at: store.root, withIntermediateDirectories: true)+ let tmp = try scratch(in: store)defer { try? fm.removeItem(at: tmp) }let ditto = Process()@@ -67,7 +98,7 @@ enum EngineInstaller {ditto.waitUntilExit()guard ditto.terminationStatus == 0 else { throw Failure.unpack }- // A release asset may wrap the tree in one folder (`engine/…`, or `<something>/engine/…`): descend+ // A release asset may wrap the tree in one folder (`engine/...`, or `<something>/engine/...`): descend// while the folder has no build script and exactly one subdirectory, two levels at most.let marker = "scripts/build_ipa_from_nsp.py"var source = tmp@@ -77,48 +108,253 @@ enum EngineInstaller {guard kids.count == 1, let only = kids.first else { break }source = only}- guard fm.fileExists(atPath: source.appendingPathComponent("scripts/build_ipa_from_nsp.py").path),- fm.fileExists(atPath: source.appendingPathComponent("dm/tools/romfs_image.py").path),- fm.fileExists(atPath: source.appendingPathComponent(".upstream-ref").path) else {+ return try place(source, as: engineID, in: store)+ }++ /// Copy a tree that is already on disk (the one in the app bundle) into the store. The bundle is read-only and+ /// signed, so the build never runs in it.+ @discardableResult+ static func seed(from tree: URL, engineID: String, store: EngineStore) throws -> URL {+ guard store.directory(for: engineID) != nil else { throw Failure.badID(engineID) }+ if store.isUsable(engineID), let dest = store.directory(for: engineID) { return dest }+ let fm = FileManager.default+ let tmp = try scratch(in: store)+ defer { try? fm.removeItem(at: tmp) }+ let copy = tmp.appendingPathComponent("engine", isDirectory: true)+ do { try fm.copyItem(at: tree, to: copy) } catch { throw Failure.move }+ return try place(copy, as: engineID, in: store)+ }++ private static func scratch(in store: EngineStore) throws -> URL {+ let tmp = store.root.appendingPathComponent(".tmp-" + UUID().uuidString, isDirectory: true)+ do { try FileManager.default.createDirectory(at: tmp, withIntermediateDirectories: true) } catch { throw Failure.move }+ return tmp+ }++ /// Check `source` and rename it to `<store>/<engineID>`. A folder of that name that is not usable (left by an+ /// older launcher or damaged) is replaced.+ private static func place(_ source: URL, as engineID: String, in store: EngineStore) throws -> URL {+ let fm = FileManager.default+ guard EngineStore.requiredFiles.allSatisfy({ fm.fileExists(atPath: source.appendingPathComponent($0).path) }) else {throw Failure.incomplete(source.lastPathComponent)}-- try? fm.removeItem(at: dest)+ let found = EngineStore.identity(of: source) ?? ""+ guard found == engineID else { throw Failure.wrongEngine(found) }+ guard let dest = store.directory(for: engineID) else { throw Failure.badID(engineID) }+ if fm.fileExists(atPath: dest.path) { try? fm.removeItem(at: dest) }do { try fm.moveItem(at: source, to: dest) } catch { throw Failure.move }- guard store.isUsable(pin) else { throw Failure.incomplete(pin) }-- var keep: Set<String> = [pin]- if let previous, previous != pin { keep.insert(previous) }- store.prune(keeping: keep)+ guard store.isUsable(engineID) else { throw Failure.incomplete(engineID) }return dest}}-/// Which channel and pin the launcher is on, remembered between launches.+/// Which channel and engine the launcher is on, remembered between launches.struct EngineSelection: Equatable {var channel: EngineChannel- var pin: String // empty: nothing installed by the launcher yet (a developer's checkout)- var previousPin: String // the one kept for a build that was already running+ var engineID: String // empty: nothing installed by the launcher yet+ var version: String // the manifest's version of that engine, for Settings+ var previousID: String // the one kept for a build that was already runningprivate static let channelKey = "engineChannel"- private static let pinKey = "enginePin"- private static let previousKey = "enginePreviousPin"+ private static let idKey = "engineID"+ private static let versionKey = "engineVersion"+ private static let previousKey = "enginePreviousID"static func load(_ defaults: UserDefaults = .standard) -> EngineSelection {EngineSelection(channel: EngineChannel(rawValue: defaults.string(forKey: channelKey) ?? "") ?? .stable,- pin: defaults.string(forKey: pinKey) ?? "",- previousPin: defaults.string(forKey: previousKey) ?? "")+ engineID: defaults.string(forKey: idKey) ?? "",+ version: defaults.string(forKey: versionKey) ?? "",+ previousID: defaults.string(forKey: previousKey) ?? "")}func save(_ defaults: UserDefaults = .standard) {defaults.set(channel.rawValue, forKey: Self.channelKey)- defaults.set(pin, forKey: Self.pinKey)- defaults.set(previousPin, forKey: Self.previousKey)+ defaults.set(engineID, forKey: Self.idKey)+ defaults.set(version, forKey: Self.versionKey)+ defaults.set(previousID, forKey: Self.previousKey)+ }++ /// Move to a newly installed engine, keeping the one we came from.+ mutating func switched(to id: String, version: String) {+ if !engineID.isEmpty, engineID != id { previousID = engineID }+ engineID = id+ self.version = version+ }++ /// The trees the store keeps: this one and the one before it.+ var kept: Set<String> { Set([engineID, previousID].filter { !$0.isEmpty }) }+}++// ---- Fetching ----++/// What a request for one file gave.+enum EngineFetch<Value> {+ case ok(Value)+ case missing // HTTP 404: the release has no such file+ case failed // offline, a timeout, any other status+}++/// The network, as the updater sees it. Tests replace it with files in memory.+protocol EngineTransport {+ func data(from url: URL) -> EngineFetch<Data>+ /// Download to `destination` (which does not exist yet). true when the whole file arrived.+ func download(from url: URL, to destination: URL) -> Bool+}++/// URLSession, waited on from a background queue as the app update does (BuildModel.downloadAndStage).+/// Nothing is cached: the experimental release replaces its files under the same names.+struct URLSessionEngineTransport: EngineTransport {+ private let session: URLSession = {+ let c = URLSessionConfiguration.ephemeral+ c.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData+ c.timeoutIntervalForRequest = 60+ return URLSession(configuration: c)+ }()++ func data(from url: URL) -> EngineFetch<Data> {+ var result: EngineFetch<Data> = .failed+ let sem = DispatchSemaphore(value: 0)+ session.dataTask(with: url) { data, response, _ in+ let status = (response as? HTTPURLResponse)?.statusCode ?? 0+ if status == 200, let data { result = .ok(data) } else if status == 404 { result = .missing }+ sem.signal()+ }.resume()+ sem.wait()+ return result+ }++ func download(from url: URL, to destination: URL) -> Bool {+ var ok = false+ let sem = DispatchSemaphore(value: 0)+ session.downloadTask(with: url) { local, response, _ in+ if let local, (response as? HTTPURLResponse)?.statusCode == 200 {+ ok = (try? FileManager.default.moveItem(at: local, to: destination)) != nil+ }+ sem.signal()+ }.resume()+ sem.wait()+ return ok+ }+}++/// One engine update: read the channel's manifest, check its signature, and put its engine into the store, from the+/// store itself, from the app bundle or from a download whose SHA-256 matches. Runs on a background queue, touches+/// no UI state, and returns what the model shows.+enum EngineUpdate {+ /// An engine as Settings names it.+ struct Engine: Equatable {+ let id: String+ let version: String+ }++ enum Via: Equatable { case store, bundle, download }++ enum Failure: Error, Equatable {+ case network // the manifest did not arrive+ case noManifest // this channel has no latest.json+ case unsigned(EngineManifest.Signature) // a key is built in and the signature is absent or wrong+ case badManifest(EngineManifest.Problem)+ case wrongChannel(String) // a manifest of the other channel+ case download // no source gave the archive+ case digest // what arrived is not what the manifest lists+ case install(EngineInstaller.Failure)+ }++ enum Outcome: Equatable {+ case current(Engine) // the selected tree is the channel's engine+ case available(Engine) // a newer one exists and `install` was off+ case installed(Engine, Via)+ case failed(Failure)}- /// Move to a newly installed pin, keeping the one we came from.- mutating func switched(to pin: String) {- if !self.pin.isEmpty, self.pin != pin { previousPin = self.pin }- self.pin = pin+ static let assetPrefix = ".download-"++ static func run(channel: EngineChannel, appVersion: String, current: String, store: EngineStore, bundled: URL?,+ publicKey: String = EngineManifest.publicKey, install: Bool, transport: EngineTransport) -> Outcome {+ guard let manifestURL = channel.manifestURL(version: appVersion) else { return .failed(.noManifest) }+ let bundledID = bundled.flatMap { EngineStore.identity(of: $0) }++ let data: Data+ switch transport.data(from: manifestURL) {+ case .ok(let d): data = d+ case .missing:+ // A release without latest.json (one from before engine updates): its stable engine is the one in the app+ guard channel == .stable, let bundled, let id = bundledID else { return .failed(.noManifest) }+ return place(Engine(id: id, version: appVersion), current: current, store: store, bundled: bundled, bundledID: id,+ install: install) { .failed(.noManifest) }+ case .failed: return .failed(.network)+ }++ var signature: String?+ if let sigURL = channel.signatureURL(version: appVersion), case .ok(let s) = transport.data(from: sigURL) {+ signature = String(decoding: s, as: UTF8.self).trimmingCharacters(in: .whitespacesAndNewlines)+ }+ let check = EngineManifest.verify(manifest: data, signature: signature, publicKey: publicKey)+ guard EngineManifest.allowsInstall(check) else { return .failed(.unsigned(check)) }++ let m: EngineManifest.Manifest+ do { m = try EngineManifest.decode(data) }+ catch { return .failed(.badManifest(error as? EngineManifest.Problem ?? .notJSON)) }+ if let c = m.channel, c != channel.rawValue { return .failed(.wrongChannel(c)) }++ return place(Engine(id: m.engineID, version: m.version), current: current, store: store, bundled: bundled, bundledID: bundledID,+ install: install) {+ downloadAndInstall(m, channel: channel, appVersion: appVersion, store: store, transport: transport)+ }+ }++ /// The engine from the cheapest place: already selected, already in the store, in the app, or downloaded.+ private static func place(_ engine: Engine, current: String, store: EngineStore, bundled: URL?, bundledID: String?,+ install: Bool, download: () -> Outcome) -> Outcome {+ if engine.id == current, store.isUsable(current) { return .current(engine) }+ guard install else { return .available(engine) }+ if store.isUsable(engine.id) { return .installed(engine, .store) }+ if let bundled, bundledID == engine.id {+ do { try EngineInstaller.seed(from: bundled, engineID: engine.id, store: store) }+ catch { return .failed(.install(error as? EngineInstaller.Failure ?? .move)) }+ return .installed(engine, .bundle)+ }+ return download()+ }++ private static func downloadAndInstall(_ m: EngineManifest.Manifest, channel: EngineChannel, appVersion: String,+ store: EngineStore, transport: EngineTransport) -> Outcome {+ let fm = FileManager.default+ try? fm.createDirectory(at: store.root, withIntermediateDirectories: true)+ var failure = Failure.download+ for url in channel.assetURLs(m.engine.asset, version: appVersion, sources: m.sources) {+ let file = store.root.appendingPathComponent(assetPrefix + UUID().uuidString + ".zip")+ defer { try? fm.removeItem(at: file) }+ guard transport.download(from: url, to: file) else { continue }+ let size = (try? fm.attributesOfItem(atPath: file.path)[.size] as? NSNumber)?.intValue+ guard m.engine.size == nil || size == m.engine.size, AppUpdate.sha256(of: file) == m.engine.sha256 else {+ failure = .digest // a mirror that serves other bytes: the next source may have the right ones+ continue+ }+ do { try EngineInstaller.install(zip: file, engineID: m.engineID, store: store) }+ catch { return .failed(.install(error as? EngineInstaller.Failure ?? .move)) }+ return .installed(Engine(id: m.engineID, version: m.version), .download)+ }+ return .failed(failure)+ }++ /// The shader chain before and after a switch, when it changed. Every game then compiles its shaders again on+ /// its next build, which the player is told.+ static func chainChange(from old: String, to new: String) -> (old: String, new: String)? {+ guard let a = BuildFolder.currentChain(repo: old), let b = BuildFolder.currentChain(repo: new), a != b else { return nil }+ return (a, b)+ }++ /// The localization key of a failure's message.+ static func key(_ failure: Failure) -> String {+ switch failure {+ case .network: return "engine.fail.network"+ case .noManifest: return "engine.fail.noManifest"+ case .unsigned: return "engine.fail.unsigned"+ case .badManifest, .wrongChannel: return "engine.fail.manifest"+ case .download: return "engine.fail.download"+ case .digest: return "engine.fail.digest"+ case .install: return "engine.fail.install"+ }}}diff --git a/frontend/Tests/NSPortBuilderTests/EngineManifestTests.swift b/frontend/Tests/NSPortBuilderTests/EngineManifestTests.swiftindex ace9ee0b..390bea57 100644--- a/frontend/Tests/NSPortBuilderTests/EngineManifestTests.swift+++ b/frontend/Tests/NSPortBuilderTests/EngineManifestTests.swift@@ -6,12 +6,16 @@ import XCTest/// temporary folders here: no network, no release.final class EngineManifestTests: XCTestCase {private lazy var digest = String(repeating: "a", count: 64)+ private let pin = String(repeating: "e", count: 40)+ private var engineID: String { pin + "-0123456789ab" }- private func json(version: String = "0.8.0", pin: String = "ee6648cb2bb4", engine: String? = nil, app: String? = nil, sources: [String] = []) -> Data {+ private func json(version: String = "0.8.0", pin: String? = nil, engineID: String? = nil, engine: String? = nil,+ app: String? = nil, sources: [String] = []) -> Data {let e = engine ?? "{\"asset\":\"engine.zip\",\"sha256\":\"\(digest)\"}"let a = app ?? "{\"asset\":\"app.zip\",\"sha256\":\"\(digest)\"}"+ let sourceList = "[" + sources.map { "\"\($0)\"" }.joined(separator: ",") + "]"return Data("""- {"version":"\(version)","pin":"\(pin)","engine":\(e),"app":\(a),"sources":\(sources)}+ {"version":"\(version)","pin":"\(pin ?? self.pin)","engine_id":"\(engineID ?? self.engineID)","engine":\(e),"app":\(a),"sources":\(sourceList)}""".utf8)}@@ -20,7 +24,8 @@ final class EngineManifestTests: XCTestCase {func testAWellFormedManifestDecodes() throws {let m = try EngineManifest.decode(json(sources: ["https://example.invalid/v0.8.0/"]))XCTAssertEqual(m.version, "0.8.0")- XCTAssertEqual(m.pin, "ee6648cb2bb4")+ XCTAssertEqual(m.pin, pin)+ XCTAssertEqual(m.engineID, engineID)XCTAssertEqual(m.engine.asset, "engine.zip")XCTAssertEqual(m.app?.asset, "app.zip")XCTAssertEqual(m.sources, ["https://example.invalid/v0.8.0/"])@@ -29,14 +34,14 @@ final class EngineManifestTests: XCTestCase {/// An older launcher reads a newer manifest: extra keys are ignored rather than rejected.func testUnknownKeysAreIgnored() throws {let data = Data("""- {"version":"1.0.0","pin":"ee6648cb2bb4","engine":{"asset":"e.zip","sha256":"\(digest)","future":true},"runtime":"arm64"}+ {"version":"1.0.0","pin":"\(pin)","engine_id":"\(engineID)","engine":{"asset":"e.zip","sha256":"\(digest)","future":true},"runtime":"arm64"}""".utf8)XCTAssertNoThrow(try EngineManifest.decode(data))}func testTheAppAssetIsOptional() throws {let data = Data("""- {"version":"0.8.0","pin":"ee6648cb2bb4","engine":{"asset":"engine.zip","sha256":"\(digest)"}}+ {"version":"0.8.0","pin":"\(pin)","engine_id":"\(engineID)","engine":{"asset":"engine.zip","sha256":"\(digest)"}}""".utf8)XCTAssertNil(try EngineManifest.decode(data).app)}@@ -45,6 +50,35 @@ final class EngineManifestTests: XCTestCase {XCTAssertThrowsError(try EngineManifest.decode(Data("not json".utf8))) { XCTAssertEqual($0 as? EngineManifest.Problem, .notJSON) }XCTAssertThrowsError(try EngineManifest.decode(json(version: " "))) { XCTAssertEqual($0 as? EngineManifest.Problem, .missingVersion) }XCTAssertThrowsError(try EngineManifest.decode(json(pin: "abc"))) { XCTAssertEqual($0 as? EngineManifest.Problem, .missingPin) }+ XCTAssertThrowsError(try EngineManifest.decode(json(pin: "../../x"))) { XCTAssertEqual($0 as? EngineManifest.Problem, .missingPin) }+ }++ /// The store is keyed on engine_id, so a manifest without one, or with one for another pin, installs nothing.+ func testTheEngineIDMustBelongToThePin() {+ let other = String(repeating: "f", count: 40) + "-0123456789ab"+ for bad in ["", other, pin, pin + "-0123456789AB", pin + "-0123", "../" + engineID, engineID + "/x"] {+ XCTAssertThrowsError(try EngineManifest.decode(json(engineID: bad))) {+ XCTAssertEqual($0 as? EngineManifest.Problem, .badEngineID(bad), bad)+ }+ }+ let noID = Data("""+ {"version":"0.8.0","pin":"\(pin)","engine":{"asset":"engine.zip","sha256":"\(digest)"}}+ """.utf8)+ XCTAssertThrowsError(try EngineManifest.decode(noID)) { XCTAssertEqual($0 as? EngineManifest.Problem, .badEngineID("")) }+ }++ /// Asset names end up in a URL and a file name, sources are where the archive comes from.+ func testAssetNamesAndSourcesAreCheckedBeforeTheyAreUsed() {+ for bad in ["../engine.zip", "dir/engine.zip", ".engine.zip", ""] {+ XCTAssertThrowsError(try EngineManifest.decode(json(engine: "{\"asset\":\"\(bad)\",\"sha256\":\"\(digest)\"}"))) {+ XCTAssertEqual($0 as? EngineManifest.Problem, .badAsset(bad), bad)+ }+ }+ for bad in ["http://example.invalid/v0.8.0/", "file:///etc/", "https:///nohost/"] {+ XCTAssertThrowsError(try EngineManifest.decode(json(sources: [bad]))) {+ XCTAssertEqual($0 as? EngineManifest.Problem, .badSource(bad), bad)+ }+ }}func testADigestThatIsNotLowercaseHexIsRejected() {@@ -90,34 +124,88 @@ final class EngineManifestTests: XCTestCase {XCTAssertTrue(EngineManifest.allowsInstall(.noKey), "no key configured: today's behaviour")}+ /// The compiled key and `frontend/engine-signing.pub` are the same value, so the release workflow's key check+ /// (which reads the file) also covers what the app verifies with. Skipped while the file is not in the tree.+ func testTheCompiledKeyMatchesTheKeyFile() throws {+ let file = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent()+ .deletingLastPathComponent().appendingPathComponent("engine-signing.pub")+ guard let text = try? String(contentsOf: file, encoding: .utf8) else { throw XCTSkip("no frontend/engine-signing.pub") }+ let key = text.split(separator: "\n").map { $0.trimmingCharacters(in: .whitespaces) }+ .first { !$0.isEmpty && !$0.hasPrefix("#") } ?? ""+ XCTAssertEqual(EngineManifest.publicKey, key)+ }+// MARK: The store- func testTheStoreKeepsOneTreePerPinAndPrunesTheRest() throws {+ private func makeStore() -> (EngineStore, URL) {let root = FileManager.default.temporaryDirectory.appendingPathComponent("engine-tests-" + UUID().uuidString)- defer { try? FileManager.default.removeItem(at: root) }- let store = EngineStore(root: root)+ addTeardownBlock { try? FileManager.default.removeItem(at: root) }+ return (EngineStore(root: root), root)+ }++ private func id(_ c: Character) -> String { String(repeating: c, count: 40) + "-0123456789ab" }++ private func writeTree(_ dir: URL, id: String?) throws {+ let fm = FileManager.default+ try fm.createDirectory(at: dir.appendingPathComponent("scripts"), withIntermediateDirectories: true)+ try fm.createDirectory(at: dir.appendingPathComponent("dm/tools"), withIntermediateDirectories: true)+ try Data([0]).write(to: dir.appendingPathComponent("scripts/build_ipa_from_nsp.py"))+ try Data([0]).write(to: dir.appendingPathComponent("dm/tools/romfs_image.py"))+ try Data([0]).write(to: dir.appendingPathComponent(".upstream-ref"))+ if let id { try Data((id + "\n").utf8).write(to: dir.appendingPathComponent(EngineStore.idFile)) }+ }++ func testTheStoreKeepsOneTreePerEngineAndPrunesTheRest() throws {+ let (store, _) = makeStore()+ for c: Character in ["a", "b", "c"] { try writeTree(store.directory(for: id(c))!, id: id(c)) }+ XCTAssertTrue(store.isUsable(id("a")))+ XCTAssertEqual(store.prune(keeping: [id("a")]), [id("b"), id("c")])+ XCTAssertTrue(store.isUsable(id("a")))+ XCTAssertFalse(FileManager.default.fileExists(atPath: store.directory(for: id("b"))!.path))+ }++ /// Prune removes only what the store itself makes: an engine folder, a scratch folder, a download.+ func testPruneLeavesEverythingElseAlone() throws {+ let (store, root) = makeStore()let fm = FileManager.default- for pin in ["aaaaaaa", "bbbbbbb", "ccccccc"] {- let dir = store.directory(for: pin)- try fm.createDirectory(at: dir.appendingPathComponent("scripts"), withIntermediateDirectories: true)- try fm.createDirectory(at: dir.appendingPathComponent("dm/tools"), withIntermediateDirectories: true)- try Data([0]).write(to: dir.appendingPathComponent("scripts/build_ipa_from_nsp.py"))- try Data([0]).write(to: dir.appendingPathComponent("dm/tools/romfs_image.py"))- try Data([0]).write(to: dir.appendingPathComponent(".upstream-ref"))+ try writeTree(store.directory(for: id("a"))!, id: id("a"))+ for name in ["notes", "aaaaaaa", ".DS_Store-dir"] {+ try fm.createDirectory(at: root.appendingPathComponent(name), withIntermediateDirectories: true)+ }+ try Data([0]).write(to: root.appendingPathComponent("readme.txt"))+ try fm.createDirectory(at: root.appendingPathComponent(".tmp-123"), withIntermediateDirectories: true)+ try Data([0]).write(to: root.appendingPathComponent(".download-123.zip"))+ XCTAssertEqual(store.prune(keeping: []), [".download-123.zip", ".tmp-123", id("a")])+ for name in ["notes", "aaaaaaa", ".DS_Store-dir", "readme.txt"] {+ XCTAssertTrue(fm.fileExists(atPath: root.appendingPathComponent(name).path), name)+ }+ }++ func testOnlyAnEngineIDBecomesAFolder() {+ let (store, _) = makeStore()+ XCTAssertNotNil(store.directory(for: id("a")))+ for bad in ["", "..", "../x", "aaaaaaa", id("a") + "/..", "/" + id("a"), id("a").uppercased()] {+ XCTAssertNil(store.directory(for: bad), bad)+ XCTAssertFalse(store.isUsable(bad), bad)}- XCTAssertTrue(store.isUsable("aaaaaaa"))- XCTAssertEqual(store.prune(keeping: ["aaaaaaa"]), ["bbbbbbb", "ccccccc"])- XCTAssertTrue(store.isUsable("aaaaaaa"))- XCTAssertFalse(fm.fileExists(atPath: store.directory(for: "bbbbbbb").path))}func testAPartiallyUnpackedTreeIsNotUsable() throws {- let root = FileManager.default.temporaryDirectory.appendingPathComponent("engine-tests-" + UUID().uuidString)- defer { try? FileManager.default.removeItem(at: root) }- let store = EngineStore(root: root)- let dir = store.directory(for: "ddddddd")+ let (store, _) = makeStore()+ let dir = store.directory(for: id("d"))!try FileManager.default.createDirectory(at: dir.appendingPathComponent("scripts"), withIntermediateDirectories: true)try Data([0]).write(to: dir.appendingPathComponent("scripts/build_ipa_from_nsp.py"))- XCTAssertFalse(store.isUsable("ddddddd"), "no dm/tools/romfs_image.py and no .upstream-ref")+ XCTAssertFalse(store.isUsable(id("d")), "no dm/tools/romfs_image.py, no .upstream-ref, no .engine-id")+ }++ /// A tree is the engine its folder names only when its own .engine-id says so.+ func testATreeThatCallsItselfSomethingElseIsNotUsable() throws {+ let (store, _) = makeStore()+ try writeTree(store.directory(for: id("a"))!, id: id("b"))+ XCTAssertFalse(store.isUsable(id("a")))+ try writeTree(store.directory(for: id("c"))!, id: nil)+ XCTAssertFalse(store.isUsable(id("c")))+ XCTAssertNil(EngineStore.identity(of: store.directory(for: id("c"))!))+ XCTAssertEqual(EngineStore.identity(of: store.directory(for: id("a"))!), id("b"), "the newline is not part of the id")}}diff --git a/frontend/Tests/NSPortBuilderTests/EngineUpdaterTests.swift b/frontend/Tests/NSPortBuilderTests/EngineUpdaterTests.swiftindex 94376121..4f478a47 100644--- a/frontend/Tests/NSPortBuilderTests/EngineUpdaterTests.swift+++ b/frontend/Tests/NSPortBuilderTests/EngineUpdaterTests.swift@@ -1,8 +1,9 @@+import CryptoKitimport XCTest@testable import NSPortBuilder-/// Channels, asset URLs and the installer (EngineUpdater.swift). The archive is built here with `ditto`,-/// so the unpack path is real; nothing touches the network.+/// Channels, asset URLs, the installer and the update check (EngineUpdater.swift, EngineUpdateModel.swift). The+/// archive is built here with `ditto`, so the unpack path is real. Nothing touches the network.final class EngineUpdaterTests: XCTestCase {private var root: URL!@@ -37,10 +38,32 @@ final class EngineUpdaterTests: XCTestCase {"https://github.com/LNPANDY/NX2Apple/releases/download/engine-experimental/engine.zip")}+ func testAnAssetIsTriedAtEverySourceAndThenOnGitHub() {+ let urls = EngineChannel.stable.assetURLs("engine.zip", version: "0.7.2",+ sources: ["https://mirror.invalid/v0.7.2", EngineChannel.releaseBase + "v0.7.2/"])+ XCTAssertEqual(urls.map(\.absoluteString), ["https://mirror.invalid/v0.7.2/engine.zip",+ "https://github.com/LNPANDY/NX2Apple/releases/download/v0.7.2/engine.zip"],+ "GitHub once, in the place the manifest put it")+ XCTAssertEqual(EngineChannel.experimental.assetURLs("e.zip", version: "0.7.2", sources: ["https://mirror.invalid/x/"]).count, 2)+ }++ func testTheSignatureSitsNextToTheManifest() {+ XCTAssertEqual(EngineChannel.experimental.signatureURL(version: "0.7.2")?.absoluteString,+ "https://github.com/LNPANDY/NX2Apple/releases/download/engine-experimental/latest.json.sig")+ }++ func testEverySwitchOfChannelIsWarnedAbout() {+ XCTAssertEqual(EngineChannel.stable.switchWarningKey(to: .experimental), "engine.switch.toExperimental")+ XCTAssertEqual(EngineChannel.experimental.switchWarningKey(to: .stable), "engine.switch.toStable")+ XCTAssertNil(EngineChannel.stable.switchWarningKey(to: .stable))+ }+// MARK: Installing- /// A tree as the release publishes it: the files the launcher runs by path, at the archive's root.- private func makeTree(_ name: String = "tree") throws -> URL {+ private func id(_ c: Character) -> String { String(repeating: c, count: 40) + "-0123456789ab" }++ /// A tree as the release publishes it: the files the launcher runs by path and its .engine-id, at the archive's root.+ private func makeTree(_ name: String = "tree", id: String? = nil, chain: String? = nil) throws -> URL {let fm = FileManager.defaultlet dir = root.appendingPathComponent(name, isDirectory: true)try fm.createDirectory(at: dir.appendingPathComponent("scripts"), withIntermediateDirectories: true)@@ -48,6 +71,11 @@ final class EngineUpdaterTests: XCTestCase {try Data("print()".utf8).write(to: dir.appendingPathComponent("scripts/build_ipa_from_nsp.py"))try Data([0]).write(to: dir.appendingPathComponent("dm/tools/romfs_image.py"))try Data("ee6648cb".utf8).write(to: dir.appendingPathComponent(".upstream-ref"))+ try Data(((id ?? self.id("a")) + "\n").utf8).write(to: dir.appendingPathComponent(".engine-id"))+ if let chain {+ try fm.createDirectory(at: dir.appendingPathComponent("dm/gfx/nvn"), withIntermediateDirectories: true)+ try Data("#pragma once\n#define DMSH_CHAIN \"\(chain)\"\n".utf8).write(to: dir.appendingPathComponent("dm/gfx/nvn/dmsh_chain.h"))+ }return dir}@@ -61,68 +89,278 @@ final class EngineUpdaterTests: XCTestCase {return out}- func testInstallingATreeMakesItUsableAndKeepsTheChosenPins() throws {- let store = EngineStore(root: root.appendingPathComponent("engine"))- let archive = try zip(makeTree())+ private var store: EngineStore { EngineStore(root: root.appendingPathComponent("engine")) }- try EngineInstaller.install(zip: archive, pin: "aaaaaaa", store: store)- XCTAssertTrue(store.isUsable("aaaaaaa"))-- try EngineInstaller.install(zip: archive, pin: "bbbbbbb", store: store, keeping: "aaaaaaa")- XCTAssertTrue(store.isUsable("bbbbbbb"))- XCTAssertTrue(store.isUsable("aaaaaaa"), "the previous pin stays for a build that is still running")+ func testInstallingATreeMakesItUsable() throws {+ let archive = try zip(makeTree(id: id("a")))+ let dest = try EngineInstaller.install(zip: archive, engineID: id("a"), store: store)+ XCTAssertEqual(dest, store.directory(for: id("a")))+ XCTAssertTrue(store.isUsable(id("a")))+ XCTAssertNoThrow(try EngineInstaller.install(zip: archive, engineID: id("a"), store: store), "a second install is a no-op")+ let left = try FileManager.default.contentsOfDirectory(atPath: store.root.path)+ XCTAssertEqual(left, [id("a")], "no scratch folder stays behind")+ }- try EngineInstaller.install(zip: archive, pin: "ccccccc", store: store, keeping: "bbbbbbb")- XCTAssertTrue(store.isUsable("ccccccc"))- XCTAssertFalse(FileManager.default.fileExists(atPath: store.directory(for: "aaaaaaa").path), "older trees are pruned")+ /// The archive has to be the engine the manifest names, or the store would hold the wrong files under its name.+ func testATreeWithAnotherIdentityIsRefused() throws {+ let archive = try zip(makeTree(id: id("b")))+ XCTAssertThrowsError(try EngineInstaller.install(zip: archive, engineID: id("a"), store: store)) {+ XCTAssertEqual($0 as? EngineInstaller.Failure, .wrongEngine(id("b")))+ }+ XCTAssertFalse(store.isUsable(id("a")))+ XCTAssertThrowsError(try EngineInstaller.install(zip: archive, engineID: "../evil", store: store)) {+ XCTAssertEqual($0 as? EngineInstaller.Failure, .badID("../evil"))+ }}- /// A release may wrap the tree in one folder; the installer takes that level.+ /// A release may wrap the tree in one folder. The installer takes that level.func testAWrappedTreeIsFound() throws {- let store = EngineStore(root: root.appendingPathComponent("engine"))let wrapped = root.appendingPathComponent("wrap", isDirectory: true)try FileManager.default.createDirectory(at: wrapped, withIntermediateDirectories: true)- try FileManager.default.copyItem(at: try makeTree(), to: wrapped.appendingPathComponent("engine"))- try EngineInstaller.install(zip: try zip(wrapped), pin: "ddddddd", store: store)- XCTAssertTrue(store.isUsable("ddddddd"))+ try FileManager.default.copyItem(at: try makeTree(id: id("d")), to: wrapped.appendingPathComponent("engine"))+ try EngineInstaller.install(zip: try zip(wrapped), engineID: id("d"), store: store)+ XCTAssertTrue(store.isUsable(id("d")))}func testAnIncompleteTreeIsRefusedAndNothingIsLeftBehind() throws {- let store = EngineStore(root: root.appendingPathComponent("engine"))let broken = root.appendingPathComponent("broken", isDirectory: true)try FileManager.default.createDirectory(at: broken.appendingPathComponent("scripts"), withIntermediateDirectories: true)try Data([0]).write(to: broken.appendingPathComponent("scripts/build_ipa_from_nsp.py")) // no dm/tools, no .upstream-ref- XCTAssertThrowsError(try EngineInstaller.install(zip: try zip(broken), pin: "eeeeeee", store: store)) { error in+ XCTAssertThrowsError(try EngineInstaller.install(zip: try zip(broken), engineID: id("e"), store: store)) { error inguard case EngineInstaller.Failure.incomplete = error else { return XCTFail("\(error)") }}- XCTAssertFalse(FileManager.default.fileExists(atPath: store.directory(for: "eeeeeee").path))+ XCTAssertFalse(FileManager.default.fileExists(atPath: store.directory(for: id("e"))!.path))+ XCTAssertEqual(try FileManager.default.contentsOfDirectory(atPath: store.root.path), [])}func testSomethingThatIsNotAnArchiveFails() throws {- let store = EngineStore(root: root.appendingPathComponent("engine"))let notZip = root.appendingPathComponent("nope.zip")try Data("not a zip".utf8).write(to: notZip)- XCTAssertThrowsError(try EngineInstaller.install(zip: notZip, pin: "fffffff", store: store)) { error in+ XCTAssertThrowsError(try EngineInstaller.install(zip: notZip, engineID: id("f"), store: store)) { error inXCTAssertEqual(error as? EngineInstaller.Failure, .unpack)}}+ /// A damaged folder under the right name (an older launcher, a crash) is replaced, not trusted.+ func testADamagedTreeUnderTheSameNameIsReplaced() throws {+ let dir = store.directory(for: id("a"))!+ try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)+ try Data([0]).write(to: dir.appendingPathComponent("junk"))+ try EngineInstaller.install(zip: try zip(makeTree(id: id("a"))), engineID: id("a"), store: store)+ XCTAssertTrue(store.isUsable(id("a")))+ XCTAssertFalse(FileManager.default.fileExists(atPath: dir.appendingPathComponent("junk").path))+ }++ // MARK: The engine in the app++ func testTheAppsEngineIsFoundAndCopiedIntoTheStore() throws {+ let resources = root.appendingPathComponent("Resources", isDirectory: true)+ try FileManager.default.createDirectory(at: resources, withIntermediateDirectories: true)+ XCTAssertNil(EngineBundle.tree(in: resources), "a launcher built from the repository has none")+ XCTAssertNil(EngineBundle.tree(in: nil))+ _ = try makeTree("Resources/engine", id: id("a"))+ let bundled = try XCTUnwrap(EngineBundle.tree(in: resources))+ try EngineInstaller.seed(from: bundled, engineID: id("a"), store: store)+ XCTAssertTrue(store.isUsable(id("a")))+ XCTAssertTrue(FileManager.default.fileExists(atPath: bundled.appendingPathComponent(".engine-id").path), "the app is left as it was")+ XCTAssertThrowsError(try EngineInstaller.seed(from: bundled, engineID: id("b"), store: store)) {+ XCTAssertEqual($0 as? EngineInstaller.Failure, .wrongEngine(id("a")))+ }+ }+// MARK: Remembering the choicefunc testTheSelectionSurvivesAndRemembersWhereItCameFrom() {let suite = UserDefaults(suiteName: "engine-selection-" + UUID().uuidString)!var s = EngineSelection.load(suite)XCTAssertEqual(s.channel, .stable, "stable by default")- XCTAssertEqual(s.pin, "")+ XCTAssertEqual(s.engineID, "")+ XCTAssertEqual(s.kept, [])s.channel = .experimental- s.switched(to: "aaaaaaa"); s.save(suite)- XCTAssertEqual(EngineSelection.load(suite).pin, "aaaaaaa")+ s.switched(to: id("a"), version: "0.7.3-experimental"); s.save(suite)+ XCTAssertEqual(EngineSelection.load(suite).engineID, id("a"))XCTAssertEqual(EngineSelection.load(suite).channel, .experimental)+ XCTAssertEqual(EngineSelection.load(suite).previousID, "", "nothing came before")- s.switched(to: "bbbbbbb"); s.save(suite)+ s.switched(to: id("b"), version: "0.7.4-experimental"); s.save(suite)let back = EngineSelection.load(suite)- XCTAssertEqual(back.pin, "bbbbbbb")- XCTAssertEqual(back.previousPin, "aaaaaaa", "the pin we came from is kept")+ XCTAssertEqual(back.engineID, id("b"))+ XCTAssertEqual(back.version, "0.7.4-experimental")+ XCTAssertEqual(back.previousID, id("a"), "the engine we came from is kept")+ XCTAssertEqual(back.kept, [id("a"), id("b")])++ s.switched(to: id("b"), version: "0.7.4-experimental")+ XCTAssertEqual(s.previousID, id("a"), "the same engine again does not forget the previous one")+ }++ // MARK: Checking and downloading++ /// Serves files from a dictionary and counts what was asked for. A missing URL is a 404, `failing` is offline.+ private final class FakeTransport: EngineTransport {+ var files: [String: Data] = [:]+ var failing = false+ var requested: [String] = []+ var downloads: [String] = []+ func data(from url: URL) -> EngineFetch<Data> {+ requested.append(url.absoluteString)+ if failing { return .failed }+ return files[url.absoluteString].map { .ok($0) } ?? .missing+ }+ func download(from url: URL, to destination: URL) -> Bool {+ downloads.append(url.absoluteString)+ guard !failing, let data = files[url.absoluteString] else { return false }+ return (try? data.write(to: destination)) != nil+ }+ }++ private let stableBase = "https://github.com/LNPANDY/NX2Apple/releases/download/v0.7.3/"+ private let experimentalBase = "https://github.com/LNPANDY/NX2Apple/releases/download/engine-experimental/"++ private func manifest(id: String, channel: String = "stable", version: String = "0.7.3", digest: String,+ size: Int? = nil, sources: [String] = []) -> Data {+ let pin = String(id.prefix(40))+ let sizeField = size.map { ",\"size\":\($0)" } ?? ""+ let list = "[" + sources.map { "\"\($0)\"" }.joined(separator: ",") + "]"+ return Data("""+ {"version":"\(version)","pin":"\(pin)","engine_id":"\(id)","channel":"\(channel)",+ "engine":{"asset":"NX2Apple-engine.zip","sha256":"\(digest)"\(sizeField)},"sources":\(list)}+ """.utf8)+ }++ /// A release with an engine archive of `id`: the manifest and the zip at `base`.+ private func publish(_ t: FakeTransport, id: String, channel: String = "stable", base: String, sources: [String] = []) throws -> Data {+ let archive = try Data(contentsOf: zip(makeTree("pub-" + UUID().uuidString, id: id)))+ let digest = AppUpdate.sha256(of: try write(archive))!+ let m = manifest(id: id, channel: channel, version: channel == "stable" ? "0.7.3" : "0.7.3-experimental",+ digest: digest, size: archive.count, sources: sources)+ t.files[base + "latest.json"] = m+ for b in (sources.isEmpty ? [base] : sources) { t.files[b + "NX2Apple-engine.zip"] = archive }+ return m+ }++ private func write(_ data: Data) throws -> URL {+ let url = root.appendingPathComponent(UUID().uuidString)+ try data.write(to: url)+ return url+ }++ private func run(_ t: FakeTransport, channel: EngineChannel = .stable, current: String = "", bundled: URL? = nil,+ publicKey: String = "", install: Bool = true) -> EngineUpdate.Outcome {+ EngineUpdate.run(channel: channel, appVersion: "0.7.3", current: current, store: store, bundled: bundled,+ publicKey: publicKey, install: install, transport: t)+ }++ func testANewEngineIsDownloadedVerifiedAndInstalled() throws {+ let t = FakeTransport()+ _ = try publish(t, id: id("a"), base: stableBase)+ XCTAssertEqual(run(t), .installed(.init(id: id("a"), version: "0.7.3"), .download))+ XCTAssertTrue(store.isUsable(id("a")))+ XCTAssertEqual(try FileManager.default.contentsOfDirectory(atPath: store.root.path), [id("a")], "the download is gone")+ XCTAssertEqual(run(t, current: id("a")), .current(.init(id: id("a"), version: "0.7.3")))+ }++ func testTheExperimentalChannelReadsItsOwnTag() throws {+ let t = FakeTransport()+ _ = try publish(t, id: id("b"), channel: "experimental", base: experimentalBase)+ XCTAssertEqual(run(t, channel: .experimental), .installed(.init(id: id("b"), version: "0.7.3-experimental"), .download))+ }++ func testWithAutomaticUpdatesOffANewEngineIsOnlyOffered() throws {+ let t = FakeTransport()+ _ = try publish(t, id: id("a"), base: stableBase)+ XCTAssertEqual(run(t, current: id("c"), install: false), .available(.init(id: id("a"), version: "0.7.3")))+ XCTAssertTrue(t.downloads.isEmpty)+ }++ /// A mirror that serves other bytes is passed over, and the next source is tried.+ func testAWrongArchiveFromOneSourceFallsBackToTheNext() throws {+ let t = FakeTransport()+ let mirror = "https://mirror.invalid/v0.7.3/"+ _ = try publish(t, id: id("a"), base: stableBase, sources: [mirror, stableBase])+ t.files[mirror + "NX2Apple-engine.zip"] = Data("tampered".utf8)+ XCTAssertEqual(run(t), .installed(.init(id: id("a"), version: "0.7.3"), .download))+ XCTAssertEqual(t.downloads, [mirror + "NX2Apple-engine.zip", stableBase + "NX2Apple-engine.zip"])+ }++ func testAnArchiveThatMatchesNoDigestInstallsNothing() throws {+ let t = FakeTransport()+ _ = try publish(t, id: id("a"), base: stableBase)+ t.files[stableBase + "NX2Apple-engine.zip"] = Data("tampered".utf8)+ XCTAssertEqual(run(t), .failed(.digest))+ XCTAssertFalse(store.isUsable(id("a")))+ XCTAssertEqual((try? FileManager.default.contentsOfDirectory(atPath: store.root.path)) ?? [], [])+ }++ func testNoArchiveAnywhereIsADownloadFailure() throws {+ let t = FakeTransport()+ _ = try publish(t, id: id("a"), base: stableBase)+ t.files[stableBase + "NX2Apple-engine.zip"] = nil+ XCTAssertEqual(run(t), .failed(.download))+ }++ func testOfflineIsANetworkFailure() {+ let t = FakeTransport()+ t.failing = true+ XCTAssertEqual(run(t), .failed(.network))+ }++ /// A release from before engine updates has no latest.json. Its stable engine is the one in the app.+ func testAReleaseWithoutAManifestUsesTheAppsEngine() throws {+ let resources = root.appendingPathComponent("Resources", isDirectory: true)+ _ = try makeTree("Resources/engine", id: id("a"))+ let bundled = try XCTUnwrap(EngineBundle.tree(in: resources))+ let t = FakeTransport()+ XCTAssertEqual(run(t, bundled: bundled), .installed(.init(id: id("a"), version: "0.7.3"), .bundle))+ XCTAssertEqual(run(t, channel: .experimental, bundled: bundled), .failed(.noManifest),+ "experimental has nothing to fall back on")+ }++ /// The manifest names the engine the app already carries: it is copied, not downloaded.+ func testTheAppsOwnEngineIsNotDownloadedAgain() throws {+ let resources = root.appendingPathComponent("Resources", isDirectory: true)+ _ = try makeTree("Resources/engine", id: id("a"))+ let t = FakeTransport()+ _ = try publish(t, id: id("a"), base: stableBase)+ XCTAssertEqual(run(t, bundled: EngineBundle.tree(in: resources)), .installed(.init(id: id("a"), version: "0.7.3"), .bundle))+ XCTAssertTrue(t.downloads.isEmpty)+ }++ func testAManifestOfTheOtherChannelIsRefused() throws {+ let t = FakeTransport()+ _ = try publish(t, id: id("a"), channel: "experimental", base: stableBase)+ XCTAssertEqual(run(t), .failed(.wrongChannel("experimental")))+ }++ func testABrokenManifestIsRefused() {+ let t = FakeTransport()+ t.files[stableBase + "latest.json"] = Data("{}".utf8)+ XCTAssertEqual(run(t), .failed(.badManifest(.notJSON)))+ }++ /// With a key built in, the signature decides. Without one (today) the manifest installs unsigned.+ func testWithAKeyOnlyASignedManifestInstalls() throws {+ let key = Curve25519.Signing.PrivateKey()+ let pub = key.publicKey.rawRepresentation.base64EncodedString()+ let t = FakeTransport()+ let m = try publish(t, id: id("a"), base: stableBase)+ XCTAssertEqual(run(t, publicKey: pub), .failed(.unsigned(.absent)))+ t.files[stableBase + "latest.json.sig"] = Data(try Curve25519.Signing.PrivateKey().signature(for: m).base64EncodedString().utf8)+ XCTAssertEqual(run(t, publicKey: pub), .failed(.unsigned(.invalid)))+ XCTAssertTrue(t.downloads.isEmpty, "nothing is fetched for a manifest that is refused")+ t.files[stableBase + "latest.json.sig"] = Data((try key.signature(for: m).base64EncodedString() + "\n").utf8)+ XCTAssertEqual(run(t, publicKey: pub), .installed(.init(id: id("a"), version: "0.7.3"), .download))+ }++ // MARK: The shader chain++ func testAChangedShaderChainIsReported() throws {+ let old = try makeTree("old", id: id("a"), chain: "v41"), same = try makeTree("same", id: id("b"), chain: "v41")+ let new = try makeTree("new", id: id("c"), chain: "v42"), none = try makeTree("none", id: id("d"))+ XCTAssertNil(EngineUpdate.chainChange(from: old.path, to: same.path))+ let change = try XCTUnwrap(EngineUpdate.chainChange(from: old.path, to: new.path))+ XCTAssertEqual(change.old, "v41")+ XCTAssertEqual(change.new, "v42")+ XCTAssertNil(EngineUpdate.chainChange(from: none.path, to: new.path), "no chain to compare with")}}