Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add errSecMissingEntitlement error #224

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions SimpleKeychain/SimpleKeychainError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
case itemNotFound
case interactionNotAllowed
case decodeFailed
case missingEntitlement
case other(status: OSStatus)
case unknown(message: String)

init(rawValue: OSStatus) {

Check warning on line 19 in SimpleKeychain/SimpleKeychainError.swift

View workflow job for this annotation

GitHub Actions / Lint code with SwiftLint

Function should have complexity 10 or less; currently complexity is 11 (cyclomatic_complexity)
switch rawValue {
case errSecUnimplemented: self = .operationNotImplemented
case errSecParam: self = .invalidParameters
Expand All @@ -26,6 +27,7 @@
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 @@
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 @@
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 @@
/// 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