1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2024.
//! Interface for running capsule tests.
//!
//! As Tock capsules are asynchronous, it is difficult for a test runner to
//! determine when a test has finished. This interface provides a `done()`
//! callback used by the test implementation to notify when the test has
//! completed.
//!
//! A simple example of a test capsule using this interface:
//!
//! ```rust,ignore
//! pub struct TestSensorX {
//! client: OptionalCell<&'static dyn CapsuleTestClient>,
//! }
//!
//! impl TestSensorX {
//! pub fn new() -> Self {
//! TestHmacSha256 {
//! client: OptionalCell::empty(),
//! }
//! }
//! }
//!
//! impl CapsuleTest for TestSensorX {
//! fn set_client(&self, client: &'static dyn CapsuleTestClient) {
//! self.client.set(client);
//! }
//! }
//!
//! impl AsyncClient for TestSensorX {
//! fn operation_complete(&self) {
//! // Test has finished at this point.
//! self.client.map(|client| {
//! client.done(Ok(()));
//! });
//! }
//! }
//! ```
use kernel::ErrorCode;
/// Errors for the result of a failed test.
pub enum CapsuleTestError {
/// The test computed some result (e.g., a checksum or hash) and the result
/// is not correct (e.g., it doesn't match the intended value, say the
/// correct checksum or hash).
IncorrectResult,
/// An error occurred while running the test, and the resulting `ErrorCode`
/// is provided.
ErrorCode(ErrorCode),
}
/// Client for receiving test done events.
pub trait CapsuleTestClient {
/// Called when the test is finished. If the test was successful, `result`
/// is `Ok(())`. If the test failed, `result` is `Err()` with a suitable
/// error.
fn done(&'static self, result: Result<(), CapsuleTestError>);
}
/// Identify a test as a capsule test. This is only used for setting the client
/// for test complete callbacks.
pub trait CapsuleTest {
/// Set the client for the done callback.
fn set_client(&self, client: &'static dyn CapsuleTestClient);
}