Use `hex_lit::hex` in tests

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.
This commit is contained in:
Martin Habovstiak 2024-07-25 08:35:47 +02:00
parent c4c029fca2
commit 13c5366238
5 changed files with 24 additions and 23 deletions

View File

@ -110,6 +110,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30ed443af458ccb6d81c1e7e661545f94d3176752fb1df2f543b902a1e0f51e2"
[[package]]
name = "hex_lit"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd"
[[package]]
name = "itoa"
version = "0.3.0"
@ -262,6 +268,7 @@ dependencies = [
"bincode",
"bitcoin_hashes",
"getrandom",
"hex_lit",
"rand",
"rand_core",
"secp256k1-sys",

View File

@ -86,6 +86,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30ed443af458ccb6d81c1e7e661545f94d3176752fb1df2f543b902a1e0f51e2"
[[package]]
name = "hex_lit"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3011d1213f159867b13cfd6ac92d2cd5f1345762c63be3554e84092d85a50bbd"
[[package]]
name = "js-sys"
version = "0.3.61"
@ -183,6 +189,7 @@ dependencies = [
"bincode",
"bitcoin_hashes",
"getrandom",
"hex_lit",
"rand",
"rand_core",
"secp256k1-sys",

View File

@ -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"

View File

@ -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};
@ -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]);

View File

@ -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
@ -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\