2019-10-18 11:45:19 -07:00
|
|
|
//
|
|
|
|
// Tests+Utils.swift
|
|
|
|
// ZcashLightClientKitTests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 18/09/2019.
|
|
|
|
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2020-04-09 15:25:43 -07:00
|
|
|
import GRPC
|
2019-11-26 14:32:20 -08:00
|
|
|
import ZcashLightClientKit
|
2019-10-18 11:45:19 -07:00
|
|
|
import XCTest
|
2020-04-09 15:25:43 -07:00
|
|
|
import NIO
|
2019-11-26 14:32:20 -08:00
|
|
|
class LightWalletEndpointBuilder {
|
|
|
|
static var `default`: LightWalletEndpoint {
|
2020-04-09 15:25:43 -07:00
|
|
|
LightWalletEndpoint(address: Constants.address, port: 9067, secure: false)
|
2019-11-26 14:32:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
class ChannelProvider {
|
2020-04-09 15:25:43 -07:00
|
|
|
func channel(secure: Bool = false) -> GRPCChannel {
|
|
|
|
let endpoint = LightWalletEndpointBuilder.default
|
|
|
|
|
|
|
|
let configuration = ClientConnection.Configuration(target: .hostAndPort(endpoint.host, endpoint.port), eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1), tls: secure ? .init() : nil)
|
|
|
|
return ClientConnection(configuration: configuration)
|
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MockDbInit {
|
|
|
|
@discardableResult static func emptyFile(at path: String) -> Bool {
|
|
|
|
|
|
|
|
FileManager.default.createFile(atPath: path, contents: Data("".utf8), attributes: nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static func destroy(at path: String) throws {
|
|
|
|
try FileManager.default.removeItem(atPath: path)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension XCTestExpectation {
|
2019-10-30 13:18:57 -07:00
|
|
|
func subscribe(to notification: Notification.Name, object: Any?) {
|
2019-10-18 11:45:19 -07:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(fulfill), name: notification, object: object)
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:18:57 -07:00
|
|
|
func unsubscribe(from notification: Notification.Name) {
|
2019-10-18 11:45:19 -07:00
|
|
|
NotificationCenter.default.removeObserver(self, name: notification, object: nil)
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:18:57 -07:00
|
|
|
func unsubscribeFromNotifications() {
|
2019-10-18 11:45:19 -07:00
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func __documentsDirectory() throws -> URL {
|
|
|
|
try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func __cacheDbURL() throws -> URL {
|
|
|
|
try __documentsDirectory().appendingPathComponent("cache.db", isDirectory: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func __dataDbURL() throws -> URL {
|
|
|
|
try __documentsDirectory().appendingPathComponent("data.db", isDirectory: false)
|
|
|
|
}
|
|
|
|
|
2019-11-26 14:32:20 -08:00
|
|
|
func __spendParamsURL() throws -> URL {
|
2019-12-06 04:38:47 -08:00
|
|
|
URL(string: Bundle.testBundle.url(forResource: "sapling-spend", withExtension: "params")!.path)!
|
2019-11-26 14:32:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func __outputParamsURL() throws -> URL {
|
2019-12-06 04:38:47 -08:00
|
|
|
URL(string: Bundle.testBundle.url(forResource: "sapling-output", withExtension: "params")!.path)!
|
2019-11-26 14:32:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func copyParametersToDocuments() throws -> (spend: URL, output: URL) {
|
|
|
|
|
|
|
|
let spendURL = try __documentsDirectory().appendingPathComponent("sapling-spend.params", isDirectory: false)
|
|
|
|
let outputURL = try __documentsDirectory().appendingPathComponent("sapling-output.params", isDirectory: false)
|
|
|
|
try FileManager.default.copyItem(at: try __spendParamsURL(), to: spendURL)
|
|
|
|
try FileManager.default.copyItem(at: try __outputParamsURL(), to: outputURL)
|
|
|
|
|
|
|
|
return (spendURL, outputURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteParametersFromDocuments() throws {
|
|
|
|
let documents = try __documentsDirectory()
|
|
|
|
deleteParamsFrom(spend: documents.appendingPathComponent("sapling-spend.params"), output: documents.appendingPathComponent("sapling-output.params"))
|
|
|
|
}
|
|
|
|
func deleteParamsFrom(spend: URL, output: URL) {
|
|
|
|
try? FileManager.default.removeItem(at: spend)
|
|
|
|
try? FileManager.default.removeItem(at: output)
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
func parametersReady() -> Bool {
|
|
|
|
|
2019-11-26 14:32:20 -08:00
|
|
|
guard let output = try? __outputParamsURL(),
|
|
|
|
let spend = try? __spendParamsURL(),
|
2019-10-18 11:45:19 -07:00
|
|
|
FileManager.default.isReadableFile(atPath: output.absoluteString),
|
|
|
|
FileManager.default.isReadableFile(atPath: spend.absoluteString) else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-11-26 14:32:20 -08:00
|
|
|
|
|
|
|
class StubTest: XCTestCase {}
|
|
|
|
extension Bundle {
|
|
|
|
static var testBundle: Bundle {
|
|
|
|
Bundle(for: StubTest.self)
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 10:11:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
class TestSeed: SeedProvider {
|
|
|
|
|
|
|
|
/**
|
|
|
|
test account: "still champion voice habit trend flight survey between bitter process artefact blind carbon truly provide dizzy crush flush breeze blouse charge solid fish spread"
|
|
|
|
*/
|
|
|
|
let seedString = "f550d5399659396587a59b6ad446eb89da7741ebb1e42f87c22451d20ece8bb1e09ccb3c19f967f37fbf435367bc295c692c0ce000c52f5b991f1ca91169565e"
|
|
|
|
|
|
|
|
func seed() -> [UInt8] {
|
|
|
|
[UInt8](seedString.hexDecodedData())
|
|
|
|
}
|
|
|
|
}
|