12
12
//===----------------------------------------------------------------------===//
13
13
14
14
@testable import WebAuthn
15
- import XCTest
15
+ import Testing
16
+ import Foundation
16
17
import SwiftCBOR
17
18
import Crypto
18
19
19
- final class WebAuthnManagerAuthenticationTests : XCTestCase {
20
+ struct WebAuthnManagerAuthenticationTests {
20
21
var webAuthnManager : WebAuthnManager !
21
22
22
23
let challenge : [ UInt8 ] = [ 1 , 0 , 1 ]
23
24
let relyingPartyID = " example.com "
24
25
let relyingPartyName = " Testy test "
25
26
let relyingPartyOrigin = " https://example.com "
26
27
27
- override func setUp ( ) {
28
+ init ( ) {
28
29
let configuration = WebAuthnManager . Configuration (
29
30
relyingPartyID: relyingPartyID,
30
31
relyingPartyName: relyingPartyName,
@@ -33,98 +34,101 @@ final class WebAuthnManagerAuthenticationTests: XCTestCase {
33
34
webAuthnManager = . init( configuration: configuration, challengeGenerator: . mock( generate: challenge) )
34
35
}
35
36
36
- func testBeginAuthentication( ) async throws {
37
+ @Test
38
+ func beginAuthentication( ) async throws {
37
39
let allowCredentials : [ PublicKeyCredentialDescriptor ] = [ . init( type: . publicKey, id: [ 1 , 0 , 2 , 30 ] ) ]
38
40
let options = webAuthnManager. beginAuthentication (
39
41
timeout: . seconds( 1234 ) ,
40
42
allowCredentials: allowCredentials,
41
43
userVerification: . preferred
42
44
)
43
45
44
- XCTAssertEqual ( options. challenge, challenge)
45
- XCTAssertEqual ( options. timeout, . seconds( 1234 ) )
46
- XCTAssertEqual ( options. relyingPartyID, relyingPartyID)
47
- XCTAssertEqual ( options. allowCredentials, allowCredentials)
48
- XCTAssertEqual ( options. userVerification, . preferred)
46
+ #expect ( options. challenge == challenge)
47
+ #expect ( options. timeout == . seconds( 1234 ) )
48
+ #expect ( options. relyingPartyID == relyingPartyID)
49
+ #expect ( options. allowCredentials == allowCredentials)
50
+ #expect ( options. userVerification == . preferred)
49
51
}
50
52
51
- func testFinishAuthenticationFailsIfCredentialTypeIsInvalid ( ) throws {
52
- try assertThrowsError (
53
- finishAuthentication ( type : " invalid " ) ,
54
- expect : WebAuthnError . invalidAssertionCredentialType
55
- )
53
+ @ Test
54
+ func finishAuthenticationFailsIfCredentialTypeIsInvalid ( ) throws {
55
+ #expect ( throws : WebAuthnError . invalidAssertionCredentialType ) {
56
+ try finishAuthentication ( type : " invalid " )
57
+ }
56
58
}
57
59
58
- func testFinishAuthenticationFailsIfClientDataJSONDecodingFails( ) throws {
59
- try assertThrowsError ( finishAuthentication ( clientDataJSON: [ 0 ] ) ) { ( _: DecodingError ) in
60
- return
60
+ @Test
61
+ func finishAuthenticationFailsIfClientDataJSONDecodingFails( ) throws {
62
+ #expect( throws: DecodingError . self) {
63
+ try finishAuthentication ( clientDataJSON: [ 0 ] )
61
64
}
62
65
}
63
-
64
- func testFinishAuthenticationFailsIfCeremonyTypeDoesNotMatch( ) throws {
66
+
67
+ @Test
68
+ func finishAuthenticationFailsIfCeremonyTypeDoesNotMatch( ) throws {
65
69
var clientDataJSON = TestClientDataJSON ( )
66
70
clientDataJSON. type = " webauthn.create "
67
- try assertThrowsError (
68
- finishAuthentication ( clientDataJSON: clientDataJSON. jsonBytes) ,
69
- expect: CollectedClientData . CollectedClientDataVerifyError. ceremonyTypeDoesNotMatch
70
- )
71
+ #expect( throws: CollectedClientData . CollectedClientDataVerifyError. ceremonyTypeDoesNotMatch) {
72
+ try finishAuthentication ( clientDataJSON: clientDataJSON. jsonBytes)
73
+ }
71
74
}
72
75
73
- func testFinishAuthenticationFailsIfRelyingPartyIDHashDoesNotMatch( ) throws {
74
- try assertThrowsError (
75
- finishAuthentication (
76
+ @Test
77
+ func finishAuthenticationFailsIfRelyingPartyIDHashDoesNotMatch( ) throws {
78
+ #expect( throws: WebAuthnError . relyingPartyIDHashDoesNotMatch) {
79
+ try finishAuthentication (
76
80
authenticatorData: TestAuthDataBuilder ( )
77
81
. validAuthenticationMock ( )
78
82
. relyingPartyIDHash ( fromRelyingPartyID: " wrong-id.org " )
79
83
. build ( )
80
84
. byteArrayRepresentation
81
- ) ,
82
- expect: WebAuthnError . relyingPartyIDHashDoesNotMatch
83
- )
85
+ )
86
+ }
84
87
}
85
88
86
- func testFinishAuthenticationFailsIfUserPresentFlagIsNotSet( ) throws {
87
- try assertThrowsError (
88
- finishAuthentication (
89
+ @Test
90
+ func finishAuthenticationFailsIfUserPresentFlagIsNotSet( ) throws {
91
+ #expect( throws: WebAuthnError . userPresentFlagNotSet) {
92
+ try finishAuthentication (
89
93
authenticatorData: TestAuthDataBuilder ( )
90
94
. validAuthenticationMock ( )
91
95
. flags ( 0b10000000 )
92
96
. build ( )
93
97
. byteArrayRepresentation
94
- ) ,
95
- expect: WebAuthnError . userPresentFlagNotSet
96
- )
98
+ )
99
+ }
97
100
}
98
101
99
- func testFinishAuthenticationFailsIfUserIsNotVerified( ) throws {
100
- try assertThrowsError (
101
- finishAuthentication (
102
+ @Test
103
+ func finishAuthenticationFailsIfUserIsNotVerified( ) throws {
104
+ #expect( throws: WebAuthnError . userVerifiedFlagNotSet) {
105
+ try finishAuthentication (
102
106
authenticatorData: TestAuthDataBuilder ( )
103
107
. validAuthenticationMock ( )
104
108
. flags ( 0b10000001 )
105
109
. build ( )
106
110
. byteArrayRepresentation,
107
111
requireUserVerification: true
108
- ) ,
109
- expect: WebAuthnError . userVerifiedFlagNotSet
110
- )
112
+ )
113
+ }
111
114
}
112
115
113
- func testFinishAuthenticationFailsIfCredentialCounterIsNotUpToDate( ) throws {
114
- try assertThrowsError (
115
- finishAuthentication (
116
+ @Test
117
+ func finishAuthenticationFailsIfCredentialCounterIsNotUpToDate( ) throws {
118
+ #expect( throws: WebAuthnError . potentialReplayAttack) {
119
+ try finishAuthentication (
116
120
authenticatorData: TestAuthDataBuilder ( )
117
121
. validAuthenticationMock ( )
118
122
. counter ( [ 0 , 0 , 0 , 1 ] ) // signCount = 1
119
123
. build ( )
120
124
. byteArrayRepresentation,
121
125
credentialCurrentSignCount: 2
122
- ) ,
123
- expect: WebAuthnError . potentialReplayAttack
124
- )
126
+ )
127
+ }
125
128
}
126
129
127
- func testFinishAuthenticationSucceeds( ) throws {
130
+ @Test
131
+ func finishAuthenticationSucceeds( ) throws {
128
132
let credentialID = TestConstants . mockCredentialID
129
133
let oldSignCount : UInt32 = 0
130
134
@@ -152,8 +156,8 @@ final class WebAuthnManagerAuthenticationTests: XCTestCase {
152
156
credentialCurrentSignCount: oldSignCount
153
157
)
154
158
155
- XCTAssertEqual ( verifiedAuthentication. credentialID, credentialID. base64URLEncodedString ( ) )
156
- XCTAssertEqual ( verifiedAuthentication. newSignCount, oldSignCount + 1 )
159
+ #expect ( verifiedAuthentication. credentialID == credentialID. base64URLEncodedString ( ) )
160
+ #expect ( verifiedAuthentication. newSignCount == oldSignCount + 1 )
157
161
}
158
162
159
163
/// Using the default parameters `finishAuthentication` should succeed.
0 commit comments