Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Linux fallback paths without use of cfg()s #628

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

josephlr
Copy link
Member

@josephlr josephlr commented Mar 9, 2025

This PR changes our Linux fallback tests to test the "real" fallback paths rather than using various cfg()s within the implementation.

As these tests require being run sequentally and only work on Linux, they are configured to require manual invocation.

Also renames tests/mod.rs to tests/common.rs to reuse our tests.

Actually testing the netbsd fallback is harder, so that's best left for a future PR.

@josephlr josephlr requested a review from newpavlov March 9, 2025 07:29
This PR changes our Linux fallback tests to test the "real" fallback
paths rather than using various `cfg()`s within the implementation.

As these tests require being run sequentally and only work on Linux,
they are configured to require manual invocation.

Also renames `tests/mod.rs` to `tests/common.rs` to reuse our tests.

Actually testing the netbsd fallback is harder, so that's best left for
a future PR.

Signed-off-by: Joe Richey <[email protected]>
path.to_str() == Some("/dev/urandom")
}
let mut dir = read_dir("/proc/self/fd").expect("/proc/self exists");
assert!(dir.any(|path| is_urandom(path.expect("entry exists"))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply inline the is_urandom function?

let has_open_urandom = read_dir("/proc/self/fd")
    .expect("/proc/self exists")
    .any(|dir_entry| {
        let path = dir_entry.expect("entry exists").path();
        let path = path.canonicalize().expect("entry is valid");
        path.to_str() == Some("/dev/urandom")
    });
assert!(has_open_urandom);

// Override libc::getrandom to simulate failure
#[export_name = "getrandom"]
pub unsafe extern "C" fn fake_getrandom(_: *mut c_void, _: size_t, _: c_uint) -> ssize_t {
FAKE_GETRANDOM_CALLED.store(true, Ordering::SeqCst);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think AcqRel should be sufficient here?

@@ -53,6 +53,8 @@ jobs:
targets: ${{ matrix.target }}
- uses: Swatinem/rust-cache@v2
- run: cargo test --target=${{ matrix.target }} --features=std
- run: cargo test --test linux_no_fallback -- --test-threads 1
- run: cargo test --test linux_force_fallback -- --test-threads 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that the order of tests is guaranteed?

@newpavlov
Copy link
Member

Overall, it's an interesting approach, but I am not sure if it's simpler than the cfg-based one, plus it relies on several non-trivial things like overriding getrandom symbol linking, execution order of tests, and reading /proc/self/fd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants