// A test can never reach the owner's Contacts, Photos, Calendar or Mail — held // by a sweep rather than by care. // // **The near miss.** Run 5653's swift job on the Mac mini ran a tidy test // against the Mac's own `CNContactStore`: the signed-in owner's iCloud address // book, reached over XPC. Every write was refused and the run hung; one grant // later it would have written invented cards into his real contacts. Moving // that one test to a simulator fixed the test, not the class. The class is // fixed in the shape of the code: the package builds no store, the phone app // target builds the address book and installs it into `LiveStores` at launch, // and a test bundle links the package and never an app — so a test cannot // name the live book (the compile fails), and in a test process every door is // shut (`LiveStores.addressBook` is nil, a read is refused, and no // permission prompt for the person's contacts or photos is raised). // // This sweep is what keeps that shape: // * no package source names a live store, and in the apps only the one // file per app that builds the address book does; // * nothing but an app's launch installs one; // * no test source names a live store at all; // * nothing builds a contacts save: the address book is read, never written. // Calendar and Mail have no bridge today; their live types and the Mail and // Calendar data paths are listed so that the first bridge has to come // through `LiveStores` and get a line here. import CottonKit import Foundation import XCTest @testable import CottonUI final class LiveStoresSweepTests: XCTestCase { /// What reaches a person's own store: building one, fetching from one, /// changing one, or scripting the app that holds one. private static let liveStores = [ // Contacts "CNContactStore(", // Photos — the picker (`PhotosUI`) runs out of process and reads nothing; these do. "PHPhotoLibrary.shared", "PHAsset.fetch", "PHAssetCollection.fetch", "PHCollectionList.fetch", "PHImageManager", "PHCachingImageManager", "PHAssetChangeRequest", "PHAssetCreationRequest", "Photos Library.photoslibrary", // Calendar and Reminders "EKEventStore", "Library/Calendars", // Mail — the Mac's Mail app and its store "SBApplication", "NSAppleScript", "OSAScript", "com.apple.mail", "Library/Mail", "group.com.apple.mail", // The address book's files on a Mac "Application Support/AddressBook", ] /// Only an app's launch installs a store. private static let installing = "LiveStores.install(" /// In a TEST source, any mention of these is a reach for a person's store: /// even a standing-permission read means the test is about the real thing. private static let liveInTests = liveStores + [ "CNContactStore", "PHPhotoLibrary", "SystemAddressBook", installing, ] private struct Exemption { let api: String let file: String let reason: String } private static let exemptions: [Exemption] = [ Exemption( api: "CNContactStore(", file: "App/Cotton/AddressBook+Contacts.swift", reason: "The phone app's address book: the one store it builds, installed at launch."), Exemption( api: "PHPhotoLibrary.shared", file: "App/Cotton/CameraRoll+Photos.swift", reason: "The phone's camera roll, write-only: a Camera tool shot added after it is in Cotton, installed at launch."), Exemption( api: "PHAssetCreationRequest", file: "App/Cotton/CameraRoll+Photos.swift", reason: "The one asset a shot creates in the camera roll; nothing is read back."), Exemption( api: installing, file: "App/Cotton/CottonApp.swift", reason: "The phone app's launch, before any screen exists."), ] /// The sweeps themselves name what they look for. private static let sweeps = ["LiveStoresSweepTests.swift"] // MARK: - The package and the apps /// RULE: no package source names a live Contacts, Photos, Calendar or Mail store, and in the apps only the one file per app that builds the address book does — so nothing a test links can reach the owner's own. func testOnlyTheAppsBuildAStoreAndOnlyTheirLaunchInstallsIt() throws { let sources = try AppSources.all() /// RULE: The live-store guard must read source before it can reject a second store. XCTAssertFalse(sources.isEmpty, "the sweep read no sources") var offenders: [String] = [] var used = Set() for (name, text) in sources { let code = SourceSweep.withoutComments(text) for api in Self.liveStores + [Self.installing] where code.contains(api) { if let exemption = Self.exemptions.first(where: { $0.api == api && name.hasSuffix($0.file) }) { used.insert(exemption.api + " " + exemption.file) } else { offenders.append("\(name) says \(api)") } } } XCTAssertEqual( offenders, [], """ A person's own store reached from outside the one place that may. The package \ builds no store: the apps build it and install it into `LiveStores` at launch, \ and everything else goes through `LiveStores` — which is shut in a test process. \ A new bridge (Photos, Calendar, Mail) is a protocol in `LiveStores`, an \ implementation in each app target, and an exemption here with its reason. """) // Every exemption is still used — one that names nothing covers nothing. for exemption in Self.exemptions { XCTAssertTrue( used.contains(exemption.api + " " + exemption.file), "\(exemption.file) is allowed \(exemption.api) and no longer says it: drop the exemption") XCTAssertGreaterThan(exemption.reason.count, 30, "\(exemption.file) has no reason written down") } } // MARK: - The tests /// RULE: no test source names a live store or installs one — not even on a simulator: every test fakes the protocol, so nothing a test does can reach a person's own book. func testNoTestReachesAPersonsStore() throws { let tests = try Self.testSources() XCTAssertGreaterThan(tests.count, 100, "the sweep read almost no tests") var offenders: [String] = [] for (name, text) in tests where !Self.sweeps.contains(where: { name.hasSuffix($0) }) { let code = SourceSweep.withoutComments(text) let hits = Self.liveInTests.filter { code.contains($0) } guard !hits.isEmpty else { continue } offenders.append("\(name) says \(hits.joined(separator: ", "))") } XCTAssertEqual( offenders, [], """ A test that reaches a person's own store. On a Mac the test process is signed \ in to the owner's iCloud, so a store built there is HIS. Fake the protocol \ (`AddressBook`) instead. """) } /// RULE: the phone's address book is a source Cotton imports from, never a sink it rewrites (architect, 14 Oct 2026): no source in the package or the apps builds a contacts save, and the address-book door offers reads only — writing into system contacts is the one act that can wreck data outside Cotton. func testNothingWritesThePersonsAddressBook() throws { let sources = try AppSources.all() XCTAssertFalse(sources.isEmpty, "the sweep read no sources") let writes = ["CNSaveRequest", "CNMutableContact", "CNMutableGroup"] var offenders: [String] = [] for (name, text) in sources { let code = SourceSweep.withoutComments(text) for write in writes where code.contains(write) { offenders.append("\(name) says \(write)") } } XCTAssertEqual(offenders, [], "Cotton reads the address book and never writes it: People is the clean book") } // MARK: - This process /// RULE: a test process has no live store — nothing installed one, a read through the address-book door is refused, and the person's contacts are never asked for. func testThisProcessHasNoLiveStore() async { XCTAssertNil(LiveStores.addressBook) XCTAssertFalse(LiveStores.installed) XCTAssertThrowsError(try LiveStores.reachAddressBook()) { error in XCTAssertEqual(error as? LiveStoreUnreachable, LiveStoreUnreachable(store: "address book")) } XCTAssertNil(ContactBook.picture(of: "any-card")) } // MARK: - Reading private static func testSources() throws -> [(name: String, text: String)] { let root = SourceSweep.repoRoot.appendingPathComponent("apps/cotton/Tests") var found: [(String, String)] = [] let walker = FileManager.default.enumerator(at: root, includingPropertiesForKeys: nil) while let url = walker?.nextObject() as? URL { guard url.pathExtension == "swift" else { continue } let text = try String(contentsOf: url, encoding: .utf8) found.append((url.path.replacingOccurrences(of: root.path + "/", with: "Tests/"), text)) } return found.sorted { $0.0 < $1.0 } } }