-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from outfoxx/fix/trust-failure-formatting
More complete error information for trust evaluation failures
- Loading branch information
Showing
4 changed files
with
223 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// | ||
// Errors.swift | ||
// Shield | ||
// | ||
// Copyright © 2021 Outfox, inc. | ||
// | ||
// | ||
// Distributed under the MIT License, See LICENSE for details. | ||
// | ||
|
||
import Foundation | ||
|
||
extension CFError { | ||
|
||
var humanReadableDescription: String { | ||
humanReadableDescriptionLines.joined(separator: "\n") | ||
} | ||
|
||
var humanReadableDescriptionLines: [String] { | ||
|
||
guard let userInfo = CFErrorCopyUserInfo(self) as? [CFString: Any] else { | ||
return [CFErrorCopyDescription(self) as String] | ||
} | ||
|
||
var lines: [String] = [] | ||
|
||
lines += [ | ||
"Code: \(CFErrorGetCode(self))", | ||
"Domain: \(CFErrorGetDomain(self)!)", | ||
] | ||
|
||
if let localizedErrorDesc = userInfo[kCFErrorLocalizedDescriptionKey] { | ||
lines.append("Description: \(localizedErrorDesc)") | ||
} | ||
|
||
if let localizedFailureReasonDesc = userInfo[kCFErrorLocalizedFailureReasonKey] { | ||
lines.append("Failure Reason: \(localizedFailureReasonDesc)") | ||
} | ||
|
||
if let localizedRecoverySuggestion = userInfo[kCFErrorLocalizedRecoverySuggestionKey] { | ||
lines.append("Recovery Suggestion: \(localizedRecoverySuggestion)") | ||
} | ||
|
||
if let underlyingError = userInfo[kCFErrorUnderlyingErrorKey] { | ||
lines += describe(title: "Underlying Error", error: underlyingError) | ||
} | ||
|
||
return lines | ||
} | ||
|
||
} | ||
|
||
extension NSError { | ||
|
||
var humanReadableDescription: String { | ||
humanReadableDescriptionLines.joined(separator: "\n") | ||
} | ||
|
||
var humanReadableDescriptionLines: [String] { | ||
|
||
var lines: [String] = [] | ||
|
||
lines += [ | ||
"Code: \(code)", | ||
"Domain: \(domain)", | ||
"Description: \(localizedDescription)", | ||
] | ||
|
||
if let localizedFailureReason { | ||
lines.append("Failure Reason: \(localizedFailureReason)") | ||
} | ||
|
||
if let localizedRecoverySuggestion { | ||
lines.append("Recovery Suggestion: \(localizedRecoverySuggestion)") | ||
} | ||
|
||
if let localizedRecoveryOptions { | ||
lines += ["Recovery Options:"] + localizedRecoveryOptions.enumerated().map { idx, option in | ||
" Option \(idx): \(option)" | ||
} | ||
} | ||
|
||
if let underlyingError = userInfo[NSUnderlyingErrorKey] as? NSError { | ||
lines += describe(title: "Underlying Error", error: underlyingError) | ||
} | ||
|
||
if #available(macOS 11.3, iOS 14.5, watchOS 7.4, tvOS 14.5, *) { | ||
|
||
if let underlyingErrors = userInfo[NSMultipleUnderlyingErrorsKey] as? NSError { | ||
lines += ["Underlying Errors:"] + describe(title: "Error 0", error: underlyingErrors) | ||
} | ||
|
||
if let underlyignErrors = userInfo[NSMultipleUnderlyingErrorsKey] as? NSArray { | ||
lines += ["Underlying Errors:"] + underlyignErrors.enumerated().flatMap { idx, error in | ||
return describe(title: "Error \(idx)", error: error) | ||
}.map { " \($0)" } | ||
} | ||
|
||
} | ||
|
||
return lines | ||
} | ||
|
||
} | ||
|
||
private func describe(title: String, error: Any) -> [String] { | ||
if let error = error as? NSError { | ||
return ["\(title):"] + error.humanReadableDescriptionLines.map { " \($0)" } | ||
} | ||
else { | ||
let error = error as! CFError // swiftlint:disable:this force_cast | ||
return ["\(title):"] + error.humanReadableDescriptionLines.map { " \($0)" } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// ErrorsTests.swift | ||
// Shield | ||
// | ||
// Copyright © 2021 Outfox, inc. | ||
// | ||
// | ||
// Distributed under the MIT License, See LICENSE for details. | ||
// | ||
|
||
import Foundation | ||
@testable import ShieldSecurity | ||
import XCTest | ||
|
||
class ErrorsTests: XCTestCase { | ||
|
||
func testCFErrorHumanReadable() { | ||
|
||
let error = CFErrorCreate(nil, "TestErrorDomain" as CFString, -1234, [ | ||
kCFErrorLocalizedDescriptionKey: "An Error Occurred" as CFString, | ||
kCFErrorLocalizedFailureReasonKey: "Invalid Parameters" as CFString, | ||
kCFErrorLocalizedRecoverySuggestionKey: "Pass valid parameters" as CFString, | ||
kCFErrorUnderlyingErrorKey: CFErrorCreate(nil, kCFErrorDomainPOSIX, 1, nil)!, | ||
] as [CFString: Any] as CFDictionary)! | ||
|
||
XCTAssertEqual( | ||
error.humanReadableDescription, | ||
""" | ||
Code: -1234 | ||
Domain: TestErrorDomain | ||
Description: An Error Occurred | ||
Failure Reason: Invalid Parameters | ||
Recovery Suggestion: Pass valid parameters | ||
Underlying Error: | ||
Code: 1 | ||
Domain: NSPOSIXErrorDomain | ||
Description: The operation couldn’t be completed. Operation not permitted | ||
Failure Reason: Operation not permitted | ||
""" | ||
) | ||
} | ||
|
||
@available(macOS 11.3, iOS 14.5, watchOS 7.4, tvOS 14.5, *) | ||
func testNSErrorHumanReadable() { | ||
|
||
let error = NSError(domain: "TestErrorDomain", code: -1234, userInfo: [ | ||
kCFErrorLocalizedDescriptionKey: "An Error Occurred" as CFString, | ||
kCFErrorLocalizedFailureReasonKey: "Invalid Parameters" as CFString, | ||
kCFErrorLocalizedRecoverySuggestionKey: "Pass valid parameters" as CFString, | ||
NSLocalizedRecoveryOptionsErrorKey as CFString: [ | ||
"Parameter 1 must be a string", | ||
"Parameter 2 must be a boolean", | ||
], | ||
kCFErrorUnderlyingErrorKey: POSIXError(.EPERM), | ||
NSMultipleUnderlyingErrorsKey as CFString: [POSIXError(.EPERM), POSIXError(.ENOENT)], | ||
] as [String: Any]) | ||
|
||
XCTAssertEqual( | ||
error.humanReadableDescription, | ||
""" | ||
Code: -1234 | ||
Domain: TestErrorDomain | ||
Description: An Error Occurred | ||
Failure Reason: Invalid Parameters | ||
Recovery Suggestion: Pass valid parameters | ||
Recovery Options: | ||
Option 0: Parameter 1 must be a string | ||
Option 1: Parameter 2 must be a boolean | ||
Underlying Error: | ||
Code: 1 | ||
Domain: NSPOSIXErrorDomain | ||
Description: The operation couldn’t be completed. Operation not permitted | ||
Failure Reason: Operation not permitted | ||
Underlying Errors: | ||
Error 0: | ||
Code: 1 | ||
Domain: NSPOSIXErrorDomain | ||
Description: The operation couldn’t be completed. Operation not permitted | ||
Failure Reason: Operation not permitted | ||
Error 1: | ||
Code: 2 | ||
Domain: NSPOSIXErrorDomain | ||
Description: The operation couldn’t be completed. No such file or directory | ||
Failure Reason: No such file or directory | ||
""" | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters