Refactor and rename in test code

This commit is contained in:
Danny Moesch 2020-04-19 15:43:13 +02:00 committed by Mingshen Sun
parent f643a63fe1
commit e2e3275293
4 changed files with 61 additions and 70 deletions

View file

@ -31,29 +31,30 @@ class PGPAgentTest: XCTestCase {
super.tearDown()
}
func basicEncryptDecrypt(using pgpAgent: PGPAgent, keyID: String, encryptKeyID: String? = nil, requestPassphrase: @escaping (String) -> String = requestPGPKeyPassphrase, encryptInArmored: Bool = true, encryptInArmoredNow: Bool = true) throws -> Data? {
func basicEncryptDecrypt(using pgpAgent: PGPAgent, keyID: String, encryptKeyID: String? = nil, requestPassphrase: @escaping (String) -> String = requestPGPKeyPassphrase, encryptInArmored: Bool = true, decryptFromArmored: Bool = true) throws -> Data? {
passKit.Defaults.encryptInArmored = encryptInArmored
let encryptedData = try pgpAgent.encrypt(plainData: testData, keyID: keyID)
passKit.Defaults.encryptInArmored = encryptInArmoredNow
passKit.Defaults.encryptInArmored = decryptFromArmored
return try pgpAgent.decrypt(encryptedData: encryptedData, keyID: encryptKeyID ?? keyID, requestPGPKeyPassphrase: requestPassphrase)
}
func testMultiKeys() throws {
try [
RSA2048_RSA4096,
ED25519_NISTP384
ED25519_NISTP384,
].forEach { testKeyInfo in
let keychain = DictBasedKeychain()
let pgpAgent = PGPAgent(keyStore: keychain)
try KeyFileManager(keyType: PgpKey.PUBLIC, keyPath: "", keyHandler: keychain.add).importKey(from: testKeyInfo.publicKey)
try KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: "", keyHandler: keychain.add).importKey(from: testKeyInfo.privateKey)
keychain.removeAllContent()
try importKeys(testKeyInfo.publicKeys, testKeyInfo.privateKeys)
XCTAssert(pgpAgent.isPrepared)
try pgpAgent.initKeys()
try [
(true, true), (true, false), (false, true), (false, false)
].forEach{ a, b in
for id in testKeyInfo.fingerprint {
XCTAssertEqual(try basicEncryptDecrypt(using: pgpAgent, keyID: id, encryptInArmored: a, encryptInArmoredNow: b), testData)
(true, true),
(true, false),
(false, true),
(false, false),
].forEach { encryptInArmored, decryptFromArmored in
for id in testKeyInfo.fingerprints {
XCTAssertEqual(try basicEncryptDecrypt(using: pgpAgent, keyID: id, encryptInArmored: encryptInArmored, decryptFromArmored: decryptFromArmored), testData)
}
}
}
@ -69,17 +70,18 @@ class PGPAgentTest: XCTestCase {
ED25519_SUB,
NISTP384,
].forEach { testKeyInfo in
let keychain = DictBasedKeychain()
let pgpAgent = PGPAgent(keyStore: keychain)
try KeyFileManager(keyType: PgpKey.PUBLIC, keyPath: "", keyHandler: keychain.add).importKey(from: testKeyInfo.publicKey)
try KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: "", keyHandler: keychain.add).importKey(from: testKeyInfo.privateKey)
keychain.removeAllContent()
try importKeys(testKeyInfo.publicKey, testKeyInfo.privateKey)
XCTAssert(pgpAgent.isPrepared)
try pgpAgent.initKeys()
XCTAssert(try pgpAgent.getKeyID().first!.lowercased().hasSuffix(testKeyInfo.fingerprint))
try [
(true, true), (true, false), (false, true), (false, false)
].forEach{ a, b in
XCTAssertEqual(try basicEncryptDecrypt(using: pgpAgent, keyID: testKeyInfo.fingerprint, encryptInArmored: a, encryptInArmoredNow: b), testData)
(true, true),
(true, false),
(false, true),
(false, false),
].forEach { encryptInArmored, decryptFromArmored in
XCTAssertEqual(try basicEncryptDecrypt(using: pgpAgent, keyID: testKeyInfo.fingerprint, encryptInArmored: encryptInArmored, decryptFromArmored: decryptFromArmored), testData)
}
}
}
@ -135,11 +137,11 @@ class PGPAgentTest: XCTestCase {
var passphraseRequestCalledCount = 0
let provideCorrectPassphrase: (String) -> String = { _ in
passphraseRequestCalledCount = passphraseRequestCalledCount + 1
passphraseRequestCalledCount += 1
return requestPGPKeyPassphrase(keyID: RSA2048.fingerprint)
}
let provideIncorrectPassphrase: (String) -> String = { _ in
passphraseRequestCalledCount = passphraseRequestCalledCount + 1
passphraseRequestCalledCount += 1
return "incorrect passphrase"
}

View file

@ -45,8 +45,8 @@ class PasswordStoreTest: XCTestCase {
}
let keychain = AppKeychain.shared
try KeyFileManager(keyType: PgpKey.PUBLIC, keyPath: "", keyHandler: keychain.add).importKey(from: RSA2048_RSA4096.publicKey)
try KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: "", keyHandler: keychain.add).importKey(from: RSA2048_RSA4096.privateKey)
try KeyFileManager(keyType: PgpKey.PUBLIC, keyPath: "", keyHandler: keychain.add).importKey(from: RSA2048_RSA4096.publicKeys)
try KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: "", keyHandler: keychain.add).importKey(from: RSA2048_RSA4096.privateKeys)
try PGPAgent.shared.initKeys()
let personal = try decrypt(passwordStore: passwordStore, path: "personal/github.com.gpg", passphrase: "passforios")

View file

@ -12,14 +12,6 @@ import XCTest
class PasswordTableEntryTest: XCTestCase {
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() {
let nameWithCategoryList = [
"github",

View file

@ -10,97 +10,94 @@ import XCTest
@testable import passKit
struct PGPTestKey {
struct PGPTestSet {
fileprivate static var ALL_TEST_SETS: [String: PGPTestSet] = [:]
let publicKey: String
let privateKey: String
let fingerprint: String
let passphrase: String
fileprivate func collect() -> Self {
Self.ALL_TEST_SETS[fingerprint] = self
return self
}
}
struct MultiPGPKeyTestTriple {
let publicKey: String
let privateKey: String
let fingerprint: [String]
let passphrase: [String]
struct MultiKeyPGPTestSet {
let publicKeys: String
let privateKeys: String
let fingerprints: [String]
let passphrases: [String]
}
let RSA2048 = PGPTestKey(
let RSA2048 = PGPTestSet(
publicKey: PGP_RSA2048_PUBLIC_KEY,
privateKey: PGP_RSA2048_PRIVATE_KEY,
fingerprint: "a1024dae",
passphrase: "passforios"
)
).collect()
let RSA2048_SUB = PGPTestKey(
let RSA2048_SUB = PGPTestSet(
publicKey: PGP_RSA2048_PUBLIC_KEY,
privateKey: PGP_RSA2048_PRIVATE_SUBKEY,
fingerprint: "a1024dae",
passphrase: "passforios"
)
let RSA4096 = PGPTestKey(
let RSA4096 = PGPTestSet(
publicKey: PGP_RSA4096_PUBLIC_KEY,
privateKey: PGP_RSA4096_PRIVATE_KEY,
fingerprint: "d862027e",
passphrase: "passforios"
)
).collect()
let RSA4096_SUB = PGPTestKey(
let RSA4096_SUB = PGPTestSet(
publicKey: PGP_RSA4096_PUBLIC_KEY,
privateKey: PGP_RSA4096_PRIVATE_SUBKEY,
fingerprint: "d862027e",
passphrase: "passforios"
)
let ED25519 = PGPTestKey(
let ED25519 = PGPTestSet(
publicKey: PGP_ED25519_PUBLIC_KEY,
privateKey: PGP_ED25519_PRIVATE_KEY,
fingerprint: "e9444483",
passphrase: "passforios"
)
).collect()
let ED25519_SUB = PGPTestKey(
let ED25519_SUB = PGPTestSet(
publicKey: PGP_ED25519_PUBLIC_KEY,
privateKey: PGP_ED25519_PRIVATE_SUBKEY,
fingerprint: "e9444483",
passphrase: "passforios"
)
let NISTP384 = PGPTestKey(
let NISTP384 = PGPTestSet(
publicKey: PGP_NISTP384_PUBLIC_KEY,
privateKey: PGP_NISTP384_PRIVATE_KEY,
fingerprint: "5af3c085",
passphrase: "soirofssap"
).collect()
let RSA2048_RSA4096 = MultiKeyPGPTestSet(
publicKeys: PGP_RSA2048_PUBLIC_KEY | PGP_RSA4096_PUBLIC_KEY,
privateKeys: PGP_RSA2048_PRIVATE_KEY | PGP_RSA4096_PRIVATE_KEY,
fingerprints: ["a1024dae", "d862027e"],
passphrases: ["passforios", "passforios"]
)
let TEST_KEYS: [String: PGPTestKey] = {
var keys: [String: PGPTestKey] = [:]
keys["a1024dae"] = RSA2048
keys["d862027e"] = RSA4096
keys["e9444483"] = ED25519
keys["5af3c085"] = NISTP384
return keys
}();
let RSA2048_RSA4096 = MultiPGPKeyTestTriple(
publicKey: PGP_RSA2048_PUBLIC_KEY + "\n" + PGP_RSA4096_PUBLIC_KEY,
privateKey: PGP_RSA2048_PRIVATE_KEY + "\n" + PGP_RSA4096_PRIVATE_KEY,
fingerprint: ["a1024dae", "d862027e"],
passphrase: ["passforios", "passforios"]
)
let ED25519_NISTP384 = MultiPGPKeyTestTriple(
publicKey: PGP_ED25519_PUBLIC_KEY + "\n" + PGP_NISTP384_PUBLIC_KEY,
privateKey: PGP_ED25519_PRIVATE_KEY + "\n" + PGP_NISTP384_PRIVATE_KEY,
fingerprint: ["e9444483", "5af3c085"],
passphrase: ["passforios", "soirofssap"]
let ED25519_NISTP384 = MultiKeyPGPTestSet(
publicKeys: PGP_ED25519_PUBLIC_KEY | PGP_NISTP384_PUBLIC_KEY,
privateKeys: PGP_ED25519_PRIVATE_KEY | PGP_NISTP384_PRIVATE_KEY,
fingerprints: ["e9444483", "5af3c085"],
passphrases: ["passforios", "soirofssap"]
)
func requestPGPKeyPassphrase(keyID: String) -> String {
let id = keyID.suffix(8).lowercased()
return TEST_KEYS[id]?.passphrase ?? "passforios"
return PGPTestSet.ALL_TEST_SETS[id]?.passphrase ?? "passforios"
}
let PGP_RSA2048_PUBLIC_KEY = """