Browse Source

fix test cases inside cashierdb and walletdb

ghassmo 4 years ago
parent
commit
291f8d6910
2 changed files with 42 additions and 10 deletions
  1. 18 2
      src/wallet/cashierdb.rs
  2. 24 8
      src/wallet/walletdb.rs

+ 18 - 2
src/wallet/cashierdb.rs

@@ -333,12 +333,28 @@ mod tests {
     use rand::rngs::OsRng;
 
     // TODO add more tests
+    
+
+    pub fn init_db(path: &PathBuf, password: String) -> Result<()> {
+        if !password.trim().is_empty() {
+            let contents = include_str!("../../sql/cashier.sql");
+            let conn = Connection::open(&path)?;
+            debug!(target: "CASHIERDB", "OPENED CONNECTION AT PATH {:?}", path);
+            conn.pragma_update(None, "key", &password)?;
+            conn.execute_batch(&contents)?;
+        } else {
+            debug!(target: "CASHIERDB", "Password is empty. You must set a password to use the wallet.");
+            return Err(Error::from(ClientFailed::EmptyPassword));
+        }
+        Ok(())
+    }
 
     #[test]
     pub fn test_put_withdraw_keys_and_load_them_with_token_key() -> Result<()> {
         let walletdb_path = join_config_path(&PathBuf::from("cashier_wallet_test.db"))?;
-        let wallet = CashierDb::new(&walletdb_path, "darkfi".into())?;
-        wallet.init_db()?;
+        let password: String = "darkfi".into();
+        let wallet = CashierDb::new(&walletdb_path, password.clone())?;
+        init_db(&walletdb_path, password)?;
 
         let secret2: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
         let public2 = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret2;

+ 24 - 8
src/wallet/walletdb.rs

@@ -1,7 +1,6 @@
 use super::WalletApi;
 use crate::client::ClientFailed;
-use crate::crypto::{
-    merkle::IncrementalWitness, merkle_node::MerkleNode, note::Note, OwnCoin, OwnCoins,
+use crate::crypto::{ merkle::IncrementalWitness, merkle_node::MerkleNode, note::Note, OwnCoin, OwnCoins,
 };
 use crate::serial;
 use crate::{Error, Result};
@@ -300,11 +299,26 @@ mod tests {
     use crate::util::join_config_path;
     use ff::PrimeField;
 
+    pub fn init_db(path: &PathBuf, password: String) -> Result<()> {
+        if !password.trim().is_empty() {
+            let contents = include_str!("../../sql/schema.sql");
+            let conn = Connection::open(&path)?;
+            debug!(target: "WALLETDB", "OPENED CONNECTION AT PATH {:?}", path);
+            conn.pragma_update(None, "key", &password)?;
+            conn.execute_batch(&contents)?;
+        } else {
+            debug!(target: "WALLETDB", "Password is empty. You must set a password to use the wallet.");
+            return Err(Error::from(ClientFailed::EmptyPassword));
+        }
+        Ok(())
+    }
+
     #[test]
     pub fn test_save_and_load_keypair() -> Result<()> {
         let walletdb_path = join_config_path(&PathBuf::from("test_wallet.db"))?;
-        let wallet = WalletDb::new(&walletdb_path, "darkfi".into())?;
-        wallet.init_db()?;
+        let password: String = "darkfi".into();
+        let wallet = WalletDb::new(&walletdb_path, password.clone())?;
+        init_db(&walletdb_path, password)?;
 
         let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
         let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
@@ -326,8 +340,9 @@ mod tests {
     #[test]
     pub fn test_put_and_get_own_coins() -> Result<()> {
         let walletdb_path = join_config_path(&PathBuf::from("test2_wallet.db"))?;
-        let wallet = WalletDb::new(&walletdb_path, "darkfi".into())?;
-        wallet.init_db()?;
+        let password: String = "darkfi".into();
+        let wallet = WalletDb::new(&walletdb_path, password.clone())?;
+        init_db(&walletdb_path, password)?;
 
         let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
         let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
@@ -375,8 +390,9 @@ mod tests {
     #[test]
     pub fn test_get_witnesses_and_update_them() -> Result<()> {
         let walletdb_path = join_config_path(&PathBuf::from("test3_wallet.db"))?;
-        let wallet = WalletDb::new(&walletdb_path, "darkfi".into())?;
-        wallet.init_db()?;
+        let password: String = "darkfi".into();
+        let wallet = WalletDb::new(&walletdb_path, password.clone())?;
+        init_db(&walletdb_path, password)?;
 
         let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
         let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;