Add logic for more customizable password generator

This commit is contained in:
Danny Moesch 2020-02-28 19:04:53 +01:00 committed by Mingshen Sun
parent 49a371d495
commit ff014a5699
10 changed files with 352 additions and 131 deletions

View file

@ -0,0 +1,29 @@
//
// Array+SlicesTest.swift
// passKitTests
//
// Created by Danny Moesch on 28.02.20.
// Copyright © 2020 Bob Sun. All rights reserved.
//
import XCTest
@testable import passKit
class ArraySlicesTest: XCTestCase {
func testZeroCount() {
XCTAssertEqual([1, 2, 3].slices(count: 0), [])
}
func testEmptyArray() {
XCTAssertEqual(([] as [String]).slices(count: 4), [[], [], [], []])
}
func testSlices() {
XCTAssertEqual([1, 2, 3].slices(count: 3), [[1], [2], [3]])
XCTAssertEqual([1, 2, 3, 4].slices(count: 3), [[1], [2], [3, 4]])
XCTAssertEqual([1, 2, 3, 4].slices(count: 2), [[1, 2], [3, 4]])
XCTAssertEqual([1, 2, 3, 4, 5].slices(count: 2), [[1, 2], [3, 4, 5]])
}
}

View file

@ -1,41 +0,0 @@
//
// PasswordGeneratorFlavorTest.swift
// passKitTests
//
// Created by Danny Moesch on 28.11.18.
// Copyright © 2018 Bob Sun. All rights reserved.
//
import KeychainAccess
import XCTest
@testable import passKit
class PasswordGeneratorFlavorTest: XCTestCase {
private let KEYCHAIN_PASSWORD_LENGTH = Keychain.generatePassword().count
func testLocalizedName() {
XCTAssertEqual(PasswordGeneratorFlavor.apple.localized, "Apple".localize())
XCTAssertEqual(PasswordGeneratorFlavor.random.localized, "Random".localize())
}
func testDefaultLength() {
// Ensure properly chosen default length values. So this check no longer needs to be performed in the code.
PasswordGeneratorFlavor.allCases.map { $0.defaultLength }.forEach { defaultLength in
XCTAssertLessThanOrEqual(defaultLength.min, defaultLength.max)
XCTAssertLessThanOrEqual(defaultLength.def, defaultLength.max)
XCTAssertGreaterThanOrEqual(defaultLength.def, defaultLength.min)
}
}
func testGeneratePassword() {
let apple = PasswordGeneratorFlavor.apple
let random = PasswordGeneratorFlavor.random
XCTAssertEqual(apple.generate(length: 4).count, KEYCHAIN_PASSWORD_LENGTH)
XCTAssertEqual(random.generate(length: 0).count, 0)
XCTAssertEqual(random.generate(length: 4).count, 4)
XCTAssertEqual(random.generate(length: 100).count, 100)
}
}

View file

@ -0,0 +1,21 @@
//
// PasswordGeneratorFlavorTest.swift
// passKitTests
//
// Created by Danny Moesch on 28.11.18.
// Copyright © 2018 Bob Sun. All rights reserved.
//
import XCTest
@testable import passKit
class PasswordGeneratorFlavorTest: XCTestCase {
func testLengthLimits() {
// Ensure properly chosen length limits. So this check no longer needs to be performed in the code.
PasswordGeneratorFlavor.allCases.map { $0.lengthLimits }.forEach {
XCTAssertLessThanOrEqual($0.min, $0.max)
}
}
}

View file

@ -0,0 +1,79 @@
//
// PasswordGeneratorTest.swift
// passKitTests
//
// Created by Danny Moesch on 26.02.20.
// Copyright © 2020 Bob Sun. All rights reserved.
//
import XCTest
@testable import passKit
class PasswordGeneratorTest: XCTestCase {
func testLimitedLength() {
[
PasswordGenerator(length: 15),
PasswordGenerator(length: -3),
PasswordGenerator(length: 128),
].forEach { generator in
XCTAssertLessThanOrEqual(generator.limitedLength, generator.flavor.lengthLimits.max)
XCTAssertGreaterThanOrEqual(generator.limitedLength, generator.flavor.lengthLimits.min)
}
}
func testAcceptableGroups() {
[
(15, 4),
(19, 4),
(9, 5),
(11, 6),
(259, 13),
].forEach { length, groups in
XCTAssertTrue(PasswordGenerator(length: length).isAcceptable(groups: groups))
}
}
func testNotAcceptableGroups() {
[
(15, 0),
(19, 20),
(9, 9),
(11, -1),
].forEach { length, groups in
XCTAssertFalse(PasswordGenerator(length: length).isAcceptable(groups: groups))
}
}
func testGroupsAreNotcceptableForXKCDStyle() {
var generator = PasswordGenerator(length: 15)
XCTAssertTrue(generator.isAcceptable(groups: 4))
generator.flavor = .xkcd
XCTAssertFalse(generator.isAcceptable(groups: 4))
}
func testRandomPasswordLength() {
[
PasswordGenerator(),
PasswordGenerator(groups: 1),
PasswordGenerator(length: 25),
PasswordGenerator(length: 47, groups: 12),
PasswordGenerator(useDigits: true),
].forEach { generator in
XCTAssertEqual(generator.generate().count, generator.length)
}
}
func testXKCDPasswordGeneration() {
let typicalPassword = PasswordGenerator(flavor: .xkcd).generate()
XCTAssertFalse(typicalPassword.isEmpty)
XCTAssertFalse(typicalPassword.trimmingCharacters(in: .letters).isEmpty)
let passwordWithoutSeparators = PasswordGenerator(flavor: .xkcd, useDigits: false, useSpecialSymbols: false).generate()
XCTAssertFalse(passwordWithoutSeparators.isEmpty)
XCTAssertTrue(passwordWithoutSeparators.trimmingCharacters(in: .letters).isEmpty)
}
}