Skip to content

Commit

Permalink
Add tests for SecureWalletFile trait methods
Browse files Browse the repository at this point in the history
  • Loading branch information
welf committed Nov 29, 2024
1 parent f238995 commit bddee85
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 22 deletions.
4 changes: 2 additions & 2 deletions rusk-wallet/src/wallet/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ 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
let wallet_path = WalletPath::from_str("invalid_path");

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);
Expand Down
169 changes: 149 additions & 20 deletions rusk-wallet/src/wallet/file_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 ===================================================

Expand Down Expand Up @@ -65,11 +59,6 @@ pub trait SecureWalletFile: Debug + Send + Sync + Clone {
self.path().name()
}

/// Returns current directory for this file
fn dir(&self) -> Option<PathBuf> {
self.path().dir()
}

/// Generates dir for cache based on network specified
fn cache_dir(&self) -> PathBuf {
self.path().cache_dir()
Expand Down Expand Up @@ -185,11 +174,6 @@ pub trait WalletFilePath {
Some(String::from(name))
}

/// Returns current directory for this path
fn dir(&self) -> Option<PathBuf> {
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();
Expand All @@ -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<String>,
}

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<String> {
&mut self.network
}
}

#[derive(Debug, Clone)]
struct MockSecureWalletFile {
pub path: MockWalletFilePath,
pub pwd: Vec<u8>,
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(())
}
}

0 comments on commit bddee85

Please sign in to comment.