diff --git a/rusk-wallet/src/wallet/file.rs b/rusk-wallet/src/wallet/file.rs index e5c9810bb5..d5b114cc36 100644 --- a/rusk-wallet/src/wallet/file.rs +++ b/rusk-wallet/src/wallet/file.rs @@ -208,7 +208,7 @@ mod tests { assert!( wallet_path.is_err(), - "WalletPath::try_from(the_path_is_not_a_file) should return an error" + "WalletPath::try_from should return an error when the path is not a file" ); // the path does not exist @@ -216,7 +216,7 @@ mod tests { assert!( wallet_path.is_err(), - "WalletPath::try_from(invalid_path) should return an error" + "WalletPath::try_from should return an error when the path does not exist" ); drop(file); diff --git a/rusk-wallet/src/wallet/file_service.rs b/rusk-wallet/src/wallet/file_service.rs index f460227a3e..58ebb59dc2 100644 --- a/rusk-wallet/src/wallet/file_service.rs +++ b/rusk-wallet/src/wallet/file_service.rs @@ -4,7 +4,7 @@ // // Copyright (c) DUSK NETWORK. All rights reserved. -use std::fmt::{Debug, Display}; +use std::fmt::Debug; use std::hash::Hash; use std::path::PathBuf; @@ -17,13 +17,7 @@ use crate::Error; /// Provides access to a secure wallet file pub trait SecureWalletFile: Debug + Send + Sync + Clone { /// The type of the path buffer wrapper - type PathBufWrapper: WalletFilePath - + Hash - + Eq - + PartialEq - + Debug - + Display - + Clone; + type PathBufWrapper: WalletFilePath + Hash + Eq + PartialEq + Debug + Clone; // Methods to implement =================================================== @@ -65,11 +59,6 @@ pub trait SecureWalletFile: Debug + Send + Sync + Clone { self.path().name() } - /// Returns current directory for this file - fn dir(&self) -> Option { - self.path().dir() - } - /// Generates dir for cache based on network specified fn cache_dir(&self) -> PathBuf { self.path().cache_dir() @@ -185,11 +174,6 @@ pub trait WalletFilePath { Some(String::from(name)) } - /// Returns current directory for this path - fn dir(&self) -> Option { - self.wallet_path().parent().map(PathBuf::from) - } - /// Generates dir for cache based on network specified fn cache_dir(&self) -> PathBuf { let mut cache = self.profile_dir().clone(); @@ -207,6 +191,151 @@ pub trait WalletFilePath { #[cfg(test)] mod tests { use super::*; - use std::fs::File; - use tempfile::tempdir; + + #[derive(PartialEq, Eq, Hash, Debug, Clone)] + struct MockWalletFilePath { + pub wallet_path: PathBuf, + pub profile_dir: PathBuf, + pub network: Option, + } + + impl WalletFilePath for MockWalletFilePath { + fn wallet_path(&self) -> &PathBuf { + &self.wallet_path + } + + fn wallet_path_mut(&mut self) -> &mut PathBuf { + &mut self.wallet_path + } + + fn profile_dir(&self) -> &PathBuf { + &self.profile_dir + } + + fn network(&self) -> Option<&String> { + self.network.as_ref() + } + + fn network_mut(&mut self) -> &mut Option { + &mut self.network + } + } + + #[derive(Debug, Clone)] + struct MockSecureWalletFile { + pub path: MockWalletFilePath, + pub pwd: Vec, + pub version: DatFileVersion, + } + + impl SecureWalletFile for MockSecureWalletFile { + type PathBufWrapper = MockWalletFilePath; + + fn path(&self) -> &Self::PathBufWrapper { + &self.path + } + + fn path_mut(&mut self) -> &mut Self::PathBufWrapper { + &mut self.path + } + + fn pwd(&self) -> &[u8] { + &self.pwd + } + + fn version(&self) -> DatFileVersion { + self.version.clone() + } + } + + #[test] + fn test_secure_wallet_file_trait_methods() -> Result<(), Error> { + let file_path = PathBuf::from("wallet.dat"); + let profile_dir = PathBuf::from("profile"); + let network = Some("devnet".to_string()); + + let pwd = vec![1, 2, 3, 4]; + let version = DatFileVersion::RuskBinaryFileFormat((1, 0, 0, 0, false)); + + let wallet_path = MockWalletFilePath { + wallet_path: file_path.clone(), + profile_dir: profile_dir.clone(), + network: network.clone(), + }; + + let mut wallet_file = MockSecureWalletFile { + path: wallet_path.clone(), + pwd: pwd.clone(), + version: version.clone(), + }; + + assert_eq!( + wallet_file.wallet_path(), + wallet_path.wallet_path(), + "wallet path is not correct for SecureWalletFile" + ); + assert_eq!( + wallet_file.profile_dir(), + &profile_dir, + "profile dir is not correct for SecureWalletFile" + ); + assert_eq!( + wallet_file.network(), + network.as_ref(), + "network is not correct for SecureWalletFile" + ); + + let network = Some("testnet".to_string()); + + wallet_file.set_network_name(network.clone()); + + assert_eq!( + wallet_file.network(), + network.as_ref(), + "network is not correct for SecureWalletFile after set_network_name" + ); + + assert_eq!( + wallet_file.name(), + Some("wallet".to_string()), + "name is not correct for SecureWalletFile" + ); + + assert_eq!( + wallet_file.cache_dir(), + PathBuf::from("profile/cache_testnet"), + "cache_dir is not correct for SecureWalletFile" + ); + + assert!( + !wallet_file.is_old(), + "is_old is not correct for SecureWalletFile" + ); + + let old_file = MockSecureWalletFile { + path: wallet_path.clone(), + pwd: pwd.clone(), + version: DatFileVersion::Legacy, + }; + + assert!( + old_file.is_old(), + "is_old is not correct for SecureWalletFile with old file" + ); + + let another_old_file = MockSecureWalletFile { + path: wallet_path.clone(), + pwd: pwd.clone(), + version: DatFileVersion::OldWalletCli((1, 0, 0, 0, false)), + }; + + assert!( + another_old_file.is_old(), + "is_old is not correct for SecureWalletFile with another old file" + ); + + // TODO: test get_seed_and_address + + Ok(()) + } }