# Full-Disk Access Automation Test on macOS

Canonical: https://figment.so/blog/mac-full-disk-access-automation-test

Reset Full Disk Access with tccutil, run your helper as a subprocess and assert its NSError domain and code. The denied half runs on any Mac you administer. Re-granting needs MDM, SIP changes or a second pre-granted host.

**Reset Full Disk Access (FDA) with `tccutil reset SystemPolicyAllFiles <bundleID>`, run your helper as a subprocess, and assert the `NSError` domain and code it reports.** The denied half of the test needs only macOS, `tccutil` and XCTest on a Mac you administer, and `tccutil reset` does not need SIP disabled. Granting FDA again is the hard half, and the options are below. Reference APIs: [NSError](https://developer.apple.com/documentation/foundation/nserror) and the [App Sandbox overview](https://developer.apple.com/documentation/security/app_sandbox).

## How does macOS enforce Full Disk Access?

Through the **Transparency, Consent, and Control (TCC)** subsystem. When an app or helper reads a protected path (`~/Library/Mail`, `~/Library/Safari`, parts of `~/Library/Application Support`, or system locations under `/private`), the TCC daemon checks for a grant against the service identifier `kTCCServiceSystemPolicyAllFiles`. The string form used with `tccutil` is `SystemPolicyAllFiles`.

With no grant, the read fails. On the macOS versions we checked, the error is typically in `NSCocoaErrorDomain` with code `NSFileReadNoPermissionError` (257), or in `NSPOSIXErrorDomain` with `EACCES` (13) when the helper surfaces the underlying `errno`. Verify the numeric value against the headers in your installed Xcode:

```bash
open $(xcrun --show-sdk-path)/System/Library/Frameworks/Foundation.framework/Headers/FoundationErrors.h
```

Your helper should surface a specific documented error rather than a generic failure. That specificity is what this test pins down.

## How do I remove Full Disk Access with tccutil?

Run `tccutil reset`. It clears grants in the user-writable part of the TCC store, so it does not require SIP to be disabled.

```bash
tccutil reset SystemPolicyAllFiles com.example.HelperApp
```

After this, the next FDA-gated operation from that bundle ID has no grant row and fails. Calling it in `setUpWithError()` starts every run from a known denied state.

```swift
override func setUpWithError() throws {
    let proc = Process()
    proc.executableURL = URL(fileURLWithPath: "/usr/bin/tccutil")
    proc.arguments = ["reset", "SystemPolicyAllFiles", "com.example.HelperApp"]
    try proc.run()
    proc.waitUntilExit()
    guard proc.terminationStatus == 0 else {
        throw XCTSkip("tccutil reset failed; verify bundle ID and host permissions")
    }
}
```

## How do I assert the helper's NSError?

Launch the helper as a subprocess, read a JSON-encoded error payload from its standard error, and decode it. The test treats the helper as a black box and asserts only the public error contract.

```swift
func testHelperReportsMissingFDAError() throws {
    let (domain, code) = try runHelperAndCaptureError()

    XCTAssertEqual(domain, NSCocoaErrorDomain,
        "Helper must report NSCocoaErrorDomain for a file permission error")
    XCTAssertEqual(code, NSFileReadNoPermissionError,
        "Error code must be NSFileReadNoPermissionError (257) when FDA is denied")

    attachErrorEvidence(phase: "fda_denied",
                        domain: domain, code: code, to: self)
}

private func runHelperAndCaptureError() throws -> (domain: String, code: Int) {
    let helperURL = Bundle(for: Self.self)
        .url(forResource: "HelperApp", withExtension: nil)!

    let pipe = Pipe()
    let proc = Process()
    proc.executableURL = helperURL
    proc.standardError = pipe
    try proc.run()
    proc.waitUntilExit()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    guard let json = try? JSONDecoder().decode(ErrorPayload.self, from: data) else {
        throw XCTSkip("Helper did not produce a JSON error payload on stderr")
    }
    return (json.domain, json.code)
}

private struct ErrorPayload: Decodable {
    let domain: String
    let code: Int
}
```

This is an untested example. Run it against your own helper and confirm the exact code your code paths emit.

## How do I test the granted state after a reset?

`tccutil reset` removes grants but cannot add them. Inserting a grant means writing to the TCC database, which **System Integrity Protection (SIP)** protects from modification even by root. Disabling SIP requires rebooting into Recovery and running `csrutil disable` there. The TCC schema is not a documented public interface, and changes between releases have broken third-party scripts. Pick one of these approaches:

- **Two separate jobs.** One job resets FDA and asserts the error. A second job runs on a machine where FDA is already granted and asserts success. A reporting step compares the two JSON evidence files. This never touches SIP, and it is the simplest option for most teams.
- **MDM configuration profile.** In a managed fleet, a Privacy Preferences Policy Control (PPPC) payload grants FDA without SIP changes. This suits enterprise CI.
- **SIP disabled on a dedicated test host.** Use this only on a machine that is not a developer's daily driver, because SIP removes a layer of kernel-level protection. Verify behavior on the exact macOS build before relying on any TCC-write technique. We have not confirmed that a Figment Mac or VM can boot into Recovery, so do not plan around this option on a hosted Mac without checking.

The granted-state half of the two-job pattern:

```swift
func testHelperSucceedsWithFDA() throws {
    // Must run on an FDA-granted host. Guard against misrouting:
    let probe = FileManager.default.homeDirectoryForCurrentUser
        .appendingPathComponent("Library/Safari/History.db")
    guard FileManager.default.isReadableFile(atPath: probe.path) else {
        throw XCTSkip("FDA not granted on this host; route to the granted CI agent")
    }

    let (domain, code) = try runHelperSuccessResult()
    XCTAssertNil(domain, "No error expected when FDA is granted")
    XCTAssertEqual(code, 0, "Helper should exit 0 after a protected-file read")

    attachErrorEvidence(phase: "fda_granted",
                        domain: domain ?? "none", code: code, to: self)
}
```

## How do I capture before and after as JSON evidence?

Attach one JSON file per phase to the test, stored inside the `.xcresult` bundle.

```swift
func attachErrorEvidence(phase: String,
                         domain: String,
                         code: Int,
                         to test: XCTestCase) {
    let payload: [String: Any] = [
        "phase":     phase,
        "domain":    domain,
        "code":      code,
        "timestamp": ISO8601DateFormatter().string(from: Date()),
        "bundleID":  "com.example.HelperApp"
    ]
    guard let data = try? JSONSerialization.data(
        withJSONObject: payload, options: .prettyPrinted) else { return }

    let attachment = XCTAttachment(data: data,
                                   uniformTypeIdentifier: "public.json")
    attachment.name = "fda-evidence-\(phase).json"
    attachment.lifetime = .keepAlways
    test.add(attachment)
}
```

`fda-evidence-fda_denied.json` and `fda-evidence-fda_granted.json` land together in the result bundle and form an auditable before and after record for change management or security review.

## Where do the denied-state and granted-state jobs run?

For the denied-state job, the Intel Mac VM ($25 a month, $2 a day) is enough: it runs macOS Sequoia with `tccutil`, XCTest and Xcode 26.3 or earlier, and you have administrator access to your own VM. Use the Apple Silicon Mac VM ($39 a month) when you need Xcode 26.4 or later. The granted-state job needs a host where FDA is already granted, for example by MDM or a manual grant through System Settings in a remote desktop session. See the [Mac hosting plans](/mac-hosting) and check the macOS version shown before purchase. We have not run this test on any plan.

**Related guides:**
- [Safari WebRTC Permissions Test](/blog/mac-safari-webrtc-permissions-test)
- [iOS Simulator Regression Screenshots](/blog/mac-ios-simulator-regression-screenshots)
- [Self-Hosted GitHub Actions Runner on Mac](/blog/mac-github-actions-self-hosted-runner)
