2020-06-08 13:07:48 -07:00
|
|
|
//
|
|
|
|
// NullBytesTests.swift
|
|
|
|
// ZcashLightClientKit-Unit-Tests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 6/5/20.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
2022-02-28 09:03:20 -08:00
|
|
|
@testable import TestUtils
|
2020-06-08 13:07:48 -07:00
|
|
|
@testable import ZcashLightClientKit
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2020-06-08 13:07:48 -07:00
|
|
|
class NullBytesTests: XCTestCase {
|
2021-07-28 09:59:10 -07:00
|
|
|
let networkType = NetworkType.mainnet
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2020-06-08 13:07:48 -07:00
|
|
|
func testZaddrNullBytes() throws {
|
2021-09-23 06:26:41 -07:00
|
|
|
// this is a valid zAddr. if you send ZEC to it, you will be contributing to Human Rights Foundation. see more ways to help at https://paywithz.cash/
|
|
|
|
let validZaddr = "zs1gqtfu59z20s9t20mxlxj86zpw6p69l0ev98uxrmlykf2nchj2dw8ny5e0l22kwmld2afc37gkfp"
|
|
|
|
let zAddrWithNullBytes = "\(validZaddr)\0something else that makes the address invalid"
|
2020-06-08 13:07:48 -07:00
|
|
|
|
2022-10-07 07:36:21 -07:00
|
|
|
XCTAssertFalse(ZcashRustBackend.isValidSaplingAddress(zAddrWithNullBytes, networkType: networkType))
|
2020-06-08 13:07:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func testTaddrNullBytes() throws {
|
2021-09-23 06:26:41 -07:00
|
|
|
// this is a valid tAddr. if you send ZEC to it, you will be contributing to Human Rights Foundation. see more ways to help at https://paywithz.cash/
|
|
|
|
let validTAddr = "t1J5pTRzJi7j8Xw9VJTrPxPEkaigr69gKVT"
|
|
|
|
let tAddrWithNullBytes = "\(validTAddr)\0fasdfasdf"
|
|
|
|
|
2022-10-07 07:36:21 -07:00
|
|
|
XCTAssertFalse(ZcashRustBackend.isValidTransparentAddress(tAddrWithNullBytes, networkType: networkType))
|
2020-06-08 13:07:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func testInitAccountTableNullBytes() throws {
|
|
|
|
let wrongHash = "000000000\015c597fab53f\058b9e1ededbe8bd83ca0203788e2039eceeb0d65ca6"
|
|
|
|
let goodHash = "00000000015c597fab53f58b9e1ededbe8bd83ca0203788e2039eceeb0d65ca6"
|
|
|
|
let time: UInt32 = 1582235356
|
|
|
|
let height: Int32 = 735000
|
2021-09-23 06:26:41 -07:00
|
|
|
|
|
|
|
// swiftlint:disable:next line_length
|
2020-06-08 13:07:48 -07:00
|
|
|
let wrongTree = "0161f2ff97ff6ac6a90f9bce76c11710460f4944d8695aecc7dc99e34cad0131040011015325b185e23e82562db27817be996ffade9597181244f67efc40561aeb9dde1101daeffadc9e38f755bcb55a847a1278518a0ba4a2ef33b2fe01bbb3eb242ab0070000000000011c51f9077e3f7e28e8e337eaf4bb99b41acbc853a37dcc1e172467a1c919fe4100010bb1f55481b2268ef31997dc0fb6b48a530bc17870220f156d832326c433eb0a010b3768d3bf7868a67823e022f49be67982d0588e7041c498a756024\0750065a4a0001a9e1bf4bccb48b14b544e770f21d48f2d3ad8d6ca54eccc92f60634e3078eb48013a1f7fb005388ac6f04099b647ed85d8b025d8ae4b178c2376b473b121b8c052000001d2ea556f49fb934dc76f087935a5c07788000b4e3aae24883adfec51b5f4d260"
|
2021-09-23 06:26:41 -07:00
|
|
|
|
|
|
|
// swiftlint:disable:next line_length
|
2020-06-08 13:07:48 -07:00
|
|
|
let goodTree = "0161f2ff97ff6ac6a90f9bce76c11710460f4944d8695aecc7dc99e34cad0131040011015325b185e23e82562db27817be996ffade9597181244f67efc40561aeb9dde1101daeffadc9e38f755bcb55a847a1278518a0ba4a2ef33b2fe01bbb3eb242ab0070000000000011c51f9077e3f7e28e8e337eaf4bb99b41acbc853a37dcc1e172467a1c919fe4100010bb1f55481b2268ef31997dc0fb6b48a530bc17870220f156d832326c433eb0a010b3768d3bf7868a67823e022f49be67982d0588e7041c498a756024750065a4a0001a9e1bf4bccb48b14b544e770f21d48f2d3ad8d6ca54eccc92f60634e3078eb48013a1f7fb005388ac6f04099b647ed85d8b025d8ae4b178c2376b473b121b8c052000001d2ea556f49fb934dc76f087935a5c07788000b4e3aae24883adfec51b5f4d260"
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertThrowsError(
|
|
|
|
try ZcashRustBackend.initBlocksTable(
|
|
|
|
dbData: __dataDbURL(),
|
|
|
|
height: height,
|
|
|
|
hash: wrongHash,
|
|
|
|
time: time,
|
|
|
|
saplingTree: goodTree,
|
|
|
|
networkType: networkType
|
|
|
|
),
|
|
|
|
"InitBlocksTable with Null bytes on hash string should have failed"
|
|
|
|
) { error in
|
2020-06-08 13:07:48 -07:00
|
|
|
guard let rustError = error as? RustWeldingError else {
|
|
|
|
XCTFail("Expected RustWeldingError")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rustError {
|
2022-10-02 19:11:17 -07:00
|
|
|
case .invalidInput:
|
2020-06-08 13:07:48 -07:00
|
|
|
XCTAssertTrue(true)
|
|
|
|
default:
|
2022-10-07 07:36:21 -07:00
|
|
|
XCTFail("expected \(String(describing: RustWeldingError.invalidInput)) and got \(rustError)")
|
2020-06-08 13:07:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertThrowsError(
|
|
|
|
try ZcashRustBackend.initBlocksTable(
|
|
|
|
dbData: __dataDbURL(),
|
|
|
|
height: height,
|
|
|
|
hash: goodHash,
|
|
|
|
time: time,
|
|
|
|
saplingTree: wrongTree,
|
|
|
|
networkType: networkType
|
|
|
|
),
|
|
|
|
"InitBlocksTable with Null bytes on saplingTree string should have failed"
|
|
|
|
) { error in
|
2020-06-08 13:07:48 -07:00
|
|
|
guard let rustError = error as? RustWeldingError else {
|
|
|
|
XCTFail("Expected RustWeldingError")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rustError {
|
2022-10-02 19:11:17 -07:00
|
|
|
case .invalidInput:
|
2020-06-08 13:07:48 -07:00
|
|
|
XCTAssertTrue(true)
|
|
|
|
default:
|
2022-10-07 07:36:21 -07:00
|
|
|
XCTFail("expected \(String(describing: RustWeldingError.invalidInput)) and got \(rustError)")
|
2020-06-08 13:07:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 08:09:04 -08:00
|
|
|
// TODO: [#716] fix, https://github.com/zcash/ZcashLightClientKit/issues/716
|
2020-06-08 13:07:48 -07:00
|
|
|
func testderiveExtendedFullViewingKeyWithNullBytes() throws {
|
2022-10-02 19:11:17 -07:00
|
|
|
// let wrongSpendingKeys = SaplingExtendedSpendingKey(validatedEncoding: "secret-extended-key-main1qw28psv0qqqqpqr2ru0kss5equx6h0xjsuk5299xrsgdqnhe0cknkl8uqff34prwkyuegyhh5d4rdr8025nl7e0hm8r2txx3fuea5mq\0uy3wnsr9tlajsg4wwvw0xcfk8357k4h850rgj72kt4rx3fjdz99zs9f4neda35cq8tn3848yyvlg4w38gx75cyv9jdpve77x9eq6rtl6d9qyh8det4edevlnc70tg5kse670x50764gzhy60dta0yv3wsd4fsuaz686lgszc7nc9vv") // this spending key corresponds to the "demo app reference seed"
|
|
|
|
//
|
|
|
|
// let goodSpendingKeys = SaplingExtendedSpendingKey(validatedEncoding: "secret-extended-key-main1qw28psv0qqqqpqr2ru0kss5equx6h0xjsuk5299xrsgdqnhe0cknkl8uqff34prwkyuegyhh5d4rdr8025nl7e0hm8r2txx3fuea5mquy3wnsr9tlajsg4wwvw0xcfk8357k4h850rgj72kt4rx3fjdz99zs9f4neda35cq8tn3848yyvlg4w38gx75cyv9jdpve77x9eq6rtl6d9qyh8det4edevlnc70tg5kse670x50764gzhy60dta0yv3wsd4fsuaz686lgszc7nc9vv")
|
|
|
|
//
|
|
|
|
// XCTAssertThrowsError(
|
|
|
|
// try ZcashRustBackend.deriveSaplingExtendedFullViewingKey(wrongSpendingKeys, networkType: networkType),
|
|
|
|
// "Should have thrown an error but didn't! this is dangerous!"
|
|
|
|
// ) { error in
|
|
|
|
// guard let rustError = error as? RustWeldingError else {
|
|
|
|
// XCTFail("Expected RustWeldingError")
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// switch rustError {
|
|
|
|
// case .malformedStringInput:
|
|
|
|
// XCTAssertTrue(true)
|
|
|
|
// default:
|
|
|
|
// XCTFail("expected \(RustWeldingError.malformedStringInput) and got \(rustError)")
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// XCTAssertNoThrow(try ZcashRustBackend.deriveSaplingExtendedFullViewingKey(goodSpendingKeys, networkType: networkType))
|
2020-06-08 13:07:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckNullBytes() throws {
|
2021-09-23 06:26:41 -07:00
|
|
|
// this is a valid zAddr. if you send ZEC to it, you will be contributing to Human Rights Foundation. see more ways to help at https://paywithz.cash/
|
|
|
|
let validZaddr = "zs1gqtfu59z20s9t20mxlxj86zpw6p69l0ev98uxrmlykf2nchj2dw8ny5e0l22kwmld2afc37gkfp"
|
|
|
|
|
2020-06-08 13:07:48 -07:00
|
|
|
XCTAssertFalse(validZaddr.containsCStringNullBytesBeforeStringEnding())
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertTrue(
|
2022-05-31 05:27:24 -07:00
|
|
|
"zs1gqtfu59z20s\u{0}9t20mxlxj86zpw6p69l0ev98uxrmlykf2nchj2dw8ny5e0l22kwmld2afc37gkfp"
|
2021-09-23 06:26:41 -07:00
|
|
|
.containsCStringNullBytesBeforeStringEnding()
|
|
|
|
)
|
2022-05-31 05:27:24 -07:00
|
|
|
XCTAssertTrue("\u{0}".containsCStringNullBytesBeforeStringEnding())
|
2020-06-08 13:07:48 -07:00
|
|
|
XCTAssertFalse("".containsCStringNullBytesBeforeStringEnding())
|
|
|
|
}
|
2022-05-31 05:27:24 -07:00
|
|
|
|
|
|
|
func testTrimTrailingNullBytes() throws {
|
|
|
|
let nullTrailedString = "This Is a memo with text and trailing null bytes\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}"
|
|
|
|
|
|
|
|
let nonNullTrailedString = "This Is a memo with text and trailing null bytes"
|
|
|
|
|
2023-01-18 08:09:04 -08:00
|
|
|
let trimmedString = String(nullTrailedString.reversed().drop(while: { $0 == "\u{0}" }).reversed())
|
2022-05-31 05:27:24 -07:00
|
|
|
|
|
|
|
XCTAssertEqual(trimmedString, nonNullTrailedString)
|
|
|
|
}
|
2020-06-08 13:07:48 -07:00
|
|
|
}
|