|
| 1 | +import CryptoKit |
| 2 | +import Foundation |
| 3 | +import XCTest |
| 4 | + |
| 5 | +@testable import AuthenticatorSyncKit |
| 6 | + |
| 7 | +final class SharedKeychainRepositoryTests: AuthenticatorSyncKitTestCase { |
| 8 | + // MARK: Properties |
| 9 | + |
| 10 | + let accessGroup = "group.com.example.bitwarden" |
| 11 | + var keychainService: MockAuthenticatorKeychainService! |
| 12 | + var subject: DefaultSharedKeychainRepository! |
| 13 | + |
| 14 | + // MARK: Setup & Teardown |
| 15 | + |
| 16 | + override func setUp() { |
| 17 | + keychainService = MockAuthenticatorKeychainService() |
| 18 | + subject = DefaultSharedKeychainRepository( |
| 19 | + sharedAppGroupIdentifier: accessGroup, |
| 20 | + keychainService: keychainService |
| 21 | + ) |
| 22 | + } |
| 23 | + |
| 24 | + override func tearDown() { |
| 25 | + keychainService = nil |
| 26 | + subject = nil |
| 27 | + } |
| 28 | + |
| 29 | + // MARK: Tests |
| 30 | + |
| 31 | + /// Verify that `deleteAuthenticatorKey()` issues a delete with the correct search attributes specified. |
| 32 | + /// |
| 33 | + func test_deleteAuthenticatorKey_success() async throws { |
| 34 | + try subject.deleteAuthenticatorKey() |
| 35 | + |
| 36 | + let queries = try XCTUnwrap(keychainService.deleteQueries as? [[CFString: Any]]) |
| 37 | + XCTAssertEqual(queries.count, 1) |
| 38 | + |
| 39 | + let query = try XCTUnwrap(queries.first) |
| 40 | + try XCTAssertEqual(XCTUnwrap(query[kSecAttrAccessGroup] as? String), accessGroup) |
| 41 | + try XCTAssertEqual(XCTUnwrap(query[kSecAttrAccessible] as? String), |
| 42 | + String(kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly)) |
| 43 | + try XCTAssertEqual(XCTUnwrap(query[kSecAttrAccount] as? String), |
| 44 | + SharedKeychainItem.authenticatorKey.unformattedKey) |
| 45 | + try XCTAssertEqual(XCTUnwrap(query[kSecClass] as? String), |
| 46 | + String(kSecClassGenericPassword)) |
| 47 | + } |
| 48 | + |
| 49 | + /// Verify that `getAuthenticatorKey()` returns a value successfully when one is set. Additionally, verify the |
| 50 | + /// search attributes are specified correctly. |
| 51 | + /// |
| 52 | + func test_getAuthenticatorKey_success() async throws { |
| 53 | + let key = SymmetricKey(size: .bits256) |
| 54 | + let data = key.withUnsafeBytes { Data(Array($0)) } |
| 55 | + |
| 56 | + keychainService.setSearchResultData(data) |
| 57 | + |
| 58 | + let returnData = try await subject.getAuthenticatorKey() |
| 59 | + XCTAssertEqual(returnData, data) |
| 60 | + |
| 61 | + let query = try XCTUnwrap(keychainService.searchQuery as? [CFString: Any]) |
| 62 | + try XCTAssertEqual(XCTUnwrap(query[kSecAttrAccessGroup] as? String), accessGroup) |
| 63 | + try XCTAssertEqual(XCTUnwrap(query[kSecAttrAccessible] as? String), |
| 64 | + String(kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly)) |
| 65 | + try XCTAssertEqual(XCTUnwrap(query[kSecAttrAccount] as? String), |
| 66 | + SharedKeychainItem.authenticatorKey.unformattedKey) |
| 67 | + try XCTAssertEqual(XCTUnwrap(query[kSecClass] as? String), String(kSecClassGenericPassword)) |
| 68 | + try XCTAssertEqual(XCTUnwrap(query[kSecMatchLimit] as? String), String(kSecMatchLimitOne)) |
| 69 | + try XCTAssertTrue(XCTUnwrap(query[kSecReturnAttributes] as? Bool)) |
| 70 | + try XCTAssertTrue(XCTUnwrap(query[kSecReturnData] as? Bool)) |
| 71 | + } |
| 72 | + |
| 73 | + /// Verify that `getAuthenticatorKey()` fails with a `keyNotFound` error when an unexpected |
| 74 | + /// result is returned instead of the key data from the keychain |
| 75 | + /// |
| 76 | + func test_getAuthenticatorKey_badResult() async throws { |
| 77 | + let error = AuthenticatorKeychainServiceError.keyNotFound(SharedKeychainItem.authenticatorKey) |
| 78 | + keychainService.searchResult = .success([kSecValueData as String: NSObject()] as AnyObject) |
| 79 | + |
| 80 | + await assertAsyncThrows(error: error) { |
| 81 | + _ = try await subject.getAuthenticatorKey() |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// Verify that `getAuthenticatorKey()` fails with a `keyNotFound` error when a nil |
| 86 | + /// result is returned instead of the key data from the keychain |
| 87 | + /// |
| 88 | + func test_getAuthenticatorKey_nilResult() async throws { |
| 89 | + let error = AuthenticatorKeychainServiceError.keyNotFound(SharedKeychainItem.authenticatorKey) |
| 90 | + keychainService.searchResult = .success(nil) |
| 91 | + |
| 92 | + await assertAsyncThrows(error: error) { |
| 93 | + _ = try await subject.getAuthenticatorKey() |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /// Verify that `getAuthenticatorKey()` fails with an error when the Authenticator key is not |
| 98 | + /// present in the keychain |
| 99 | + /// |
| 100 | + func test_getAuthenticatorKey_keyNotFound() async throws { |
| 101 | + let error = AuthenticatorKeychainServiceError.keyNotFound(SharedKeychainItem.authenticatorKey) |
| 102 | + keychainService.searchResult = .failure(error) |
| 103 | + |
| 104 | + await assertAsyncThrows(error: error) { |
| 105 | + _ = try await subject.getAuthenticatorKey() |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// Verify that `setAuthenticatorKey(_:)` sets a value with the correct search attributes specified. |
| 110 | + /// |
| 111 | + func test_setAuthenticatorKey_success() async throws { |
| 112 | + let key = SymmetricKey(size: .bits256) |
| 113 | + let data = key.withUnsafeBytes { Data(Array($0)) } |
| 114 | + try await subject.setAuthenticatorKey(data) |
| 115 | + |
| 116 | + let attributes = try XCTUnwrap(keychainService.addAttributes as? [CFString: Any]) |
| 117 | + try XCTAssertEqual(XCTUnwrap(attributes[kSecAttrAccessGroup] as? String), accessGroup) |
| 118 | + try XCTAssertEqual(XCTUnwrap(attributes[kSecAttrAccessible] as? String), |
| 119 | + String(kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly)) |
| 120 | + try XCTAssertEqual(XCTUnwrap(attributes[kSecAttrAccount] as? String), |
| 121 | + SharedKeychainItem.authenticatorKey.unformattedKey) |
| 122 | + try XCTAssertEqual(XCTUnwrap(attributes[kSecClass] as? String), |
| 123 | + String(kSecClassGenericPassword)) |
| 124 | + try XCTAssertEqual(XCTUnwrap(attributes[kSecValueData] as? Data), data) |
| 125 | + } |
| 126 | +} |
0 commit comments