Change tests to reflect changes in code structure.

This commit is contained in:
Katarzyna Streich 2016-10-13 18:17:12 +01:00
parent e823e11e85
commit b31598b801

View File

@ -3,12 +3,17 @@ package com.r3corda.core.crypto
import com.r3corda.contracts.asset.* import com.r3corda.contracts.asset.*
import com.r3corda.core.contracts.DOLLARS import com.r3corda.core.contracts.DOLLARS
import com.r3corda.core.contracts.TransactionType import com.r3corda.core.contracts.TransactionType
import com.r3corda.core.contracts.`issued by`
import com.r3corda.core.contracts.`with notary` import com.r3corda.core.contracts.`with notary`
import com.r3corda.core.serialization.serialize import com.r3corda.core.serialization.serialize
import com.r3corda.core.transactions.MerkleTransaction import com.r3corda.core.transactions.FilterFuns
import com.r3corda.core.transactions.buildFilteredTransaction
import com.r3corda.core.transactions.getMerkleRoot
import com.r3corda.core.transactions.hashConcat import com.r3corda.core.transactions.hashConcat
import com.r3corda.core.utilities.DUMMY_NOTARY import com.r3corda.core.utilities.DUMMY_NOTARY
import com.r3corda.testing.ALICE_PUBKEY import com.r3corda.core.utilities.DUMMY_PUBKEY_1
import com.r3corda.testing.*
import org.junit.Test import org.junit.Test
import java.util.* import java.util.*
@ -17,7 +22,6 @@ import kotlin.test.assertFailsWith
import kotlin.test.assertFalse import kotlin.test.assertFalse
//todo //todo
//different filtering fun
//verification - different root //verification - different root
//tests failsWith //tests failsWith
@ -30,107 +34,108 @@ class PartialMerkleTreeTest{
nodes.mapTo(hashed, { it.serialize().sha256() }) nodes.mapTo(hashed, { it.serialize().sha256() })
firstTwo = hashed[0].hashConcat(hashed[1]) firstTwo = hashed[0].hashConcat(hashed[1])
} }
private fun makeTX() = TransactionType.General.Builder(DUMMY_NOTARY).withItems(1000.DOLLARS.CASH `issued by` DUMMY_CASH_ISSUER `owned by` ALICE_PUBKEY `with notary` DUMMY_NOTARY) private fun makeTX() = TransactionType.General.Builder(DUMMY_NOTARY).withItems(
1000.DOLLARS.CASH `issued by` DUMMY_CASH_ISSUER `owned by` ALICE_PUBKEY `with notary` DUMMY_NOTARY)
val testLedger = ledger {
unverifiedTransaction {
output("MEGA_CORP cash") {
Cash.State(
amount = 1000.DOLLARS `issued by` MEGA_CORP.ref(1, 1),
owner = MEGA_CORP_PUBKEY
)
}
}
transaction {
input("MEGA_CORP cash")
output("MEGA_CORP cash".output<Cash.State>().copy(owner = DUMMY_PUBKEY_1))
command(MEGA_CORP_PUBKEY) { Cash.Commands.Move() }
this.verifies()
}
}
val testTx = testLedger.interpreter.transactionsToVerify[0]
//Building full Merkle Tree tests.
@Test @Test
fun `building not full Merkle tree with 6 nodes`(){ fun `building not full Merkle tree with 6 nodes`(){
assertEquals(6, hashed.size) assertEquals(6, hashed.size)
val mtl = ArrayList<SecureHash>() val mr = getMerkleRoot(hashed)
mtl.addAll(hashed) assertEquals(root, mr)
MerkleTransaction.Companion.buildMerkleTree(mtl, hashed)
assertEquals(12, mtl.size)
assertEquals(root, mtl.last())
} }
@Test @Test
fun `building Merkle tree one node`(){ fun `building Merkle tree one node`(){
val node = 'a'.serialize().sha256() val node = 'a'.serialize().sha256()
val mtl = mutableListOf<SecureHash>(node) val mr = getMerkleRoot(listOf(node))
MerkleTransaction.Companion.buildMerkleTree(mtl, listOf(node)) assertEquals(node, mr)
assertEquals(1, mtl.size)
assertEquals(node, mtl.last())
} }
@Test // @Test
fun `building Merkle tree odd number of nodes`(){ // fun `building Merkle tree odd number of nodes`(){
val odd = hashed.subList(0, 3) // val odd = hashed.subList(0, 3)
val mtl = ArrayList<SecureHash>() // val h1 = hashed[0].hashConcat(hashed[1])
val h1 = hashed[0].hashConcat(hashed[1]) // val h2 = hashed[2].hashConcat(hashed[2])
val h2 = hashed[2].hashConcat(hashed[2]) // val mtl = MerkleTransaction.Companion.buildMerkleTree(odd)
mtl.addAll(odd) // assertEquals(6, mtl.size)
MerkleTransaction.Companion.buildMerkleTree(mtl, odd) // assertEquals(h1, mtl[3])
assertEquals(6, mtl.size) // assertEquals(h2, mtl[4])
assertEquals(h1, mtl[3]) // }
assertEquals(h2, mtl[4])
}
@Test @Test
fun `building Merkle tree for a transaction`(){ fun `building Merkle tree for a transaction`(){
val tx = makeTX() val filterFuns = FilterFuns(
tx.addCommand(Cash.Commands.Move(), ALICE_PUBKEY) filterCommands = { x -> ALICE_PUBKEY in x.signers},
tx.addCommand(Cash.Commands.Issue(), ALICE_PUBKEY) filterOutputs = {true},
tx.addCommand(Cash.Commands.Issue(0), ALICE_PUBKEY) filterInputs = {true})
tx.addCommand(Cash.Commands.Issue(1), ALICE_PUBKEY) val mt = testTx.buildFilteredTransaction(filterFuns)
val wtx = tx.toWireTransaction() assert(mt.verify(testTx.id))
val mt = MerkleTransaction.buildMerkleTransaction(wtx, {x -> true} )
assert(mt.verify())
} }
@Test @Test
fun `ordering insensitive tree`(){ fun `ordering insensitive tree`(){
val filterFuns = FilterFuns(filterCommands = { x -> true })
val tx1 = makeTX() val tx1 = makeTX()
tx1.addCommand(Cash.Commands.Issue(1), ALICE_PUBKEY) tx1.addCommand(Cash.Commands.Issue(1), ALICE_PUBKEY)
tx1.addCommand(Cash.Commands.Issue(0), ALICE_PUBKEY) tx1.addCommand(Cash.Commands.Issue(0), ALICE_PUBKEY)
val wtx1 = tx1.toWireTransaction() val wtx1 = tx1.toWireTransaction()
val mt1 = MerkleTransaction.buildMerkleTransaction(wtx1, {x -> true} )
val tx2 = makeTX() val tx2 = makeTX()
tx2.addCommand(Cash.Commands.Issue(0), ALICE_PUBKEY) tx2.addCommand(Cash.Commands.Issue(0), ALICE_PUBKEY)
tx2.addCommand(Cash.Commands.Issue(1), ALICE_PUBKEY) tx2.addCommand(Cash.Commands.Issue(1), ALICE_PUBKEY)
val wtx2 = tx2.toWireTransaction() val wtx2 = tx2.toWireTransaction()
val mt2 = MerkleTransaction.buildMerkleTransaction(wtx2, {x -> true} ) assertEquals(wtx1.id, wtx2.id)
assert(mt1.merkleRoot == mt2.merkleRoot)
} }
//Partial Merkle Tree building tests //Partial Merkle Tree building tests
@Test @Test
fun `Partial Merkle Tree, only left nodes branch`(){ fun `Partial Merkle Tree, only left nodes branch`(){
val includeLeaves = listOf(false, false, false, true, false, true) val includeLeaves = listOf(false, false, false, true, false, true)
val inclHashes = listOf(hashed[3], hashed[5])
val pmt = PartialMerkleTree.build(includeLeaves, hashed) val pmt = PartialMerkleTree.build(includeLeaves, hashed)
assert(!pmt.includeBranch[2] && !pmt.includeBranch[4]&& !pmt.includeBranch[8]) assert(!pmt.includeBranch[2] && !pmt.includeBranch[4]&& !pmt.includeBranch[8])
assertEquals(5, pmt.branchHashes.size) assert(pmt.verify(inclHashes, root))
assertEquals(10, pmt.includeBranch.size)
} }
@Test @Test
fun `Partial Merkle Tree, include zero leaves`(){ fun `Partial Merkle Tree, include zero leaves`(){
val includeLeaves = listOf(false, false, false, false, false, false) val includeLeaves = listOf(false, false, false, false, false, false)
val pmt = PartialMerkleTree.build(includeLeaves, hashed) val pmt = PartialMerkleTree.build(includeLeaves, hashed)
assertEquals(1, pmt.branchHashes.size)
assertEquals(1, pmt.includeBranch.size)
assertEquals(root, pmt.branchHashes[0]) assertEquals(root, pmt.branchHashes[0])
assert(pmt.verify(emptyList(), root))
} }
@Test @Test
fun `Partial Merkle Tree, include all leaves`(){ fun `Partial Merkle Tree, include all leaves`(){
val includeLeaves = listOf(true, true, true, true, true, true) val includeLeaves = listOf(true, true, true, true, true, true)
val pmt = PartialMerkleTree.build(includeLeaves, hashed) val pmt = PartialMerkleTree.build(includeLeaves, hashed)
assertEquals(6, pmt.branchHashes.size) assert(pmt.verify(hashed, root))
assertEquals(12, pmt.includeBranch.size)
assertEquals(pmt.branchHashes, hashed)
}
//Verification
@Test
fun `verify Partial Merkle Tree on a simple example`(){
val includeLeaves = listOf(false, false, false, true, false, true)
val inclHashes = listOf(hashed[3], hashed[5])
val pmt = PartialMerkleTree.build(includeLeaves, hashed)
assert(pmt.verify(inclHashes, root))
} }
@Test @Test
fun `verify Partial Merkle Tree - failure`(){ fun `verify Partial Merkle Tree - too many leaves failure`(){
val includeLeaves = listOf(false, false, false, true, false, true) val includeLeaves = listOf(false, false, false, true, false, true)
val inclHashes = listOf(hashed[3], hashed[5], hashed[0]) val inclHashes = listOf(hashed[3], hashed[5], hashed[0])
val pmt = PartialMerkleTree.build(includeLeaves, hashed) val pmt = PartialMerkleTree.build(includeLeaves, hashed)