1
1
import XCTest
2
2
@testable import WebAuthn
3
3
4
- // swiftlint:disable line_length
5
-
6
4
final class AuthenticatorDataTests : XCTestCase {
7
5
// Information about authenticator data: https://w3c.github.io/webauthn/#authenticator-data
8
6
7
+ // Authenticator data
8
+ let rpIdHash = [ UInt8] ( repeating: 0 , count: 32 )
9
+ let signCount = [ UInt8] ( repeating: 0 , count: 4 )
10
+
11
+ // Attested credential data
12
+ let aaguid = [ UInt8] ( repeating: 0 , count: 16 )
13
+ let publicKeyBytes : [ UInt8 ] = [ 1 , 2 , 3 , 4 , 5 , 6 ]
14
+
9
15
func testInitFromBytesFailsIfAuthDataIsTooShort( ) throws {
10
16
let tooManyBytes = [ UInt8] ( repeating: 1 , count: 36 )
11
17
XCTAssertThrowsError ( try AuthenticatorData ( bytes: Data ( tooManyBytes) ) ) { error in
@@ -14,9 +20,7 @@ final class AuthenticatorDataTests: XCTestCase {
14
20
}
15
21
16
22
func testInitFromBytesFailsIfAttestedCredentialDataFlagIsSetButDataIsActuallyNotThere( ) throws {
17
- let rpIdHash = [ UInt8] ( repeating: 0 , count: 32 )
18
23
let flagsByte : [ UInt8 ] = [ 0b01000000 ] // "attested credential data included"
19
- let signCount = [ UInt8] ( repeating: 0 , count: 4 )
20
24
21
25
let bytes = rpIdHash + flagsByte + signCount
22
26
@@ -26,9 +30,7 @@ final class AuthenticatorDataTests: XCTestCase {
26
30
}
27
31
28
32
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 )
33
+ let flagsByte : [ UInt8 ] = [ 0b00000000 ] // "attested credential data not included"
32
34
let fakeAttestedCredentialData : [ UInt8 ] = [ UInt8] ( repeating: 0 , count: 4 )
33
35
34
36
let bytes = rpIdHash + flagsByte + signCount + fakeAttestedCredentialData
@@ -37,4 +39,58 @@ final class AuthenticatorDataTests: XCTestCase {
37
39
XCTAssertEqual ( error as? WebAuthnError , . attestedCredentialFlagNotSet)
38
40
}
39
41
}
42
+
43
+ func testInitFromBytesFailsIfExtensionDataFlagIsSetButDataIsNotIncluded( ) throws {
44
+ let flagsByte : [ UInt8 ] = [ 0b10000000 ] // "extension data included"
45
+
46
+ let bytes = rpIdHash + flagsByte + signCount
47
+
48
+ XCTAssertThrowsError ( try AuthenticatorData ( bytes: Data ( bytes) ) ) { error in
49
+ XCTAssertEqual ( error as? WebAuthnError , . extensionDataMissing)
50
+ }
51
+ }
52
+
53
+ func testInitFromBytesFailsIfCredentialIdIsTooShort( ) throws {
54
+ let flagsByte : [ UInt8 ] = [ 0b01000000 ] // "attested credential data included"
55
+
56
+ let credentialLength : [ UInt8 ] = [ 0 , 0b00000010 ] // here we say credentialId has length 2
57
+ let credentialID : [ UInt8 ] = [ 13 ] // but we only provide a credentialId of length 1
58
+
59
+ let attestedCredentialData = aaguid + credentialLength + credentialID
60
+ let bytes = rpIdHash + flagsByte + signCount + attestedCredentialData
61
+
62
+ XCTAssertThrowsError ( try AuthenticatorData ( bytes: Data ( bytes) ) ) { error in
63
+ XCTAssertEqual ( error as? WebAuthnError , . credentialIDTooShort)
64
+ }
65
+ }
66
+
67
+ func testInitFromBytesSucceeds( ) throws {
68
+ let flagsByte : [ UInt8 ] = [ 0b01000000 ] // "attested credential data included"
69
+
70
+ let credentialLength : [ UInt8 ] = [ 0 , 0b00000010 ] // here we say credentialId has length 2
71
+ let credentialID : [ UInt8 ] = [ 13 , 12 ] // but we only provide a credentialId of length 1
72
+
73
+ let attestedCredentialData = aaguid + credentialLength + credentialID
74
+ let bytes = rpIdHash + flagsByte + signCount + attestedCredentialData + publicKeyBytes
75
+
76
+ let authenticatorData = try AuthenticatorData ( bytes: Data ( bytes) )
77
+
78
+ XCTAssertEqual ( authenticatorData. relyingPartyIDHash, rpIdHash)
79
+ XCTAssertEqual (
80
+ authenticatorData. flags,
81
+ . init(
82
+ userPresent: false ,
83
+ userVerified: false ,
84
+ isBackupEligible: false ,
85
+ isCurrentlyBackedUp: false ,
86
+ attestedCredentialData: true ,
87
+ extensionDataIncluded: false
88
+ )
89
+ )
90
+ XCTAssertEqual ( authenticatorData. counter, Data ( signCount) . toInteger ( endian: . big) )
91
+ XCTAssertEqual ( authenticatorData. extData, nil )
92
+ XCTAssertEqual ( authenticatorData. attestedData? . aaguid, aaguid)
93
+ XCTAssertEqual ( authenticatorData. attestedData? . credentialID, credentialID)
94
+ XCTAssertEqual ( authenticatorData. attestedData? . publicKey, publicKeyBytes)
95
+ }
40
96
}
0 commit comments