| 1 | import Foundation |
| 2 | |
| 3 | @main |
| 4 | struct MacDiagnosticTest { |
| 5 | static func main() throws { |
| 6 | let temporary = FileManager.default.temporaryDirectory.appendingPathComponent("cage-native-diagnostics-" + UUID().uuidString) |
| 7 | try FileManager.default.createDirectory(at: temporary, withIntermediateDirectories: false) |
| 8 | defer { try? FileManager.default.removeItem(at: temporary) } |
| 9 | let logs = temporary.appendingPathComponent("Library/Logs/Cage") |
| 10 | try GuestDiagnostic.append("window", "window_changed", directory: logs) |
| 11 | try GuestDiagnostic.append("clipboard", "clipboard_write_failed", directory: logs) |
| 12 | let file = logs.appendingPathComponent("window.jsonl") |
| 13 | let original = try Data(contentsOf: file) |
| 14 | try GuestDiagnostic.append("window", "password=private-secret", directory: logs) |
| 15 | guard try Data(contentsOf: file) == original else { throw CocoaError(.fileWriteUnknown) } |
| 16 | for _ in 0..<7 { |
| 17 | try Data(repeating: 120, count: 1 << 20).write(to: file) |
| 18 | try GuestDiagnostic.append("window", "frame_unavailable", directory: logs) |
| 19 | } |
| 20 | let names = try FileManager.default.contentsOfDirectory(atPath: logs.path) |
| 21 | guard names.filter({ $0.hasPrefix("window") && $0.hasSuffix(".jsonl") }).count == 5 else { throw CocoaError(.fileWriteUnknown) } |
| 22 | let target = temporary.appendingPathComponent("preserved.txt") |
| 23 | try Data("preserved".utf8).write(to: target) |
| 24 | try FileManager.default.removeItem(at: file) |
| 25 | try FileManager.default.createSymbolicLink(at: file, withDestinationURL: target) |
| 26 | do { |
| 27 | try GuestDiagnostic.append("window", "window_changed", directory: logs) |
| 28 | throw CocoaError(.fileWriteUnknown) |
| 29 | } catch CocoaError.fileReadNoPermission { } |
| 30 | guard try Data(contentsOf: target) == Data("preserved".utf8) else { throw CocoaError(.fileWriteUnknown) } |
| 31 | print("Native macOS diagnostic schema, rotation and link rejection passed.") |
| 32 | } |
| 33 | } |