Repository Public License
1// ORIX Secure2// File: Packages/ORIXSecureEngines/Tests/ORIXSecureEnginesTests/DeviceStoragePrivacyTests.swift3// Description: Device diagnostics and advice, storage thresholds and verified cleanup of app-owned folders, privacy audit, and the4// live system probes on the host Mac.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import ORIXSecureModel10import Testing11@testable import ORIXSecureEngines1213@Suite struct DeviceDiagnosticsTests {14 let engine = DeviceDiagnosticsEngine()1516 @Test func sectionsShowReadingsAndKeepUnavailableOnesExplicit() {17 let device = Fixture.device(simulator: true, battery: .unavailable(.simulator, "The Simulator has no battery."), state: .unavailable(.simulator, "no"))18 let diagnostics = engine.diagnostics(device)19 #expect(diagnostics.sections.map(\.id) == ["identity", "display", "processing", "power", "storage"])20 let rows = diagnostics.sections.flatMap(\.rows)21 #expect(Set(rows.map(\.id)).count == rows.count)22 #expect(rows.first { $0.id == "battery" }?.value.unavailability?.kind == .simulator)23 #expect(rows.first { $0.id == "thermal" }?.note?.contains("temperature") == true)24 #expect(rows.first { $0.id == "model" }?.value.value == "iPhone18,1")25 #expect(!rows.contains { $0.source.isEmpty })26 // No reading carries a degree symbol, a cycle count or a "health" percentage: iOS does not provide them.27 let text = rows.compactMap(\.value.value).joined(separator: " ")28 #expect(!text.contains("°"))29 let unavailable = Set(diagnostics.unavailable.map(\.id))30 #expect(unavailable.isSuperset(of: ["battery.cycles", "battery.capacity", "cpu.temperature", "memory.others", "cpu.system"]))31 }3233 @Test func thermalAndCoreFindings() {34 #expect(engine.findings(Fixture.device()).map(\.state) == [.verified])35 let hot = engine.findings(Fixture.device(thermal: .critical, cores: 6, activeCores: 4))36 #expect(hot.first { $0.id == "device.thermal" }?.severity == .moderate)37 #expect(hot.contains { $0.id == "device.cores" })38 #expect(engine.findings(Fixture.device(thermal: .serious)).first?.severity == .low)39 #expect(engine.findings(Fixture.device(lowPower: true)).contains { $0.id == "device.lowPower" && $0.state == .verified })40 #expect(Fixture.assertHonest(hot).isEmpty, "\(Fixture.assertHonest(hot))")41 }4243 @Test func adviceIsGroundedInReadings() {44 let low = engine.advice(Fixture.device(battery: .measured(0.15)))45 #expect(low.first { $0.id == "battery.low" }?.basis.first?.value.contains("15") == true)46 // Not suggested when Low Power Mode is already on, or while charging.47 #expect(!engine.advice(Fixture.device(lowPower: true, battery: .measured(0.15))).contains { $0.id == "battery.low" })48 #expect(!engine.advice(Fixture.device(battery: .measured(0.15), state: .measured(.charging))).contains { $0.id == "battery.low" })49 #expect(engine.advice(Fixture.device(battery: .measured(1), state: .measured(.full))).contains { $0.id == "battery.full" })50 #expect(engine.advice(Fixture.device(thermal: .serious)).contains { $0.id == "thermal.hot" })51 #expect(engine.advice(Fixture.device(total: 100 * Fixture.gb, free: 5 * Fixture.gb)).contains { $0.id == "storage.pressure" })52 // With no battery reading, no battery observation is made up.53 let simulated = engine.advice(Fixture.device(battery: .unavailable(.simulator, "x"), state: .unavailable(.simulator, "x")))54 #expect(!simulated.contains { $0.kind == .observation })55 #expect(simulated.contains { $0.id == "check.health" && $0.kind == .userCheck })56 for advice in low where advice.kind == .observation { #expect(!advice.basis.isEmpty) }57 #expect(DeviceDiagnosticsEngine.notOffered.count == 4)58 }5960 @Test @MainActor func systemProbeReturnsRealValuesOnThisMac() {61 let snapshot = SystemDeviceProbe().snapshot()62 #expect(!snapshot.modelIdentifier.isEmpty && snapshot.modelIdentifier != "unknown")63 #expect(snapshot.systemMajorVersion >= 15)64 #expect(snapshot.processorCount >= 1)65 #expect(snapshot.physicalMemory > 1_000_000_000)66 #expect((snapshot.volume.total.value ?? 0) > 1_000_000_000)67 #expect((snapshot.volume.available.value ?? 0) > 0)68 #expect((snapshot.appMemoryFootprint.value ?? 0) > 1_000_000)69 let boot = snapshot.lastBoot.value70 #expect(boot != nil && boot! < Date() && boot! > Date(timeIntervalSince1970: 1_600_000_000))71 // On macOS the iOS-only readings are explicitly unavailable.72 #expect(snapshot.batteryLevel.unavailability?.kind == .unsupportedOnDevice)73 #expect(snapshot.appAvailableMemory.unavailability?.kind == .unsupportedOnDevice)74 let signals = SystemSecurityProbe().signals(isSimulator: false, screenCaptured: .unavailable(.notRequested, "test"))75 #expect(signals.debuggerAttached.value == false)76 #expect(signals.integrityChecksPerformed >= SystemSecurityProbe.jailbreakPaths.count)77 #expect(signals.passcodeSet.isMeasured || signals.passcodeSet.unavailability != nil)78 print("host probe: \(snapshot.modelIdentifier), \(snapshot.systemName) \(snapshot.systemVersion), footprint \(snapshot.appMemoryFootprint.value ?? 0) B, integrity checks \(signals.integrityChecksPerformed), indicators \(signals.integrityIndicators.map(\.observation))")79 }80}8182@Suite struct StorageAnalysisTests {83 private func makeLocations() throws -> (OwnedLocations, URL) {84 let root = FileManager.default.temporaryDirectory.appendingPathComponent("orix-storage-\(UUID().uuidString)")85 let locations = OwnedLocations(exports: root.appendingPathComponent("exports"),86 caches: root.appendingPathComponent("caches"), temporary: root.appendingPathComponent("tmp"),87 database: root.appendingPathComponent("db"))88 for kind in OwnedStorageEntry.Kind.allCases {89 try FileManager.default.createDirectory(at: locations.url(for: kind), withIntermediateDirectories: true)90 }91 return (locations, root)92 }9394 private func write(_ bytes: Int, to url: URL) throws {95 try Data(repeating: 7, count: bytes).write(to: url)96 }9798 private func volume(total: Int64, free: Int64, important: Bool = true) -> VolumeCapacity {99 VolumeCapacity(total: .measured(total), available: .measured(free),100 availableForImportantUsage: important ? .measured(free) : .unavailable(.failed, "n/a"),101 availableForOpportunisticUsage: .unavailable(.failed, "n/a"))102 }103104 @Test func capacityThresholds() throws {105 let (locations, root) = try makeLocations()106 defer { try? FileManager.default.removeItem(at: root) }107 let engine = StorageAnalysisEngine(locations: locations)108 let gb = Fixture.gb109 #expect(engine.capacityFinding(volume(total: 256 * gb, free: 100 * gb)).state == .verified)110 #expect(engine.capacityFinding(volume(total: 256 * gb, free: 20 * gb)).severity == .low) // under 10 %111 #expect(engine.capacityFinding(volume(total: 32 * gb, free: 4 * gb)).severity == .low) // 12.5 % but under 5 GB112 #expect(engine.capacityFinding(volume(total: 256 * gb, free: 10 * gb)).severity == .moderate) // under 5 %113 #expect(engine.capacityFinding(volume(total: 16 * gb, free: Int64(1.5 * 1e9))).severity == .moderate) // under 2 GB114 let unknown = VolumeCapacity(total: .unavailable(.failed, "no"), available: .unavailable(.failed, "no"),115 availableForImportantUsage: .unavailable(.failed, "no"), availableForOpportunisticUsage: .unavailable(.failed, "no"))116 #expect(engine.capacityFinding(unknown).state == .notAvailable)117 // Falls back to plain free space, and says which key it used.118 let fallback = engine.capacityFinding(volume(total: 256 * gb, free: 100 * gb, important: false))119 #expect(fallback.evidence.first?.source == "URLResourceKey.volumeAvailableCapacityKey")120 #expect(Fixture.assertHonest([engine.capacityFinding(volume(total: 256 * gb, free: 10 * gb))]).isEmpty)121 }122123 @Test func ownedStorageIsMeasuredAndCleanupIsVerified() throws {124 let (locations, root) = try makeLocations()125 defer { try? FileManager.default.removeItem(at: root) }126 try write(200_000, to: locations.caches.appendingPathComponent("fingerprints.bin"))127 try FileManager.default.createDirectory(at: locations.temporary.appendingPathComponent("nested"), withIntermediateDirectories: true)128 try write(50_000, to: locations.temporary.appendingPathComponent("nested/a.tmp"))129 try write(50_000, to: locations.temporary.appendingPathComponent("b.tmp"))130 try write(30_000, to: locations.database.appendingPathComponent("store.sqlite"))131 let engine = StorageAnalysisEngine(locations: locations)132133 let overview = engine.overview(volume: volume(total: 256 * Fixture.gb, free: 100 * Fixture.gb))134 let byKind = Dictionary(uniqueKeysWithValues: overview.owned.map { ($0.kind, $0) })135 #expect(byKind[.caches]?.fileCount == 1)136 #expect((byKind[.caches]?.bytes ?? 0) >= 200_000)137 #expect(byKind[.temporary]?.fileCount == 2)138 #expect(byKind[.exports]?.bytes == 0)139 #expect(overview.clearableBytes == (byKind[.caches]?.bytes ?? 0) + (byKind[.temporary]?.bytes ?? 0))140 #expect(overview.notExposed.map(\.id) == ["storage.categories"])141142 let result = engine.clear([.caches, .temporary, .database])143 #expect(result.freedBytes == overview.clearableBytes)144 #expect(result.filesRemoved == 3)145 #expect(result.errors.count == 1, "the database is refused")146 #expect(FileManager.default.fileExists(atPath: locations.database.appendingPathComponent("store.sqlite").path))147 #expect(StorageAnalysisEngine.measure(locations.temporary).files == 0)148 // Clearing again frees nothing and reports nothing.149 #expect(engine.clear([.caches, .temporary]).freedBytes == 0)150 // The database is never cleared by cleanup, whatever is asked.151 let refused = engine.clear([.database])152 #expect(refused.filesRemoved == 0)153 #expect(refused.errors.count == 1)154 }155156 @Test func measureHandlesMissingPathsAndSingleFiles() throws {157 #expect(StorageAnalysisEngine.measure(URL(fileURLWithPath: "/nonexistent/orix")).bytes == 0)158 let file = FileManager.default.temporaryDirectory.appendingPathComponent("orix-one-\(UUID().uuidString)")159 try write(10_000, to: file)160 defer { try? FileManager.default.removeItem(at: file) }161 #expect(StorageAnalysisEngine.measure(file).files == 1)162 #expect(StorageAnalysisEngine.measure(file).bytes >= 10_000)163 }164}165166@Suite struct PrivacyAuditTests {167 @Test func appPermissionsAreFactsAboutThisAppOnly() {168 let permissions = [169 AppPermission(kind: .photos, status: .limited, source: "PHPhotoLibrary.authorizationStatus(for: .readWrite)"),170 AppPermission(kind: .camera, status: .notRequested, source: "AVCaptureDevice.authorizationStatus(for: .video)"),171 AppPermission(kind: .location, status: .denied, source: "CLLocationManager.authorizationStatus"),172 AppPermission(kind: .localNetwork, status: .unknown, source: "none"),173 ]174 let audit = PrivacyAuditEngine().audit(permissions: permissions, responses: [], now: Fixture.now)175 #expect(audit.appPermissions.count == 4)176 #expect(audit.appPermissions.first { $0.id == "privacy.app.localNetwork" }?.state == .notAvailable)177 #expect(audit.appPermissions.first { $0.id == "privacy.app.photos" }?.state == .verified)178 #expect(!audit.appPermissions.contains { $0.state == .attention }, "a denied permission is the user's choice, not a problem")179 for finding in audit.appPermissions { #expect(finding.notInspected.contains { $0.contains("other apps") }) }180 #expect(audit.checklist.count == ChecklistCatalog.privacyItems.count)181 #expect(audit.notAssessable.contains { $0.id == "apps.permissions" })182 #expect(audit.practices.count >= 6)183 #expect(Fixture.assertHonest(audit.all).isEmpty, "\(Fixture.assertHonest(audit.all))")184 }185186 @Test func everyPermissionKindAndStatusIsExplained() {187 for kind in AppPermissionKind.allCases {188 #expect(!kind.purpose.isEmpty && !kind.settingsPath.isEmpty && !kind.title.isEmpty)189 for status in AppPermissionStatus.allCases {190 let audit = PrivacyAuditEngine().audit(permissions: [AppPermission(kind: kind, status: status, source: "test")], responses: [])191 #expect(audit.appPermissions.first?.explanation.contains(kind.purpose) == true)192 }193 }194 }195}196