2020-04-11 23:23:38 -07:00
|
|
|
//
|
|
|
|
|
// PasswordStoreTest.swift
|
|
|
|
|
// passKitTests
|
|
|
|
|
//
|
|
|
|
|
// Copyright © 2020 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import ObjectiveGit
|
2020-06-28 21:25:40 +02:00
|
|
|
import XCTest
|
2020-04-11 23:23:38 -07:00
|
|
|
|
|
|
|
|
@testable import passKit
|
|
|
|
|
|
|
|
|
|
class PasswordStoreTest: XCTestCase {
|
2020-06-28 21:25:40 +02:00
|
|
|
let cloneOptions: [String: GTCredentialProvider] = {
|
2020-07-05 23:32:43 +02:00
|
|
|
let credentialProvider = GTCredentialProvider { _, _, _ -> (GTCredential?) in
|
2020-06-28 21:25:40 +02:00
|
|
|
try? GTCredential(userName: "", password: "")
|
|
|
|
|
}
|
|
|
|
|
return [GTRepositoryCloneOptionsCredentialProvider: credentialProvider]
|
|
|
|
|
}()
|
2020-04-12 19:32:58 -07:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
let remoteRepoURL = URL(string: "https://github.com/mssun/passforios-password-store.git")!
|
2020-04-13 01:30:00 -07:00
|
|
|
|
|
|
|
|
func testCloneAndDecryptMultiKeys() throws {
|
2020-04-12 19:32:58 -07:00
|
|
|
let url = URL(fileURLWithPath: "\(Globals.repositoryPath)-test")
|
|
|
|
|
let passwordStore = PasswordStore(url: url)
|
2020-04-13 01:30:00 -07:00
|
|
|
let expectation = self.expectation(description: "clone")
|
2020-04-12 19:32:58 -07:00
|
|
|
try passwordStore.cloneRepository(
|
|
|
|
|
remoteRepoURL: remoteRepoURL,
|
|
|
|
|
options: cloneOptions,
|
|
|
|
|
branchName: "master",
|
|
|
|
|
transferProgressBlock: { _, _ in },
|
2020-07-05 00:34:34 +02:00
|
|
|
checkoutProgressBlock: { _, _, _ in },
|
|
|
|
|
completion: { expectation.fulfill() }
|
|
|
|
|
)
|
2020-04-13 01:30:00 -07:00
|
|
|
waitForExpectations(timeout: 3, handler: nil)
|
|
|
|
|
|
|
|
|
|
[
|
|
|
|
|
("work/github.com", "4712286271220DB299883EA7062E678DA1024DAE"),
|
2020-06-28 21:25:40 +02:00
|
|
|
("personal/github.com", "787EAE1A5FA3E749AA34CC6AA0645EBED862027E"),
|
|
|
|
|
].forEach { path, id in
|
2020-04-13 01:30:00 -07:00
|
|
|
let keyID = findGPGID(from: url.appendingPathComponent(path))
|
|
|
|
|
XCTAssertEqual(keyID, id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let keychain = AppKeychain.shared
|
2020-04-19 15:43:13 +02:00
|
|
|
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)
|
2020-04-13 01:30:00 -07:00
|
|
|
try PGPAgent.shared.initKeys()
|
|
|
|
|
|
|
|
|
|
let personal = try decrypt(passwordStore: passwordStore, path: "personal/github.com.gpg", passphrase: "passforios")
|
|
|
|
|
XCTAssertEqual(personal.plainText, "passwordforpersonal\n")
|
|
|
|
|
|
|
|
|
|
let work = try decrypt(passwordStore: passwordStore, path: "work/github.com.gpg", passphrase: "passforios")
|
|
|
|
|
XCTAssertEqual(work.plainText, "passwordforwork\n")
|
|
|
|
|
|
2020-04-13 10:25:01 -07:00
|
|
|
let testPassword = Password(name: "test", url: URL(string: "test.gpg")!, plainText: "testpassword")
|
|
|
|
|
let testPasswordEntity = try passwordStore.add(password: testPassword)!
|
2020-06-28 21:25:40 +02:00
|
|
|
let testPasswordPlain = try passwordStore.decrypt(passwordEntity: testPasswordEntity, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
|
2020-04-13 10:25:01 -07:00
|
|
|
XCTAssertEqual(testPasswordPlain.plainText, "testpassword")
|
|
|
|
|
|
2020-04-12 19:32:58 -07:00
|
|
|
passwordStore.erase()
|
|
|
|
|
}
|
2020-04-13 01:30:00 -07:00
|
|
|
|
2020-06-28 21:25:40 +02:00
|
|
|
private func decrypt(passwordStore: PasswordStore, path: String, passphrase _: String) throws -> Password {
|
2020-04-13 01:30:00 -07:00
|
|
|
let entity = passwordStore.getPasswordEntity(by: path, isDir: false)!
|
2020-06-28 21:25:40 +02:00
|
|
|
return try passwordStore.decrypt(passwordEntity: entity, requestPGPKeyPassphrase: requestPGPKeyPassphrase)
|
2020-04-13 01:30:00 -07:00
|
|
|
}
|
2020-04-11 23:23:38 -07:00
|
|
|
}
|