move and rename test functions
This commit is contained in:
parent
a56193dc86
commit
db60b52605
1 changed files with 53 additions and 47 deletions
|
|
@ -31,34 +31,7 @@ final class PGPAgentTest: XCTestCase {
|
|||
super.tearDown()
|
||||
}
|
||||
|
||||
private 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 = decryptFromArmored
|
||||
return try pgpAgent.decrypt(encryptedData: encryptedData, keyID: encryptKeyID ?? keyID, requestPGPKeyPassphrase: requestPassphrase)
|
||||
}
|
||||
|
||||
func testMultiKeys() throws {
|
||||
try [
|
||||
RSA2048_RSA4096,
|
||||
ED25519_NISTP384,
|
||||
].forEach { testKeyInfo in
|
||||
keychain.removeAllContent()
|
||||
try importKeys(testKeyInfo.publicKeys, testKeyInfo.privateKeys)
|
||||
XCTAssert(pgpAgent.isPrepared)
|
||||
try pgpAgent.initKeys()
|
||||
try [
|
||||
(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// - MARK: Basic encrypt and decrypt tests
|
||||
|
||||
func testBasicEncryptDecrypt() throws {
|
||||
try [
|
||||
|
|
@ -87,25 +60,6 @@ final class PGPAgentTest: XCTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
func testMultiKeysSelectMatchingPrivateKeyToDecrypt() throws {
|
||||
keychain.removeAllContent()
|
||||
try importKeys(RSA2048_RSA4096.publicKeys, RSA2048_RSA4096.privateKeys)
|
||||
try pgpAgent.initKeys()
|
||||
try [
|
||||
(true, true),
|
||||
(true, false),
|
||||
(false, true),
|
||||
(false, false),
|
||||
].forEach { encryptInArmored, decryptFromArmored in
|
||||
passKit.Defaults.encryptInArmored = encryptInArmored
|
||||
let encryptedData = try pgpAgent.encrypt(plainData: testData, keyID: RSA2048.fingerprint)
|
||||
passKit.Defaults.encryptInArmored = decryptFromArmored
|
||||
// Note: not specifying the keyID to decrypt, so that the agent needs to find the matching private key by itself.
|
||||
let decryptedData = try pgpAgent.decrypt(encryptedData: encryptedData, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
|
||||
XCTAssertEqual(decryptedData, testData)
|
||||
}
|
||||
}
|
||||
|
||||
func testNoPrivateKey() throws {
|
||||
try KeyFileManager(keyType: PGPKey.PUBLIC, keyPath: "", keyHandler: keychain.add).importKey(from: RSA2048.publicKey)
|
||||
XCTAssertFalse(pgpAgent.isPrepared)
|
||||
|
|
@ -180,6 +134,49 @@ final class PGPAgentTest: XCTestCase {
|
|||
XCTAssertEqual(passphraseRequestCalledCount, 3)
|
||||
}
|
||||
|
||||
func testMultipleKeysLoaded() throws {
|
||||
try [
|
||||
RSA2048_RSA4096,
|
||||
ED25519_NISTP384,
|
||||
].forEach { testKeyInfo in
|
||||
keychain.removeAllContent()
|
||||
try importKeys(testKeyInfo.publicKeys, testKeyInfo.privateKeys)
|
||||
XCTAssert(pgpAgent.isPrepared)
|
||||
try pgpAgent.initKeys()
|
||||
try [
|
||||
(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testMultiKeysSelectMatchingPrivateKeyToDecrypt() throws {
|
||||
keychain.removeAllContent()
|
||||
try importKeys(RSA2048_RSA4096.publicKeys, RSA2048_RSA4096.privateKeys)
|
||||
try pgpAgent.initKeys()
|
||||
try [
|
||||
(true, true),
|
||||
(true, false),
|
||||
(false, true),
|
||||
(false, false),
|
||||
].forEach { encryptInArmored, decryptFromArmored in
|
||||
passKit.Defaults.encryptInArmored = encryptInArmored
|
||||
let encryptedData = try pgpAgent.encrypt(plainData: testData, keyID: RSA2048.fingerprint)
|
||||
passKit.Defaults.encryptInArmored = decryptFromArmored
|
||||
// Note: not specifying the keyID to decrypt, so that the agent needs to find the matching private key by itself.
|
||||
let decryptedData = try pgpAgent.decrypt(encryptedData: encryptedData, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
|
||||
XCTAssertEqual(decryptedData, testData)
|
||||
}
|
||||
}
|
||||
|
||||
// - MARK: Encrypt with multiple keys
|
||||
|
||||
func testEncryptWithAllKeys() throws {
|
||||
// When multiple keys are imported, the agent should be able to encrypt without specifying the keyID.
|
||||
// It should use all public keys for which we also have private keys, and the encrypted message should be able to be decrypted by any of the private keys.
|
||||
|
|
@ -209,8 +206,17 @@ final class PGPAgentTest: XCTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
// - MARK: Helpers
|
||||
|
||||
private func importKeys(_ publicKey: String, _ privateKey: String) throws {
|
||||
try KeyFileManager(keyType: PGPKey.PUBLIC, keyPath: "", keyHandler: keychain.add).importKey(from: publicKey)
|
||||
try KeyFileManager(keyType: PGPKey.PRIVATE, keyPath: "", keyHandler: keychain.add).importKey(from: privateKey)
|
||||
}
|
||||
|
||||
private 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 = decryptFromArmored
|
||||
return try pgpAgent.decrypt(encryptedData: encryptedData, keyID: encryptKeyID ?? keyID, requestPGPKeyPassphrase: requestPassphrase)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue