Skip to content

Commit

Permalink
Use hex_lit::hex in tests
Browse files Browse the repository at this point in the history
The tests defined custom `hex!` macros (yes, two actually) that
evaluated to `Vec<u8>`. While the performance didn't matter it made it
harder to use with interfaces that require arrays and all current uses
were passing it as slices anyway.

So, in preparation for upcoming changes, this commit introduces
`hex_lit` dev-dependency which evaluates to array allowing better
interaction with type checker.
  • Loading branch information
Kixunil committed Jul 25, 2024
1 parent 55c2efc commit e8c3a51
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ rand_core = "0.6"
serde_cbor = "0.10.0"
serde_test = "1.0.19"
bincode = "1.3.3"
hex_lit = "0.1.1"

[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.3"
Expand Down
11 changes: 2 additions & 9 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,8 @@ impl<'de> serde::Deserialize<'de> for XOnlyPublicKey {
mod test {
use core::str::FromStr;

#[cfg(not(secp256k1_fuzz))]
use hex_lit::hex;
#[cfg(feature = "rand")]
use rand::{self, rngs::mock::StepRng, RngCore};
use serde_test::{Configure, Token};
Expand All @@ -1537,15 +1539,6 @@ mod test {
use crate::Error::{InvalidPublicKey, InvalidSecretKey};
use crate::{constants, from_hex, to_hex, Scalar};

#[cfg(not(secp256k1_fuzz))]
macro_rules! hex {
($hex:expr) => {{
let mut result = vec![0; $hex.len() / 2];
from_hex($hex, &mut result).expect("valid hex string");
result
}};
}

#[test]
fn skey_from_slice() {
let sk = SecretKey::from_slice(&[1; 31]);
Expand Down
21 changes: 7 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,19 +508,12 @@ pub(crate) fn random_32_bytes<R: rand::Rng + ?Sized>(rng: &mut R) -> [u8; 32] {
mod tests {
use std::str::FromStr;

use hex_lit::hex;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;

use super::*;

macro_rules! hex {
($hex:expr) => {{
let mut result = vec![0; $hex.len() / 2];
from_hex($hex, &mut result).expect("valid hex string");
result
}};
}

#[test]
#[cfg(all(feature = "rand", feature = "std"))]
// In rustc 1.72 this Clippy lint was pulled out of clippy and into rustc, and
Expand Down Expand Up @@ -678,17 +671,17 @@ mod tests {

#[test]
fn signature_display() {
let hex_str = "3046022100839c1fbc5304de944f697c9f4b1d01d1faeba32d751c0f7acb21ac8a0f436a72022100e89bd46bb3a5a62adc679f659b7ce876d83ee297c7a5587b2011c4fcc72eab45";
let byte_str = hex!(hex_str);
const HEX_STR: &str = "3046022100839c1fbc5304de944f697c9f4b1d01d1faeba32d751c0f7acb21ac8a0f436a72022100e89bd46bb3a5a62adc679f659b7ce876d83ee297c7a5587b2011c4fcc72eab45";
let byte_str = hex!(HEX_STR);

assert_eq!(
ecdsa::Signature::from_der(&byte_str).expect("byte str decode"),
ecdsa::Signature::from_str(hex_str).expect("byte str decode")
ecdsa::Signature::from_str(HEX_STR).expect("byte str decode")
);

let sig = ecdsa::Signature::from_str(hex_str).expect("byte str decode");
assert_eq!(&sig.to_string(), hex_str);
assert_eq!(&format!("{:?}", sig), hex_str);
let sig = ecdsa::Signature::from_str(HEX_STR).expect("byte str decode");
assert_eq!(&sig.to_string(), HEX_STR);
assert_eq!(&format!("{:?}", sig), HEX_STR);

assert!(ecdsa::Signature::from_str(
"3046022100839c1fbc5304de944f697c9f4b1d01d1faeba32d751c0f7acb21ac8a0f436a\
Expand Down

0 comments on commit e8c3a51

Please sign in to comment.