SPB Git

Repository Public

WebAuthnTests.swift 3.7 KB 62 lines swift

1// SP Drive iOS2// File: Tests/WebAuthnTests.swift3// Description: Unit tests of the passkey plumbing that can be verified without the system sheet: options parsing and response encoding.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9@testable import SPDrive1011struct WebAuthnTests {12    @Test func base64URLRoundTrip() throws {13        let raw = Data([0xfb, 0xff, 0x00, 0x10, 0x3e, 0x3f])14        let s = WebAuthnJSON.b64url(raw)15        #expect(!s.contains("+") && !s.contains("/") && !s.contains("="))16        #expect(WebAuthnJSON.data(b64url: s) == raw)17        #expect(WebAuthnJSON.data(b64url: "AQID") == Data([1, 2, 3]))18    }1920    @Test func assertionOptionsFromServerJSON() throws {21        let json = Data(#"{"challenge":"AQIDBA","rpId":"drive.spboucher.ai","allowCredentials":[{"id":"BQYH","type":"public-key"}],"userVerification":"required"}"#.utf8)22        let o = try WebAuthnJSON.assertionOptions(json, defaultRP: "x")23        #expect(o.challenge == Data([1, 2, 3, 4]))24        #expect(o.rpId == "drive.spboucher.ai")25        #expect(o.allowed == [Data([5, 6, 7])])26        // discoverable sign-in: no rpId, no allow list27        let bare = try WebAuthnJSON.assertionOptions(Data(#"{"challenge":"AQ"}"#.utf8), defaultRP: "drive.spboucher.ai")28        #expect(bare.rpId == "drive.spboucher.ai" && bare.allowed.isEmpty)29        #expect(throws: PasskeyError.self) { try WebAuthnJSON.assertionOptions(Data("{}".utf8), defaultRP: "x") }30    }3132    @Test func registrationOptionsFromServerJSON() throws {33        let json = Data(#"{"challenge":"AQID","rp":{"id":"drive.spboucher.ai","name":"SP Drive"},"user":{"id":"CQk","name":"a@b.c","displayName":"A"},"excludeCredentials":[{"id":"BQYH"}]}"#.utf8)34        let o = try WebAuthnJSON.registrationOptions(json, defaultRP: "x")35        #expect(o.userName == "a@b.c" && o.userID == Data([9, 9]) && o.excluded.count == 1 && o.rpId == "drive.spboucher.ai")36    }3738    @Test func assertionResponseHasTheShapeTheServerVerifies() throws {39        let data = WebAuthnJSON.assertionResponse(credentialID: Data([1, 2]), clientDataJSON: Data("{}".utf8), authenticatorData: Data([3]), signature: Data([4]), userHandle: Data([5]), platform: true)40        let o = try #require(try JSONSerialization.jsonObject(with: data) as? [String: Any])41        #expect(o["id"] as? String == "AQI" && o["rawId"] as? String == "AQI" && o["type"] as? String == "public-key")42        let r = try #require(o["response"] as? [String: String])43        #expect(r["clientDataJSON"] == "e30" && r["authenticatorData"] == "Aw" && r["signature"] == "BA" && r["userHandle"] == "BQ")44        #expect(o["clientExtensionResults"] is [String: Any])45        // no user handle (security key): the field is absent, not empty46        let bare = WebAuthnJSON.assertionResponse(credentialID: Data([1]), clientDataJSON: Data(), authenticatorData: Data(), signature: Data(), userHandle: nil, platform: false)47        let b = try #require(try JSONSerialization.jsonObject(with: bare) as? [String: Any])48        #expect((b["response"] as? [String: String])?["userHandle"] == nil)49        #expect(b["authenticatorAttachment"] as? String == "cross-platform")50    }5152    @Test func safeNamesNeverEscapeTheirFolder() {53        #expect(LocalFiles.safeName("../../etc/passwd") == "_.._etc_passwd")54        #expect(LocalFiles.safeName("..") == "sans-nom")55        #expect(LocalFiles.safeName("a/b\\c:d") == "a_b_c_d")56        #expect(LocalFiles.safeName("  .caché  ") == "caché")57        #expect(!LocalFiles.safeName(String(repeating: "é", count: 400) + ".pdf").contains("/"))58        #expect(LocalFiles.safeName(String(repeating: "é", count: 400) + ".pdf").utf8.count <= 240)59        #expect(LocalFiles.safeID("../x/..") == "x")60    }61}62
Files main
Menu

Theme