|
| 1 | +import XCTest |
| 2 | +@testable import WebAuthn |
| 3 | + |
| 4 | +// swiftlint:disable line_length |
| 5 | + |
| 6 | +final class AuthenticatorDataTests: XCTestCase { |
| 7 | + // Information about authenticator data: https://w3c.github.io/webauthn/#authenticator-data |
| 8 | + |
| 9 | + func testInitFromBytesFailsIfAuthDataIsTooShort() throws { |
| 10 | + let tooManyBytes = [UInt8](repeating: 1, count: 36) |
| 11 | + XCTAssertThrowsError(try AuthenticatorData(bytes: Data(tooManyBytes))) { error in |
| 12 | + XCTAssertEqual(error as? WebAuthnError, .authDataTooShort) |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + func testInitFromBytesFailsIfAttestedCredentialDataFlagIsSetButDataIsActuallyNotThere() throws { |
| 17 | + let rpIdHash = [UInt8](repeating: 0, count: 32) |
| 18 | + let flagsByte: [UInt8] = [0b01000000] // "attested credential data included" |
| 19 | + let signCount = [UInt8](repeating: 0, count: 4) |
| 20 | + |
| 21 | + let bytes = rpIdHash + flagsByte + signCount |
| 22 | + |
| 23 | + XCTAssertThrowsError(try AuthenticatorData(bytes: Data(bytes))) { error in |
| 24 | + XCTAssertEqual(error as? WebAuthnError, .attestedCredentialDataMissing) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + func testInitFromBytesFailsIfAttestedCredentialDataFlagIsNotSetButThereActuallyIsData() throws { |
| 29 | + let rpIdHash = [UInt8](repeating: 0, count: 32) |
| 30 | + let flagsByte: [UInt8] = [0b00000000] // "attested credential data included" |
| 31 | + let signCount = [UInt8](repeating: 0, count: 4) |
| 32 | + let fakeAttestedCredentialData: [UInt8] = [UInt8](repeating: 0, count: 4) |
| 33 | + |
| 34 | + let bytes = rpIdHash + flagsByte + signCount + fakeAttestedCredentialData |
| 35 | + |
| 36 | + XCTAssertThrowsError(try AuthenticatorData(bytes: Data(bytes))) { error in |
| 37 | + XCTAssertEqual(error as? WebAuthnError, .attestedCredentialFlagNotSet) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
0 commit comments