Skip to content

Commit 6e6c012

Browse files
authored
Migrated tests to Swift Testing (#99)
1 parent 0e13155 commit 6e6c012

8 files changed

+257
-342
lines changed

Tests/WebAuthnTests/AuthenticatorAttestationGloballyUniqueIDTests.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
import XCTest
14+
import Foundation
15+
import Testing
1516
@testable import WebAuthn
1617

17-
final class AuthenticatorAttestationGloballyUniqueIDTests: XCTestCase {
18-
func testByteCoding() throws {
18+
struct AuthenticatorAttestationGloballyUniqueIDTests {
19+
@Test
20+
func byteCoding() {
1921
let aaguid = AuthenticatorAttestationGloballyUniqueID(bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
20-
XCTAssertNotNil(aaguid)
21-
XCTAssertEqual(aaguid?.bytes, [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f])
22-
XCTAssertEqual(aaguid?.id, UUID(uuid: (0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f)))
23-
XCTAssertEqual(aaguid, AuthenticatorAttestationGloballyUniqueID(uuid: UUID(uuid: (0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f))))
24-
XCTAssertEqual(aaguid, AuthenticatorAttestationGloballyUniqueID(uuidString: "00010203-0405-0607-0809-0A0B0C0D0E0F" ))
22+
#expect(aaguid != nil)
23+
#expect(aaguid?.bytes == [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f])
24+
#expect(aaguid?.id == UUID(uuid: (0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f)))
25+
#expect(aaguid == AuthenticatorAttestationGloballyUniqueID(uuid: UUID(uuid: (0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f))))
26+
#expect(aaguid == AuthenticatorAttestationGloballyUniqueID(uuidString: "00010203-0405-0607-0809-0A0B0C0D0E0F" ))
2527
}
2628

27-
func testInvalidByteDecoding() throws {
28-
XCTAssertNil(AuthenticatorAttestationGloballyUniqueID(bytes: []))
29-
XCTAssertNil(AuthenticatorAttestationGloballyUniqueID(bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]))
30-
XCTAssertNil(AuthenticatorAttestationGloballyUniqueID(bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]))
29+
@Test
30+
func invalidByteDecoding() {
31+
#expect(AuthenticatorAttestationGloballyUniqueID(bytes: []) == nil)
32+
#expect(AuthenticatorAttestationGloballyUniqueID(bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) == nil)
33+
#expect(AuthenticatorAttestationGloballyUniqueID(bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) == nil)
3134
}
3235
}

Tests/WebAuthnTests/DurationTests.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
import XCTest
14+
import Testing
1515
@testable import WebAuthn
1616

17-
final class DurationTests: XCTestCase {
18-
func testMilliseconds() throws {
19-
XCTAssertEqual(Duration.milliseconds(1234).milliseconds, 1234)
20-
XCTAssertEqual(Duration.milliseconds(-1234).milliseconds, -1234)
21-
XCTAssertEqual(Duration.microseconds(12345).milliseconds, 12)
22-
XCTAssertEqual(Duration.microseconds(-12345).milliseconds, -12)
17+
struct DurationTests {
18+
@Test
19+
func milliseconds() {
20+
#expect(Duration.milliseconds(1234).milliseconds == 1234)
21+
#expect(Duration.milliseconds(-1234).milliseconds == -1234)
22+
#expect(Duration.microseconds(12345).milliseconds == 12)
23+
#expect(Duration.microseconds(-12345).milliseconds == -12)
2324
}
2425
}

Tests/WebAuthnTests/HelpersTests.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,29 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
import XCTest
15-
14+
import Foundation
15+
import Testing
1616
@testable import WebAuthn
1717

18-
final class HelpersTests: XCTestCase {
19-
func testBase64URLEncodeReturnsCorrectString() {
18+
struct HelpersTests {
19+
@Test
20+
func base64URLEncodeReturnsCorrectString() {
2021
let input: [UInt8] = [1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0]
2122
let expectedBase64 = "AQABAAEBAAEAAQEAAAABAA=="
2223
let expectedBase64URL = "AQABAAEBAAEAAQEAAAABAA"
2324

2425
let base64Encoded = input.base64EncodedString()
2526
let base64URLEncoded = input.base64URLEncodedString()
2627

27-
XCTAssertEqual(expectedBase64, base64Encoded.asString())
28-
XCTAssertEqual(expectedBase64URL, base64URLEncoded.asString())
28+
#expect(expectedBase64 == base64Encoded.asString())
29+
#expect(expectedBase64URL == base64URLEncoded.asString())
2930
}
3031

31-
func testEncodeBase64Codable() throws {
32+
@Test
33+
func encodeBase64Codable() throws {
3234
let base64 = EncodedBase64("AQABAAEBAAEAAQEAAAABAA==")
3335
let json = try JSONEncoder().encode(base64)
3436
let decodedBase64 = try JSONDecoder().decode(EncodedBase64.self, from: json)
35-
XCTAssertEqual(base64, decodedBase64)
37+
#expect(base64 == decodedBase64)
3638
}
3739
}

Tests/WebAuthnTests/Utils/assert+async.swift

Lines changed: 0 additions & 50 deletions
This file was deleted.

Tests/WebAuthnTests/Utils/assert+expect.swift

Lines changed: 0 additions & 50 deletions
This file was deleted.

Tests/WebAuthnTests/WebAuthnManagerAuthenticationTests.swift

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
@testable import WebAuthn
15-
import XCTest
15+
import Testing
16+
import Foundation
1617
import SwiftCBOR
1718
import Crypto
1819

19-
final class WebAuthnManagerAuthenticationTests: XCTestCase {
20+
struct WebAuthnManagerAuthenticationTests {
2021
var webAuthnManager: WebAuthnManager!
2122

2223
let challenge: [UInt8] = [1, 0, 1]
2324
let relyingPartyID = "example.com"
2425
let relyingPartyName = "Testy test"
2526
let relyingPartyOrigin = "https://example.com"
2627

27-
override func setUp() {
28+
init() {
2829
let configuration = WebAuthnManager.Configuration(
2930
relyingPartyID: relyingPartyID,
3031
relyingPartyName: relyingPartyName,
@@ -33,98 +34,101 @@ final class WebAuthnManagerAuthenticationTests: XCTestCase {
3334
webAuthnManager = .init(configuration: configuration, challengeGenerator: .mock(generate: challenge))
3435
}
3536

36-
func testBeginAuthentication() async throws {
37+
@Test
38+
func beginAuthentication() async throws {
3739
let allowCredentials: [PublicKeyCredentialDescriptor] = [.init(type: .publicKey, id: [1, 0, 2, 30])]
3840
let options = webAuthnManager.beginAuthentication(
3941
timeout: .seconds(1234),
4042
allowCredentials: allowCredentials,
4143
userVerification: .preferred
4244
)
4345

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)
4951
}
5052

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+
}
5658
}
5759

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])
6164
}
6265
}
63-
64-
func testFinishAuthenticationFailsIfCeremonyTypeDoesNotMatch() throws {
66+
67+
@Test
68+
func finishAuthenticationFailsIfCeremonyTypeDoesNotMatch() throws {
6569
var clientDataJSON = TestClientDataJSON()
6670
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+
}
7174
}
7275

73-
func testFinishAuthenticationFailsIfRelyingPartyIDHashDoesNotMatch() throws {
74-
try assertThrowsError(
75-
finishAuthentication(
76+
@Test
77+
func finishAuthenticationFailsIfRelyingPartyIDHashDoesNotMatch() throws {
78+
#expect(throws: WebAuthnError.relyingPartyIDHashDoesNotMatch) {
79+
try finishAuthentication(
7680
authenticatorData: TestAuthDataBuilder()
7781
.validAuthenticationMock()
7882
.relyingPartyIDHash(fromRelyingPartyID: "wrong-id.org")
7983
.build()
8084
.byteArrayRepresentation
81-
),
82-
expect: WebAuthnError.relyingPartyIDHashDoesNotMatch
83-
)
85+
)
86+
}
8487
}
8588

86-
func testFinishAuthenticationFailsIfUserPresentFlagIsNotSet() throws {
87-
try assertThrowsError(
88-
finishAuthentication(
89+
@Test
90+
func finishAuthenticationFailsIfUserPresentFlagIsNotSet() throws {
91+
#expect(throws: WebAuthnError.userPresentFlagNotSet) {
92+
try finishAuthentication(
8993
authenticatorData: TestAuthDataBuilder()
9094
.validAuthenticationMock()
9195
.flags(0b10000000)
9296
.build()
9397
.byteArrayRepresentation
94-
),
95-
expect: WebAuthnError.userPresentFlagNotSet
96-
)
98+
)
99+
}
97100
}
98101

99-
func testFinishAuthenticationFailsIfUserIsNotVerified() throws {
100-
try assertThrowsError(
101-
finishAuthentication(
102+
@Test
103+
func finishAuthenticationFailsIfUserIsNotVerified() throws {
104+
#expect(throws: WebAuthnError.userVerifiedFlagNotSet) {
105+
try finishAuthentication(
102106
authenticatorData: TestAuthDataBuilder()
103107
.validAuthenticationMock()
104108
.flags(0b10000001)
105109
.build()
106110
.byteArrayRepresentation,
107111
requireUserVerification: true
108-
),
109-
expect: WebAuthnError.userVerifiedFlagNotSet
110-
)
112+
)
113+
}
111114
}
112115

113-
func testFinishAuthenticationFailsIfCredentialCounterIsNotUpToDate() throws {
114-
try assertThrowsError(
115-
finishAuthentication(
116+
@Test
117+
func finishAuthenticationFailsIfCredentialCounterIsNotUpToDate() throws {
118+
#expect(throws: WebAuthnError.potentialReplayAttack) {
119+
try finishAuthentication(
116120
authenticatorData: TestAuthDataBuilder()
117121
.validAuthenticationMock()
118122
.counter([0, 0, 0, 1]) // signCount = 1
119123
.build()
120124
.byteArrayRepresentation,
121125
credentialCurrentSignCount: 2
122-
),
123-
expect: WebAuthnError.potentialReplayAttack
124-
)
126+
)
127+
}
125128
}
126129

127-
func testFinishAuthenticationSucceeds() throws {
130+
@Test
131+
func finishAuthenticationSucceeds() throws {
128132
let credentialID = TestConstants.mockCredentialID
129133
let oldSignCount: UInt32 = 0
130134

@@ -152,8 +156,8 @@ final class WebAuthnManagerAuthenticationTests: XCTestCase {
152156
credentialCurrentSignCount: oldSignCount
153157
)
154158

155-
XCTAssertEqual(verifiedAuthentication.credentialID, credentialID.base64URLEncodedString())
156-
XCTAssertEqual(verifiedAuthentication.newSignCount, oldSignCount + 1)
159+
#expect(verifiedAuthentication.credentialID == credentialID.base64URLEncodedString())
160+
#expect(verifiedAuthentication.newSignCount == oldSignCount + 1)
157161
}
158162

159163
/// Using the default parameters `finishAuthentication` should succeed.

0 commit comments

Comments
 (0)