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() 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 passKit.Defaults.encryptInArmored = encryptInArmored
let encryptedData = try pgpAgent.encrypt(plainData: testData, keyID: keyID) 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) return try pgpAgent.decrypt(encryptedData: encryptedData, keyID: encryptKeyID ?? keyID, requestPGPKeyPassphrase: requestPassphrase)
} }
func testMultiKeys() throws { func testMultiKeys() throws {
try [ try [
RSA2048_RSA4096, RSA2048_RSA4096,
ED25519_NISTP384 ED25519_NISTP384,
].forEach { testKeyInfo in ].forEach { testKeyInfo in
let keychain = DictBasedKeychain() keychain.removeAllContent()
let pgpAgent = PGPAgent(keyStore: keychain) try importKeys(testKeyInfo.publicKeys, testKeyInfo.privateKeys)
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)
XCTAssert(pgpAgent.isPrepared) XCTAssert(pgpAgent.isPrepared)
try pgpAgent.initKeys() try pgpAgent.initKeys()
try [ try [
(true, true), (true, false), (false, true), (false, false) (true, true),
].forEach{ a, b in (true, false),
for id in testKeyInfo.fingerprint { (false, true),
XCTAssertEqual(try basicEncryptDecrypt(using: pgpAgent, keyID: id, encryptInArmored: a, encryptInArmoredNow: b), testData) (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, ED25519_SUB,
NISTP384, NISTP384,
].forEach { testKeyInfo in ].forEach { testKeyInfo in
let keychain = DictBasedKeychain() keychain.removeAllContent()
let pgpAgent = PGPAgent(keyStore: keychain) try importKeys(testKeyInfo.publicKey, testKeyInfo.privateKey)
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)
XCTAssert(pgpAgent.isPrepared) XCTAssert(pgpAgent.isPrepared)
try pgpAgent.initKeys() try pgpAgent.initKeys()
XCTAssert(try pgpAgent.getKeyID().first!.lowercased().hasSuffix(testKeyInfo.fingerprint)) XCTAssert(try pgpAgent.getKeyID().first!.lowercased().hasSuffix(testKeyInfo.fingerprint))
try [ try [
(true, true), (true, false), (false, true), (false, false) (true, true),
].forEach{ a, b in (true, false),
XCTAssertEqual(try basicEncryptDecrypt(using: pgpAgent, keyID: testKeyInfo.fingerprint, encryptInArmored: a, encryptInArmoredNow: b), testData) (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 var passphraseRequestCalledCount = 0
let provideCorrectPassphrase: (String) -> String = { _ in let provideCorrectPassphrase: (String) -> String = { _ in
passphraseRequestCalledCount = passphraseRequestCalledCount + 1 passphraseRequestCalledCount += 1
return requestPGPKeyPassphrase(keyID: RSA2048.fingerprint) return requestPGPKeyPassphrase(keyID: RSA2048.fingerprint)
} }
let provideIncorrectPassphrase: (String) -> String = { _ in let provideIncorrectPassphrase: (String) -> String = { _ in
passphraseRequestCalledCount = passphraseRequestCalledCount + 1 passphraseRequestCalledCount += 1
return "incorrect passphrase" return "incorrect passphrase"
} }

View file

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

View file

@ -12,14 +12,6 @@ import XCTest
class PasswordTableEntryTest: XCTestCase { 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() { func testExample() {
let nameWithCategoryList = [ let nameWithCategoryList = [
"github", "github",

View file

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