KeychainStorageTests.swift (1312B)
1 // 2 // KeychainStorageTests.swift 3 // damusTests 4 // 5 // Created by Bryan Montz on 5/3/23. 6 // 7 8 import XCTest 9 @testable import damus 10 import Security 11 12 final class KeychainStorageTests: XCTestCase { 13 @KeychainStorage(account: "test-keyname") 14 var secret: String? 15 16 override func tearDownWithError() throws { 17 secret = nil 18 } 19 20 func testWriteToKeychain() throws { 21 // write a secret to the keychain using the property wrapper's setter 22 secret = "super-secure-key" 23 24 // verify it exists in the keychain using the property wrapper's getter 25 XCTAssertEqual(secret, "super-secure-key") 26 27 // verify it exists in the keychain directly 28 let query = [ 29 kSecAttrService: "damus", 30 kSecAttrAccount: "test-keyname", 31 kSecClass: kSecClassGenericPassword, 32 kSecReturnData: true, 33 kSecMatchLimit: kSecMatchLimitOne 34 ] as [CFString: Any] as CFDictionary 35 36 var result: AnyObject? 37 let status = SecItemCopyMatching(query, &result) 38 XCTAssertEqual(status, errSecSuccess) 39 40 let data = try XCTUnwrap(result as? Data) 41 let the_secret = String(data: data, encoding: .utf8) 42 43 XCTAssertEqual(the_secret, "super-secure-key") 44 } 45 46 }