Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 3 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -241,33 +244,3 @@ pub static ZERO_HASHES: LazyLock<Vec<[u8; HASH_LEN]>> = 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<u8> = b"hello world".as_ref().into();

let output = hash(input.as_ref());
let expected_hex = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
let expected: Vec<u8> = 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]);
}
}
}
108 changes: 108 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -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<u8> = "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<u8> = "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<u8> = "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<u8> = "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<u8> = "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<u8> = "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);
}
}
Loading