| 1 | import Foundation |
| 2 | import Security |
| 3 | import LocalAuthentication |
| 4 | |
| 5 | @main |
| 6 | enum AccountDeviceTests { |
| 7 | static func check(_ value: Bool, line: UInt = #line) throws { |
| 8 | if !value { throw CageAccountError("AccountDeviceTests failed at line \(line)") } |
| 9 | } |
| 10 | static func main() throws { |
| 11 | let source = "cage-account-test-" + UUID().uuidString |
| 12 | let destination = "cage-account-test-" + UUID().uuidString |
| 13 | let credential = ["id": UUID().uuidString, "secret": UUID().uuidString] |
| 14 | let kinds = CommandLine.arguments.contains("--biometric") ? ["automatic", "biometric"] : ["automatic"] |
| 15 | for kind in kinds { |
| 16 | defer { |
| 17 | try? CageAccountKeychain.remove(binding: source, kind: kind) |
| 18 | try? CageAccountKeychain.remove(binding: destination, kind: kind) |
| 19 | } |
| 20 | let present = kind == "biometric" ? ["available", "authentication_required"] : ["available"] |
| 21 | try CageAccountKeychain.store(credential, binding: source, kind: kind) |
| 22 | try check(present.contains(CageAccountKeychain.status(binding: source, kind: kind, expectedID: credential["id"]))) |
| 23 | |
| 24 | |
| 25 | try check(CageAccountKeychain.status(binding: destination, kind: kind, expectedID: credential["id"]) == "missing") |
| 26 | let mismatched = CageAccountKeychain.status(binding: source, kind: kind, expectedID: "other-id") |
| 27 | try check(mismatched == "missing" || (kind == "biometric" && mismatched == "authentication_required")) |
| 28 | try check(CageAccountKeychain.status(binding: source, kind: kind, expectedID: nil) == "disabled") |
| 29 | try CageAccountKeychain.store(credential, binding: destination, kind: kind) |
| 30 | try check(present.contains(CageAccountKeychain.status(binding: destination, kind: kind, expectedID: credential["id"]))) |
| 31 | if kind == "automatic" { |
| 32 | let read = try CageAccountKeychain.read(binding: destination, kind: kind) |
| 33 | try check(read["id"] as? String == credential["id"]) |
| 34 | try check(read["secret"] as? String == credential["secret"]) |
| 35 | } |
| 36 | try CageAccountKeychain.remove(binding: destination, kind: kind) |
| 37 | try check(CageAccountKeychain.status(binding: destination, kind: kind, expectedID: credential["id"]) == "missing") |
| 38 | print("PASS: \(kind) binding, migration, metadata/authentication status, re-enrollment and removal") |
| 39 | } |
| 40 | |
| 41 | try check(CageAccountKeychain.availability(status: errSecSuccess, attributes: [:], expectedID: "legacy", kind: "automatic") == "available") |
| 42 | for error in [errSecInteractionNotAllowed, errSecNotAvailable, errSecMissingEntitlement, errSecAuthFailed, errSecUserCanceled] { |
| 43 | try check(CageAccountKeychain.availability(status: error, attributes: nil, expectedID: "id", kind: "automatic") == "unavailable") |
| 44 | } |
| 45 | print("PASS: legacy metadata and transient/permission errors are not treated as missing credentials") |
| 46 | } |
| 47 | } |