Skip to content

Commit

Permalink
Test file fallback on Linux (#506)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
newpavlov authored Oct 9, 2024
1 parent 286e486 commit 0f787bc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion src/linux_android_with_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ pub fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> 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
Expand Down

0 comments on commit 0f787bc

Please sign in to comment.