From 0f787bc7faee7b782cd98a06833507c47ff0b9c8 Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 9 Oct 2024 16:04:52 +0300 Subject: [PATCH] Test file fallback on Linux (#506) Introduces new `getrandom_test_linux_fallback` configuration flag which forces `is_getrandom_available` to always return `false`. This flag is used in CI to test that file fallback works correctly in the `linux_android_with_fallback` backend. --- .github/workflows/tests.yml | 3 +++ Cargo.toml | 1 + src/linux_android_with_fallback.rs | 4 +++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 49828c8e..885e2458 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -71,6 +71,9 @@ jobs: - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="linux_getrandom" run: cargo test ${{ matrix.cargo_test_opts }} --target=${{ matrix.target }} --features=std + - env: + RUSTFLAGS: -Dwarnings --cfg getrandom_test_linux_fallback + run: cargo test --features=std - env: RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand" run: cargo test --features=std diff --git a/Cargo.toml b/Cargo.toml index a688779e..83e22501 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ level = "warn" check-cfg = [ 'cfg(getrandom_backend, values("custom", "rdrand", "linux_getrandom", "wasm_js", "esp_idf"))', 'cfg(getrandom_browser_test)', + 'cfg(getrandom_test_linux_fallback)', ] [package.metadata.docs.rs] diff --git a/src/linux_android_with_fallback.rs b/src/linux_android_with_fallback.rs index b633faad..ec7a1216 100644 --- a/src/linux_android_with_fallback.rs +++ b/src/linux_android_with_fallback.rs @@ -19,7 +19,9 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { } fn is_getrandom_available() -> bool { - if linux_android::getrandom_syscall(&mut []) < 0 { + if cfg!(getrandom_test_linux_fallback) { + false + } else if linux_android::getrandom_syscall(&mut []) < 0 { match last_os_error().raw_os_error() { Some(libc::ENOSYS) => false, // No kernel support // The fallback on EPERM is intentionally not done on Android since this workaround