From cc006ed07e6b48d7e4f18f2a30b1ea3fc0e8d784 Mon Sep 17 00:00:00 2001 From: Leonardo Nerone Date: Wed, 13 Jul 2022 01:31:46 -0400 Subject: [PATCH] test(utils): add unit tests for utils.rs (HashType and serde) (#59) * test(utils): add unit tests for utils.rs (HashType and serde) * docs(CHANGELOG): update changelog to reflect new tests in utils.rs --- CHANGELOG.md | 5 +++ Cargo.toml | 2 + src/util.rs | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 442d138..d8f7f92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add tests for types implementing `HashType` in `utils.rs` ([#59](https://github.com/monero-ecosystem/monero-rpc-rs/pull/59)) +- Add tests for `HashString`'s implementation of the traits `Debug`, `Serialize`, and `Deserialize`, in `utils.rs` ([#59](https://github.com/monero-ecosystem/monero-rpc-rs/pull/59)) + ### Removed - Remove `SubAddressIndex` from `src/models.rs` ([#55](https://github.com/monero-ecosystem/monero-rpc-rs/pull/55)) diff --git a/Cargo.toml b/Cargo.toml index a10e097..a5e05a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,4 +27,6 @@ uuid = { version = "1.1", features = ["v4"] } [dev-dependencies] # Async rand = "0.8.4" +rustc-hex = "2.1" +serde_test = "1.0" tokio = { version = "1.12.0", features = ["full"] } diff --git a/src/util.rs b/src/util.rs index 704d524..313c601 100644 --- a/src/util.rs +++ b/src/util.rs @@ -49,7 +49,7 @@ impl HashType for Vec { } /// Wrapper type to help serializating types through string. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub struct HashString(pub T); impl Display for HashString @@ -85,3 +85,103 @@ where Ok(Self(T::from_str(&s).map_err(serde::de::Error::custom)?)) } } + +#[cfg(test)] +mod tests { + use super::*; + use serde_test::{assert_tokens, Token}; + + #[test] + fn trait_hash_type_for_payment_id() { + use monero::util::address::PaymentId; + + let payment_id = PaymentId([0, 1, 2, 3, 4, 5, 6, 7]); + + assert_eq!(payment_id.bytes(), &[0, 1, 2, 3, 4, 5, 6, 7]); + + assert!(::from_str("") + .unwrap_err() + .is::()); + assert!(::from_str("0x01234567") + .unwrap_err() + .is::()); + assert!(::from_str("0xgg") + .unwrap_err() + .is::()); + + assert_eq!( + ::from_str("0x0001020304050607").unwrap(), + payment_id + ); + assert_eq!( + ::from_str("0001020304050607").unwrap(), + payment_id + ); + } + + #[test] + fn trait_hash_type_for_cryptonote_hash() { + use monero::cryptonote::hash::Hash; + + let hash = Hash([250; 32]); + + assert_eq!(hash.bytes(), [250; 32].as_slice()); + + assert!(::from_str("") + .unwrap_err() + .is::()); + assert!(::from_str("0x01234567") + .unwrap_err() + .is::()); + assert!(::from_str("0xgg") + .unwrap_err() + .is::()); + + let hash_str = "fa".repeat(32); + assert_eq!(::from_str(&hash_str).unwrap(), hash); + + let hash_str = format!("0x{}", hash_str); + assert_eq!(::from_str(&hash_str).unwrap(), hash); + } + + #[test] + fn trait_hash_type_for_vec_u8() { + let vec_non_empty = vec![0, 1, 2, 3, 4]; + + assert_eq!(vec_non_empty.bytes(), &[0, 1, 2, 3, 4]); + + assert_eq!( + as HashType>::from_str("").unwrap(), + Vec::::new() + ); + assert!( as HashType>::from_str("0x01234567") + .unwrap_err() + .is::()); + assert!( as HashType>::from_str("0xgg") + .unwrap_err() + .is::()); + + assert!( as HashType>::from_str("0x0001020304") + .unwrap_err() + .is::()); + assert_eq!( + as HashType>::from_str("0001020304").unwrap(), + vec_non_empty + ); + } + + #[test] + fn display_for_hash_string() { + let vec = vec![0, 1, 2, 3, 4]; + let hash_string = HashString(vec); + assert_eq!(hash_string.to_string(), "0001020304"); + } + + #[test] + fn se_de_for_hash_string() { + let vec = vec![0, 1, 2, 3, 4]; + let hash_string = HashString(vec); + + assert_tokens(&hash_string, &[Token::Str("0001020304")]); + } +}