Merge pull request #292 from SimplyDanny/make-simulator-untouched-by-tests

Do not influence the Simulator by tests
This commit is contained in:
Yishi Lin 2019-07-21 13:39:53 +08:00 committed by GitHub
commit 032e442d78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 139 additions and 62 deletions

View file

@ -12,15 +12,21 @@ import KeychainAccess
import Gopenpgpwrapper
public class PGPAgent {
private let keyStore: KeyStore
public init(keyStore: KeyStore = AppKeychain.shared) {
self.keyStore = keyStore
}
public var pgpKeyID: String?
// PGP passphrase
public var passphrase: String? {
set {
AppKeychain.add(string: newValue, for: "pgpKeyPassphrase")
keyStore.add(string: newValue, for: "pgpKeyPassphrase")
}
get {
return AppKeychain.get(for: "pgpKeyPassphrase")
return keyStore.get(for: "pgpKeyPassphrase")
}
}
@ -68,12 +74,12 @@ public class PGPAgent {
}
// Read the key data from keychain.
guard let pgpKeyData: Data = AppKeychain.get(for: keyType.getKeychainKey()) else {
guard let pgpKeyData: Data = keyStore.get(for: keyType.getKeychainKey()) else {
throw AppError.KeyImport
}
// Remove the key data from keychain temporary, in case the following step crashes repeatedly.
AppKeychain.removeContent(for: keyType.getKeychainKey())
keyStore.removeContent(for: keyType.getKeychainKey())
// Try GopenpgpwrapperReadKey first.
if let key = GopenpgpwrapperReadKey(pgpKeyData) {
@ -83,7 +89,7 @@ public class PGPAgent {
case .PRIVATE:
self.privateKey = key
}
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
keyStore.add(data: pgpKeyData, for: keyType.getKeychainKey())
return
}
@ -98,7 +104,7 @@ public class PGPAgent {
case .PRIVATE:
self.privateKeyV2 = key
}
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
keyStore.add(data: pgpKeyData, for: keyType.getKeychainKey())
return
}
@ -107,19 +113,19 @@ public class PGPAgent {
public func initPGPKey(from url: URL, keyType: PgpKey) throws {
let pgpKeyData = try Data(contentsOf: url)
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
keyStore.add(data: pgpKeyData, for: keyType.getKeychainKey())
try initPGPKey(keyType)
}
public func initPGPKey(with armorKey: String, keyType: PgpKey) throws {
let pgpKeyData = armorKey.data(using: .ascii)!
AppKeychain.add(data: pgpKeyData, for: keyType.getKeychainKey())
keyStore.add(data: pgpKeyData, for: keyType.getKeychainKey())
try initPGPKey(keyType)
}
public func initPGPKeyFromFileSharing() throws {
try KeyFileManager.PublicPgp.importKeyAndDeleteFile()
try KeyFileManager.PrivatePgp.importKeyAndDeleteFile()
try KeyFileManager.PublicPgp.importKeyAndDeleteFile(keyHandler: keyStore.add)
try KeyFileManager.PrivatePgp.importKeyAndDeleteFile(keyHandler: keyStore.add)
try initPGPKeys()
}
@ -167,8 +173,8 @@ public class PGPAgent {
}
public func removePGPKeys() {
AppKeychain.removeContent(for: PgpKey.PUBLIC.getKeychainKey())
AppKeychain.removeContent(for: PgpKey.PRIVATE.getKeychainKey())
keyStore.removeContent(for: PgpKey.PUBLIC.getKeychainKey())
keyStore.removeContent(for: PgpKey.PRIVATE.getKeychainKey())
passphrase = nil
publicKey = nil
privateKey = nil

View file

@ -12,7 +12,7 @@ public class PasscodeLock {
private static let identifier = Globals.bundleIdentifier + "passcode"
/// Cached passcode to avoid frequent access to Keychain
private var passcode: String? = AppKeychain.get(for: PasscodeLock.identifier)
private var passcode: String? = AppKeychain.shared.get(for: PasscodeLock.identifier)
/// Constructor used to migrate passcode from SharedDefaults to Keychain
private init() {
@ -27,7 +27,7 @@ public class PasscodeLock {
}
public func save(passcode: String) {
AppKeychain.add(string: passcode, for: PasscodeLock.identifier)
AppKeychain.shared.add(string: passcode, for: PasscodeLock.identifier)
self.passcode = passcode
}
@ -36,7 +36,7 @@ public class PasscodeLock {
}
public func delete() {
AppKeychain.removeContent(for: PasscodeLock.identifier)
AppKeychain.shared.removeContent(for: PasscodeLock.identifier)
passcode = nil
}
}

View file

@ -39,19 +39,19 @@ public class PasswordStore {
public var gitPassword: String? {
set {
AppKeychain.add(string: newValue, for: "gitPassword")
AppKeychain.shared.add(string: newValue, for: "gitPassword")
}
get {
return AppKeychain.get(for: "gitPassword")
return AppKeychain.shared.get(for: "gitPassword")
}
}
public var gitSSHPrivateKeyPassphrase: String? {
set {
AppKeychain.add(string: newValue, for: "gitSSHPrivateKeyPassphrase")
AppKeychain.shared.add(string: newValue, for: "gitSSHPrivateKeyPassphrase")
}
get {
return AppKeychain.get(for: "gitSSHPrivateKeyPassphrase")
return AppKeychain.shared.get(for: "gitSSHPrivateKeyPassphrase")
}
}
@ -128,7 +128,7 @@ public class PasswordStore {
}
public func initGitSSHKey(with armorKey: String) throws {
AppKeychain.add(string: armorKey, for: SshKey.PRIVATE.getKeychainKey())
AppKeychain.shared.add(string: armorKey, for: SshKey.PRIVATE.getKeychainKey())
}
public func repositoryExisted() -> Bool {
@ -640,7 +640,7 @@ public class PasswordStore {
self.pgpAgent.removePGPKeys()
AppKeychain.removeAllContent()
AppKeychain.shared.removeAllContent()
deleteCoreData(entityName: "PasswordEntity")
@ -720,7 +720,7 @@ public class PasswordStore {
Defaults.remove(.gitSSHKeySource)
Defaults.remove(.gitSSHPrivateKeyArmor)
Defaults.remove(.gitSSHPrivateKeyURL)
AppKeychain.removeContent(for: SshKey.PRIVATE.getKeychainKey())
AppKeychain.shared.removeContent(for: SshKey.PRIVATE.getKeychainKey())
gitSSHPrivateKeyPassphrase = nil
}