Sha256: 1e944ae523b62dba53fe3daf1b964a2498c8fdd21dfa7afe53781bff2fcf276e

Contents?: true

Size: 1.32 KB

Versions: 32

Compression:

Stored size: 1.32 KB

Contents

// 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};

fn len7_err() -> Error {
    NonZeroU32::new(Error::INTERNAL_START + 7).unwrap().into()
}

fn super_insecure_rng(buf: &mut [u8]) -> Result<(), Error> {
    // `getrandom` guarantees it will not call any implementation if the output
    // buffer is empty.
    assert!(!buf.is_empty());
    // 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);
    }
    Ok(())
}

register_custom_getrandom!(super_insecure_rng);

use getrandom::getrandom as getrandom_impl;
mod common;

#[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]);
}

#[test]
fn rng_err_output() {
    assert_eq!(getrandom(&mut [0; 7]), Err(len7_err()));
}

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/getrandom-0.2.15/tests/custom.rs
wasmtime-28.0.0 ./ext/cargo-vendor/getrandom-0.2.15/tests/custom.rs
wasmtime-27.0.0 ./ext/cargo-vendor/getrandom-0.2.15/tests/custom.rs
wasmtime-26.0.0 ./ext/cargo-vendor/getrandom-0.2.15/tests/custom.rs
wasmtime-25.0.2 ./ext/cargo-vendor/getrandom-0.2.15/tests/custom.rs
wasmtime-25.0.1 ./ext/cargo-vendor/getrandom-0.2.15/tests/custom.rs
wasmtime-25.0.0 ./ext/cargo-vendor/getrandom-0.2.15/tests/custom.rs
wasmtime-24.0.0 ./ext/cargo-vendor/getrandom-0.2.15/tests/custom.rs
wasmtime-23.0.2 ./ext/cargo-vendor/getrandom-0.2.11/tests/custom.rs
wasmtime-22.0.0 ./ext/cargo-vendor/getrandom-0.2.11/tests/custom.rs
wasmtime-21.0.1 ./ext/cargo-vendor/getrandom-0.2.11/tests/custom.rs
wasmtime-20.0.2 ./ext/cargo-vendor/getrandom-0.2.11/tests/custom.rs
wasmtime-20.0.0 ./ext/cargo-vendor/getrandom-0.2.11/tests/custom.rs
wasmtime-18.0.3 ./ext/cargo-vendor/getrandom-0.2.11/tests/custom.rs
wasmtime-17.0.1 ./ext/cargo-vendor/getrandom-0.2.11/tests/custom.rs
wasmtime-17.0.0 ./ext/cargo-vendor/getrandom-0.2.11/tests/custom.rs
wasmtime-16.0.0 ./ext/cargo-vendor/getrandom-0.2.11/tests/custom.rs
wasmtime-15.0.1 ./ext/cargo-vendor/getrandom-0.2.11/tests/custom.rs
wasmtime-15.0.0 ./ext/cargo-vendor/getrandom-0.2.11/tests/custom.rs
wasmtime-14.0.4 ./ext/cargo-vendor/getrandom-0.2.10/tests/custom.rs