summaryrefslogtreecommitdiffstats
path: root/third_party/rust/getrandom/tests/common
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/getrandom/tests/common
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/getrandom/tests/common')
-rw-r--r--third_party/rust/getrandom/tests/common/mod.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/third_party/rust/getrandom/tests/common/mod.rs b/third_party/rust/getrandom/tests/common/mod.rs
new file mode 100644
index 0000000000..006f230d7c
--- /dev/null
+++ b/third_party/rust/getrandom/tests/common/mod.rs
@@ -0,0 +1,66 @@
+use super::getrandom_impl;
+
+#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
+use wasm_bindgen_test::wasm_bindgen_test as test;
+
+#[cfg(feature = "test-in-browser")]
+wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
+
+#[test]
+fn test_zero() {
+ // Test that APIs are happy with zero-length requests
+ getrandom_impl(&mut [0u8; 0]).unwrap();
+}
+
+#[test]
+fn test_diff() {
+ let mut v1 = [0u8; 1000];
+ getrandom_impl(&mut v1).unwrap();
+
+ let mut v2 = [0u8; 1000];
+ getrandom_impl(&mut v2).unwrap();
+
+ let mut n_diff_bits = 0;
+ for i in 0..v1.len() {
+ n_diff_bits += (v1[i] ^ v2[i]).count_ones();
+ }
+
+ // Check at least 1 bit per byte differs. p(failure) < 1e-1000 with random input.
+ assert!(n_diff_bits >= v1.len() as u32);
+}
+
+#[test]
+fn test_huge() {
+ let mut huge = [0u8; 100_000];
+ getrandom_impl(&mut huge).unwrap();
+}
+
+// On WASM, the thread API always fails/panics
+#[cfg(not(target_arch = "wasm32"))]
+#[test]
+fn test_multithreading() {
+ extern crate std;
+ use std::{sync::mpsc::channel, thread, vec};
+
+ let mut txs = vec![];
+ for _ in 0..20 {
+ let (tx, rx) = channel();
+ txs.push(tx);
+
+ thread::spawn(move || {
+ // wait until all the tasks are ready to go.
+ rx.recv().unwrap();
+ let mut v = [0u8; 1000];
+
+ for _ in 0..100 {
+ getrandom_impl(&mut v).unwrap();
+ thread::yield_now();
+ }
+ });
+ }
+
+ // start all the tasks
+ for tx in txs.iter() {
+ tx.send(()).unwrap();
+ }
+}