diff --git a/SimpleKeychain/SimpleKeychainError.swift b/SimpleKeychain/SimpleKeychainError.swift index fce2d05..438ed24 100644 --- a/SimpleKeychain/SimpleKeychainError.swift +++ b/SimpleKeychain/SimpleKeychainError.swift @@ -12,6 +12,7 @@ public struct SimpleKeychainError: LocalizedError, CustomDebugStringConvertible case itemNotFound case interactionNotAllowed case decodeFailed + case missingEntitlement case other(status: OSStatus) case unknown(message: String) @@ -26,6 +27,7 @@ public struct SimpleKeychainError: LocalizedError, CustomDebugStringConvertible case errSecItemNotFound: self = .itemNotFound case errSecInteractionNotAllowed: self = .interactionNotAllowed case errSecDecode: self = .decodeFailed + case errSecMissingEntitlement: self = .missingEntitlement default: self = .other(status: rawValue) } } @@ -41,6 +43,7 @@ public struct SimpleKeychainError: LocalizedError, CustomDebugStringConvertible case .itemNotFound: return errSecItemNotFound case .interactionNotAllowed: return errSecInteractionNotAllowed case .decodeFailed: return errSecDecode + case .missingEntitlement: return errSecMissingEntitlement case let .other(status): return status case .unknown: return errSecSuccess // This is not a Keychain error } @@ -91,6 +94,8 @@ public struct SimpleKeychainError: LocalizedError, CustomDebugStringConvertible return "errSecInteractionNotAllowed: Interaction with the Security Server is not allowed." case .decodeFailed: return "errSecDecode: Unable to decode the provided data." + case .missingEntitlement: + return "errSecMissingEntitlement: A required entitlement is missing." case .other: return "Unspecified Keychain error: \(self.status)." case let .unknown(message): @@ -136,6 +141,10 @@ public struct SimpleKeychainError: LocalizedError, CustomDebugStringConvertible /// See [errSecDecode](https://developer.apple.com/documentation/security/errsecdecode). public static let decodeFailed: SimpleKeychainError = .init(code: .decodeFailed) + /// A required entitlement is missing. + /// See [errSecMissingEntitlement](https://developer.apple.com/documentation/security/errsecmissingentitlement). + public static let missingEntitlement: SimpleKeychainError = .init(code: .missingEntitlement) + /// Other Keychain error. /// The `OSStatus` of the Keychain operation can be accessed via the ``status`` property. public static let other: SimpleKeychainError = .init(code: .other(status: 0)) diff --git a/SimpleKeychainTests/SimpleKeychainErrorSpec.swift b/SimpleKeychainTests/SimpleKeychainErrorSpec.swift index c9ecf67..2b37a2c 100644 --- a/SimpleKeychainTests/SimpleKeychainErrorSpec.swift +++ b/SimpleKeychainTests/SimpleKeychainErrorSpec.swift @@ -108,7 +108,13 @@ class SimpleKeychainErrorSpec: XCTestCase { let sut = SimpleKeychainError(code: .decodeFailed) XCTAssertEqual(sut.localizedDescription, message) } - + + func testErrorMessage_shouldReturnMessageForMissingEntitlement() { + let message = "errSecMissingEntitlement: A required entitlement is missing." + let sut = SimpleKeychainError(code: .missingEntitlement) + XCTAssertEqual(sut.localizedDescription, message) + } + func testErrorMessage_shouldReturnMessageForOtherError() { let status: OSStatus = 123 let message = "Unspecified Keychain error: \(status)." @@ -167,7 +173,12 @@ class SimpleKeychainErrorSpec: XCTestCase { let sut = SimpleKeychainError.Code(rawValue: errSecDecode) XCTAssertEqual(sut, SimpleKeychainError.decodeFailed.code) } - + + func testMapErrSecMissingEntitlement() { + let sut = SimpleKeychainError.Code(rawValue: errSecMissingEntitlement) + XCTAssertEqual(sut, SimpleKeychainError.missingEntitlement.code) + } + func testMapOtherStatusValue() { let status: OSStatus = 1234 let sut = SimpleKeychainError.Code(rawValue: status) @@ -205,7 +216,11 @@ class SimpleKeychainErrorSpec: XCTestCase { func testMapDecodeFailed() { XCTAssertEqual(SimpleKeychainError.decodeFailed.code.rawValue, errSecDecode) } - + + func testMapMissingEntitlement() { + XCTAssertEqual(SimpleKeychainError.missingEntitlement.code.rawValue, errSecMissingEntitlement) + } + func testMapOther() { let status: OSStatus = 1234 XCTAssertEqual(SimpleKeychainError(code: .other(status: status)).status, status)