diff --git a/Cargo.toml b/Cargo.toml index effa2b4..57016e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,11 @@ serde-std = ["serde/std"] unstable = [] # for benchmarking [dependencies] +# Only enable this if you explicitly do not want to use "std", otherwise enable "serde-std". serde = { version = "1.0", default-features = false, optional = true } +# Can only be used with "std" enabled. schemars = { version = "<=0.8.3", optional = true } +# Only enable this if you explicitly do not want to use an allocator, otherwise enable "alloc". core2 = { version = "0.3.0", optional = true, default_features = false } [dev-dependencies] diff --git a/contrib/test.sh b/contrib/test.sh index 119093a..df045a2 100755 --- a/contrib/test.sh +++ b/contrib/test.sh @@ -1,5 +1,6 @@ #!/bin/sh -ex +# TODO: Add core2 here once we bump MSRV past 1.29 FEATURES="serde serde-std std" if [ "$DO_ALLOC_TESTS" = true ]; then @@ -28,21 +29,22 @@ if [ "$DO_FEATURE_MATRIX" = true ]; then # All features cargo build --all --no-default-features --features="$FEATURES" - cargo test --all --features="$FEATURES" + cargo test --all --no-default-features --features="$FEATURES" # Single features for feature in ${FEATURES} do cargo build --all --no-default-features --features="$feature" - cargo test --all --features="$feature" + cargo test --all --no-default-features --features="$feature" # All combos of two features for featuretwo in ${FEATURES}; do cargo build --all --no-default-features --features="$feature $featuretwo" - cargo test --all --features="$feature $featuretwo" + cargo test --all --no-default-features --features="$feature $featuretwo" done done # Other combos - cargo test --all --features="serde-std" + # TODO: Add this test once we bump MSRV past 1.29 + # cargo test --all --no-default-features --features="std,schemars" fi if [ "$DO_SCHEMARS_TESTS" = true ]; then @@ -69,11 +71,11 @@ if [ "$DO_ASAN" = true ]; then CC='clang -fsanitize=address -fno-omit-frame-pointer' \ RUSTFLAGS='-Zsanitizer=address -Clinker=clang -Cforce-frame-pointers=yes' \ ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' \ - cargo test --lib --all --features="$FEATURES" -Zbuild-std --target x86_64-unknown-linux-gnu + cargo test --lib --all --no-default-features --features="$FEATURES" -Zbuild-std --target x86_64-unknown-linux-gnu cargo clean CC='clang -fsanitize=memory -fno-omit-frame-pointer' \ RUSTFLAGS='-Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes' \ - cargo test --lib --all --features="$FEATURES" -Zbuild-std --target x86_64-unknown-linux-gnu + cargo test --lib --all --no-default-features --features="$FEATURES" -Zbuild-std --target x86_64-unknown-linux-gnu fi # Bench diff --git a/src/hash160.rs b/src/hash160.rs index 368d097..137fc45 100644 --- a/src/hash160.rs +++ b/src/hash160.rs @@ -104,20 +104,20 @@ impl HashTrait for Hash { #[cfg(test)] mod tests { - use hash160; - use hex::{FromHex, ToHex}; - use Hash; - use HashEngine; - - #[derive(Clone)] - struct Test { - input: Vec, - output: Vec, - output_str: &'static str, - } - #[test] + #[cfg(any(feature = "std", feature = "alloc"))] fn test() { + use {hash160, Hash, HashEngine}; + use hex::{FromHex, ToHex}; + + #[derive(Clone)] + #[cfg(any(feature = "std", feature = "alloc"))] + struct Test { + input: Vec, + output: Vec, + output_str: &'static str, + } + let tests = vec![ // Uncompressed pubkey obtained from Bitcoin key; data from validateaddress Test { @@ -161,8 +161,8 @@ mod tests { #[cfg(feature = "serde")] #[test] fn ripemd_serde() { - use serde_test::{Configure, Token, assert_tokens}; + use {hash160, Hash}; static HASH_BYTES: [u8; 20] = [ 0x13, 0x20, 0x72, 0xdf, diff --git a/src/hex.rs b/src/hex.rs index e28ba3b..dc06253 100644 --- a/src/hex.rs +++ b/src/hex.rs @@ -274,6 +274,7 @@ mod tests { use core::fmt; #[test] + #[cfg(any(feature = "std", feature = "alloc"))] fn hex_roundtrip() { let expected = "0123456789abcdef"; let expected_up = "0123456789ABCDEF"; @@ -361,6 +362,7 @@ mod tests { } #[test] + #[cfg(any(feature = "std", feature = "alloc"))] fn hex_error() { let oddlen = "0123456789abcdef0"; let badchar1 = "Z123456789abcdef"; diff --git a/src/hmac.rs b/src/hmac.rs index 38c24e3..6417408 100644 --- a/src/hmac.rs +++ b/src/hmac.rs @@ -239,19 +239,18 @@ impl<'de, T: HashTrait + Deserialize<'de>> Deserialize<'de> for Hmac { #[cfg(test)] mod tests { - use sha256; - #[cfg(feature = "serde")] use sha512; - use {Hash, HashEngine, Hmac, HmacEngine}; - - #[derive(Clone)] - struct Test { - key: Vec, - input: Vec, - output: Vec, - } - #[test] + #[cfg(any(feature = "std", feature = "alloc"))] fn test() { + use {sha256, HashEngine, HmacEngine, Hash, Hmac}; + + #[derive(Clone)] + struct Test { + key: Vec, + input: Vec, + output: Vec, + } + let tests = vec![ // Test vectors copied from libsecp256k1 // Sadly the RFC2104 test vectors all use MD5 as their underlying hash function, @@ -369,6 +368,7 @@ mod tests { #[test] fn hmac_sha512_serde() { use serde_test::{Configure, Token, assert_tokens}; + use {sha512, Hash, Hmac}; static HASH_BYTES: [u8; 64] = [ 0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21, diff --git a/src/ripemd160.rs b/src/ripemd160.rs index 6e88e44..b360e91 100644 --- a/src/ripemd160.rs +++ b/src/ripemd160.rs @@ -459,20 +459,20 @@ impl HashEngine { #[cfg(test)] mod tests { - use ripemd160; - use hex::{FromHex, ToHex}; - use Hash; - use HashEngine; - - #[derive(Clone)] - struct Test { - input: &'static str, - output: Vec, - output_str: &'static str, - } - #[test] + #[cfg(any(feature = "std", feature = "alloc"))] fn test() { + use ripemd160; + use {Hash, HashEngine}; + use hex::{FromHex, ToHex}; + + #[derive(Clone)] + struct Test { + input: &'static str, + output: Vec, + output_str: &'static str, + } + let tests = vec![ // Test messages from FIPS 180-1 Test { @@ -545,6 +545,7 @@ mod tests { #[test] fn ripemd_serde() { use serde_test::{Configure, Token, assert_tokens}; + use {ripemd160, Hash}; static HASH_BYTES: [u8; 20] = [ 0x13, 0x20, 0x72, 0xdf, diff --git a/src/sha1.rs b/src/sha1.rs index 340a6f7..daebbd1 100644 --- a/src/sha1.rs +++ b/src/sha1.rs @@ -197,20 +197,20 @@ impl HashEngine { #[cfg(test)] mod tests { - use sha1; - use hex::{FromHex, ToHex}; - use Hash; - use HashEngine; - - #[derive(Clone)] - struct Test { - input: &'static str, - output: Vec, - output_str: &'static str, - } - #[test] + #[cfg(any(feature = "std", feature = "alloc"))] fn test() { + use {sha1, Hash, HashEngine}; + use hex::{FromHex, ToHex}; + + #[derive(Clone)] + struct Test { + input: &'static str, + output: Vec, + output_str: &'static str, + } + + let tests = vec![ // Examples from wikipedia Test { @@ -270,6 +270,7 @@ mod tests { #[test] fn sha1_serde() { use serde_test::{Configure, Token, assert_tokens}; + use {sha1, Hash}; static HASH_BYTES: [u8; 20] = [ 0x13, 0x20, 0x72, 0xdf, diff --git a/src/sha256.rs b/src/sha256.rs index bbc26a1..4f18a51 100644 --- a/src/sha256.rs +++ b/src/sha256.rs @@ -373,18 +373,20 @@ impl HashEngine { #[cfg(test)] mod tests { use sha256; - use hex::{FromHex, ToHex}; use {Hash, HashEngine}; - #[derive(Clone)] - struct Test { - input: &'static str, - output: Vec, - output_str: &'static str, - } - #[test] + #[cfg(any(feature = "std", feature = "alloc"))] fn test() { + use hex::{FromHex, ToHex}; + + #[derive(Clone)] + struct Test { + input: &'static str, + output: Vec, + output_str: &'static str, + } + let tests = vec![ // Examples from wikipedia Test { diff --git a/src/sha256d.rs b/src/sha256d.rs index cd9b339..6927df9 100644 --- a/src/sha256d.rs +++ b/src/sha256d.rs @@ -100,20 +100,19 @@ impl HashTrait for Hash { #[cfg(test)] mod tests { - use sha256d; - use hex::{FromHex, ToHex}; - use Hash; - use HashEngine; - - #[derive(Clone)] - struct Test { - input: &'static str, - output: Vec, - output_str: &'static str, - } - #[test] + #[cfg(any(feature = "std", feature = "alloc"))] fn test() { + use {sha256d, Hash, HashEngine}; + use hex::{FromHex, ToHex}; + + #[derive(Clone)] + struct Test { + input: &'static str, + output: Vec, + output_str: &'static str, + } + let tests = vec![ // Test vector copied out of rust-bitcoin Test { @@ -150,6 +149,7 @@ mod tests { #[test] fn sha256_serde() { use serde_test::{Configure, Token, assert_tokens}; + use {sha256d, Hash}; static HASH_BYTES: [u8; 32] = [ 0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7, diff --git a/src/sha256t.rs b/src/sha256t.rs index 128c5b2..d810b40 100644 --- a/src/sha256t.rs +++ b/src/sha256t.rs @@ -246,6 +246,7 @@ impl<'de, T: Tag> ::serde::Deserialize<'de> for Hash { #[cfg(test)] mod tests { use ::{Hash, sha256, sha256t}; + #[cfg(any(feature = "std", feature = "alloc"))] use ::hex::ToHex; const TEST_MIDSTATE: [u8; 32] = [ @@ -266,11 +267,13 @@ mod tests { } /// A hash tagged with `$name`. + #[cfg(any(feature = "std", feature = "alloc"))] pub type TestHash = sha256t::Hash; sha256t_hash_newtype!(NewTypeHash, NewTypeTag, TEST_MIDSTATE, 64, doc="test hash", true); #[test] + #[cfg(any(feature = "std", feature = "alloc"))] fn test_sha256t() { assert_eq!( TestHash::hash(&[0]).to_hex(), diff --git a/src/sha512.rs b/src/sha512.rs index eda213b..2406d5a 100644 --- a/src/sha512.rs +++ b/src/sha512.rs @@ -348,20 +348,19 @@ impl HashEngine { #[cfg(test)] mod tests { - use sha512; - use hex::{FromHex, ToHex}; - use Hash; - use HashEngine; - - #[derive(Clone)] - struct Test { - input: &'static str, - output: Vec, - output_str: &'static str, - } - #[test] + #[cfg(any(feature = "std", feature = "alloc"))] fn test() { + use {sha512, Hash, HashEngine}; + use hex::{FromHex, ToHex}; + + #[derive(Clone)] + struct Test { + input: &'static str, + output: Vec, + output_str: &'static str, + } + let tests = vec![ // Test vectors computed with `sha512sum` Test { @@ -430,6 +429,7 @@ mod tests { #[test] fn sha512_serde() { use serde_test::{Configure, Token, assert_tokens}; + use {sha512, Hash}; static HASH_BYTES: [u8; 64] = [ 0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21,