SPB Git

Repository Public BSD-3-Clause

PrivacyTests.swift 7.3 KB 140 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Maps9// File             : Packages/ORIXPrivacy/Tests/ORIXPrivacyTests/PrivacyTests.swift10// Module           : ORIXPrivacyTests11// Repository       : https://git.spboucher.ai/orix-maps12// Created          : 2026-11-2513// Last Modified    : 2026-11-2514//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import ORIXGeoCore90import Testing91@testable import ORIXPrivacy9293@Suite struct RedactionTests {94    @Test func coordinatesAndIdentifiersAreScrubbed() {95        #expect(Redaction.scrub("route from 45.50884, -73.58781 to 46.81390,-71.20800 failed") == "route from <redacted> to <redacted> failed")96        #expect(Redaction.scrub("fix 45,50884 -73,58781") == "fix <redacted>")97        #expect(Redaction.scrub("at 45°30′31.8″N then") == "at <redacted> then")98        #expect(Redaction.scrub("call 4185701340 now") == "call <redacted> now")99        #expect(Redaction.scrub("region montreal version 2026.11.24, 3 files, 96 MB") == "region montreal version 2026.11.24, 3 files, 96 MB")100        #expect(Redaction.coarse(Coordinate(latitude: 45.50884, longitude: -73.58781)) == "~45.5,-73.6")101        #expect(Redaction.coarse(Coordinate(latitude: 99, longitude: 0)) == "invalid")102        #expect(Redaction.text("1234 rue Sherbrooke") == "<19 chars>")103    }104}105106@Suite struct NetworkPolicyTests {107    private let policy = NetworkPolicy(allowedHosts: ["www.orixlab.dev"])108    private func request(_ url: String, _ configure: (inout URLRequest) -> Void = { _ in }) -> URLRequest {109        var r = URLRequest(url: URL(string: url)!)110        configure(&r)111        return r112    }113114    @Test func allowsPlainCatalogAndRangeDownloads() throws {115        try policy.validate(request("https://www.orixlab.dev/maps/v1/catalog.json"))116        try policy.validate(request("https://WWW.ORIXLAB.DEV/maps/v1/files/montreal/2026.11.24/montreal-2026.11.24.orixmap") {117            $0.setValue("bytes=100-", forHTTPHeaderField: "Range"); $0.setValue("\"abc\"", forHTTPHeaderField: "If-Range"); $0.setValue("ORIXMaps/1.0", forHTTPHeaderField: "User-Agent")118        })119        try policy.validate(request("https://www.orixlab.dev/maps/v1/catalog.json") { $0.httpMethod = "HEAD" })120    }121122    @Test func refusesEverythingThatCouldCarryPersonalData() {123        #expect(throws: NetworkPolicy.Violation.insecureScheme) { try policy.validate(request("http://www.orixlab.dev/maps/v1/catalog.json")) }124        #expect(throws: NetworkPolicy.Violation.hostNotAllowed("example.com")) { try policy.validate(request("https://example.com/maps/v1/catalog.json")) }125        #expect(throws: NetworkPolicy.Violation.carriesParameters) { try policy.validate(request("https://www.orixlab.dev/maps/v1/catalog.json?lat=45.5&lon=-73.6")) }126        #expect(throws: NetworkPolicy.Violation.carriesParameters) { try policy.validate(request("https://www.orixlab.dev/maps/v1/catalog.json#x")) }127        #expect(throws: NetworkPolicy.Violation.carriesParameters) { try policy.validate(request("https://user:pw@www.orixlab.dev/maps/v1/catalog.json")) }128        #expect(throws: NetworkPolicy.Violation.forbiddenHeader("Cookie")) { try policy.validate(request("https://www.orixlab.dev/a") { $0.setValue("id=1", forHTTPHeaderField: "Cookie") }) }129        #expect(throws: NetworkPolicy.Violation.forbiddenHeader("X-Device-ID")) { try policy.validate(request("https://www.orixlab.dev/a") { $0.setValue("1", forHTTPHeaderField: "X-Device-ID") }) }130        #expect(throws: NetworkPolicy.Violation.hasBody) { try policy.validate(request("https://www.orixlab.dev/a") { $0.httpMethod = "POST" }) }131        #expect(throws: NetworkPolicy.Violation.hasBody) { try policy.validate(request("https://www.orixlab.dev/a") { $0.httpBody = Data([1]) }) }132    }133134    @Test func loopbackHTTPIsForTestsOnly() throws {135        #expect(throws: NetworkPolicy.Violation.insecureScheme) { try policy.validate(request("http://127.0.0.1:8080/catalog.json")) }136        try policy.validate(request("http://127.0.0.1:8080/catalog.json"), allowLoopbackHTTP: true)137        #expect(throws: NetworkPolicy.Violation.insecureScheme) { try policy.validate(request("http://www.orixlab.dev/catalog.json"), allowLoopbackHTTP: true) }138    }139}140
Files main
Menu

Theme