An iOS TOTP generator app.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

67 lines
2.5 KiB

//
// TOTPKeyTests.swift
// OTPKitTests
//
// Created by Shadowfacts on 8/21/21.
//
import XCTest
@testable import OTPKit
class TOTPKeyTests: XCTestCase {
func testDecodeSimpleKey() throws {
let decoded = TOTPKey(urlComponents: URLComponents(string: "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example")!)
XCTAssertNotNil(decoded)
let key = decoded!
XCTAssertEqual(key.secret, Data([
UInt8(ascii: "H"),
UInt8(ascii: "e"),
UInt8(ascii: "l"),
UInt8(ascii: "l"),
UInt8(ascii: "o"),
UInt8(ascii: "!"),
0xDE, 0xAD, 0xBE, 0xEF
]))
XCTAssertEqual(key.issuer, "Example")
XCTAssertEqual(key.label, "alice@google.com")
}
func testDecodeOptionalParameters() throws {
let decoded = TOTPKey(urlComponents: URLComponents(string: "otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=8&period=60")!)
XCTAssertNotNil(decoded)
let key = decoded!
XCTAssertEqual(key.secret, Data([
0x3D, 0xC6, 0xCA, 0xA4,
0x82, 0x4A, 0x6D, 0x28,
0x87, 0x67, 0xB2, 0x33,
0x1E, 0x20, 0xB4, 0x31,
0x66, 0xCB, 0x85, 0xD9,
]))
XCTAssertEqual(key.issuer, "ACME Co")
XCTAssertEqual(key.label, "john.doe@email.com")
XCTAssertEqual(key.period, 60)
XCTAssertEqual(key.digits, 8)
}
func testDecodeInvalidAlgorithm() {
let components = URLComponents(string: "otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&algorithm=SHA256")!
XCTAssertNil(TOTPKey(urlComponents: components))
}
func testRoundtripURL() {
let components = URLComponents(string: "otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=8&period=60")!
let key = TOTPKey(urlComponents: components)
XCTAssertNotNil(key)
let outputComponents = URLComponents(url: key!.url, resolvingAgainstBaseURL: false)!
XCTAssertEqual(outputComponents.scheme, components.scheme)
XCTAssertEqual(outputComponents.host, components.host)
XCTAssertEqual(outputComponents.path, components.path)
XCTAssertEqual(
outputComponents.queryItems!.sorted { (a, b) in a.name < b.name },
components.queryItems!.sorted { (a, b) in a.name < b.name }
)
}
}