Skip to content

Commit

Permalink
added tests for TxHandler method
Browse files Browse the repository at this point in the history
  • Loading branch information
music committed Mar 3, 2018
1 parent 888ca3e commit dba2958
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
21 changes: 12 additions & 9 deletions week_05/project_2/public_tests/PublicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ private void assertTestSetIsValid(final UtxoTestSet utxoTestSet) {
for (Transaction tx: txsValidation.allElements()) {
assertEquals(txHandler.isValidTx(tx), txsValidation.isValid(tx) );
}
assertEquals(txHandler.handleTxs(txsValidation.allElements().toArray(
new Transaction[txsValidation.allElements().size()])).length,
txsValidation.nonConflictingTransactions());
}

// Test 1: test isValidTx() with valid transactions
@Test
public void testIsValidWithValidTransactions()
throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
// Create a new set of transactions for testing
// Create a new set of transactions for testing
final UtxoTestSet utxoTestSet = UtxoTestSet.builder()
.setPeopleSize(10)
.setUtxoTxNumber(10)
Expand All @@ -57,11 +60,11 @@ public void testIsValidWithValidTransactions()
// check against student solution
assertTestSetIsValid(utxoTestSet);
}

// Test 2: test isValidTx() with transactions containing signatures of incorrect data
@Test
public void testIsValidWithInvalidSignatures() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
// Create a new set of transactions for testing
// Create a new set of transactions for testing
final UtxoTestSet utxoTestSet = UtxoTestSet.builder()
.setPeopleSize(10)
.setUtxoTxNumber(10)
Expand All @@ -76,11 +79,11 @@ public void testIsValidWithInvalidSignatures() throws NoSuchAlgorithmException,
// check against student solution
assertTestSetIsValid(utxoTestSet);
}

// Test 3: test isValidTx() with transactions containing signatures using incorrect private keys
@Test
public void testIsValidSignaturesWithInvalidPrivateKeys() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
// Create a new set of transactions for testing
// Create a new set of transactions for testing
final UtxoTestSet utxoTestSet = UtxoTestSet.builder()
.setPeopleSize(10)
.setUtxoTxNumber(10)
Expand All @@ -99,7 +102,7 @@ public void testIsValidSignaturesWithInvalidPrivateKeys() throws NoSuchAlgorithm
// Test 4: test isValidTx() with transactions whose total output value exceeds total input value
@Test
public void testIsValidTotalOutputExceedsTotalInput() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
// Create a new set of transactions for testing
// Create a new set of transactions for testing
final UtxoTestSet utxoTestSet = UtxoTestSet.builder()
.setPeopleSize(10)
.setUtxoTxNumber(10)
Expand All @@ -115,11 +118,11 @@ public void testIsValidTotalOutputExceedsTotalInput() throws NoSuchAlgorithmExce
assertTestSetIsValid(utxoTestSet);
}


// Test 5: test isValidTx() with transactions that claim outputs not in the current utxoPool
@Test
public void testIsValidTransactionsClamingOuputsNotInThePool() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
// Create a new set of transactions for testing
// Create a new set of transactions for testing
final UtxoTestSet utxoTestSet = UtxoTestSet.builder()
.setPeopleSize(10)
.setUtxoTxNumber(10)
Expand Down Expand Up @@ -157,7 +160,7 @@ public void testIsValidTransactionsClaimingTheSameUTXOSeveralTimes() throws NoSu
// Test 7: test isValidTx() with transactions that contain a negative output value
@Test
public void testIsValidTransactionsWithNegativeOutput() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
// Create a new set of transactions for testing
// Create a new set of transactions for testing
final UtxoTestSet utxoTestSet = UtxoTestSet.builder()
.setPeopleSize(10)
.setUtxoTxNumber(10)
Expand Down
15 changes: 14 additions & 1 deletion week_05/project_2/public_tests/UtxoTestSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,24 @@ private ValidationLists<Transaction> generateTrxWithCorruptedSignaturePercentage

if (corrupted){
invalid.add(tx);
} else{
} else {
valid.add(tx);
}

}

HashSet<UTXO> globalUTXOs = new HashSet<>();
for (Transaction transaction : valid) {
for (Transaction.Input input : transaction.getInputs()) {
UTXO utxo = new UTXO(input.prevTxHash, input.outputIndex);
if (globalUTXOs.contains(utxo)) {
conflicted.add(transaction);
break;
} else {
globalUTXOs.add(utxo);
}
}
}

return ValidationLists.builder(Transaction.class)
.setValid(valid)
Expand Down
4 changes: 4 additions & 0 deletions week_05/project_2/public_tests/ValidationLists.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public List<E> allElements() {
list.addAll(invalid);
return list;
}

public int nonConflictingTransactions() {
return valid.size() - conflicted.size();
}

public static <T> Builder<T> builder(Class<T> c){
return new Builder<T>();
Expand Down

0 comments on commit dba2958

Please sign in to comment.