| 1 |
//! Implements the [`zeroize::Zeroize`] trait for [`CompactString`] |
| 2 |
|
| 3 |
use crate::CompactString; |
| 4 |
use zeroize::Zeroize; |
| 5 |
|
| 6 |
#[cfg_attr(docsrs, doc(cfg(feature = "zeroize")))] |
| 7 |
impl Zeroize for CompactString { |
| 8 |
fn zeroize(&mut self) { |
| 9 |
self.0.zeroize(); |
| 10 |
} |
| 11 |
} |
| 12 |
|
| •13 |
#[cfg(all(test, feature = "std"))] |
| 14 |
mod tests { |
| 15 |
use std::string::ToString; |
| 16 |
|
| 17 |
use alloc::string::String; |
| 18 |
use test_strategy::proptest; |
| 19 |
|
| 20 |
use super::*; |
| 21 |
use crate::tests::rand_unicode; |
| 22 |
|
| 23 |
#[test] |
| 24 |
fn smoketest_zeroize() { |
| 25 |
let mut short = CompactString::from("hello"); |
| 26 |
let mut control = short.as_str().to_string(); |
| 27 |
short.zeroize(); |
| 28 |
assert_eq!(short, ""); |
| 29 |
control.zeroize(); |
| 30 |
assert_eq!(control, short); |
| 31 |
|
| 32 |
let mut long = CompactString::from("I am a long string that will be on the heap"); |
| 33 |
let mut control = long.as_str().to_string(); |
| 34 |
long.zeroize(); |
| 35 |
assert_eq!(long, ""); |
| 36 |
assert!(long.is_heap_allocated()); |
| 37 |
control.zeroize(); |
| 38 |
assert_eq!(long, control); |
| 39 |
} |
| 40 |
|
| 41 |
#[proptest] |
| 42 |
#[cfg_attr(miri, ignore)] |
| 43 |
fn proptest_zeroize(#[strategy(rand_unicode())] s: String) { |
| 44 |
let mut compact = CompactString::new(s.clone()); |
| 45 |
let mut control = s.clone(); |
| 46 |
|
| 47 |
compact.zeroize(); |
| 48 |
control.zeroize(); |
| 49 |
|
| 50 |
assert_eq!(compact, control); |
| 51 |
} |
| 52 |
} |