diff --git a/.github/workflows/test-suite.yml b/.github/workflows/test-suite.yml index 72945a3..81359db 100644 --- a/.github/workflows/test-suite.yml +++ b/.github/workflows/test-suite.yml @@ -57,8 +57,9 @@ jobs: - name: Check code coverage with cargo-tarpaulin (all features) run: cargo-tarpaulin --workspace --out xml --skip-clean --all-features - name: Upload to codecov.io - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v5 + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} with: - fail_ci_if_error: true - token: ${{ secrets.CODECOV_TOKEN }} + fail_ci_if_error: false informational: true diff --git a/src/lib.rs b/src/lib.rs index a41443b..a139d3f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,9 @@ //! Now this crate serves primarily as a wrapper over two SHA256 crates: `sha2` and `ring` – which //! it switches between at runtime based on the availability of SHA intrinsics. +#[cfg(test)] +mod tests; + mod sha2_impl; pub use self::DynamicContext as Context; @@ -241,33 +244,3 @@ pub static ZERO_HASHES: LazyLock> = LazyLock::new(|| { hashes }); - -#[cfg(test)] -mod tests { - use super::*; - use rustc_hex::FromHex; - - #[cfg(target_arch = "wasm32")] - use wasm_bindgen_test::*; - - #[cfg_attr(not(target_arch = "wasm32"), test)] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn test_hashing() { - let input: Vec = b"hello world".as_ref().into(); - - let output = hash(input.as_ref()); - let expected_hex = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"; - let expected: Vec = expected_hex.from_hex().unwrap(); - assert_eq!(expected, output); - } - - #[cfg(feature = "zero_hash_cache")] - mod zero_hash { - use super::*; - - #[test] - fn zero_hash_zero() { - assert_eq!(ZERO_HASHES[0], [0; 32]); - } - } -} diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..9d42deb --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,108 @@ +use super::*; +use rustc_hex::FromHex; + +#[cfg(target_arch = "wasm32")] +use wasm_bindgen_test::*; + +#[cfg_attr(not(target_arch = "wasm32"), test)] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn test_hash() { + let output = hash(b"hello world"); + let expected: Vec = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" + .from_hex() + .unwrap(); + assert_eq!(expected, output); +} + +#[cfg_attr(not(target_arch = "wasm32"), test)] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn test_hash_fixed() { + let output = hash_fixed(b"hello world"); + let expected: Vec = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" + .from_hex() + .unwrap(); + assert_eq!(expected.as_slice(), &output); +} + +#[cfg_attr(not(target_arch = "wasm32"), test)] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn test_hash32_concat() { + let h1 = [0u8; 32]; + let h2 = [1u8; 32]; + let mut combined = Vec::new(); + combined.extend_from_slice(&h1); + combined.extend_from_slice(&h2); + assert_eq!(hash32_concat(&h1, &h2), hash_fixed(&combined)); +} + +#[cfg_attr(not(target_arch = "wasm32"), test)] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn test_have_sha_extensions() { + let _ = have_sha_extensions(); +} + +#[cfg(feature = "ring")] +mod ring_tests { + use super::*; + + #[test] + fn test_ring_impl() { + let ring_impl = RingImpl; + let expected: Vec = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" + .from_hex() + .unwrap(); + assert_eq!(expected, ring_impl.hash(b"hello world")); + assert_eq!(expected.as_slice(), &ring_impl.hash_fixed(b"hello world")); + } + + #[test] + fn test_ring_context() { + let mut ctx: ring::digest::Context = Sha256Context::new(); + Sha256Context::update(&mut ctx, b"hello world"); + let expected: Vec = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" + .from_hex() + .unwrap(); + assert_eq!(expected.as_slice(), &Sha256Context::finalize(ctx)); + } +} + +#[cfg(any(target_arch = "x86_64", feature = "sha2"))] +mod sha2_tests { + use super::*; + use crate::sha2_impl::Sha2CrateImpl; + + #[test] + fn test_sha2_impl() { + let sha2_impl = Sha2CrateImpl; + let expected: Vec = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" + .from_hex() + .unwrap(); + assert_eq!(expected, sha2_impl.hash(b"hello world")); + assert_eq!(expected.as_slice(), &sha2_impl.hash_fixed(b"hello world")); + } + + #[test] + fn test_sha2_context() { + let mut ctx: sha2::Sha256 = Sha256Context::new(); + Sha256Context::update(&mut ctx, b"hello world"); + let expected: Vec = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" + .from_hex() + .unwrap(); + assert_eq!(expected.as_slice(), &Sha256Context::finalize(ctx)); + } +} + +#[cfg(feature = "zero_hash_cache")] +mod zero_hash_tests { + use super::*; + + #[test] + fn test_zero_hashes() { + assert_eq!(ZERO_HASHES[0], [0; 32]); + assert_eq!( + ZERO_HASHES[1], + hash32_concat(&ZERO_HASHES[0], &ZERO_HASHES[0]) + ); + assert_eq!(ZERO_HASHES.len(), ZERO_HASHES_MAX_INDEX + 1); + } +}