From 581d882a5f23a82b5311d622a2a9f507abe54c14 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 21 Oct 2019 20:43:30 -0700 Subject: [PATCH] Additional tests --- lib/src/lightwallet.rs | 17 ++++++++++++++--- lib/src/lightwallet/tests.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/lib/src/lightwallet.rs b/lib/src/lightwallet.rs index 74f14dd..7a37c1b 100644 --- a/lib/src/lightwallet.rs +++ b/lib/src/lightwallet.rs @@ -1313,11 +1313,22 @@ impl LightWallet { } let start_time = now(); + if tos.len() == 0 { + return Err("Need at least one destination address".to_string()); + } + + // Check for duplicates in the to list + if tos.len() > 1 { + let mut to_addresses = tos.iter().map(|t| t.0.to_string()).collect::>(); + to_addresses.sort(); + for i in 0..to_addresses.len()-1 { + if to_addresses[i] == to_addresses[i+1] { + return Err(format!("To address {} is duplicated", to_addresses[i])); + } + } + } let total_value = tos.iter().map(|to| to.1).sum::(); - - // TODO: Check for duplicates in destination addresses - println!( "0: Creating transaction sending {} ztoshis to {} addresses", total_value, tos.len() diff --git a/lib/src/lightwallet/tests.rs b/lib/src/lightwallet/tests.rs index bda0847..469d022 100644 --- a/lib/src/lightwallet/tests.rs +++ b/lib/src/lightwallet/tests.rs @@ -1407,6 +1407,17 @@ fn test_bad_send() { let raw_tx = wallet.send_to_address(branch_id, &ss, &so, vec![(&ext_taddr, AMOUNT1 + 10, None)]); assert!(raw_tx.err().unwrap().contains("Insufficient verified funds")); + + // Duplicated addresses + let raw_tx = wallet.send_to_address(branch_id, &ss, &so, + vec![(&ext_taddr, AMOUNT1 + 10, None), + (&ext_taddr, AMOUNT1 + 10, None)]); + assert!(raw_tx.err().unwrap().contains("duplicate")); + + // No addresses + let raw_tx = wallet.send_to_address(branch_id, &ss, &so, vec![]); + assert!(raw_tx.err().unwrap().contains("at least one")); + } #[test] @@ -1698,6 +1709,30 @@ fn test_lock_unlock() { assert_eq!(taddr1, wallet2.address_from_sk(&tkeys[1])); assert_eq!(taddr2, wallet2.address_from_sk(&tkeys[2])); } + + // Remove encryption from a unlocked wallet should succeed + wallet2.remove_encryption("somepassword".to_string()).unwrap(); + assert_eq!(seed, wallet2.seed); + + // Now encrypt with a different password + wallet2.encrypt("newpassword".to_string()).unwrap(); + assert_eq!([0u8; 32], wallet2.seed); // Seed is cleared out + + // Locking should fail because it is already locked + assert!(wallet2.lock().is_err()); + + // The old password shouldn't work + assert!(wallet2.remove_encryption("somepassword".to_string()).is_err()); + + // Remove encryption with the right password + wallet2.remove_encryption("newpassword".to_string()).unwrap(); + assert_eq!(seed, wallet2.seed); + + // Unlocking a wallet without encryption is an error + assert!(wallet2.remove_encryption("newpassword".to_string()).is_err()); + // Can't lock/unlock a wallet that's not encrypted + assert!(wallet2.lock().is_err()); + assert!(wallet2.unlock("newpassword".to_string()).is_err()); } #[test]