import Foundation import Security @main struct ProfileStoreChecks { static func require(_ condition: @autoclosure () -> Bool, _ message: String) throws { if !condition() { throw NSError(domain: "ProfileStoreChecks", code: 1, userInfo: [NSLocalizedDescriptionKey: message]) } } static func fails(_ action: () throws -> Void) throws { var rejected = false do { try action() } catch { rejected = true } try require(rejected, "Unsafe operation was accepted") } static func main() throws { let fm = FileManager.default let temporary = fm.temporaryDirectory.appendingPathComponent(UUID().uuidString) try fm.createDirectory(at: temporary, withIntermediateDirectories: true) defer { try? fm.removeItem(at: temporary) } var calls = 0 let root = temporary.appendingPathComponent("profiles") let store = LocalProfileStore(root: root, legacyReader: { _, interactive in calls += 1 try require(!interactive, "Startup requested interactive authorization") return Data("{\"nodes\":[]}".utf8) }) let first = try store.load(.proxy) try require(first == "{\"nodes\":[]}", "Silent migration lost content") try store.save("{\"nodes\":[\"fixture\"]}", kind: .proxy) let loaded = try store.load(.proxy) try require(loaded == "{\"nodes\":[\"fixture\"]}", "Save/load mismatch") try require(calls == 1, "Normal reads accessed Keychain") let dirAttrs = try fm.attributesOfItem(atPath: root.path) let file = root.appendingPathComponent("proxy-profile.json") let attrs = try fm.attributesOfItem(atPath: file.path) try require((dirAttrs[.posixPermissions] as? NSNumber)?.intValue == 0o700, "Directory is not private") try require((attrs[.posixPermissions] as? NSNumber)?.intValue == 0o600, "File is not private") try fails { try store.save(String(repeating: "x", count: 65537), kind: .proxy) } let retained = try store.load(.proxy) try require(retained == loaded, "Rejected save replaced valid content") try store.delete(.proxy) let deleted = try store.load(.proxy) try require(deleted == nil && calls == 1, "Deleted profile resurrected from Keychain") var attempts = [Bool]() let pendingRoot = temporary.appendingPathComponent("pending") let pending = LocalProfileStore(root: pendingRoot, legacyReader: { _, interactive in attempts.append(interactive) if !interactive { throw NSError(domain: NSOSStatusErrorDomain, code: Int(errSecInteractionNotAllowed)) } return Data("network_name = \"fixture\"".utf8) }) _ = try pending.load(.network) _ = try pending.load(.network) try require(attempts == [false], "Startup retried Keychain repeatedly") let hasPending = try pending.migrationPending() try require(hasPending, "Missing manual migration state") try pending.migratePending() let network = try pending.load(.network) let stillPending = try pending.migrationPending() try require(network == "network_name = \"fixture\"" && !stillPending && attempts == [false, true], "Manual migration failed") let reopened = LocalProfileStore(root: pendingRoot, legacyReader: { _, _ in throw NSError(domain: "Unexpected Keychain access", code: 1) }) let reopenedText = try reopened.load(.network) try require(reopenedText == network, "Restart did not read local profile") let outside = temporary.appendingPathComponent("outside") try Data("unchanged".utf8).write(to: outside) try fm.createSymbolicLink(at: file, withDestinationURL: outside) try fails { _ = try store.load(.proxy) } try fails { try store.save("replacement", kind: .proxy) } let outsideText = try String(contentsOf: outside, encoding: .utf8) try require(outsideText == "unchanged", "Symlink target was modified") try fm.removeItem(at: file) try Data([0xff]).write(to: file) try fails { _ = try store.load(.proxy) } try require(calls == 1, "Corrupt file silently fell back to legacy credentials") print("PASS: local read/write, private permissions, atomic replacement, migration, deletion, restart, size limit and symlink/corruption handling") } }