2019-06-29 23:09:24 +02:00
|
|
|
//
|
|
|
|
|
// KeyFileManagerTest.swift
|
|
|
|
|
// passKitTests
|
|
|
|
|
//
|
|
|
|
|
// Created by Danny Moesch on 01.07.19.
|
|
|
|
|
// Copyright © 2019 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
|
|
|
|
|
|
@testable import passKit
|
|
|
|
|
|
|
|
|
|
class KeyFileManagerTest: XCTestCase {
|
|
|
|
|
private static let filePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("test.txt").path
|
2019-09-08 23:00:46 +02:00
|
|
|
private static let keyFileManager = KeyFileManager(keyType: PgpKey.PUBLIC, keyPath: filePath) { _, _ in }
|
2019-06-29 23:09:24 +02:00
|
|
|
|
|
|
|
|
override func tearDown() {
|
2021-10-03 05:46:07 +02:00
|
|
|
try? FileManager.default.removeItem(atPath: Self.filePath)
|
2019-06-29 23:09:24 +02:00
|
|
|
super.tearDown()
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-08 23:00:46 +02:00
|
|
|
func testImportKeyFromFileSharing() throws {
|
2019-06-29 23:09:24 +02:00
|
|
|
let fileContent = "content".data(using: .ascii)
|
2019-09-08 23:00:46 +02:00
|
|
|
var storage: [String: String] = [:]
|
2021-10-03 05:46:07 +02:00
|
|
|
let keyFileManager = KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: Self.filePath) { storage[$1] = $0 }
|
2019-06-29 23:09:24 +02:00
|
|
|
|
2021-10-03 05:46:07 +02:00
|
|
|
FileManager.default.createFile(atPath: Self.filePath, contents: fileContent, attributes: nil)
|
2019-09-08 23:00:46 +02:00
|
|
|
try keyFileManager.importKeyFromFileSharing()
|
2019-06-29 23:09:24 +02:00
|
|
|
|
2021-10-03 05:46:07 +02:00
|
|
|
XCTAssertFalse(FileManager.default.fileExists(atPath: Self.filePath))
|
2019-09-08 23:00:46 +02:00
|
|
|
XCTAssertEqual(storage[PgpKey.PRIVATE.getKeychainKey()], "content")
|
2019-06-29 23:09:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testErrorReadingFile() throws {
|
2021-10-03 05:46:07 +02:00
|
|
|
XCTAssertThrowsError(try Self.keyFileManager.importKeyFromFileSharing())
|
2019-09-08 23:00:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testImportKeyFromUrl() throws {
|
|
|
|
|
let fileContent = "content".data(using: .ascii)
|
2021-10-03 05:46:07 +02:00
|
|
|
let url = URL(fileURLWithPath: Self.filePath)
|
2019-09-08 23:00:46 +02:00
|
|
|
var storage: [String: String] = [:]
|
2021-10-03 05:46:07 +02:00
|
|
|
let keyFileManager = KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: Self.filePath) { storage[$1] = $0 }
|
2019-09-08 23:00:46 +02:00
|
|
|
|
2021-10-03 05:46:07 +02:00
|
|
|
FileManager.default.createFile(atPath: Self.filePath, contents: fileContent, attributes: nil)
|
2019-09-08 23:00:46 +02:00
|
|
|
try keyFileManager.importKey(from: url)
|
|
|
|
|
|
|
|
|
|
XCTAssertEqual(storage[PgpKey.PRIVATE.getKeychainKey()], "content")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testImportKeyFromString() throws {
|
|
|
|
|
let string = "content"
|
|
|
|
|
var storage: [String: String] = [:]
|
2021-10-03 05:46:07 +02:00
|
|
|
let keyFileManager = KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: Self.filePath) { storage[$1] = $0 }
|
2019-09-08 23:00:46 +02:00
|
|
|
|
|
|
|
|
try keyFileManager.importKey(from: string)
|
|
|
|
|
|
|
|
|
|
XCTAssertEqual(storage[PgpKey.PRIVATE.getKeychainKey()], string)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testImportKeyFromNonAsciiString() throws {
|
2021-10-03 05:46:07 +02:00
|
|
|
XCTAssertThrowsError(try Self.keyFileManager.importKey(from: "≠")) {
|
2020-09-20 15:07:18 +02:00
|
|
|
XCTAssertEqual($0 as! AppError, AppError.encoding)
|
2019-06-29 23:09:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testFileExists() {
|
2021-10-03 05:46:07 +02:00
|
|
|
FileManager.default.createFile(atPath: Self.filePath, contents: nil, attributes: nil)
|
2019-06-29 23:09:24 +02:00
|
|
|
|
2021-10-03 05:46:07 +02:00
|
|
|
XCTAssertTrue(Self.keyFileManager.doesKeyFileExist())
|
2019-06-29 23:09:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testFileDoesNotExist() {
|
2021-10-03 05:46:07 +02:00
|
|
|
XCTAssertFalse(Self.keyFileManager.doesKeyFileExist())
|
2019-06-29 23:09:24 +02:00
|
|
|
}
|
|
|
|
|
}
|