Memo tests (#111)

* test scaffold

* Memo Tests

* emoji memo test
This commit is contained in:
Francisco Gindre 2020-04-24 16:50:23 -03:00 committed by GitHub
parent 462ce0f5d7
commit 36f2d0730a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 160 additions and 46 deletions

View File

@ -14,29 +14,5 @@ public extension Data {
func toHexStringTxId() -> String {
self.hexEncodedString().toTxIdString()
}
}
extension String {
func toTxIdString() -> String {
var id = ""
self.reversed().pairs.map {
$0.reversed()
}.forEach { (reversed) in
id.append(String(reversed))
}
return id
}
}
extension Collection {
var pairs: [SubSequence] {
var startIndex = self.startIndex
let count = self.count
let n = count / 2 + count % 2
return (0..<n).map { _ in
let endIndex = index(startIndex, offsetBy: 2, limitedBy: self.endIndex) ?? self.endIndex
defer { startIndex = endIndex }
return self[startIndex..<endIndex]
}
}
}

View File

@ -0,0 +1,33 @@
//
// Data+internal.swift
// ZcashLightClientKit
//
// Created by Francisco Gindre on 4/23/20.
//
import Foundation
extension String {
func toTxIdString() -> String {
var id = ""
self.reversed().pairs.map {
$0.reversed()
}.forEach { (reversed) in
id.append(String(reversed))
}
return id
}
}
extension Collection {
var pairs: [SubSequence] {
var startIndex = self.startIndex
let count = self.count
let n = count / 2 + count % 2
return (0..<n).map { _ in
let endIndex = index(startIndex, offsetBy: 2, limitedBy: self.endIndex) ?? self.endIndex
defer { startIndex = endIndex }
return self[startIndex..<endIndex]
}
}
}

View File

@ -0,0 +1,34 @@
//
// ZcashRust+Utils.swift
// ZcashLightClientKit
//
// Created by Francisco Gindre on 12/09/2019.
// Copyright © 2019 Electric Coin Company. All rights reserved.
//
import Foundation
/**
Attempts to show the data as a Zcash Transaction Memo
*/
public extension Data {
func asZcashTransactionMemo() -> String? {
self.withUnsafeBytes { (rawPointer) -> String? in
let unsafeBufferPointer = rawPointer.bindMemory(to: CChar.self)
if let unsafePointer = unsafeBufferPointer.baseAddress, let utf8Memo = String(validatingUTF8: unsafePointer) {
return utf8Memo.isEmpty ? nil : utf8Memo
} else {
return nil
}
}
}
}
/**
Attempts to convert this string to a Zcash Transaction Memo data
*/
public extension String {
func encodeAsZcashTransactionMemo() -> Data? {
return self.data(using: .utf8)
}
}

View File

@ -1,21 +0,0 @@
//
// ZcashRust+Utils.swift
// ZcashLightClientKit
//
// Created by Francisco Gindre on 12/09/2019.
// Copyright © 2019 Electric Coin Company. All rights reserved.
//
import Foundation
extension Data {
func asZcashTransactionMemo() -> String? {
return String(bytes: self, encoding: .utf8)
}
}
extension String {
func encodeAsZcashTransactionMemo() -> Data? {
return self.data(using: .utf8)
}
}

View File

@ -0,0 +1,92 @@
//
// MemoTests.swift
// ZcashLightClientKit-Unit-Tests
//
// Created by Francisco Gindre on 4/23/20.
//
import XCTest
@testable import ZcashLightClientKit
class MemoTests: XCTestCase {
/**
Non-utf8 memos are properly ignored
*/
func testNonUnicodeMemos() throws {
XCTAssertNil(Self.randomMemoData()!.asZcashTransactionMemo())
}
/**
Memo length is correct, padding characters are ignored
*/
func testMemoLength() throws {
XCTAssertEqual(validMemoData.count, 512)
XCTAssertEqual(validMemoData.asZcashTransactionMemo()!.count, Self.validMemoDataExpectedString.count)
}
/**
Verify support for common unicode characters
*/
func testUnicodeCharacters() throws {
let memo = validMemoData.asZcashTransactionMemo()
XCTAssertNotNil(memo)
XCTAssertEqual(memo!, Self.validMemoDataExpectedString)
}
func testEmojiUnicodeCharacters() throws {
let memo = Self.emojiMemoData.asZcashTransactionMemo()
XCTAssertNotNil(memo)
XCTAssertEqual(memo!, Self.expectedEmojiMemoString)
}
/**
Blank memos are ignored
*/
func testBlankMemos() throws {
// This is an example of a functional test case.
XCTAssertNil(emptyMemoData.asZcashTransactionMemo())
}
/**
*******
* mocked memos
* ******
*/
/**
Real text: "Here's gift from the Zec Fairy @ ECC!"
*/
static let validMemoDataExpectedString = "Here's gift from the Zec Fairy @ ECC!"
static let validMemoDataBase64 = "SGVyZSdzIGdpZnQgZnJvbSB0aGUgWmVjIEZhaXJ5IEAgRUNDIQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
let validMemoData = Data(base64Encoded: validMemoDataBase64)!
let emptyMemoData = Data([UInt8](repeating: 0, count: 512))
let totallyRandomDataMemo = randomMemoData()!
static let emojiDataBase64 = "8J+SlfCfkpXwn5KV8J+mk/CfppPwn6aT8J+bofCfm6Hwn5uhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
static let emojiMemoData = Data(base64Encoded: emojiDataBase64)!
static let expectedEmojiMemoString = "💕💕💕🦓🦓🦓🛡🛡🛡"
static func randomMemoData() -> Data? {
let length: Int = 512
var keyData = Data(count: length)
let result = keyData.withUnsafeMutableBytes {
SecRandomCopyBytes(kSecRandomDefault, length, $0.baseAddress!)
}
if result == errSecSuccess {
return keyData
} else {
print("Problem generating random bytes")
return nil
}
}
}

View File

@ -42,7 +42,7 @@ class TransactionEnhancementTests: XCTestCase {
try? FileManager.default.removeItem(at: processorConfig.cacheDb)
try? FileManager.default.removeItem(at: processorConfig.dataDb)
rustBackend.initAccountsTable(dbData: processorConfig.dataDb, seed: <#T##[UInt8]#>, accounts: <#T##Int32#>)
_ = rustBackend.initAccountsTable(dbData: processorConfig.dataDb, seed: TestSeed().seed(), accounts: 1)
let service = DarksideWalletService()
darksideWalletService = service
let storage = CompactBlockStorage.init(connectionProvider: SimpleConnectionProvider(path: processorConfig.cacheDb.absoluteString))