Repository Public License
1// ORIX Secure2// File: Packages/ORIXSecureEngines/Tests/ORIXSecureEnginesTests/InvestigationTests.swift3// Description: Guided investigations: plans are complete, ordinary causes outrank compromise unless something concrete supports4// it, observations and reports stay separate, and reports say what could not be inspected.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import ORIXSecureModel10import Testing11@testable import ORIXSecureEngines1213@Suite struct InvestigationTests {14 let engine = InvestigationEngine()1516 @Test func everySymptomHasACompletePlan() {17 #expect(Symptom.allCases.count == 8)18 for symptom in Symptom.allCases {19 let plan = engine.plan(for: symptom)20 #expect(plan.questions.count >= 4, "\(symptom)")21 #expect(Set(plan.questions.map(\.id)).count == plan.questions.count)22 #expect(!plan.settingsChecks.isEmpty && !plan.tools.isEmpty)23 #expect(!symptom.title.isEmpty && !symptom.introduction.isEmpty && !symptom.systemImage.isEmpty)24 for check in plan.settingsChecks { #expect(!check.path.isEmpty && !check.lookFor.isEmpty) }25 }26 }2728 @Test func withNoEvidenceCompromiseIsAlwaysLastAndLessLikely() {29 for symptom in Symptom.allCases {30 let result = engine.evaluate(InvestigationInput(symptom: symptom, device: Fixture.device(), signals: Fixture.signals()), now: Fixture.now)31 #expect(result.hypotheses.last?.id == "z.compromise", "\(symptom)")32 #expect(result.hypotheses.last?.support == .lessLikely, "\(symptom)")33 #expect(!result.hypotheses.dropLast().contains { $0.support == .lessLikely })34 #expect(!result.actions.isEmpty && !result.escalation.isEmpty)35 #expect(result.notInspected.contains { $0.contains("no App Store app can determine") })36 #expect(result.reported.isEmpty)37 }38 }3940 @Test func answersMoveHypothesesToSupportedWithTheirBasis() {41 let input = InvestigationInput(symptom: .batteryDrain, answers: ["recentUpdate": .yes, "healthLow": .no, "oneApp": .notSure],42 device: Fixture.device(thermal: .serious), signals: Fixture.signals())43 let result = engine.evaluate(input, now: Fixture.now)44 #expect(result.hypotheses.first?.support == .supported)45 let supported = Set(result.hypotheses.filter { $0.support == .supported }.map(\.id))46 #expect(supported == ["a.indexing", "e.heat"])47 #expect(result.hypotheses.first { $0.id == "a.indexing" }?.basis.isEmpty == false)48 #expect(result.reported.count == 3)49 #expect(result.observations.contains { $0.label == "Thermal state" && $0.value == "Serious" })50 #expect(result.actions.first?.contains("Wait two days") == true)51 // Observations come from APIs; reports come from the user.52 #expect(!result.observations.contains { $0.source == "user" })53 #expect(!result.reported.contains { $0.source != "user" })54 }5556 @Test func compromiseNeedsAConcreteSign() {57 let indicator = IntegrityIndicator(id: "path:/var/jb", observation: "/var/jb", source: "FileManager.fileExists")58 let tampered = engine.evaluate(InvestigationInput(symptom: .crashes, device: Fixture.device(), signals: Fixture.signals(indicators: [indicator])), now: Fixture.now)59 #expect(tampered.hypotheses.first { $0.id == "z.compromise" }?.support == .supported)60 #expect(tampered.actions.first?.contains("restore iOS") == true)6162 let profile = engine.evaluate(InvestigationInput(symptom: .unknownProfile, answers: ["workDevice": .no, "installedByApp": .no, "hasCertificate": .yes, "hasVPNProxy": .yes]), now: Fixture.now)63 #expect(profile.hypotheses.first { $0.id == "z.compromise" }?.support == .supported)64 #expect(profile.hypotheses.first { $0.id == "c.intercept" }?.support == .supported)6566 let work = engine.evaluate(InvestigationInput(symptom: .unknownProfile, answers: ["workDevice": .yes, "hasCertificate": .yes, "hasVPNProxy": .yes]), now: Fixture.now)67 #expect(work.hypotheses.first { $0.id == "z.compromise" }?.support == .lessLikely)68 #expect(work.actions.first?.contains("IT department") == true)69 }7071 @Test func suspiciousLinkSeparatesHarmlessVisitFromPhishing() {72 let nothing = engine.evaluate(InvestigationInput(symptom: .suspiciousLink, answers: ["enteredPassword": .no, "installedProfile": .no, "installedApp": .no, "approvedPrompt": .no, "upToDate": .yes]), now: Fixture.now)73 #expect(nothing.hypotheses.first?.id == "a.nothing")74 #expect(nothing.hypotheses.filter { $0.support == .supported }.count == 1)75 let phished = engine.evaluate(InvestigationInput(symptom: .suspiciousLink, answers: ["enteredPassword": .yes, "upToDate": .no]), now: Fixture.now)76 #expect(phished.hypotheses.first { $0.id == "b.phished" }?.support == .supported)77 #expect(phished.actions.first?.contains("Change that password") == true)78 #expect(phished.actions.contains { $0.contains("pending iOS update") })79 #expect(phished.hypotheses.first { $0.id == "z.compromise" }?.basis.isEmpty == false)80 }8182 @Test func toolFindingsFeedNetworkHypotheses() {83 let proxy = Finding(id: "net.proxy.configured", category: .network, title: "A proxy is configured", state: .attention, severity: .moderate,84 confidence: .measured, verification: .publicAPI, evidence: [Evidence("Proxy", "10.0.0.9:3128")], explanation: "e", recommendation: "r")85 let result = engine.evaluate(InvestigationInput(symptom: .networkBehavior, answers: ["everyNetwork": .yes], toolFindings: [proxy]), now: Fixture.now)86 #expect(result.hypotheses.first?.id == "b.config")87 #expect(result.observations.contains { $0.value == "A proxy is configured" })88 #expect(result.toolFindings == [proxy])89 }9091 @Test func reportKeepsTheStructureAndTheLimits() {92 let input = InvestigationInput(symptom: .storageAnomaly, answers: ["photos": .yes, "recentlyDeleted": .yes], device: Fixture.device(total: 128 * Fixture.gb, free: 3 * Fixture.gb))93 let result = engine.evaluate(input, now: Fixture.now)94 let report = result.report()95 #expect(report.kind == .investigation)96 #expect(report.sections.map(\.title) == ["What was measured", "What you reported", "Possible explanations", "Recommended actions", "If the problem continues"])97 #expect(report.summary.contains("does not determine whether the device is compromised"))98 #expect(report.unavailable == result.notInspected)99 #expect(report.sections[2].paragraphs.first?.contains("None is a confirmed diagnosis") == true)100 #expect(report.sections[0].facts.contains { $0.label == "Capacity" })101 let text = (report.sections.flatMap(\.bullets) + report.sections.flatMap(\.paragraphs)).joined(separator: " ").lowercased()102 for word in ["spyware detected", "malware detected", "you have been hacked", "virus"] { #expect(!text.contains(word)) }103 }104105 @Test func noHypothesisTextClaimsDetection() {106 for symptom in Symptom.allCases {107 let plan = engine.plan(for: symptom)108 // Worst case: every question answered yes.109 let answers = Dictionary(uniqueKeysWithValues: plan.questions.map { ($0.id, ChecklistAnswer.yes) })110 let result = engine.evaluate(InvestigationInput(symptom: symptom, answers: answers, device: Fixture.device(thermal: .critical), signals: Fixture.signals()), now: Fixture.now)111 let text = (result.hypotheses.map { $0.title + " " + $0.explanation } + result.actions + [result.escalation]).joined(separator: " ").lowercased()112 for word in ["detected spyware", "malware detected", "is infected", "virus", "hacked"] { #expect(!text.contains(word), "\(symptom): \(word)") }113 let data = try? JSONEncoder().encode(result)114 #expect(data != nil)115 }116 }117}118