import Foundation import Darwin /// Run only inside a temporary signed app with the built host's entitlements. /// This opt-in integration check contacts the same HTTPS endpoint as the app. @main struct SandboxNetworkChecks { static func main() async { let before = persistentNetworkFiles() let service = PublicIPAddressService() do { guard await service.address(route: "sandbox-check") != nil else { print("FAIL: production public-address lookup did not return a valid address") exit(1) } // Repeat both the memory-cache path and a fresh route without enabling HTTP storage. guard await service.address(route: "sandbox-check") != nil, await service.address(route: "sandbox-new-route") != nil else { exit(1) } try await Task.sleep(for: .seconds(1)) let after = persistentNetworkFiles() guard after == before else { print("FAIL: public-address lookup created or changed persistent network files") exit(1) } print("PASS: signed sandbox production HTTPS, repeat/route lookup and no persistent network files") } catch { // Keep the public address and potentially identifying error URLs out of logs. let error = error as NSError print("FAIL: sandbox HTTPS request: \(error.domain) code \(error.code)") exit(1) } } private static func persistentNetworkFiles() -> [String: String] { let library = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Library") var files: [String: String] = [:] for directory in ["Caches", "HTTPStorages", "Cookies", "WebKit"] { let root = library.appendingPathComponent(directory) guard let entries = FileManager.default.enumerator(at: root, includingPropertiesForKeys: [.isRegularFileKey, .fileSizeKey, .contentModificationDateKey], options: []) else { continue } for case let url as URL in entries { guard let value = try? url.resourceValues(forKeys: [.isRegularFileKey, .fileSizeKey, .contentModificationDateKey]), value.isRegularFile == true else { continue } files[url.path] = "\(value.fileSize ?? 0)|\(value.contentModificationDate?.timeIntervalSince1970 ?? 0)" } } return files } }