Skip to content

Commit

Permalink
test(utils): add unit tests for utils.rs (HashType and serde) (#59)
Browse files Browse the repository at this point in the history
* test(utils): add unit tests for utils.rs (HashType and serde)

* docs(CHANGELOG): update changelog to reflect new tests in utils.rs
  • Loading branch information
LeoNero authored Jul 13, 2022
1 parent faec014 commit cc006ed
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
102 changes: 101 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl HashType for Vec<u8> {
}

/// Wrapper type to help serializating types through string.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct HashString<T>(pub T);

impl<T> Display for HashString<T>
Expand Down Expand Up @@ -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!(<PaymentId as HashType>::from_str("")
.unwrap_err()
.is::<rustc_hex::FromHexError>());
assert!(<PaymentId as HashType>::from_str("0x01234567")
.unwrap_err()
.is::<rustc_hex::FromHexError>());
assert!(<PaymentId as HashType>::from_str("0xgg")
.unwrap_err()
.is::<rustc_hex::FromHexError>());

assert_eq!(
<PaymentId as HashType>::from_str("0x0001020304050607").unwrap(),
payment_id
);
assert_eq!(
<PaymentId as HashType>::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!(<Hash as HashType>::from_str("")
.unwrap_err()
.is::<rustc_hex::FromHexError>());
assert!(<Hash as HashType>::from_str("0x01234567")
.unwrap_err()
.is::<rustc_hex::FromHexError>());
assert!(<Hash as HashType>::from_str("0xgg")
.unwrap_err()
.is::<rustc_hex::FromHexError>());

let hash_str = "fa".repeat(32);
assert_eq!(<Hash as HashType>::from_str(&hash_str).unwrap(), hash);

let hash_str = format!("0x{}", hash_str);
assert_eq!(<Hash as HashType>::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!(
<Vec<u8> as HashType>::from_str("").unwrap(),
Vec::<u8>::new()
);
assert!(<Vec<u8> as HashType>::from_str("0x01234567")
.unwrap_err()
.is::<hex::FromHexError>());
assert!(<Vec<u8> as HashType>::from_str("0xgg")
.unwrap_err()
.is::<hex::FromHexError>());

assert!(<Vec<u8> as HashType>::from_str("0x0001020304")
.unwrap_err()
.is::<hex::FromHexError>());
assert_eq!(
<Vec<u8> 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")]);
}
}

0 comments on commit cc006ed

Please sign in to comment.