Skip to content

Commit

Permalink
Test RDRAND
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Richey <[email protected]>
  • Loading branch information
josephlr committed Jun 20, 2024
1 parent 71a2ab8 commit b028603
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ jobs:
toolchain: ${{ matrix.toolchain }}
- uses: Swatinem/rust-cache@v2
- run: cargo test
# Make sure enabling the std and custom features don't break anything
- run: cargo test --features=std,custom
# Ensure enabling features works, and run feature-specific tests.
- run: cargo test --features=std,custom,rdrand
- run: cargo test --features=linux_disable_fallback
- if: ${{ matrix.toolchain == 'nightly' }}
run: cargo test --benches
Expand Down
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ mod util;
mod custom;
#[cfg(feature = "std")]
mod error_impls;
// If the rdrand feature is enabled, always bring in the rdrand module, so
// that the RDRAND implementation can be tested.
#[cfg(all(
any(target_env = "sgx", feature = "rdrand"),
any(target_arch = "x86_64", target_arch = "x86"),
))]
mod rdrand;

pub use crate::error::Error;

Expand Down Expand Up @@ -330,10 +337,10 @@ cfg_if! {
} else if #[cfg(windows)] {
#[path = "windows.rs"] mod imp;
} else if #[cfg(all(target_arch = "x86_64", target_env = "sgx"))] {
#[path = "rdrand.rs"] mod imp;
use rdrand as imp;
} else if #[cfg(all(feature = "rdrand",
any(target_arch = "x86_64", target_arch = "x86")))] {
#[path = "rdrand.rs"] mod imp;
use rdrand as imp;
} else if #[cfg(all(feature = "js",
any(target_arch = "wasm32", target_arch = "wasm64"),
target_os = "unknown"))] {
Expand Down
6 changes: 6 additions & 0 deletions src/rdrand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ fn is_rdrand_good() -> bool {
unsafe { self_test() }
}

#[allow(dead_code)]
pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
static RDRAND_GOOD: LazyBool = LazyBool::new();
if !RDRAND_GOOD.unsync_init(is_rdrand_good) {
Expand Down Expand Up @@ -121,3 +122,8 @@ unsafe fn rdrand_exact(dest: &mut [MaybeUninit<u8>]) -> Option<()> {
}
Some(())
}

#[cfg(test)]
mod tests {
crate::tests::define_tests!(super::getrandom_inner);
}

0 comments on commit b028603

Please sign in to comment.