2023 07 17 coretest script refactor (#5150)

* Refactor coreTest script test cases to consolidate

* Refactor blockchain tests in coreTest

* Refactor transaction tests in coreTest

* Refactor address tests in coreTest

* Fix missing asserts

* Fix missing asserts
This commit is contained in:
Chris Stewart 2023-07-17 12:15:47 -05:00 committed by GitHub
parent e66c078863
commit ba8a0bf75a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 353 additions and 552 deletions

View File

@ -1,87 +0,0 @@
package org.bitcoins.core.protocol
import org.bitcoins.core.util.{Bech32, Bech32Encoding}
import org.bitcoins.testkitcore.gen._
import org.bitcoins.testkitcore.gen.ln.LnInvoiceGen
import org.scalacheck.{Prop, Properties}
import scala.annotation.tailrec
import scala.util.{Random, Success}
class Bech32Spec extends Properties("Bech32Spec") {
property("split all LN invoices into HRP and data") = {
Prop.forAll(LnInvoiceGen.lnInvoice) { invoice =>
val splitT =
Bech32.splitToHrpAndData(invoice.toString, Bech32Encoding.Bech32)
splitT.isSuccess
}
}
property("split all Bech32 addresses into HRP and data") = {
Prop.forAll(AddressGenerator.bech32Address) { address =>
val splitT =
Bech32.splitToHrpAndData(address.value, Bech32Encoding.Bech32)
splitT.isSuccess
}
}
property("serialization symmetry") = {
Prop.forAll(ScriptGenerators.witnessScriptPubKeyV0,
ChainParamsGenerator.networkParams) {
case ((witSPK, _), network) =>
val addr = Bech32Address(witSPK, network)
val spk = Bech32Address.fromStringToWitSPK(addr.value)
spk == Success(witSPK)
}
}
property("checksum must not work if we modify a char") = {
Prop.forAll(AddressGenerator.bech32Address) { addr: Bech32Address =>
val old = addr.value
val rand = Math.abs(Random.nextInt())
val idx = rand % old.length
val (f, l) = old.splitAt(idx)
val replacementChar = pickReplacementChar(l.head)
val replaced = s"$f$replacementChar${l.tail}"
//should fail because we replaced a char in the addr, so checksum invalid
Bech32Address.fromStringT(replaced).isFailure
}
}
property("must fail if we have a mixed case") = {
Prop.forAllNoShrink(AddressGenerator.bech32Address) { addr: Bech32Address =>
val old = addr.value
val replaced = switchCaseRandChar(old)
//should fail because we we switched the case of a random char
val actual = Bech32Address.fromStringT(replaced)
actual.isFailure
}
}
@tailrec
private def pickReplacementChar(oldChar: Char): Char = {
val rand = Math.abs(Random.nextInt())
val newChar = Bech32.charset(rand % Bech32.charset.size)
//make sure we don't pick the same char we are replacing in the bech32 address
if (oldChar == newChar) pickReplacementChar(oldChar)
else newChar
}
@tailrec
private def switchCaseRandChar(addr: String): String = {
val rand = Math.abs(Random.nextInt())
val idx = rand % addr.length
val (f, l) = addr.splitAt(idx)
if (l.head.isDigit) {
switchCaseRandChar(addr)
} else {
val middle =
if (l.head.isUpper) {
l.head.toLower
} else {
l.head.toUpper
}
s"$f$middle${l.tail}"
}
}
}

View File

@ -8,10 +8,12 @@ import org.bitcoins.core.protocol.script._
import org.bitcoins.core.util.{Bech32, Bech32Encoding}
import org.bitcoins.crypto.ECPublicKey
import org.bitcoins.testkitcore.gen._
import org.bitcoins.testkitcore.gen.ln.LnInvoiceGen
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
import scodec.bits.ByteVector
import scala.util.Success
import scala.annotation.tailrec
import scala.util.{Random, Success}
class Bech32Test extends BitcoinSUnitTest {
@ -269,4 +271,79 @@ class Bech32Test extends BitcoinSUnitTest {
assert(Bech32Address.fromStringT(bech32m.toString).isFailure)
}
}
it must "split all LN invoices into HRP and data" in {
forAll(LnInvoiceGen.lnInvoice) { invoice =>
val splitT =
Bech32.splitToHrpAndData(invoice.toString, Bech32Encoding.Bech32)
assert(splitT.isSuccess)
}
}
it must "split all Bech32 addresses into HRP and data" in {
forAll(AddressGenerator.bech32Address) { address =>
val splitT =
Bech32.splitToHrpAndData(address.value, Bech32Encoding.Bech32)
assert(splitT.isSuccess)
}
}
it must "serialization symmetry" in {
forAll(ScriptGenerators.witnessScriptPubKeyV0,
ChainParamsGenerator.networkParams) { case ((witSPK, _), network) =>
val addr = Bech32Address(witSPK, network)
val spk = Bech32Address.fromStringToWitSPK(addr.value)
assert(spk == Success(witSPK))
}
}
it must "checksum must not work if we modify a char" in {
forAll(AddressGenerator.bech32Address) { addr: Bech32Address =>
val old = addr.value
val rand = Math.abs(Random.nextInt())
val idx = rand % old.length
val (f, l) = old.splitAt(idx)
val replacementChar = pickReplacementChar(l.head)
val replaced = s"$f$replacementChar${l.tail}"
//should fail because we replaced a char in the addr, so checksum invalid
assert(Bech32Address.fromStringT(replaced).isFailure)
}
}
it must "fail if we have a mixed case" in {
forAll(AddressGenerator.bech32Address) { addr: Bech32Address =>
val old = addr.value
val replaced = switchCaseRandChar(old)
//should fail because we we switched the case of a random char
val actual = Bech32Address.fromStringT(replaced)
assert(actual.isFailure)
}
}
@tailrec
private def pickReplacementChar(oldChar: Char): Char = {
val rand = Math.abs(Random.nextInt())
val newChar = Bech32.charset(rand % Bech32.charset.size)
//make sure we don't pick the same char we are replacing in the bech32 address
if (oldChar == newChar) pickReplacementChar(oldChar)
else newChar
}
@tailrec
private def switchCaseRandChar(addr: String): String = {
val rand = Math.abs(Random.nextInt())
val idx = rand % addr.length
val (f, l) = addr.splitAt(idx)
if (l.head.isDigit) {
switchCaseRandChar(addr)
} else {
val middle =
if (l.head.isUpper) {
l.head.toLower
} else {
l.head.toUpper
}
s"$f$middle${l.tail}"
}
}
}

View File

@ -1,82 +0,0 @@
package org.bitcoins.core.protocol
import org.bitcoins.core.protocol.script.WitnessVersion0
import org.bitcoins.core.util.{Bech32, Bech32Encoding}
import org.bitcoins.testkitcore.gen._
import org.scalacheck.{Prop, Properties}
import scala.annotation.tailrec
import scala.util.{Random, Success}
class Bech32mSpec extends Properties("Bech32mSpec") {
property("split all Bech32m addresses into HRP and data") = {
Prop.forAll(AddressGenerator.bech32mAddress) { address =>
val splitT =
Bech32.splitToHrpAndData(address.value, Bech32Encoding.Bech32m)
splitT.isSuccess
}
}
property("serialization symmetry") = {
Prop.forAll(ScriptGenerators.witnessScriptPubKey.suchThat(
_._1.witnessVersion != WitnessVersion0),
ChainParamsGenerator.networkParams) {
case ((witSPK, _), network) =>
val addr = Bech32mAddress(witSPK, network)
val spk = Bech32mAddress.fromStringToWitSPK(addr.value)
spk == Success(witSPK)
}
}
property("checksum must not work if we modify a char") = {
Prop.forAll(AddressGenerator.bech32mAddress) { addr: Bech32mAddress =>
val old = addr.value
val rand = Math.abs(Random.nextInt())
val idx = rand % old.length
val (f, l) = old.splitAt(idx)
val replacementChar = pickReplacementChar(l.head)
val replaced = s"$f$replacementChar${l.tail}"
//should fail because we replaced a char in the addr, so checksum invalid
Bech32mAddress.fromStringT(replaced).isFailure
}
}
property("must fail if we have a mixed case") = {
Prop.forAllNoShrink(AddressGenerator.bech32mAddress) {
addr: Bech32mAddress =>
val old = addr.value
val replaced = switchCaseRandChar(old)
//should fail because we we switched the case of a random char
val actual = Bech32mAddress.fromStringT(replaced)
actual.isFailure
}
}
@tailrec
private def pickReplacementChar(oldChar: Char): Char = {
val rand = Math.abs(Random.nextInt())
val newChar = Bech32.charset(rand % Bech32.charset.size)
//make sure we don't pick the same char we are replacing in the bech32m address
if (oldChar == newChar) pickReplacementChar(oldChar)
else newChar
}
@tailrec
private def switchCaseRandChar(addr: String): String = {
val rand = Math.abs(Random.nextInt())
val idx = rand % addr.length
val (f, l) = addr.splitAt(idx)
if (l.head.isDigit) {
switchCaseRandChar(addr)
} else {
val middle =
if (l.head.isUpper) {
l.head.toLower
} else {
l.head.toUpper
}
s"$f$middle${l.tail}"
}
}
}

View File

@ -35,19 +35,6 @@ class Bech32mTest extends BitcoinSUnitTest {
}
}
it must "checksum must not work if we modify a char" in {
forAll(AddressGenerator.bech32mAddress) { addr: Bech32mAddress =>
val old = addr.value
val rand = Math.abs(Random.nextInt())
val idx = rand % old.length
val (f, l) = old.splitAt(idx)
val replacementChar = pickReplacementChar(l.head)
val replaced = s"$f$replacementChar${l.tail}"
//should fail because we replaced a char in the addr, so checksum invalid
Bech32mAddress.fromStringT(replaced).isFailure
}
}
it must "must fail if we have a mixed case" in {
forAll(AddressGenerator.bech32mAddress) { addr: Bech32mAddress =>
val old = addr.value
@ -225,6 +212,29 @@ class Bech32mTest extends BitcoinSUnitTest {
.isStandard)
}
it must "checksum must not work if we modify a char" in {
forAll(AddressGenerator.bech32mAddress) { addr: Bech32mAddress =>
val old = addr.value
val rand = Math.abs(Random.nextInt())
val idx = rand % old.length
val (f, l) = old.splitAt(idx)
val replacementChar = pickReplacementChar(l.head)
val replaced = s"$f$replacementChar${l.tail}"
//should fail because we replaced a char in the addr, so checksum invalid
assert(Bech32mAddress.fromStringT(replaced).isFailure)
}
}
it must "fail if we have a mixed case" in {
forAll(AddressGenerator.bech32mAddress) { addr: Bech32mAddress =>
val old = addr.value
val replaced = switchCaseRandChar(old)
//should fail because we we switched the case of a random char
val actual = Bech32mAddress.fromStringT(replaced)
assert(actual.isFailure)
}
}
@tailrec
private def pickReplacementChar(oldChar: Char): Char = {
val rand = Math.abs(Random.nextInt())

View File

@ -1,54 +0,0 @@
package org.bitcoins.core.protocol
import org.bitcoins.core.config.TestNet3
import org.bitcoins.core.protocol.script.P2SHScriptPubKey
import org.bitcoins.crypto.CryptoUtil
import org.bitcoins.testkitcore.gen.{
AddressGenerator,
CryptoGenerators,
ScriptGenerators
}
import org.scalacheck.{Prop, Properties}
/** Created by chris on 7/21/16.
*/
class BitcoinAddressSpec extends Properties("BitcoinAddressSpec") {
property("get the same p2sh address no matter what factory function we use") =
Prop.forAll(ScriptGenerators.randomNonP2SHScriptPubKey) {
case (scriptPubKey, _) =>
//we should get the same address no matter which factory function we use
val p2shScriptPubKey = P2SHScriptPubKey(scriptPubKey)
P2SHAddress(scriptPubKey, TestNet3) == P2SHAddress(p2shScriptPubKey,
TestNet3)
}
property("All p2sh addresses created from factory functions must be valid") =
Prop.forAll(ScriptGenerators.randomNonP2SHScriptPubKey) {
case (scriptPubKey, _) =>
//we should get the same address no matter which factory function we use
val addr = P2SHAddress(scriptPubKey, TestNet3)
P2SHAddress.isValid(addr.toString)
}
property(
"get the same p2pkh address no matter what factory function we use") =
Prop.forAll(CryptoGenerators.publicKey) { pubKey =>
val hash = CryptoUtil.sha256Hash160(pubKey.bytes)
P2PKHAddress(pubKey, TestNet3) == P2PKHAddress(hash, TestNet3)
}
property("All p2pkh addresses created from factory functions must be valid") =
Prop.forAll(CryptoGenerators.publicKey) { pubKey =>
val addr = P2PKHAddress(pubKey, TestNet3)
P2PKHAddress.isValid(addr.toString)
}
property("serialization symmetry between script and address") = {
Prop.forAll(AddressGenerator.address) { addr =>
val spk = addr.scriptPubKey
val network = addr.networkParameters
Address.fromScriptPubKey(spk, network) == addr
}
}
}

View File

@ -2,7 +2,12 @@ package org.bitcoins.core.protocol
import org.bitcoins.core.config.{MainNet, RegTest, TestNet3}
import org.bitcoins.core.protocol.script._
import org.bitcoins.crypto.{ECPublicKey, Sha256Hash160Digest}
import org.bitcoins.crypto.{CryptoUtil, ECPublicKey, Sha256Hash160Digest}
import org.bitcoins.testkitcore.gen.{
AddressGenerator,
CryptoGenerators,
ScriptGenerators
}
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
import scala.util.{Failure, Success, Try}
@ -123,4 +128,46 @@ class BitcoinAddressTest extends BitcoinSUnitTest {
val scriptPubKey = P2SHScriptPubKey(EmptyScriptPubKey)
assert(Address.fromScriptPubKeyT(scriptPubKey, RegTest).isSuccess)
}
it must "get the same p2sh address no matter what factory function we use" in {
forAll(ScriptGenerators.randomNonP2SHScriptPubKey) {
case (scriptPubKey, _) =>
//we should get the same address no matter which factory function we use
val p2shScriptPubKey = P2SHScriptPubKey(scriptPubKey)
assert(
P2SHAddress(scriptPubKey, TestNet3) == P2SHAddress(p2shScriptPubKey,
TestNet3))
}
}
it must "All p2sh addresses created from factory functions must be valid" in {
forAll(ScriptGenerators.randomNonP2SHScriptPubKey) {
case (scriptPubKey, _) =>
//we should get the same address no matter which factory function we use
val addr = P2SHAddress(scriptPubKey, TestNet3)
assert(P2SHAddress.isValid(addr.toString))
}
}
it must "get the same p2pkh address no matter what factory function we use" in {
forAll(CryptoGenerators.publicKey) { pubKey =>
val hash = CryptoUtil.sha256Hash160(pubKey.bytes)
assert(P2PKHAddress(pubKey, TestNet3) == P2PKHAddress(hash, TestNet3))
}
}
it must "All p2pkh addresses created from factory functions must be valid" in {
forAll(CryptoGenerators.publicKey) { pubKey =>
val addr = P2PKHAddress(pubKey, TestNet3)
assert(P2PKHAddress.isValid(addr.toString))
}
}
it must "serialization symmetry between script and address" in {
forAll(AddressGenerator.address) { addr =>
val spk = addr.scriptPubKey
val network = addr.networkParameters
assert(Address.fromScriptPubKey(spk, network) == addr)
}
}
}

View File

@ -1,13 +1,15 @@
package org.bitcoins.core.protocol.blockchain
import org.bitcoins.testkitcore.gen.BlockchainElementsGenerator
import org.scalacheck.{Prop, Properties}
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
/** Created by tom on 7/6/16.
*/
class BlockHeaderSpec extends Properties("BlockHeaderSpec") {
property("serialization symmetry") =
Prop.forAll(BlockchainElementsGenerator.blockHeader) { header =>
BlockHeader(header.hex) == header
class BlockHeaderSpec extends BitcoinSUnitTest {
it must "serialization symmetry" in {
forAll(BlockchainElementsGenerator.blockHeader) { header =>
assert(BlockHeader(header.hex) == header)
}
}
}

View File

@ -1,24 +0,0 @@
package org.bitcoins.core.protocol.blockchain
import org.bitcoins.crypto.DoubleSha256Digest
import org.bitcoins.testkitcore.gen.MerkleGenerator
import org.scalacheck.{Prop, Properties}
/** Created by chris on 8/12/16.
*/
class MerkleBlockSpec extends Properties("MerkleBlockSpec") {
//TODO: This is *extremely* slow, this is currently the longest running property we have taking about 6 minutes to run
//I think it is the generator MerkleGenerator.merkleBlockWithInsertTxIds
property(
"contains all inserted txids when we directly create a merkle block from the txids && " +
"contains all txids matched by a bloom filter && " +
"serialization symmetry") =
Prop.forAllNoShrink(MerkleGenerator.merkleBlockWithInsertedTxIds) {
case (merkleBlock: MerkleBlock, _, txIds: Seq[DoubleSha256Digest]) =>
val extractedMatches = merkleBlock.partialMerkleTree.extractMatches
extractedMatches == txIds &&
extractedMatches.intersect(txIds) == txIds &&
MerkleBlock(merkleBlock.hex) == merkleBlock
}
}

View File

@ -5,6 +5,7 @@ import org.bitcoins.core.number.UInt32
import org.bitcoins.core.protocol.transaction.TransactionOutPoint
import org.bitcoins.core.util.BytesUtil
import org.bitcoins.crypto.{DoubleSha256Digest, ECPublicKeyBytes}
import org.bitcoins.testkitcore.gen.MerkleGenerator
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
/** Created by chris on 8/9/16.
@ -369,4 +370,19 @@ class MerkleBlockTests extends BitcoinSUnitTest {
val (merkleBlock, _) = MerkleBlock(block, filterWithTxIdsInserted)
merkleBlock.partialMerkleTree.extractMatches must be(txMatches)
}
//TODO: This is *extremely* slow, this is currently the longest running property we have taking about 6 minutes to run
//I think it is the generator MerkleGenerator.merkleBlockWithInsertTxIds
it must "contains all inserted txids when we directly create a merkle block from the txids && " +
"contains all txids matched by a bloom filter && " +
"serialization symmetry" in {
forAll(MerkleGenerator.merkleBlockWithInsertedTxIds) {
case (merkleBlock: MerkleBlock, _, txIds: Seq[DoubleSha256Digest]) =>
val extractedMatches = merkleBlock.partialMerkleTree.extractMatches
assert(
extractedMatches == txIds &&
extractedMatches.intersect(txIds) == txIds &&
MerkleBlock(merkleBlock.hex) == merkleBlock)
}
}
}

View File

@ -1,31 +0,0 @@
package org.bitcoins.core.protocol.blockchain
import org.bitcoins.crypto.DoubleSha256Digest
import org.bitcoins.testkitcore.gen.MerkleGenerator
import org.scalacheck.{Prop, Properties}
/** Created by chris on 2/13/17.
*/
class PartialMerkleTreeSpec extends Properties("PartialMerkleTreeSpec") {
property(
"must be able to extract all of the txids we indicated to be matches") =
Prop.forAll(MerkleGenerator.partialMerkleTree) {
case (partialMerkleTree: PartialMerkleTree,
txMatches: Seq[(Boolean, DoubleSha256Digest)]) =>
val matchedTxs = txMatches.filter(_._1).map(_._2)
partialMerkleTree.extractMatches == matchedTxs
}
property(
"must generate the same partial merkle tree from the same parameters") =
Prop.forAll(MerkleGenerator.partialMerkleTree) {
case (partialMerkleTree: PartialMerkleTree, _) =>
val partialMerkleTree2 =
PartialMerkleTree(partialMerkleTree.transactionCount,
partialMerkleTree.hashes,
partialMerkleTree.bits)
partialMerkleTree2 == partialMerkleTree
}
}

View File

@ -4,6 +4,7 @@ import org.bitcoins.core.bloom.{BloomFilter, BloomUpdateAll}
import org.bitcoins.core.number.UInt32
import org.bitcoins.core.util.{BytesUtil, Leaf, Node}
import org.bitcoins.crypto.DoubleSha256Digest
import org.bitcoins.testkitcore.gen.MerkleGenerator
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
import scodec.bits.BitVector
@ -280,4 +281,23 @@ class PartialMerkleTreeTests extends BitcoinSUnitTest {
true)
}
it must "be able to extract all of the txids we indicated to be matches" in {
forAll(MerkleGenerator.partialMerkleTree) {
case (partialMerkleTree: PartialMerkleTree,
txMatches: Seq[(Boolean, DoubleSha256Digest)]) =>
val matchedTxs = txMatches.filter(_._1).map(_._2)
assert(partialMerkleTree.extractMatches == matchedTxs)
}
}
it must "generate the same partial merkle tree from the same parameters" in {
forAll(MerkleGenerator.partialMerkleTree) {
case (partialMerkleTree: PartialMerkleTree, _) =>
val partialMerkleTree2 =
PartialMerkleTree(partialMerkleTree.transactionCount,
partialMerkleTree.hashes,
partialMerkleTree.bits)
assert(partialMerkleTree2 == partialMerkleTree)
}
}
}

View File

@ -1,14 +0,0 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
/** Created by tom on 8/23/16.
*/
class CLTVScriptPubKeySpec extends Properties("CLTVScriptPubKeySpec") {
property("Serialization symmetry") =
Prop.forAll(ScriptGenerators.cltvScriptPubKey) {
case (cltvScriptPubKey, _) =>
CLTVScriptPubKey(cltvScriptPubKey.hex) == cltvScriptPubKey
}
}

View File

@ -11,6 +11,7 @@ import org.bitcoins.core.script.crypto.{OP_CHECKSIG, OP_HASH160}
import org.bitcoins.core.script.locktime.OP_CHECKLOCKTIMEVERIFY
import org.bitcoins.core.script.stack.{OP_DROP, OP_DUP}
import org.bitcoins.crypto.ECPrivateKey
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.bitcoins.testkitcore.util.TestUtil
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
@ -53,4 +54,10 @@ class CLTVScriptPubKeyTest extends BitcoinSUnitTest {
CLTVScriptPubKey(scriptNum17, p2pkh).locktime must be(scriptNum17)
CLTVScriptPubKey(scriptNum5, p2pkh).locktime must be(scriptNum5)
}
it must "serialization symmetry" in {
forAll(ScriptGenerators.cltvScriptPubKey) { case (cltvScriptPubKey, _) =>
assert(CLTVScriptPubKey(cltvScriptPubKey.hex) == cltvScriptPubKey)
}
}
}

View File

@ -1,13 +0,0 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
/** Created by tom on 8/23/16.
*/
class CSVScriptPubKeySpec extends Properties("CSVScriptPubKeySpec") {
property("Serialization Symmetry") =
Prop.forAll(ScriptGenerators.csvScriptPubKey) { case (csvScriptPubKey, _) =>
CSVScriptPubKey(csvScriptPubKey.hex) == csvScriptPubKey
}
}

View File

@ -11,6 +11,7 @@ import org.bitcoins.core.script.crypto.{OP_CHECKSIG, OP_HASH160}
import org.bitcoins.core.script.locktime.OP_CHECKSEQUENCEVERIFY
import org.bitcoins.core.script.stack.{OP_DROP, OP_DUP}
import org.bitcoins.crypto.ECPrivateKey
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.bitcoins.testkitcore.util.TestUtil
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
@ -53,4 +54,10 @@ class CSVScriptPubKeyTest extends BitcoinSUnitTest {
CSVScriptPubKey(scriptNum17, p2pkh).locktime must be(scriptNum17)
CSVScriptPubKey(scriptNum5, p2pkh).locktime must be(scriptNum5)
}
it must "serialization symmetry" in {
forAll(ScriptGenerators.csvScriptPubKey) { case (csvScriptPubKey, _) =>
assert(CSVScriptPubKey(csvScriptPubKey.hex) == csvScriptPubKey)
}
}
}

View File

@ -1,18 +0,0 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
/** Created by chris on 6/22/16.
*/
class MultiSignatureScriptPubKeySpec
extends Properties("MultiSignatureScriptPubKeySpec") {
property("Serialization symmetry") =
Prop.forAll(ScriptGenerators.multiSigScriptPubKey) {
case (multiSigScriptPubKey, _) =>
MultiSignatureScriptPubKey(
multiSigScriptPubKey.hex) == multiSigScriptPubKey
}
}

View File

@ -1,6 +1,7 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.crypto.ECPublicKeyBytes
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.bitcoins.testkitcore.util.{BitcoinSUnitTest, TestUtil}
/** Created by chris on 3/8/16.
@ -62,4 +63,13 @@ class MultiSignatureScriptPubKeyTest extends BitcoinSUnitTest {
}
it must "serialization symmetry" in {
forAll(ScriptGenerators.multiSigScriptPubKey) {
case (multiSigScriptPubKey, _) =>
assert(
MultiSignatureScriptPubKey(
multiSigScriptPubKey.hex) == multiSigScriptPubKey)
}
}
}

View File

@ -1,18 +0,0 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
/** Created by chris on 6/22/16.
*/
class MultiSignatureScriptSignatureSpec
extends Properties("MultiSignatureScriptSigSpec") {
property("Serialization symmetry") =
Prop.forAll(ScriptGenerators.multiSignatureScriptSignature) {
multiSigScriptSig =>
MultiSignatureScriptSignature(
multiSigScriptSig.hex) == multiSigScriptSig
}
}

View File

@ -1,5 +1,6 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.bitcoins.testkitcore.util.{BitcoinSJvmTest, TransactionTestUtil}
/** Created by chris on 3/8/16.
@ -17,4 +18,12 @@ class MultiSignatureScriptSignatureTest extends BitcoinSJvmTest {
assert(!MultiSignatureScriptSignature.isValidAsm(Vector.empty))
}
it must "serialization symmetry" in {
forAll(ScriptGenerators.multiSignatureScriptSignature) {
multiSigScriptSig =>
assert(
MultiSignatureScriptSignature(
multiSigScriptSig.hex) == multiSigScriptSig)
}
}
}

View File

@ -1,21 +0,0 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.{CryptoGenerators, ScriptGenerators}
import org.scalacheck.{Prop, Properties}
/** Created by chris on 6/22/16.
*/
class P2PKHScriptPubKeySpec extends Properties("P2PKHScriptPubKeySpec") {
property("Serialization symmetry") =
Prop.forAll(ScriptGenerators.p2pkhScriptPubKey) {
case (p2pkhScriptPubKey, _) =>
P2PKHScriptPubKey(p2pkhScriptPubKey.hex) == p2pkhScriptPubKey
}
property("find pubkeyhash in scriptPubKey") =
Prop.forAll(CryptoGenerators.sha256Hash160Digest) { hash =>
val scriptPubKey = P2PKHScriptPubKey(hash)
scriptPubKey.pubKeyHash == hash
}
}

View File

@ -1,7 +1,7 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.Implicits._
import org.bitcoins.testkitcore.gen.CryptoGenerators
import org.bitcoins.testkitcore.gen.{CryptoGenerators, ScriptGenerators}
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
class P2PKHScriptPubKeyTest extends BitcoinSUnitTest {
@ -11,4 +11,17 @@ class P2PKHScriptPubKeyTest extends BitcoinSUnitTest {
val p2pkhScriptPubKey = P2PKHScriptPubKey(hash)
p2pkhScriptPubKey.pubKeyHash must be(hash)
}
it must "serialization symmetry" in {
forAll(ScriptGenerators.p2pkhScriptPubKey) { case (p2pkhScriptPubKey, _) =>
assert(P2PKHScriptPubKey(p2pkhScriptPubKey.hex) == p2pkhScriptPubKey)
}
}
it must "find pubkeyhash in scriptPubKey" in {
forAll(CryptoGenerators.sha256Hash160Digest) { hash =>
val scriptPubKey = P2PKHScriptPubKey(hash)
assert(scriptPubKey.pubKeyHash == hash)
}
}
}

View File

@ -1,15 +0,0 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
/** Created by chris on 6/22/16.
*/
class P2PKHScriptSignatureSpec extends Properties("P2PKHSpec") {
property("Serialization symmetry") =
Prop.forAll(ScriptGenerators.p2pkhScriptSignature) { p2pkhScriptSig =>
P2PKHScriptSignature(p2pkhScriptSig.hex) == p2pkhScriptSig
}
}

View File

@ -1,6 +1,7 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.crypto.{ECDigitalSignature, HashType}
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.bitcoins.testkitcore.util.{BitcoinSJvmTest, TestUtil}
import scodec.bits.ByteVector
@ -27,4 +28,11 @@ class P2PKHScriptSignatureTest extends BitcoinSJvmTest {
"3044022016ffdbb7c57634903c5e018fcfc48d59f4e37dc4bc3bbc9ba4e6ee39150bca030220119c2241a931819bc1a75d3596e4029d803d1cd6de123bf8a1a1a2c3665e1fac01"))
}
it must "serialization symmetry" in {
forAll(ScriptGenerators.p2pkhScriptSignature) { p2pkhScriptSig =>
assert(P2PKHScriptSignature(p2pkhScriptSig.hex) == p2pkhScriptSig)
}
}
}

View File

@ -1,16 +0,0 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
/** Created by chris on 6/22/16.
*/
class P2PKScriptPubKeySpec extends Properties("P2PKScriptPubKeySpec") {
property("Serialization symmetry") =
Prop.forAll(ScriptGenerators.p2pkScriptPubKey) {
case (p2pkScriptPubKey, _) =>
P2PKScriptPubKey(p2pkScriptPubKey.hex) == p2pkScriptPubKey
}
}

View File

@ -1,6 +1,7 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.crypto.ECPublicKeyBytes
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.bitcoins.testkitcore.util.{BitcoinSUnitTest, TestUtil}
/** Created by chris on 4/1/16.
@ -17,4 +18,10 @@ class P2PKScriptPubKeyTest extends BitcoinSUnitTest {
p2pkScriptPubKey.publicKey must be(ECPublicKeyBytes(
"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"))
}
it must "serialization symmetry" in {
forAll(ScriptGenerators.p2pkScriptPubKey) { case (p2pkScriptPubKey, _) =>
assert(P2PKScriptPubKey(p2pkScriptPubKey.hex) == p2pkScriptPubKey)
}
}
}

View File

@ -1,14 +0,0 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
/** Created by chris on 6/22/16.
*/
class P2PKScriptSignatureSpec extends Properties("P2PKSpec") {
property("Serialization symmetry") =
Prop.forAll(ScriptGenerators.p2pkScriptSignature) { p2pkScriptSig =>
P2PKScriptSignature(p2pkScriptSig.hex) == p2pkScriptSig
}
}

View File

@ -1,6 +1,7 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.crypto.ECDigitalSignature
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.bitcoins.testkitcore.util.TestUtil
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
@ -17,4 +18,9 @@ class P2PKScriptSignatureTest extends BitcoinSUnitTest {
"304402200a5c6163f07b8d3b013c4d1d6dba25e780b39658d79ba37af7057a3b7f15ffa102201fd9b4eaa9943f734928b99a83592c2e7bf342ea2680f6a2bb705167966b742001"))
}
it must "serialization symmetry" in {
forAll(ScriptGenerators.p2pkScriptSignature) { p2pkScriptSig =>
assert(P2PKScriptSignature(p2pkScriptSig.hex) == p2pkScriptSig)
}
}
}

View File

@ -1,15 +1,15 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
/** Created by chris on 6/24/16.
*/
class P2SHScriptPubKeySpec extends Properties("P2SHScriptPubKeySpec") {
class P2SHScriptPubKeySpec extends BitcoinSUnitTest {
property("Symmetrical serialization") =
Prop.forAll(ScriptGenerators.p2shScriptPubKey) {
case (p2shScriptPubKey, _, _) =>
P2SHScriptPubKey(p2shScriptPubKey.hex) == p2shScriptPubKey
it must "symmetrical serialization" in {
forAll(ScriptGenerators.p2shScriptPubKey) { case (p2shScriptPubKey, _, _) =>
assert(P2SHScriptPubKey(p2shScriptPubKey.hex) == p2shScriptPubKey)
}
}
}

View File

@ -1,25 +0,0 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
/** Created by chris on 6/24/16.
*/
class P2SHScriptSignatureSpec extends Properties("P2SHScriptSignatureSpec") {
property("Symmetrical serialization") =
Prop.forAll(ScriptGenerators.p2shScriptSignature) { p2shScriptSig =>
P2SHScriptSignature(p2shScriptSig.hex) == p2shScriptSig
}
property(
"place a witness scriptPubKey in a p2shScriptSig, then extract the witScriptPubKey again") =
Prop.forAll(ScriptGenerators.witnessScriptPubKeyV0) {
case (witScriptPubKey, _) =>
val p2shScriptSig = P2SHScriptSignature(witScriptPubKey)
p2shScriptSig.redeemScript == witScriptPubKey
p2shScriptSig.scriptSignatureNoRedeemScript == EmptyScriptSignature
}
}

View File

@ -6,6 +6,7 @@ import org.bitcoins.core.script.constant.{
ScriptConstant
}
import org.bitcoins.crypto.ECPublicKeyBytes
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.bitcoins.testkitcore.util.{BitcoinSJvmTest, TestUtil}
/** Created by chris on 3/8/16.
@ -48,4 +49,21 @@ class P2SHScriptSignatureTest extends BitcoinSJvmTest {
))
}
it must "symmetrical serialization" in {
forAll(ScriptGenerators.p2shScriptSignature) { p2shScriptSig =>
assert(P2SHScriptSignature(p2shScriptSig.hex) == p2shScriptSig)
}
}
it must
"place a witness scriptPubKey in a p2shScriptSig, then extract the witScriptPubKey again" in {
forAll(ScriptGenerators.witnessScriptPubKeyV0) {
case (witScriptPubKey, _) =>
val p2shScriptSig = P2SHScriptSignature(witScriptPubKey)
assert(p2shScriptSig.redeemScript == witScriptPubKey)
assert(
p2shScriptSig.scriptSignatureNoRedeemScript == EmptyScriptSignature)
}
}
}

View File

@ -1,21 +1,21 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
class ScriptSpec extends Properties("ScriptSpec") {
class ScriptSpec extends BitcoinSUnitTest {
property(
"serialization symmetry for ScriptFactory.fromAsmBytes with ScriptPubKeys") = {
Prop.forAllNoShrink(ScriptGenerators.scriptPubKey) { case (spk, _) =>
ScriptPubKey.fromAsmBytes(spk.asmBytes) == spk
it must
"serialization symmetry for ScriptFactory.fromAsmBytes with ScriptPubKeys" in {
forAll(ScriptGenerators.scriptPubKey) { case (spk, _) =>
assert(ScriptPubKey.fromAsmBytes(spk.asmBytes) == spk)
}
}
}
property(
"serialization symmetry for ScriptFactory.fromAsmBytes with ScriptSignatures") = {
Prop.forAllNoShrink(ScriptGenerators.scriptSignature) { case ss =>
ScriptSignature.fromAsmBytes(ss.asmBytes) == ss
it must
"serialization symmetry for ScriptFactory.fromAsmBytes with ScriptSignatures" in {
forAll(ScriptGenerators.scriptSignature) { case ss =>
assert(ScriptSignature.fromAsmBytes(ss.asmBytes) == ss)
}
}
}
}

View File

@ -1,14 +0,0 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
/** Created by chris on 1/3/17.
*/
class WitnessCommitmentSpec extends Properties("WitnessCommitmentSpec") {
property("serialization symmetry") =
Prop.forAll(ScriptGenerators.witnessCommitment) { case (commitment, _) =>
WitnessCommitment(commitment.hex) == commitment
}
}

View File

@ -1,5 +1,6 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
/** Created by chris on 12/23/16.
@ -19,4 +20,10 @@ class WitnessCommitmentTest extends BitcoinSUnitTest {
commitment.witnessRootHash.hex must be(
"309cfb38d1015c266667d5b7888c83def872a531b8ac277fe8df623c32b562b5")
}
it must "serialization symmetry" in {
forAll(ScriptGenerators.witnessCommitment) { case (commitment, _) =>
assert(WitnessCommitment(commitment.hex) == commitment)
}
}
}

View File

@ -1,52 +1,57 @@
package org.bitcoins.core.protocol.script
import org.bitcoins.testkitcore.gen.ScriptGenerators
import org.scalacheck.{Prop, Properties}
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
/** Created by chris on 1/3/17.
*/
class WitnessScriptPubKeySpec extends Properties("WitnessScriptPubKeySpec") {
class WitnessScriptPubKeySpec extends BitcoinSUnitTest {
property("witnessScriptPubKeyV0 serialization symmetry") =
Prop.forAll(ScriptGenerators.witnessScriptPubKeyV0) {
it must "witnessScriptPubKeyV0 serialization symmetry" in {
forAll(ScriptGenerators.witnessScriptPubKeyV0) {
case (witScriptPubKeyV0, _) =>
witScriptPubKeyV0 match {
case p2wpkh: P2WPKHWitnessSPKV0 =>
P2WPKHWitnessSPKV0(p2wpkh.hex) == witScriptPubKeyV0
assert(P2WPKHWitnessSPKV0(p2wpkh.hex) == witScriptPubKeyV0)
case p2wsh: P2WSHWitnessSPKV0 =>
P2WSHWitnessSPKV0(p2wsh.hex) == witScriptPubKeyV0
assert(P2WSHWitnessSPKV0(p2wsh.hex) == witScriptPubKeyV0)
}
}
}
property("witnessScriptPubKeyV0 fromAsm symmetry") =
Prop.forAll(ScriptGenerators.witnessScriptPubKeyV0) {
it must "witnessScriptPubKeyV0 fromAsm symmetry" in {
forAll(ScriptGenerators.witnessScriptPubKeyV0) {
case (witScriptPubKeyV0, _) =>
witScriptPubKeyV0 match {
case p2wpkh: P2WPKHWitnessSPKV0 =>
P2WPKHWitnessSPKV0.fromAsm(p2wpkh.asm) == witScriptPubKeyV0
assert(P2WPKHWitnessSPKV0.fromAsm(p2wpkh.asm) == witScriptPubKeyV0)
case p2wsh: P2WSHWitnessSPKV0 =>
P2WSHWitnessSPKV0.fromAsm(p2wsh.asm) == witScriptPubKeyV0
assert(P2WSHWitnessSPKV0.fromAsm(p2wsh.asm) == witScriptPubKeyV0)
}
}
}
property("unassignedWitnessScriptPubKey serialization symmetry") =
Prop.forAll(ScriptGenerators.unassignedWitnessScriptPubKey) {
it must "unassignedWitnessScriptPubKey serialization symmetry" in {
forAll(ScriptGenerators.unassignedWitnessScriptPubKey) {
case (unassignedWitScriptPubKey, _) =>
UnassignedWitnessScriptPubKey(
unassignedWitScriptPubKey.hex) == unassignedWitScriptPubKey
assert(
UnassignedWitnessScriptPubKey(
unassignedWitScriptPubKey.hex) == unassignedWitScriptPubKey)
}
}
property("unassignedWitnessScriptPubKey fromAsm symmetry") =
Prop.forAll(ScriptGenerators.unassignedWitnessScriptPubKey) {
it must "unassignedWitnessScriptPubKey fromAsm symmetry" in {
forAll(ScriptGenerators.unassignedWitnessScriptPubKey) {
case (unassignedWitScriptPubKey, _) =>
UnassignedWitnessScriptPubKey.fromAsm(
unassignedWitScriptPubKey.asm) == unassignedWitScriptPubKey
assert(
UnassignedWitnessScriptPubKey.fromAsm(
unassignedWitScriptPubKey.asm) == unassignedWitScriptPubKey)
}
}
property("witnessScriptPubKey fromAsm symmetry") = {
Prop.forAll(ScriptGenerators.witnessScriptPubKey) {
case (witScriptPubKey, _) =>
WitnessScriptPubKey(witScriptPubKey.asm) == witScriptPubKey
it must "witnessScriptPubKey fromAsm symmetry" in {
forAll(ScriptGenerators.witnessScriptPubKey) { case (witScriptPubKey, _) =>
assert(WitnessScriptPubKey(witScriptPubKey.asm) == witScriptPubKey)
}
}
}

View File

@ -1,16 +0,0 @@
package org.bitcoins.core.protocol.transaction
import org.bitcoins.testkitcore.gen.TransactionGenerators
import org.scalacheck.{Prop, Properties}
/** Created by chris on 6/24/16.
*/
class TransactionInputSpec extends Properties("TransactionInputSpec") {
property("Serialization symmetry") = {
Prop.forAllNoShrink(TransactionGenerators.input) { input =>
val result = TransactionInput(input.hex) == input
result
}
}
}

View File

@ -4,6 +4,7 @@ import org.bitcoins.core.protocol.script.{
EmptyScriptSignature,
P2PKScriptSignature
}
import org.bitcoins.testkitcore.gen.TransactionGenerators
import org.bitcoins.testkitcore.util.TestUtil
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
@ -50,4 +51,11 @@ class TransactionInputTest extends BitcoinSUnitTest {
TransactionInput(c.previousOutput, c.scriptSignature, c.sequence).hex)
}
it must "serialization symmetry" in {
forAll(TransactionGenerators.input) { input =>
val result = TransactionInput(input.hex) == input
assert(result)
}
}
}

View File

@ -1,14 +0,0 @@
package org.bitcoins.core.protocol.transaction
import org.bitcoins.testkitcore.gen.TransactionGenerators
import org.scalacheck.{Prop, Properties}
/** Created by chris on 6/21/16.
*/
class TransactionOutPointSpec extends Properties("TransactionOutPointSpec") {
property("Serialization symmetry") =
Prop.forAll(TransactionGenerators.outPoint) { outPoint =>
TransactionOutPoint(outPoint.hex) == outPoint
}
}

View File

@ -2,6 +2,7 @@ package org.bitcoins.core.protocol.transaction
import org.bitcoins.core.number.UInt32
import org.bitcoins.crypto.DoubleSha256DigestBE
import org.bitcoins.testkitcore.gen.TransactionGenerators
import org.bitcoins.testkitcore.util.TestUtil
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
@ -28,4 +29,10 @@ class TransactionOutPointTest extends BitcoinSUnitTest {
assert(TransactionOutPoint.fromString(string) == expected)
}
it must "serialization symmetry" in {
forAll(TransactionGenerators.outPoint) { outPoint =>
assert(TransactionOutPoint(outPoint.hex) == outPoint)
}
}
}

View File

@ -1,15 +0,0 @@
package org.bitcoins.core.protocol.transaction
import org.bitcoins.testkitcore.gen.TransactionGenerators
import org.scalacheck.{Prop, Properties}
/** Created by chris on 6/24/16.
*/
class TransactionOutputSpec extends Properties("TransactionOutputSpec") {
property("Serialization symmetry") =
Prop.forAll(TransactionGenerators.output) { output =>
TransactionOutput(output.hex) == output
output.hex == TransactionOutput(output.hex).hex
}
}

View File

@ -2,6 +2,7 @@ package org.bitcoins.core.protocol.transaction
import org.bitcoins.core.currency.CurrencyUnits
import org.bitcoins.core.protocol.script.EmptyScriptPubKey
import org.bitcoins.testkitcore.gen.TransactionGenerators
import org.bitcoins.testkitcore.util.BitcoinSUnitTest
/** Created by chris on 3/30/16.
@ -12,4 +13,11 @@ class TransactionOutputTest extends BitcoinSUnitTest {
EmptyTransactionOutput.scriptPubKey must be(EmptyScriptPubKey)
EmptyTransactionOutput.value must be(CurrencyUnits.negativeSatoshi)
}
it must "serialization symmetry" in {
forAll(TransactionGenerators.output) { output =>
assert(TransactionOutput(output.hex) == output)
assert(output.hex == TransactionOutput(output.hex).hex)
}
}
}