Repository Public License
1// ORIX Secure2// File: Packages/ORIXNetworkLab/Tests/ORIXNetworkLabTests/FindingsTests.swift3// Description: Findings from synthetic input: network assessment (proxy, VPN heuristic, Wi-Fi states), TLS assessment on4// generated certificates, HTTPS probe results; plus the wording and evidence rules every finding must meet.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9@testable import ORIXNetworkLab10import ORIXSecureModel11import Testing1213/// Every finding carries evidence and an explanation, and never uses the words the product bans.14func expectWellFormed(_ findings: [Finding], sourceLocation: SourceLocation = #_sourceLocation) {15 for finding in findings {16 #expect(!finding.evidence.isEmpty, "\(finding.id) has no evidence", sourceLocation: sourceLocation)17 #expect(finding.explanation.count > 20, "\(finding.id) has no explanation", sourceLocation: sourceLocation)18 #expect(!finding.title.isEmpty, sourceLocation: sourceLocation)19 #expect(!finding.notInspected.isEmpty, "\(finding.id) states no limit", sourceLocation: sourceLocation)20 #expect(finding.category == .network, sourceLocation: sourceLocation)21 let banned = bannedWord(in: finding)22 #expect(banned == nil, "\(finding.id) contains “\(banned ?? "")”", sourceLocation: sourceLocation)23 if finding.state == .attention, finding.severity >= .moderate {24 #expect(finding.recommendation != nil, "\(finding.id) has no recommendation", sourceLocation: sourceLocation)25 }26 }27 #expect(Set(findings.map(\.id)).count == findings.count, "duplicate finding identifiers", sourceLocation: sourceLocation)28}2930@Suite("Network assessment findings")31struct NetworkAssessmentTests {32 let now = Date(timeIntervalSince1970: 1_800_000_000)3334 func wifiSnapshot() -> ConnectivitySnapshot {35 ConnectivitySnapshot(status: .satisfied, interfaceKinds: [.wifi], supportsIPv4: true, supportsIPv6: true, supportsDNS: true, availableInterfaces: [PathInterface(name: "en0", kind: .wifi)])36 }3738 func address(_ name: String, _ text: String) -> InterfaceAddress {39 let parsed = IPAddress(text)40 return InterfaceAddress(name: name, family: parsed?.family ?? .ipv4, address: text, flags: InterfaceFlags(raw: UInt32(IFF_UP | IFF_RUNNING)), scope: parsed?.scope ?? .reserved)41 }4243 func input(44 snapshot: Reading<ConnectivitySnapshot>? = nil,45 interfaces: [InterfaceAddress]? = nil,46 proxy: ProxyConfiguration = ProxyConfiguration(),47 wifi: Reading<WiFiNetworkInfo> = .measured(WiFiNetworkInfo(ssid: "Home", bssid: "aa:bb:cc:dd:ee:ff", security: .personal))48 ) -> NetworkAssessmentInput {49 NetworkAssessmentInput(50 snapshot: snapshot ?? .measured(wifiSnapshot()),51 interfaces: .measured(interfaces ?? [address("en0", "192.168.2.20"), address("utun0", "fe80::1")]),52 proxy: .measured(proxy),53 wifi: wifi54 )55 }5657 func finding(_ id: String, in findings: [Finding]) -> Finding? { findings.first { $0.id == id } }5859 @Test func ordinaryHomeWiFi() throws {60 let findings = NetworkAssessment.findings(for: input(), now: now)61 expectWellFormed(findings)62 #expect(findings.count(.attention) == 0)63 let connectivity = try #require(finding("network.connectivity", in: findings))64 #expect(connectivity.state == .verified)65 #expect(connectivity.confidence == .measured)66 let proxy = try #require(finding("network.proxy", in: findings))67 #expect(proxy.state == .verified)68 let vpn = try #require(finding("network.vpn", in: findings))69 #expect(vpn.state == .verified)70 #expect(vpn.confidence == .heuristic)71 #expect(vpn.verification == .heuristic)72 #expect(vpn.explanation.contains("utun"))73 let wifi = try #require(finding("wifi.security", in: findings))74 #expect(wifi.state == .verified)75 #expect(wifi.explanation.contains("WPA2 or WPA3"))76 let ipv6 = try #require(finding("network.ipv6", in: findings))77 #expect(ipv6.state == .verified)78 #expect(finding("network.lowDataMode", in: findings) == nil)79 #expect(finding("network.expensive", in: findings) == nil)80 #expect(findings.allSatisfy { $0.assessedAt == now })81 }8283 @Test func proxyConfigured() throws {84 let proxy = ProxyConfiguration(httpProxy: ProxyEndpoint(host: "proxy.corp.example", port: 8080), pacURL: "http://wpad.example/proxy.pac")85 let findings = NetworkAssessment.findings(for: input(proxy: proxy), now: now)86 expectWellFormed(findings)87 let finding = try #require(finding("network.proxy", in: findings))88 #expect(finding.state == .attention)89 #expect(finding.severity == .moderate)90 #expect(finding.confidence == .measured)91 #expect(finding.evidence.contains { $0.value == "proxy.corp.example:8080" })92 #expect(finding.evidence.contains { $0.value == "http://wpad.example/proxy.pac" })93 #expect(finding.explanation.contains("managed work and school networks"))94 }9596 @Test func vpnHeuristic() throws {97 let findings = NetworkAssessment.findings(for: input(interfaces: [address("en0", "192.168.2.20"), address("utun0", "fe80::1"), address("utun4", "10.67.0.34")]), now: now)98 expectWellFormed(findings)99 let vpn = try #require(finding("network.vpn", in: findings))100 #expect(vpn.state == .attention)101 #expect(vpn.severity == .low)102 #expect(vpn.confidence == .heuristic)103 #expect(vpn.evidence.contains { $0.value == "utun4" })104 #expect(vpn.explanation.contains("iOS itself creates utun interfaces"))105 #expect(findings.highestSeverity == .low)106 }107108 @Test func cellularLowDataMode() throws {109 let snapshot = ConnectivitySnapshot(status: .satisfied, interfaceKinds: [.cellular], isExpensive: true, isConstrained: true, supportsIPv4: true, supportsIPv6: false, supportsDNS: true)110 let wifi: Reading<WiFiNetworkInfo> = .unavailable(.failed, "not on Wi-Fi")111 let findings = NetworkAssessment.findings(for: input(snapshot: .measured(snapshot), interfaces: [address("pdp_ip0", "100.72.1.9")], wifi: wifi), now: now)112 expectWellFormed(findings)113 #expect(finding("network.connectivity", in: findings)?.explanation.contains("cellular") == true)114 #expect(finding("network.lowDataMode", in: findings)?.state == .verified)115 #expect(finding("network.expensive", in: findings)?.state == .verified)116 #expect(finding("network.ipv6", in: findings)?.title.contains("not available") == true)117 #expect(finding("network.addresses", in: findings)?.explanation.contains("100.64.0.0/10") == true)118 // Not on Wi-Fi and no Wi-Fi reading: no Wi-Fi finding at all.119 #expect(finding("wifi.security", in: findings) == nil)120 #expect(findings.count(.attention) == 0)121 }122123 @Test func offline() throws {124 let snapshot = ConnectivitySnapshot(status: .unsatisfied, unsatisfiedReason: .wifiDenied)125 let wifi: Reading<WiFiNetworkInfo> = .unavailable(.failed, "not on Wi-Fi")126 let findings = NetworkAssessment.findings(for: input(snapshot: .measured(snapshot), interfaces: [], wifi: wifi), now: now)127 expectWellFormed(findings)128 let connectivity = try #require(finding("network.connectivity", in: findings))129 #expect(connectivity.state == .attention)130 #expect(connectivity.severity == .low)131 #expect(connectivity.evidence.contains { $0.source == "NWPath.unsatisfiedReason" })132 #expect(finding("network.ipv6", in: findings) == nil)133 }134135 @Test func openWiFiAndWEP() throws {136 let open = NetworkAssessment.findings(for: input(wifi: .measured(WiFiNetworkInfo(ssid: "Cafe", bssid: "00:11:22:33:44:55", security: .open))), now: now)137 expectWellFormed(open)138 let openFinding = try #require(finding("wifi.security", in: open))139 #expect(openFinding.state == .attention)140 #expect(openFinding.severity == .moderate)141 #expect(openFinding.evidence.contains { $0.value == "Cafe" })142 #expect(open.highestSeverity == .moderate)143144 let wep = NetworkAssessment.findings(for: input(wifi: .measured(WiFiNetworkInfo(ssid: "Old", bssid: "00:11:22:33:44:66", security: .wep, signalStrength: 0.8))), now: now)145 expectWellFormed(wep)146 let wepFinding = try #require(finding("wifi.security", in: wep))147 #expect(wepFinding.state == .attention)148 #expect(wepFinding.severity == .high)149 #expect(wepFinding.evidence.count == 4)150 #expect(wep.prioritized().first?.id == "wifi.security")151152 let enterprise = WiFiInspector.findings(for: .measured(WiFiNetworkInfo(ssid: "eduroam", bssid: "00:11:22:33:44:77", security: .enterprise)), now: now)153 #expect(enterprise.first?.state == .verified)154 let unknown = WiFiInspector.findings(for: .measured(WiFiNetworkInfo(ssid: "x", bssid: "y", security: .unknown)), now: now)155 #expect(unknown.first?.state == .userConfirmationRequired)156 expectWellFormed(enterprise + [])157 expectWellFormed(unknown)158 }159160 @Test func unavailableWiFi() throws {161 let permission = NetworkAssessment.findings(for: input(wifi: .unavailable(.permissionRequired, "Location access is not authorized.")), now: now)162 expectWellFormed(permission)163 let finding = try #require(finding("wifi.security", in: permission))164 #expect(finding.state == .userConfirmationRequired)165 #expect(finding.confidence == .none)166 #expect(finding.verification == .notVerifiable)167 #expect(finding.evidence.first?.value == "Location access is not authorized.")168 for kind in [Unavailability.Kind.entitlementRequired, .simulator, .unsupportedOnDevice, .failed] {169 let findings = WiFiInspector.findings(for: .unavailable(kind, "reason"), now: now)170 #expect(findings.first?.state == .notAvailable)171 expectWellFormed(findings)172 }173 }174175 @Test func unavailableReadingsBecomeNotAvailableFindings() {176 let input = NetworkAssessmentInput(177 snapshot: .unavailable(.failed, "no path"), interfaces: .unavailable(.failed, "no interfaces"), proxy: .unavailable(.failed, "no proxy settings"), wifi: .unavailable(.unsupportedOnDevice, "not an iPhone")178 )179 #expect(input.vpnIndicator == nil)180 let findings = NetworkAssessment.findings(for: input, now: now)181 expectWellFormed(findings)182 #expect(finding("network.connectivity", in: findings)?.state == .notAvailable)183 #expect(finding("network.proxy", in: findings)?.state == .notAvailable)184 #expect(finding("network.vpn", in: findings) == nil)185 #expect(finding("wifi.security", in: findings)?.state == .notAvailable)186 let report = NetworkAssessment.report(input: input, now: now)187 #expect(report.kind == .network)188 #expect(report.unavailable.contains("no path"))189 #expect(report.unavailable.contains { $0.contains("other apps") })190 }191192 @Test func everyFindingStatesWhatAnAppCannotSee() {193 let findings = NetworkAssessment.findings(for: input(), now: now)194 for finding in findings where finding.id.hasPrefix("network.") {195 #expect(finding.notInspected.contains { $0.contains("other apps") }, "\(finding.id)")196 #expect(finding.notInspected.contains { $0.contains("router") }, "\(finding.id)")197 #expect(finding.notInspected.contains { $0.contains("list of devices") }, "\(finding.id)")198 }199 }200201 @Test func wifiOnMacIsUnsupported() async {202 #if os(macOS)203 let reading = await WiFiInspector.currentNetwork(locationAuthorization: .authorizedPrecise, isOnWiFi: true)204 #expect(reading.unavailability?.kind == .unsupportedOnDevice)205 #endif206 #expect(WiFiInspector.requirements.contains("Access Wi-Fi Information"))207 }208209 @Test func reportIncludesProbeSections() {210 let dns = [DNSResult(host: "apple.com", addresses: [ResolvedAddress(family: .ipv4, address: "17.253.144.10", scope: .global)], duration: 0.012)]211 let stats = LatencyStatistics(durations: [0.010, 0.020, nil])212 let latency = [LatencyResult(host: "apple.com", port: 443, samples: [], statistics: stats.map { .measured($0) } ?? .unavailable(.failed, "none"))]213 let report = NetworkAssessment.report(input: input(), dns: dns, latency: latency, now: now)214 #expect(report.sections.count == 3)215 #expect(report.sections[1].facts.first?.value.contains("17.253.144.10") == true)216 #expect(report.sections[2].facts.first?.label == "apple.com:443")217 #expect(report.allFindings.count == NetworkAssessment.findings(for: input(), now: now).count)218 }219}220221@Suite("TLS assessment findings")222struct TLSAssessmentTests {223 let now = utc("2026-09-01T00:00:00Z")224 let trusted = TLSTrustVerdict(isTrusted: true)225 let untrusted = TLSTrustVerdict(isTrusted: false, errorDescription: "“www.orix.test” certificate is not trusted", errorCode: -67843)226227 func finding(_ id: String, in findings: [Finding]) -> Finding? { findings.first { $0.id == id } }228229 @Test func healthyChain() throws {230 guard let generated = GeneratedCertificates.shared else { return }231 let leaf = try X509Certificate(der: generated.leaf)232 let root = try X509Certificate(der: generated.root)233 let findings = TLSAssessment.findings(endpoint: TLSEndpoint(host: "www.orix.test"), chain: [leaf, root], trust: trusted, protocolVersion: .tls13, cipherSuite: TLSCipherSuite(code: 0x1301), now: now)234 expectWellFormed(findings)235 #expect(findings.count(.attention) == 0)236 #expect(Set(findings.map(\.id)) == ["tls.trust", "tls.validity", "tls.hostname", "tls.protocol"])237 #expect(finding("tls.protocol", in: findings)?.evidence.contains { $0.value == "TLS_AES_128_GCM_SHA256" } == true)238 #expect(finding("tls.trust", in: findings)?.explanation.contains("not whether the site itself deserves trust") == true)239 for finding in findings {240 #expect(finding.notInspected.contains { $0.contains("revocation") })241 #expect(finding.notInspected.contains { $0.contains("Certificate Transparency") })242 }243 }244245 @Test func wildcardAndAddressCoverage() throws {246 guard let generated = GeneratedCertificates.shared else { return }247 let leaf = try X509Certificate(der: generated.leaf)248 for host in ["api.apps.orix.test", "127.0.0.1", "::1", "WWW.ORIX.TEST."] {249 let findings = TLSAssessment.findings(endpoint: TLSEndpoint(host: host), chain: [leaf], trust: trusted, protocolVersion: .tls12, now: now)250 #expect(finding("tls.hostname", in: findings)?.state == .verified, "\(host)")251 }252 for host in ["orix.test", "a.b.apps.orix.test", "apps.orix.test", "127.0.0.2", "other.test"] {253 let findings = TLSAssessment.findings(endpoint: TLSEndpoint(host: host), chain: [leaf], trust: untrusted, protocolVersion: .tls12, now: now)254 let mismatch = finding("tls.hostname", in: findings)255 #expect(mismatch?.state == .attention, "\(host)")256 #expect(mismatch?.severity == .high, "\(host)")257 expectWellFormed(findings)258 }259 }260261 @Test func expiredNotYetValidAndExpiringSoon() throws {262 guard let generated = GeneratedCertificates.shared else { return }263 let expired = try X509Certificate(der: generated.expired)264 let expiredFindings = TLSAssessment.findings(endpoint: TLSEndpoint(host: "expired.orix.test"), chain: [expired], trust: untrusted, protocolVersion: .tls13, now: now)265 expectWellFormed(expiredFindings)266 #expect(finding("tls.validity", in: expiredFindings)?.title == "The certificate has expired")267 #expect(finding("tls.validity", in: expiredFindings)?.severity == .high)268269 let future = try X509Certificate(der: generated.future)270 let futureFindings = TLSAssessment.findings(endpoint: TLSEndpoint(host: "future.orix.test"), chain: [future], trust: untrusted, protocolVersion: .tls13, now: now)271 expectWellFormed(futureFindings)272 #expect(finding("tls.validity", in: futureFindings)?.title == "The certificate is not valid yet")273 #expect(finding("tls.validity", in: futureFindings)?.severity == .high)274275 let expiring = try X509Certificate(der: generated.expiring)276 let expiringFindings = TLSAssessment.findings(endpoint: TLSEndpoint(host: "soon.orix.test"), chain: [expiring], trust: trusted, protocolVersion: .tls13, now: now)277 expectWellFormed(expiringFindings)278 let soon = try #require(finding("tls.validity", in: expiringFindings))279 #expect(soon.state == .attention)280 #expect(soon.severity == .low)281 #expect(soon.evidence.contains { $0.value == "9" })282 // 15 days before the end: outside the 14-day window.283 let earlier = TLSAssessment.findings(endpoint: TLSEndpoint(host: "soon.orix.test"), chain: [expiring], trust: trusted, protocolVersion: .tls13, now: utc("2026-08-26T00:00:00Z"))284 #expect(finding("tls.validity", in: earlier)?.state == .verified)285 }286287 @Test func weakSelfSignedLongLivedCertificate() throws {288 guard let generated = GeneratedCertificates.shared else { return }289 let weak = try X509Certificate(der: generated.weak)290 let osError = TLSTrustVerdict(isTrusted: false, errorDescription: "“weak.orix.test” certificate is not trusted", errorCode: -67843)291 let findings = TLSAssessment.findings(endpoint: TLSEndpoint(host: "weak.orix.test"), chain: [weak], trust: osError, protocolVersion: .tls10, now: now)292 expectWellFormed(findings)293 let trust = try #require(finding("tls.trust", in: findings))294 #expect(trust.state == .attention)295 #expect(trust.severity == .high)296 #expect(trust.evidence.contains { $0.value == "“weak.orix.test” certificate is not trusted" })297 #expect(trust.evidence.contains { $0.value == "-67843" })298 let selfSigned = try #require(finding("tls.selfSigned", in: findings))299 #expect(selfSigned.severity == .moderate)300 #expect(selfSigned.evidence.contains { $0.source == "SecKeyVerifySignature" && $0.value == "yes" })301 #expect(finding("tls.issuer", in: findings) == nil)302 #expect(finding("tls.signature", in: findings)?.severity == .high)303 #expect(finding("tls.signature", in: findings)?.evidence.contains { $0.value == "sha1WithRSAEncryption" } == true)304 #expect(finding("tls.key", in: findings)?.severity == .high)305 #expect(finding("tls.key", in: findings)?.evidence.contains { $0.value == "RSA 1024" } == true)306 #expect(finding("tls.validityPeriod", in: findings)?.severity == .low)307 let version = try #require(finding("tls.protocol", in: findings))308 #expect(version.state == .attention)309 #expect(version.severity == .moderate)310 #expect(version.explanation.contains("TLS 1.0"))311 #expect(findings.highestSeverity == .high)312 let old = TLSAssessment.findings(endpoint: TLSEndpoint(host: "weak.orix.test"), chain: [weak], trust: osError, protocolVersion: .tls11, now: now)313 #expect(finding("tls.protocol", in: old)?.state == .attention)314 }315316 @Test func untrustedPrivateIssuer() throws {317 guard let generated = GeneratedCertificates.shared else { return }318 let leaf = try X509Certificate(der: generated.leaf)319 let findings = TLSAssessment.findings(endpoint: TLSEndpoint(host: "www.orix.test"), chain: [leaf], trust: untrusted, protocolVersion: .tls13, now: now)320 expectWellFormed(findings)321 let issuer = try #require(finding("tls.issuer", in: findings))322 #expect(issuer.confidence == .heuristic)323 #expect(issuer.explanation.contains("ORIX Test Root"))324 #expect(issuer.evidence.contains { $0.value.contains("O=ORIX Test Authority") })325 #expect(finding("tls.selfSigned", in: findings) == nil)326 #expect(finding("tls.validityPeriod", in: findings) == nil)327 }328329 @Test func handshakeFailureWithoutCertificate() {330 let findings = TLSAssessment.findings(endpoint: TLSEndpoint(host: "10.255.255.1", port: 81), chain: [], trust: nil, protocolVersion: nil, handshakeFailure: "No answer within 1,000 ms.", now: now)331 expectWellFormed(findings)332 #expect(findings.map(\.id) == ["tls.handshake"])333 #expect(findings.first?.evidence.contains { $0.value == "10.255.255.1:81" } == true)334 #expect(findings.first?.severity == .low)335 }336337 @Test func staticFixtureChainIsAssessedWithoutOpenSSL() throws {338 let leaf = try X509Certificate(der: Fixtures.leafDER)339 let root = try X509Certificate(der: Fixtures.caDER)340 let findings = TLSAssessment.findings(endpoint: TLSEndpoint(host: "api.fixture.orix.test"), chain: [leaf, root], trust: untrusted, protocolVersion: .tls12, now: utc("2026-12-25T12:00:00Z"))341 expectWellFormed(findings)342 #expect(finding("tls.hostname", in: findings)?.state == .verified)343 #expect(finding("tls.validity", in: findings)?.title == "The certificate expires soon")344 #expect(finding("tls.issuer", in: findings) != nil)345 #expect(finding("tls.selfSigned", in: findings) == nil)346 }347}348349@Suite("HTTPS probe findings")350struct HTTPSProbeFindingsTests {351 func result(requested: String, hops: [HTTPHop], hsts: String?, failure: String? = nil, limit: Bool = false) -> HTTPSProbeResult {352 let final = hops.last?.url353 var result = HTTPSProbeResult(354 requestedURL: requested, hops: hops, finalURL: final, finalStatus: hops.last?.status, hsts: hsts.flatMap(HSTSPolicy.init(header:)),355 upgradedToHTTPS: requested.hasPrefix("http://") && (final?.hasPrefix("https://") ?? false) && failure == nil,356 redirectLimitReached: limit, failure: failure, duration: 0.2, findings: [], notInspected: HTTPSProbe.notInspected357 )358 result.findings = HTTPSProbe.findings(for: result)359 return result360 }361362 @Test func upgradeWithHSTS() {363 let probe = result(requested: "http://example.com/", hops: [HTTPHop(url: "http://example.com/", status: 301, location: "https://example.com/"), HTTPHop(url: "https://example.com/", status: 200)], hsts: "max-age=31536000; includeSubDomains")364 expectWellFormed(probe.findings)365 #expect(probe.upgradedToHTTPS)366 #expect(probe.findings.map(\.id) == ["https.upgrade", "https.hsts"])367 #expect(probe.findings.count(.attention) == 0)368 #expect(probe.findings.last?.evidence.contains { $0.value == "365 days" } == true)369 }370371 @Test func noUpgrade() {372 let probe = result(requested: "http://plain.example/", hops: [HTTPHop(url: "http://plain.example/", status: 200)], hsts: nil)373 expectWellFormed(probe.findings)374 #expect(probe.findings.map(\.id) == ["https.upgrade"])375 #expect(probe.findings.first?.state == .attention)376 #expect(probe.findings.first?.severity == .moderate)377 }378379 @Test func httpsWithoutHSTSAndRedirectLimit() {380 let probe = result(requested: "https://example.com/", hops: [HTTPHop(url: "https://example.com/", status: 200)], hsts: "max-age=0", limit: true)381 expectWellFormed(probe.findings)382 #expect(probe.findings.map(\.id) == ["https.hsts", "https.redirects"])383 #expect(probe.findings.first?.severity == .low)384 }385386 @Test func failureIsNotAvailable() {387 let probe = result(requested: "https://example.com/", hops: [], hsts: nil, failure: "The request failed: timed out")388 expectWellFormed(probe.findings)389 #expect(probe.findings.map(\.state) == [.notAvailable])390 }391392 @Test func nonWebSchemesAreRefusedWithoutARequest() async throws {393 let url = try #require(URL(string: "ftp://example.com/file"))394 let probe = await HTTPSProbe.head(url)395 #expect(probe.hops.isEmpty)396 #expect(probe.failure != nil)397 let file = try #require(URL(string: "file:///etc/hosts"))398 let fileProbe = await HTTPSProbe.head(file)399 #expect(fileProbe.hops.isEmpty)400 #expect(fileProbe.failure != nil)401 }402}403