|
| 1 | +// Licensed under the Apache License, Version 2.0 or the MIT License. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | +// Copyright Tock Contributors 2024. |
| 4 | + |
| 5 | +//! Integration test for an RNG driver implementing the `Entropy32` trait. |
| 6 | +//! |
| 7 | +//! This test verifies that: |
| 8 | +//! - At least 8 `u32` values can be collected from the entropy source. |
| 9 | +//! - The driver correctly handles the `Continue` / `Done` return from the |
| 10 | +//! client callback, allowing the test to "wait" (re-request) until all |
| 11 | +//! requested values have been delivered. |
| 12 | +//! - Each collected value is stored and printed for manual inspection. |
| 13 | +//! |
| 14 | +//! # Usage |
| 15 | +//! |
| 16 | +//! Add this file alongside your driver under `capsules/extra/src/` (or |
| 17 | +//! wherever your crate keeps driver tests), then register a test board that |
| 18 | +//! wires the concrete `Entropy32` implementor to `RngEntropy32Test`. |
| 19 | +//! |
| 20 | +//! ```rust,ignore |
| 21 | +//! // In your board's main.rs (or test harness): |
| 22 | +//! let rng_test = static_init!( |
| 23 | +//! capsules_extra::rng_entropy32_test::RngEntropy32Test<'static, YourRngDriver>, |
| 24 | +//! capsules_extra::rng_entropy32_test::RngEntropy32Test::new(your_rng_driver) |
| 25 | +//! ); |
| 26 | +//! your_rng_driver.set_client(rng_test); |
| 27 | +//! rng_test.run(); |
| 28 | +//! ``` |
| 29 | +
|
| 30 | +use core::cell::Cell; |
| 31 | +use kernel::hil::entropy::{Client32, Entropy32}; |
| 32 | +use kernel::utilities::cells::OptionalCell; |
| 33 | +use kernel::{debug, ErrorCode}; |
| 34 | + |
| 35 | +/// Number of `u32` words to collect before declaring the test a success. |
| 36 | +const WORDS_REQUESTED: usize = 8; |
| 37 | + |
| 38 | +// --------------------------------------------------------------------------- |
| 39 | +// Test component |
| 40 | +// --------------------------------------------------------------------------- |
| 41 | + |
| 42 | +pub struct RngEntropy32Test<'a, E: Entropy32<'a>> { |
| 43 | + /// Reference to the entropy source under test. |
| 44 | + rng: &'a E, |
| 45 | + /// Accumulator for collected entropy words. |
| 46 | + collected: OptionalCell<[u32; WORDS_REQUESTED]>, |
| 47 | + /// How many words we have stored so far. |
| 48 | + count: Cell<usize>, |
| 49 | +} |
| 50 | + |
| 51 | +impl<'a, E: Entropy32<'a>> RngEntropy32Test<'a, E> { |
| 52 | + pub fn new(rng: &'a E) -> Self { |
| 53 | + Self { |
| 54 | + rng, |
| 55 | + collected: OptionalCell::new([0u32; WORDS_REQUESTED]), |
| 56 | + count: Cell::new(0), |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// Kick off the entropy collection. Call once after wiring the client. |
| 61 | + pub fn run(&self) { |
| 62 | + debug!( |
| 63 | + "[RNG TEST] Starting Entropy32 test — requesting {} u32 words", |
| 64 | + WORDS_REQUESTED |
| 65 | + ); |
| 66 | + match self.rng.get() { |
| 67 | + Ok(()) => {} |
| 68 | + Err(e) => { |
| 69 | + debug!("[RNG TEST] FAIL: rng.get() returned error: {:?}", e); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// Print a summary once all words have been collected. |
| 75 | + fn finish(&self) { |
| 76 | + self.collected.map(|words| { |
| 77 | + debug!( |
| 78 | + "[RNG TEST] PASS: collected {} u32 entropy words:", |
| 79 | + WORDS_REQUESTED |
| 80 | + ); |
| 81 | + for (i, w) in words.iter().enumerate() { |
| 82 | + debug!(" word[{}] = {:#010x}", i, w); |
| 83 | + } |
| 84 | + // Basic sanity: not *all* zeros (astronomically unlikely with a real RNG). |
| 85 | + let all_zero = words.iter().all(|&w| w == 0); |
| 86 | + if all_zero { |
| 87 | + debug!( |
| 88 | + "[RNG TEST] WARNING: all collected words are zero — verify your RNG source!" |
| 89 | + ); |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// --------------------------------------------------------------------------- |
| 96 | +// Entropy32 client implementation |
| 97 | +// --------------------------------------------------------------------------- |
| 98 | + |
| 99 | +impl<'a, E: Entropy32<'a>> Client32 for RngEntropy32Test<'a, E> { |
| 100 | + /// Called by the driver each time a new `u32` word of entropy is ready. |
| 101 | + /// |
| 102 | + /// Returns: |
| 103 | + /// - `Ok(Continue)` — ask the driver for another word (we still need more). |
| 104 | + /// - `Ok(Done)` — we have collected enough; release the hardware. |
| 105 | + fn entropy_available( |
| 106 | + &self, |
| 107 | + entropy: &mut dyn Iterator<Item = u32>, |
| 108 | + error: Result<(), ErrorCode>, |
| 109 | + ) -> kernel::hil::entropy::Continue { |
| 110 | + // Surface driver-level errors but keep going — some peripheral drivers |
| 111 | + // (e.g. hardware FIFOs) report transient under-run errors yet can still |
| 112 | + // produce data; we treat them as non-fatal for test purposes. |
| 113 | + if let Err(e) = error { |
| 114 | + debug!( |
| 115 | + "[RNG TEST] entropy_available reported error: {:?} — retrying", |
| 116 | + e |
| 117 | + ); |
| 118 | + return kernel::hil::entropy::Continue::More; |
| 119 | + } |
| 120 | + |
| 121 | + let mut done = false; |
| 122 | + |
| 123 | + self.collected.map(|mut words: [u32; 8]| { |
| 124 | + // Drain as many words as the iterator offers in this callback. |
| 125 | + for word in &mut *entropy { |
| 126 | + let idx = self.count.get(); |
| 127 | + if idx >= WORDS_REQUESTED { |
| 128 | + break; |
| 129 | + } |
| 130 | + words[idx] = word; |
| 131 | + self.count.set(idx + 1); |
| 132 | + debug!( |
| 133 | + "[RNG TEST] word[{}] = {:#010x} ({}/{} collected)", |
| 134 | + idx, |
| 135 | + word, |
| 136 | + idx + 1, |
| 137 | + WORDS_REQUESTED |
| 138 | + ); |
| 139 | + if idx + 1 >= WORDS_REQUESTED { |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + done = self.count.get() >= WORDS_REQUESTED; |
| 144 | + }); |
| 145 | + |
| 146 | + if done { |
| 147 | + self.finish(); |
| 148 | + kernel::hil::entropy::Continue::Done |
| 149 | + } else { |
| 150 | + // Not enough words yet — tell the driver to keep going / call us |
| 151 | + // again when more entropy is available. |
| 152 | + kernel::hil::entropy::Continue::More |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments