Skip to content

Commit

Permalink
add errSecMissingEntitlement error
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Jan 30, 2025
1 parent b694f15 commit 49a29f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 9 additions & 0 deletions SimpleKeychain/SimpleKeychainError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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))
Expand Down
21 changes: 18 additions & 3 deletions SimpleKeychainTests/SimpleKeychainErrorSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)."
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 49a29f6

Please sign in to comment.