Skip to content

Commit 4f38ae1

Browse files
committed
add AuthenticatorData tests
1 parent 27dcb3c commit 4f38ae1

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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

Comments
 (0)