-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathCodeVerifierTests.swift
More file actions
62 lines (52 loc) · 2.73 KB
/
Copy pathCodeVerifierTests.swift
File metadata and controls
62 lines (52 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@testable import WordPressAuthenticator
import XCTest
class CodeVerifierTests: XCTestCase {
func testCodeVerifierIsRandomString() throws {
XCTAssertNotEqual(
try ProofKeyForCodeExchange.CodeVerifier.makeRandomCodeVerifier(),
try ProofKeyForCodeExchange.CodeVerifier.makeRandomCodeVerifier()
)
}
func testGeneratedCodeVerifierHasLength43() throws {
// 43 is the recommended length. See https://www.rfc-editor.org/rfc/rfc7636#section-4.1
XCTAssertEqual(try ProofKeyForCodeExchange.CodeVerifier.makeRandomCodeVerifier().rawValue.count, 43)
XCTAssertEqual(try ProofKeyForCodeExchange.CodeVerifier.makeRandomCodeVerifier().rawValue.count, 43)
}
func testCodeVerifierIsRandomStringWithURLSafeCharacters() throws {
// Notice we call `inverted` and assert nil to make sure none of the characters that are
// not URL safe are in the generated string.
//
// Given the generation is random, we repeat the test twice to increase reliability.
XCTAssertNil(
try ProofKeyForCodeExchange.CodeVerifier.makeRandomCodeVerifier().rawValue
.rangeOfCharacter(from: CharacterSet.urlQueryAllowed.inverted)
)
XCTAssertNil(
try ProofKeyForCodeExchange.CodeVerifier.makeRandomCodeVerifier().rawValue
.rangeOfCharacter(from: CharacterSet.urlQueryAllowed.inverted)
)
}
// MARK: –
func testCodeVerifierInitFailsWithValueShorterThan43() {
// 43 is the minimum length from the spec
XCTAssertNil(ProofKeyForCodeExchange.CodeVerifier(value: ""))
XCTAssertNil(ProofKeyForCodeExchange.CodeVerifier(value: "a".repeated(42)))
XCTAssertEqual(ProofKeyForCodeExchange.CodeVerifier(value: "a".repeated(43))?.rawValue.count, 43)
XCTAssertEqual(ProofKeyForCodeExchange.CodeVerifier(value: "a".repeated(44))?.rawValue.count, 44)
}
func testCodeVerifierInitFailsWithValueLongerThan128() {
XCTAssertEqual(ProofKeyForCodeExchange.CodeVerifier(value: "a".repeated(127))?.rawValue.count, 127)
XCTAssertEqual(ProofKeyForCodeExchange.CodeVerifier(value: "a".repeated(128))?.rawValue.count, 128)
XCTAssertNil(ProofKeyForCodeExchange.CodeVerifier(value: "a".repeated(129)))
}
func testCodeVerifierInitFailsWithInvalidCharacters() {
XCTAssertNil(ProofKeyForCodeExchange.CodeVerifier(value: "a".repeated(43) + "?"))
XCTAssertNil(ProofKeyForCodeExchange.CodeVerifier(value: "a".repeated(43) + "^"))
XCTAssertNil(ProofKeyForCodeExchange.CodeVerifier(value: "a".repeated(43) + "🤔"))
}
}
private extension String {
func repeated(_ times: Int) -> String {
(0..<times).map { _ in self }.joined()
}
}