-
Notifications
You must be signed in to change notification settings - Fork 256
Cleanup Custom Tests #473
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
Merged
Merged
Cleanup Custom Tests #473
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,52 @@ | ||
| // Test that a custom handler works on wasm32-unknown-unknown | ||
| #![cfg(all( | ||
| target_arch = "wasm32", | ||
| target_os = "unknown", | ||
| feature = "custom", | ||
| not(feature = "js") | ||
| ))] | ||
|
|
||
| use wasm_bindgen_test::wasm_bindgen_test as test; | ||
|
|
||
| use core::num::NonZeroU32; | ||
| use getrandom::{getrandom, register_custom_getrandom, Error}; | ||
| #[cfg(all(target_family = "wasm", target_os = "unknown"))] | ||
| use wasm_bindgen_test::wasm_bindgen_test as test; | ||
|
|
||
| fn len7_err() -> Error { | ||
| NonZeroU32::new(Error::INTERNAL_START + 7).unwrap().into() | ||
| } | ||
|
|
||
| fn super_insecure_rng(buf: &mut [u8]) -> Result<(), Error> { | ||
| const LEN7_CODE: u32 = Error::CUSTOM_START + 7; | ||
| // Returns a custom error if input is length 7, otherwise fills with 0x55. | ||
| fn mock_rng(buf: &mut [u8]) -> Result<(), Error> { | ||
| // `getrandom` guarantees it will not call any implementation if the output | ||
| // buffer is empty. | ||
| assert!(!buf.is_empty()); | ||
|
josephlr marked this conversation as resolved.
|
||
| // Length 7 buffers return a custom error | ||
| if buf.len() == 7 { | ||
| return Err(len7_err()); | ||
| } | ||
| // Otherwise, fill bytes based on input length | ||
| let mut start = buf.len() as u8; | ||
| for b in buf { | ||
| *b = start; | ||
| start = start.wrapping_mul(3); | ||
| return Err(NonZeroU32::new(LEN7_CODE).unwrap().into()); | ||
| } | ||
| buf.fill(0x55); | ||
| Ok(()) | ||
| } | ||
|
|
||
| register_custom_getrandom!(super_insecure_rng); | ||
|
|
||
| use getrandom::getrandom as getrandom_impl; | ||
| mod common; | ||
| // Test registering a custom implementation, even on supported platforms. | ||
| register_custom_getrandom!(mock_rng); | ||
|
|
||
| // Invoking with an empty buffer should never call the custom implementation. | ||
| #[test] | ||
| fn custom_rng_output() { | ||
| let mut buf = [0u8; 4]; | ||
| assert_eq!(getrandom(&mut buf), Ok(())); | ||
| assert_eq!(buf, [4, 12, 36, 108]); | ||
|
|
||
| let mut buf = [0u8; 3]; | ||
| assert_eq!(getrandom(&mut buf), Ok(())); | ||
| assert_eq!(buf, [3, 9, 27]); | ||
| fn custom_empty() { | ||
| getrandom(&mut []).unwrap(); | ||
| } | ||
|
|
||
| // On a supported platform, make sure the custom implementation isn't used. We | ||
| // test on a few common platfroms, rather than duplicating the lib.rs logic. | ||
| #[cfg(any( | ||
| target_os = "linux", | ||
| target_os = "windows", | ||
| target_os = "macos", | ||
| target_os = "espidf", | ||
| target_os = "wasi", | ||
| all(target_family = "wasm", target_os = "unknown", feature = "js"), | ||
| ))] | ||
| #[test] | ||
| fn rng_err_output() { | ||
| assert_eq!(getrandom(&mut [0; 7]), Err(len7_err())); | ||
| fn custom_not_used() { | ||
| getrandom(&mut [0; 7]).unwrap(); | ||
| } | ||
| // On an unsupported platform, make sure the custom implementation is used. | ||
| #[cfg(all(target_family = "wasm", target_os = "unknown", not(feature = "js")))] | ||
| #[test] | ||
| fn custom_used() { | ||
| let err = getrandom(&mut [0; 7]).unwrap_err(); | ||
| assert_eq!(err.code().get(), LEN7_CODE); | ||
|
|
||
| let mut buf = [0; 12]; | ||
| getrandom(&mut buf).unwrap(); | ||
| assert_eq!(buf, [0x55; 12]); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.