Repository Public License
1import Foundation2import Testing3@testable import PrismSecurity45@Suite struct SecurityTests {6 @Test func redactorRemovesKnownAndPatternSecrets() {7 let key = "sk-ant-api03-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"8 let text = "POST /v1/messages x-api-key: \(key) failed; retry with Bearer abcdefghijklmnop1234; other \(key)"9 let out = Redactor.redact(text, known: [key])10 #expect(!out.contains("ABCDEFGHIJ") && !out.contains("abcdefghijklmnop1234"))11 #expect(out.contains("POST /v1/messages"))12 #expect(!Redactor.redact("key=sk-ant-zzzzzzzzzzzzzzzz").contains("zzzzzzzz"))13 #expect(Redactor.redact("nothing secret here") == "nothing secret here")14 #expect(Redactor.fingerprint(key) == "sk-ant-…6789")15 #expect(Redactor.fingerprint("short") == "••••")16 }1718 @Test func inMemoryStoreBehavesLikeTheKeychainContract() throws {19 let store = InMemorySecretStore()20 #expect(try store.secret(for: "a") == nil)21 try store.setSecret(" value-123 \n", for: "a")22 #expect(try store.secret(for: "a") == "value-123")23 #expect(throws: SecretError.self) { try store.setSecret(" ", for: "a") }24 try store.removeSecret(for: "a")25 #expect(try store.secret(for: "a") == nil)26 }2728 /// Uses a throwaway service name in the real login Keychain, and cleans up after itself.29 @Test func keychainRoundTrip() throws {30 let store = KeychainSecretStore(service: "ai.spboucher.prism.tests.\(UUID().uuidString)")31 defer { try? store.removeSecret(for: "k") }32 #expect(try store.secret(for: "k") == nil)33 try store.setSecret("first-value-000", for: "k")34 try store.setSecret("second-value-111", for: "k")35 #expect(try store.secret(for: "k") == "second-value-111")36 try store.removeSecret(for: "k")37 try store.removeSecret(for: "k")38 #expect(try store.secret(for: "k") == nil)39 }40}41