import AppKit import Foundation import Testing @testable import ParcelRealityTests @Suite struct ProtectionTests { @Test(arguments: [ "fresh", "preexisting", "unrelated-bundle", "unrelated-path", "terminated", "claimed", "pid-reuse", "missing-launch-date", "present-before-second-launch", "missing-path", "preexisting-missing-path", ]) func profileCleanupBlocksUnclaimedBrowserInstancesWithoutAdoptingThem(_ condition: String) { let bundle = RealApp.firefox.bundleID let url = URL(fileURLWithPath: "/Applications/Firefox.app") let existing = ["preexisting", "missing-launch-date", "preexisting-missing-path"].contains(condition) let claimedPID = condition == "claimed" || condition == "pid-reuse" let current = RealAppProcessIdentity( pid: existing ? 20150 : claimedPID ? 20160 : 20170, bundleID: condition == "unrelated-bundle" ? RealApp.chromium.bundleID : bundle, bundleURL: condition.contains("missing-path") ? nil : condition == "unrelated-path" ? URL(fileURLWithPath: "/Applications/Other.app") : url, launchDate: condition == "missing-launch-date" ? nil : Date(timeIntervalSince1970: existing ? 1 : condition == "claimed" ? 10 : 20), isTerminated: condition == "terminated") var launches = [ BrowserLaunchBoundary( bundleID: bundle, applicationURL: url, preexistingPIDs: [20150], requestedAt: Date(timeIntervalSince1970: 15)) ] if condition == "present-before-second-launch" { launches.append( BrowserLaunchBoundary( bundleID: bundle, applicationURL: url, preexistingPIDs: [20150, 20170], requestedAt: Date(timeIntervalSince1970: 25))) } let claimed = RealAppLaunchReceipt( app: .firefox, applicationURL: url, preexistingPIDs: [20150], pid: 20160, bundleID: bundle, bundleURL: url, isTerminated: false, launchDate: Date(timeIntervalSince1970: 10)) let blocked = BrowserProfileCleanup.unclaimedApplications([current], launches: launches, claimed: [claimed]) let shouldBlock = [ "fresh", "pid-reuse", "missing-launch-date", "present-before-second-launch", "missing-path", "preexisting-missing-path", ].contains(condition) #expect(blocked.map(\.pid) == (shouldBlock ? [current.pid] : [])) #expect(claimed.pid == 20160) } @Test func protectsUnownedVisibleWindowsNotOffscreenSystemMetadata() { let frame = ScreenRect(x: 10, y: 20, width: 600, height: 400) let rows = [ RealWindow(id: 1, pid: 10, frame: frame, layer: 0, onscreen: true, alpha: 1), RealWindow(id: 2, pid: 20, frame: frame, layer: 0, onscreen: true, alpha: 1), RealWindow(id: 3, pid: 30, frame: frame, layer: 0, onscreen: false, alpha: 1), RealWindow(id: 4, pid: 40, frame: frame, layer: 0, onscreen: true, alpha: 1), ] let protected = FrameOracle.protectedWindows(rows, ownedIDs: [2], ownedPIDs: [40]) #expect(protected.map(\.id) == [1]) } @Test @MainActor func freshLaunchConfigurationRequiresANewInstanceWithoutInteractiveErrorPrompts() { let configuration = RealApps.freshLaunchConfiguration() #expect(configuration.createsNewApplicationInstance) #expect(!configuration.promptsUserIfNeeded) #expect(!configuration.addsToRecentItems) #expect(configuration.arguments == ["-ApplePersistenceIgnoreState", "YES"]) } @Test func freshLaunchReceiptIdentifiesTheThirdInstanceWithoutAProcessListDelta() throws { let receipt = launchReceipt() #expect(try receipt.validatedPID() == 20170) let frame = ScreenRect(x: 10, y: 20, width: 600, height: 400) let rows = [20150, 20159, 20170, 20171].enumerated().map { index, pid in RealWindow(id: UInt32(index + 1), pid: Int32(pid), frame: frame, layer: 0, onscreen: true, alpha: 1) } let protected = FrameOracle.protectedWindows(rows, ownedIDs: [], ownedPIDs: [try receipt.validatedPID()]) #expect(protected.map(\.pid) == [20150, 20159, 20171]) } @Test(arguments: [Int32(-1), 0, 20150, 20159]) func launchReceiptRejectsInvalidOrPreviouslyOwnedPIDs(_ pid: Int32) { #expect(throws: RealityError.self) { try launchReceipt(pid: pid).validatedPID() } } @Test func launchReceiptRejectsATerminatedProcess() { #expect(throws: RealityError.self) { try launchReceipt(isTerminated: true).validatedPID() } } @Test(arguments: [nil, "com.apple.finder", "com.apple.TextEdit.helper"] as [String?]) func launchReceiptRejectsMissingOrUnrelatedBundleIdentity(_ bundleID: String?) { #expect(throws: RealityError.self) { try launchReceipt(bundleID: bundleID).validatedPID() } } @Test( arguments: [ nil, URL(fileURLWithPath: "/Applications/TextEdit.app"), URL(fileURLWithPath: "/System/Applications/TextEdit.app/Contents/Helpers/TextEdit.app"), URL(string: "https://example.org/System/Applications/TextEdit.app"), ] as [URL?]) func launchReceiptRejectsMissingOrDifferentBundlePaths(_ bundleURL: URL?) { #expect(throws: RealityError.self) { try launchReceipt(bundleURL: bundleURL).validatedPID() } } @Test func launchReceiptAcceptsAnEquivalentStandardizedBundlePath() throws { let receipt = launchReceipt( bundleURL: URL(fileURLWithPath: "/System/Applications/../Applications/TextEdit.app")) #expect(try receipt.validatedPID() == 20170) } @Test func launchReceiptPreservesRejectedIdentityForArtifacts() throws { let receipt = launchReceipt(pid: 20159) let restored = try JSONDecoder().decode(RealAppLaunchReceipt.self, from: JSONEncoder().encode(receipt)) #expect(restored.app == .textEdit) #expect(restored.pid == 20159) #expect(restored.preexistingPIDs == [20150, 20159]) #expect(restored.applicationURL == receipt.applicationURL) #expect(restored.bundleURL == receipt.bundleURL) #expect(restored.bundleID == receipt.bundleID) #expect(throws: RealityError.self) { try restored.validatedPID() } } private func launchReceipt( pid: Int32 = 20170, bundleID: String? = RealApp.textEdit.bundleID, bundleURL: URL? = URL(fileURLWithPath: "/System/Applications/TextEdit.app"), isTerminated: Bool = false ) -> RealAppLaunchReceipt { RealAppLaunchReceipt( app: .textEdit, applicationURL: URL(fileURLWithPath: "/System/Applications/TextEdit.app"), preexistingPIDs: [20150, 20159], pid: pid, bundleID: bundleID, bundleURL: bundleURL, isTerminated: isTerminated) } }