don't enable std by default when testing
- make tests no_std compatible by adding imports to alloc or std - feature gate tests behind the 'alloc' feature if they use anything from 'alloc' (like the `format!` macro) - schemars feature enables alloc
This commit is contained in:
parent
e83830dcfc
commit
a14cdaf859
|
@ -17,6 +17,7 @@ exclude = ["tests", "contrib"]
|
||||||
default = ["std"]
|
default = ["std"]
|
||||||
std = ["alloc", "hex/std", "bitcoin-io/std"]
|
std = ["alloc", "hex/std", "bitcoin-io/std"]
|
||||||
alloc = ["hex/alloc"]
|
alloc = ["hex/alloc"]
|
||||||
|
schemars = ["dep:schemars", "alloc"]
|
||||||
# If you want I/O you must enable either "std" or "io".
|
# If you want I/O you must enable either "std" or "io".
|
||||||
io = ["bitcoin-io"]
|
io = ["bitcoin-io"]
|
||||||
# Smaller (but slower) implementation of sha256, sha512 and ripemd160
|
# Smaller (but slower) implementation of sha256, sha512 and ripemd160
|
||||||
|
|
|
@ -51,6 +51,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn test() {
|
fn test() {
|
||||||
|
use alloc::string::ToString;
|
||||||
|
|
||||||
use super::Hash;
|
use super::Hash;
|
||||||
use crate::{hash160, HashEngine};
|
use crate::{hash160, HashEngine};
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
//! Implementation based on RFC5869, but the interface is scoped
|
//! Implementation based on RFC5869, but the interface is scoped
|
||||||
//! to BIP324's requirements.
|
//! to BIP324's requirements.
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
#[cfg(feature = "alloc")]
|
||||||
use alloc::vec;
|
use alloc::vec;
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
#[cfg(feature = "alloc")]
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ pub struct Hmac<T: GeneralHash>(T);
|
||||||
impl<T: GeneralHash + schemars::JsonSchema> schemars::JsonSchema for Hmac<T> {
|
impl<T: GeneralHash + schemars::JsonSchema> schemars::JsonSchema for Hmac<T> {
|
||||||
fn is_referenceable() -> bool { <T as schemars::JsonSchema>::is_referenceable() }
|
fn is_referenceable() -> bool { <T as schemars::JsonSchema>::is_referenceable() }
|
||||||
|
|
||||||
fn schema_name() -> std::string::String { <T as schemars::JsonSchema>::schema_name() }
|
fn schema_name() -> alloc::string::String { <T as schemars::JsonSchema>::schema_name() }
|
||||||
|
|
||||||
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
||||||
<T as schemars::JsonSchema>::json_schema(gen)
|
<T as schemars::JsonSchema>::json_schema(gen)
|
||||||
|
|
|
@ -81,8 +81,10 @@ impl_write!(
|
||||||
T: crate::GeneralHash
|
T: crate::GeneralHash
|
||||||
);
|
);
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(all(test, feature = "alloc"))] // right now every test here depends on `alloc`
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use alloc::format;
|
||||||
|
|
||||||
use bitcoin_io::Write;
|
use bitcoin_io::Write;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|
|
@ -224,9 +224,17 @@ macro_rules! hash_type_no_default {
|
||||||
|
|
||||||
#[cfg(feature = "schemars")]
|
#[cfg(feature = "schemars")]
|
||||||
impl schemars::JsonSchema for Hash {
|
impl schemars::JsonSchema for Hash {
|
||||||
fn schema_name() -> String { "Hash".to_owned() }
|
fn schema_name() -> alloc::string::String {
|
||||||
|
use alloc::borrow::ToOwned;
|
||||||
|
|
||||||
|
"Hash".to_owned()
|
||||||
|
}
|
||||||
|
|
||||||
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
||||||
|
use alloc::borrow::ToOwned;
|
||||||
|
use alloc::boxed::Box;
|
||||||
|
use alloc::string::String;
|
||||||
|
|
||||||
let len = $bits / 8;
|
let len = $bits / 8;
|
||||||
let mut schema: schemars::schema::SchemaObject = <String>::json_schema(gen).into();
|
let mut schema: schemars::schema::SchemaObject = <String>::json_schema(gen).into();
|
||||||
schema.string = Some(Box::new(schemars::schema::StringValidation {
|
schema.string = Some(Box::new(schemars::schema::StringValidation {
|
||||||
|
|
|
@ -62,7 +62,7 @@
|
||||||
//! # fn main() {}
|
//! # fn main() {}
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
// Experimental features we need.
|
// Experimental features we need.
|
||||||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||||
#![cfg_attr(bench, feature(test))]
|
#![cfg_attr(bench, feature(test))]
|
||||||
|
@ -75,9 +75,9 @@
|
||||||
#![allow(clippy::manual_range_contains)] // More readable than clippy's format.
|
#![allow(clippy::manual_range_contains)] // More readable than clippy's format.
|
||||||
#![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454
|
#![allow(clippy::needless_borrows_for_generic_args)] // https://github.com/rust-lang/rust-clippy/issues/12454
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
#[cfg(feature = "alloc")]
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
#[cfg(any(test, feature = "std"))]
|
|
||||||
extern crate core;
|
extern crate core;
|
||||||
|
|
||||||
#[cfg(feature = "bitcoin-io")]
|
#[cfg(feature = "bitcoin-io")]
|
||||||
|
@ -334,16 +334,19 @@ mod tests {
|
||||||
struct TestNewtype2(sha256d::Hash);
|
struct TestNewtype2(sha256d::Hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rustfmt::skip]
|
|
||||||
const DUMMY: TestNewtype = TestNewtype::from_byte_array([
|
|
||||||
0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89,
|
|
||||||
0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79, 0x8a,
|
|
||||||
0x14, 0x25, 0x36, 0x47, 0x58, 0x69, 0x7a, 0x8b,
|
|
||||||
0x15, 0x26, 0x37, 0x48, 0x59, 0x6a, 0x7b, 0x8c,
|
|
||||||
]);
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
fn newtype_fmt_roundtrip() {
|
fn newtype_fmt_roundtrip() {
|
||||||
|
use alloc::format;
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
const DUMMY: TestNewtype = TestNewtype::from_byte_array([
|
||||||
|
0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89,
|
||||||
|
0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79, 0x8a,
|
||||||
|
0x14, 0x25, 0x36, 0x47, 0x58, 0x69, 0x7a, 0x8b,
|
||||||
|
0x15, 0x26, 0x37, 0x48, 0x59, 0x6a, 0x7b, 0x8c,
|
||||||
|
]);
|
||||||
|
|
||||||
let orig = DUMMY;
|
let orig = DUMMY;
|
||||||
let hex = format!("{}", orig);
|
let hex = format!("{}", orig);
|
||||||
let rinsed = hex.parse::<TestNewtype>().expect("failed to parse hex");
|
let rinsed = hex.parse::<TestNewtype>().expect("failed to parse hex");
|
||||||
|
|
|
@ -413,6 +413,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn test() {
|
fn test() {
|
||||||
|
use alloc::string::ToString;
|
||||||
|
|
||||||
use crate::{ripemd160, HashEngine};
|
use crate::{ripemd160, HashEngine};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
|
@ -132,6 +132,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn test() {
|
fn test() {
|
||||||
|
use alloc::string::ToString;
|
||||||
|
|
||||||
use crate::{sha1, HashEngine};
|
use crate::{sha1, HashEngine};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
|
@ -864,12 +864,16 @@ impl HashEngine {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use core::array;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{sha256, HashEngine};
|
use crate::{sha256, HashEngine};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn test() {
|
fn test() {
|
||||||
|
use alloc::string::ToString;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Test {
|
struct Test {
|
||||||
input: &'static str,
|
input: &'static str,
|
||||||
|
@ -931,7 +935,10 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
fn fmt_roundtrips() {
|
fn fmt_roundtrips() {
|
||||||
|
use alloc::format;
|
||||||
|
|
||||||
let hash = sha256::Hash::hash(b"some arbitrary bytes");
|
let hash = sha256::Hash::hash(b"some arbitrary bytes");
|
||||||
let hex = format!("{}", hash);
|
let hex = format!("{}", hash);
|
||||||
let rinsed = hex.parse::<sha256::Hash>().expect("failed to parse hex");
|
let rinsed = hex.parse::<sha256::Hash>().expect("failed to parse hex");
|
||||||
|
@ -1023,14 +1030,13 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn hash_unoptimized() {
|
fn hash_unoptimized() {
|
||||||
assert_eq!(Hash::hash(&[]), Hash::hash_unoptimized(&[]));
|
let bytes: [u8; 256] = array::from_fn(|i| i as u8);
|
||||||
|
|
||||||
let mut bytes = Vec::new();
|
for i in 0..=256 {
|
||||||
for i in 0..256 {
|
let bytes = &bytes[0..i];
|
||||||
bytes.push(i as u8);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Hash::hash(&bytes),
|
Hash::hash(bytes),
|
||||||
Hash::hash_unoptimized(&bytes),
|
Hash::hash_unoptimized(bytes),
|
||||||
"hashes don't match for length {}",
|
"hashes don't match for length {}",
|
||||||
i + 1
|
i + 1
|
||||||
);
|
);
|
||||||
|
@ -1050,7 +1056,10 @@ mod tests {
|
||||||
fn const_midstate() { assert_eq!(Midstate::hash_tag(b"TapLeaf"), TAP_LEAF_MIDSTATE,) }
|
fn const_midstate() { assert_eq!(Midstate::hash_tag(b"TapLeaf"), TAP_LEAF_MIDSTATE,) }
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
fn regression_midstate_debug_format() {
|
fn regression_midstate_debug_format() {
|
||||||
|
use alloc::format;
|
||||||
|
|
||||||
let want = "Midstate { bytes: 9ce0e4e67c116c3938b3caf2c30f5089d3f3936c47636e607db33eeaddc6f0c9, length: 64 }";
|
let want = "Midstate { bytes: 9ce0e4e67c116c3938b3caf2c30f5089d3f3936c47636e607db33eeaddc6f0c9, length: 64 }";
|
||||||
let got = format!("{:?}", TAP_LEAF_MIDSTATE);
|
let got = format!("{:?}", TAP_LEAF_MIDSTATE);
|
||||||
assert_eq!(got, want);
|
assert_eq!(got, want);
|
||||||
|
|
|
@ -43,11 +43,14 @@ fn from_engine(e: HashEngine) -> Hash {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
#[allow(unused_imports)] // whether this is used depends on features
|
||||||
use crate::sha256d;
|
use crate::sha256d;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn test() {
|
fn test() {
|
||||||
|
use alloc::string::ToString;
|
||||||
|
|
||||||
use crate::{sha256, HashEngine};
|
use crate::{sha256, HashEngine};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
@ -97,7 +100,10 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
fn fmt_roundtrips() {
|
fn fmt_roundtrips() {
|
||||||
|
use alloc::format;
|
||||||
|
|
||||||
let hash = sha256d::Hash::hash(b"some arbitrary bytes");
|
let hash = sha256d::Hash::hash(b"some arbitrary bytes");
|
||||||
let hex = format!("{}", hash);
|
let hex = format!("{}", hash);
|
||||||
let rinsed = hex.parse::<sha256d::Hash>().expect("failed to parse hex");
|
let rinsed = hex.parse::<sha256d::Hash>().expect("failed to parse hex");
|
||||||
|
|
|
@ -23,9 +23,17 @@ pub struct Hash<T>([u8; 32], PhantomData<T>);
|
||||||
|
|
||||||
#[cfg(feature = "schemars")]
|
#[cfg(feature = "schemars")]
|
||||||
impl<T: Tag> schemars::JsonSchema for Hash<T> {
|
impl<T: Tag> schemars::JsonSchema for Hash<T> {
|
||||||
fn schema_name() -> String { "Hash".to_owned() }
|
fn schema_name() -> alloc::string::String {
|
||||||
|
use alloc::borrow::ToOwned;
|
||||||
|
|
||||||
|
"Hash".to_owned()
|
||||||
|
}
|
||||||
|
|
||||||
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
||||||
|
use alloc::borrow::ToOwned;
|
||||||
|
use alloc::boxed::Box;
|
||||||
|
use alloc::string::String;
|
||||||
|
|
||||||
let mut schema: schemars::schema::SchemaObject = <String>::json_schema(gen).into();
|
let mut schema: schemars::schema::SchemaObject = <String>::json_schema(gen).into();
|
||||||
schema.string = Some(Box::new(schemars::schema::StringValidation {
|
schema.string = Some(Box::new(schemars::schema::StringValidation {
|
||||||
max_length: Some(32 * 2),
|
max_length: Some(32 * 2),
|
||||||
|
@ -353,6 +361,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn manually_created_sha256t_hash_type() {
|
fn manually_created_sha256t_hash_type() {
|
||||||
|
use alloc::string::ToString;
|
||||||
|
|
||||||
assert_eq!(TestHash::hash(&[0]).to_string(), HASH_ZERO_FORWARD);
|
assert_eq!(TestHash::hash(&[0]).to_string(), HASH_ZERO_FORWARD);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -370,6 +380,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn macro_created_sha256t_hash_type_backward() {
|
fn macro_created_sha256t_hash_type_backward() {
|
||||||
|
use alloc::string::ToString;
|
||||||
|
|
||||||
let inner = sha256t::Hash::<NewTypeTagBackward>::hash(&[0]);
|
let inner = sha256t::Hash::<NewTypeTagBackward>::hash(&[0]);
|
||||||
let hash = NewTypeHashBackward::from_byte_array(inner.to_byte_array());
|
let hash = NewTypeHashBackward::from_byte_array(inner.to_byte_array());
|
||||||
assert_eq!(hash.to_string(), HASH_ZERO_BACKWARD);
|
assert_eq!(hash.to_string(), HASH_ZERO_BACKWARD);
|
||||||
|
@ -391,6 +403,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn macro_created_sha256t_hash_type_prints_forward() {
|
fn macro_created_sha256t_hash_type_prints_forward() {
|
||||||
|
use alloc::string::ToString;
|
||||||
|
|
||||||
let inner = sha256t::Hash::<NewTypeTagForward>::hash(&[0]);
|
let inner = sha256t::Hash::<NewTypeTagForward>::hash(&[0]);
|
||||||
let hash = NewTypeHashForward::from_byte_array(inner.to_byte_array());
|
let hash = NewTypeHashForward::from_byte_array(inner.to_byte_array());
|
||||||
assert_eq!(hash.to_string(), HASH_ZERO_FORWARD);
|
assert_eq!(hash.to_string(), HASH_ZERO_FORWARD);
|
||||||
|
|
|
@ -45,6 +45,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn test() {
|
fn test() {
|
||||||
|
use alloc::string::ToString;
|
||||||
|
|
||||||
use crate::{sha384, HashEngine};
|
use crate::{sha384, HashEngine};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
|
@ -310,6 +310,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn test() {
|
fn test() {
|
||||||
|
use alloc::string::ToString;
|
||||||
|
|
||||||
use crate::{sha512, HashEngine};
|
use crate::{sha512, HashEngine};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
|
@ -55,6 +55,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
fn test() {
|
fn test() {
|
||||||
|
use alloc::string::ToString;
|
||||||
|
|
||||||
use crate::{sha512_256, HashEngine};
|
use crate::{sha512_256, HashEngine};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
|
|
@ -400,28 +400,40 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
fn display() {
|
fn display() {
|
||||||
|
use alloc::format;
|
||||||
|
|
||||||
let want = "0000000000000000000000000000000000000000000000000000000000000000";
|
let want = "0000000000000000000000000000000000000000000000000000000000000000";
|
||||||
let got = format!("{}", TestHash::all_zeros());
|
let got = format!("{}", TestHash::all_zeros());
|
||||||
assert_eq!(got, want)
|
assert_eq!(got, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
fn display_alternate() {
|
fn display_alternate() {
|
||||||
|
use alloc::format;
|
||||||
|
|
||||||
let want = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
let want = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
||||||
let got = format!("{:#}", TestHash::all_zeros());
|
let got = format!("{:#}", TestHash::all_zeros());
|
||||||
assert_eq!(got, want)
|
assert_eq!(got, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
fn lower_hex() {
|
fn lower_hex() {
|
||||||
|
use alloc::format;
|
||||||
|
|
||||||
let want = "0000000000000000000000000000000000000000000000000000000000000000";
|
let want = "0000000000000000000000000000000000000000000000000000000000000000";
|
||||||
let got = format!("{:x}", TestHash::all_zeros());
|
let got = format!("{:x}", TestHash::all_zeros());
|
||||||
assert_eq!(got, want)
|
assert_eq!(got, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
fn lower_hex_alternate() {
|
fn lower_hex_alternate() {
|
||||||
|
use alloc::format;
|
||||||
|
|
||||||
let want = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
let want = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
||||||
let got = format!("{:#x}", TestHash::all_zeros());
|
let got = format!("{:#x}", TestHash::all_zeros());
|
||||||
assert_eq!(got, want)
|
assert_eq!(got, want)
|
||||||
|
|
Loading…
Reference in New Issue