Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import Foundation
6
import Shared
7
import XCTest
8
import SwiftKeychainWrapper
9
10
class AuthenticationKeychainInfoTests: XCTestCase {
11
12
func testEncodingAndDecoding() {
13
let passcode = "1234"
14
let authInfo = AuthenticationKeychainInfo(passcode: passcode)
15
authInfo.updateRequiredPasscodeInterval(.fiveMinutes)
16
authInfo.recordValidation()
17
authInfo.recordFailedAttempt() // failed attempt should be 1
18
authInfo.lockOutUser() //lock out a user so a lockoutInterval is set.
19
authInfo.useTouchID = true
20
21
let savedInterval = authInfo.lockOutInterval
22
let savedValidation = authInfo.lastPasscodeValidationInterval
23
24
KeychainWrapper.sharedAppContainerKeychain.setAuthenticationInfo(authInfo) //Save to disk
25
let decodedAuthInfo = KeychainWrapper.sharedAppContainerKeychain.authenticationInfo()! //Fetch from disk
26
27
XCTAssertEqual(savedInterval, decodedAuthInfo.lockOutInterval)
28
XCTAssertEqual(passcode, decodedAuthInfo.passcode)
29
XCTAssertEqual(1, decodedAuthInfo.failedAttempts, "We performed a recordFailedAttempt. This should be 1.")
30
XCTAssertTrue(decodedAuthInfo.useTouchID)
31
XCTAssertEqual(savedValidation, decodedAuthInfo.lastPasscodeValidationInterval)
32
XCTAssertEqual(PasscodeInterval.fiveMinutes, decodedAuthInfo.requiredPasscodeInterval)
33
}
34
35
func testNilIntervalsArentZero() {
36
let passcode = "1234"
37
let authInfo = AuthenticationKeychainInfo(passcode: passcode)
38
39
KeychainWrapper.sharedAppContainerKeychain.setAuthenticationInfo(authInfo) //Save to disk
40
let decodedAuthInfo = KeychainWrapper.sharedAppContainerKeychain.authenticationInfo()! //Fetch from disk
41
42
XCTAssertNil(decodedAuthInfo.lockOutInterval, "The lockoutInterval was never used. It should be nil")
43
}
44
45
}