redoubt_zero_core/
assert.rs

1// Copyright (c) 2025-2026 Federico Hoerth <memparanoid@gmail.com>
2// SPDX-License-Identifier: GPL-3.0-only
3// See LICENSE in the repository root for full license text.
4
5//! Test helpers for verifying zeroization behavior.
6
7use super::traits::AssertZeroizeOnDrop;
8
9/// Asserts that a value zeroizes itself when dropped.
10///
11/// This function verifies that the [`ZeroizeOnDropSentinel`](crate::ZeroizeOnDropSentinel) of the given value
12/// is marked as zeroized after the value is dropped.
13///
14/// # Panics
15///
16/// Panics if the value's `.zeroize()` method was not called during drop.
17///
18/// # How It Works
19///
20/// 1. Clones the value's [`ZeroizeOnDropSentinel`](crate::ZeroizeOnDropSentinel)
21/// 2. Resets the sentinel to "not zeroized" state
22/// 3. Drops the value
23/// 4. Asserts the sentinel was marked as zeroized
24///
25/// Typically used in tests for types that implement [`AssertZeroizeOnDrop`].
26pub fn assert_zeroize_on_drop<T: AssertZeroizeOnDrop>(value: T) {
27    let mut sentinel = value.clone_sentinel();
28
29    sentinel.reset();
30
31    assert!(!sentinel.is_zeroized());
32    drop(value);
33    assert!(sentinel.is_zeroized());
34}