This repository has been archived by the owner on Aug 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from bluedigits/feature/txvalidator-test
Added unit tests for TransactionValidator
- Loading branch information
Showing
1 changed file
with
121 additions
and
34 deletions.
There are no files selected for viewing
155 changes: 121 additions & 34 deletions
155
src/test/java/com/iota/iri/TransactionValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,141 @@ | ||
package com.iota.iri; | ||
|
||
import com.iota.iri.conf.Configuration; | ||
import com.iota.iri.controllers.TipsViewModel; | ||
import com.iota.iri.controllers.TransactionViewModel; | ||
import com.iota.iri.controllers.TransactionViewModelTest; | ||
import com.iota.iri.hash.Curl; | ||
import com.iota.iri.model.Hash; | ||
import com.iota.iri.network.TransactionRequester; | ||
import com.iota.iri.storage.Tangle; | ||
import com.iota.iri.storage.rocksDB.RocksDBPersistenceProviderTest; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import com.iota.iri.storage.rocksDB.RocksDBPersistenceProvider; | ||
import com.iota.iri.utils.Converter; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.*; | ||
import static com.iota.iri.controllers.TransactionViewModelTest.getRandomTransactionTrits; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Created by paul on 5/14/17. | ||
*/ | ||
/** Created by paul on 5/14/17. */ | ||
public class TransactionValidatorTest { | ||
private static final Random seed = new Random(); | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
private static final int MAINNET_MWM = 15; | ||
private static final int TESTNET_MWM = 13; | ||
private static final TemporaryFolder dbFolder = new TemporaryFolder(); | ||
private static final TemporaryFolder logFolder = new TemporaryFolder(); | ||
private static Tangle tangle; | ||
private static TransactionValidator txValidator; | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
dbFolder.create(); | ||
logFolder.create(); | ||
tangle = new Tangle(); | ||
tangle.addPersistenceProvider( | ||
new RocksDBPersistenceProvider( | ||
dbFolder.getRoot().getAbsolutePath(), logFolder.getRoot().getAbsolutePath())); | ||
tangle.init(); | ||
TipsViewModel tipsViewModel = new TipsViewModel(); | ||
TransactionRequester txRequester = new TransactionRequester(tangle); | ||
txValidator = new TransactionValidator(tangle, tipsViewModel, txRequester); | ||
txValidator.init(false, MAINNET_MWM, TESTNET_MWM); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() throws Exception { | ||
txValidator.shutdown(); | ||
tangle.shutdown(); | ||
dbFolder.delete(); | ||
logFolder.delete(); | ||
} | ||
|
||
@Test | ||
public void testMinMwm() throws InterruptedException { | ||
txValidator.shutdown(); | ||
txValidator.init(false, 5, 3); | ||
assertTrue(txValidator.getMinWeightMagnitude() == 13); | ||
txValidator.shutdown(); | ||
txValidator.init(false, MAINNET_MWM, TESTNET_MWM); | ||
} | ||
|
||
@Test | ||
public void validateBytes() throws Exception { | ||
int[] trits = getRandomTransactionTrits(); | ||
Converter.copyTrits(0, trits, 0, trits.length); | ||
byte[] bytes = Converter.bytes(trits); | ||
TransactionValidator.validate(bytes, MAINNET_MWM); | ||
} | ||
|
||
@Test | ||
public void validateTrits() { | ||
int[] trits = getRandomTransactionTrits(); | ||
Converter.copyTrits(0, trits, 0, trits.length); | ||
TransactionValidator.validate(trits, MAINNET_MWM); | ||
} | ||
|
||
@Test(expected = RuntimeException.class) | ||
public void validateTritsWithInvalidMetadata() { | ||
int[] trits = getRandomTransactionTrits(); | ||
TransactionValidator.validate(trits, MAINNET_MWM); | ||
} | ||
|
||
@Test | ||
public void validateBytesWithNewCurl() throws Exception { | ||
int[] trits = getRandomTransactionTrits(); | ||
Converter.copyTrits(0, trits, 0, trits.length); | ||
byte[] bytes = Converter.bytes(trits); | ||
TransactionValidator.validate(bytes, txValidator.getMinWeightMagnitude(), new Curl()); | ||
} | ||
|
||
@Test | ||
public void verifyTxIsSolid() throws Exception { | ||
TransactionViewModel tx = getTxWithBranchAndTrunk(); | ||
assertTrue(txValidator.checkSolidity(tx.getHash(), false)); | ||
assertTrue(txValidator.checkSolidity(tx.getHash(), true)); | ||
} | ||
|
||
@Test | ||
public void verifyTxIsNotSolid() throws Exception { | ||
TransactionViewModel tx = getTxWithoutBranchAndTrunk(); | ||
assertFalse(txValidator.checkSolidity(tx.getHash(), false)); | ||
assertFalse(txValidator.checkSolidity(tx.getHash(), true)); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
@Test | ||
public void addSolidTransactionWithoutErrors() { | ||
int[] trits = getRandomTransactionTrits(); | ||
Converter.copyTrits(0, trits, 0, trits.length); | ||
txValidator.addSolidTransaction(Hash.calculate(trits)); | ||
} | ||
|
||
@Test | ||
public void init() throws Exception { | ||
private TransactionViewModel getTxWithBranchAndTrunk() throws Exception { | ||
TransactionViewModel tx, trunkTx, branchTx; | ||
String trytes = "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999CFDEZBLZQYA9999999999999999999999999999999999999999999ZZWQHWD99C99999999C99999999CKWWDBWSCLMQULCTAAJGXDEMFJXPMGMAQIHDGHRBGEMUYNNCOK9YPHKEEFLFCZUSPMCJHAKLCIBQSGWAS999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"; | ||
|
||
} | ||
int[] trits = Converter.trits(trytes); | ||
trunkTx = new TransactionViewModel(trits, Hash.calculate(trits)); | ||
branchTx = new TransactionViewModel(trits, Hash.calculate(trits)); | ||
|
||
@Test | ||
public void validate() throws Exception { | ||
int[] childTx = getRandomTransactionTrits(); | ||
System.arraycopy(trunkTx.getHash().trits(), 0, childTx, TransactionViewModel.TRUNK_TRANSACTION_TRINARY_OFFSET, TransactionViewModel.TRUNK_TRANSACTION_TRINARY_SIZE); | ||
System.arraycopy(branchTx.getHash().trits(), 0, childTx, TransactionViewModel.BRANCH_TRANSACTION_TRINARY_OFFSET, TransactionViewModel.BRANCH_TRANSACTION_TRINARY_SIZE); | ||
tx = new TransactionViewModel(childTx, Hash.calculate(childTx)); | ||
|
||
} | ||
trunkTx.store(tangle); | ||
branchTx.store(tangle); | ||
tx.store(tangle); | ||
|
||
@Test | ||
public void validate1() throws Exception { | ||
return tx; | ||
} | ||
|
||
} | ||
private TransactionViewModel getTxWithoutBranchAndTrunk() throws Exception { | ||
int[] trits = getRandomTransactionTrits(); | ||
TransactionViewModel tx = new TransactionViewModel(trits, Hash.calculate(trits)); | ||
|
||
@Test | ||
public void validate2() throws Exception { | ||
tx.store(tangle); | ||
|
||
} | ||
} | ||
return tx; | ||
} | ||
} |