Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Keyboard9// File : Shared/Tests/SecureStorageTests/SecretStoreTests.swift10// Description : Secret store contract, key shape checks, masking and11// redaction.12// Module : SecureStorageTests13// Repository : https://git.spboucher.ai/sp-keyboard14// Created : 2026-12-0115// Last Modified : 2026-12-0116//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author : Simon-Pierre Boucher22// Position : Professor of Finance23// Institution : Université du Québec en Outaouais (UQO)24// Department : Département des sciences administratives25// Organization : SP Boucher26//27// Website : https://spboucher.ai28// Git : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General : info@spboucher.ai35// Professional : contact@spboucher.ai36// Direct : simon@spboucher.ai37// Research : research@spboucher.ai38// Development : dev@spboucher.ai39// Support : support@spboucher.ai40// Security/Admin : admin@spboucher.ai41// Notifications : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification : Public68// Distribution : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License : None specified (no license file in this repository)82// Maintainer : Simon-Pierre Boucher83// Technical Contact: dev@spboucher.ai84//85// =============================================================================86// END OF SP BOUCHER SOURCE HEADER87// =============================================================================8889import XCTest90@testable import SecureStorage9192final class SecretStoreTests: XCTestCase {93 private let fakeKey = "sk-ant-api03-" + String(repeating: "Ab1_-", count: 16)9495 func testInMemoryRoundTrip() throws {96 let store = InMemorySecretStore()97 XCTAssertNil(try store.read(SecretName.anthropicAPIKey))98 XCTAssertFalse(store.contains(SecretName.anthropicAPIKey))99 try store.write(fakeKey, for: SecretName.anthropicAPIKey)100 XCTAssertEqual(try store.read(SecretName.anthropicAPIKey), fakeKey)101 XCTAssertTrue(store.contains(SecretName.anthropicAPIKey))102 try store.delete(SecretName.anthropicAPIKey)103 XCTAssertNil(try store.read(SecretName.anthropicAPIKey))104 }105106 func testFailuresAreReportedNotSwallowed() {107 let store = InMemorySecretStore()108 store.failEverything(with: .missingEntitlement)109 XCTAssertThrowsError(try store.write(fakeKey, for: "k")) { XCTAssertEqual($0 as? SecretStoreError, .missingEntitlement) }110 XCTAssertThrowsError(try store.read("k"))111 XCTAssertFalse(store.contains("k"))112 }113114 func testKeyShape() {115 XCTAssertTrue(APIKeyFormat.looksValid(fakeKey))116 XCTAssertTrue(APIKeyFormat.looksValid(" \(fakeKey)\n"))117 XCTAssertFalse(APIKeyFormat.looksValid(""))118 XCTAssertFalse(APIKeyFormat.looksValid("sk-ant-short"))119 XCTAssertFalse(APIKeyFormat.looksValid("sk-proj-" + String(repeating: "a", count: 60)))120 XCTAssertFalse(APIKeyFormat.looksValid("sk-ant-" + String(repeating: "a b", count: 30)))121 XCTAssertFalse(APIKeyFormat.looksValid("sk-ant-" + String(repeating: "é", count: 60)))122 }123124 func testMaskingNeverShowsTheKey() {125 let masked = APIKeyFormat.masked(fakeKey)126 XCTAssertTrue(masked.hasPrefix("sk-ant-"))127 XCTAssertLessThan(masked.count, 16)128 XCTAssertFalse(masked.contains(fakeKey.dropFirst(7).prefix(20)))129 XCTAssertEqual(APIKeyFormat.masked("short"), "•••••")130 }131132 func testRedaction() {133 let text = "request failed for key \(fakeKey) at host"134 let redacted = APIKeyFormat.redacting(text)135 XCTAssertFalse(redacted.contains(fakeKey))136 XCTAssertTrue(redacted.contains("sk-ant-[redacted]"))137 XCTAssertEqual(APIKeyFormat.redacting("nothing secret"), "nothing secret")138 }139140 func testDiagnosticCodesCarryNoSecret() {141 XCTAssertEqual(SecretStoreError.missingEntitlement.diagnosticCode, "entitlement")142 XCTAssertEqual(SecretStoreError.unexpectedStatus(-34018).diagnosticCode, "osstatus:-34018")143 }144}145