#![allow(dead_code)]
use core::fmt;
use flexstr::{FlexStr, RefCountedMut, StringLike};
use flexstr_support::StringToFromBytes;
use inline_flexstr::InlineFlexStr;
use zeroize::{TryZeroize, Zeroize};
pub fn test_zeroize_inlined<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCountedMut<S>,
{
let inline = InlineFlexStr::try_from_type(s).expect("test input should be small enough");
let mut flex: FlexStr<'_, S, R> = FlexStr::from_inline(inline);
assert!(flex.is_inlined());
assert!(flex.try_zeroize());
assert!(flex.is_inlined());
assert!(StringLike::is_empty(&flex));
}
pub fn test_zeroize_borrowed<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCountedMut<S>,
{
let mut flex: FlexStr<'_, S, R> = FlexStr::from_borrowed(s);
assert!(flex.is_borrowed());
assert!(!flex.try_zeroize());
assert!(flex.is_borrowed());
assert_eq!(flex.as_ref_type(), s);
}
pub fn test_zeroize_ref_counted<S, R>(rc: R)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCountedMut<S>,
{
let mut flex: FlexStr<'_, S, R> = FlexStr::from_ref_counted(rc);
assert!(flex.is_ref_counted());
assert!(flex.try_zeroize());
assert!(flex.is_inlined());
assert!(StringLike::is_empty(&flex));
}
pub fn test_zeroize_boxed<S, R>(s: &'static S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCountedMut<S>,
Box<S>: From<S::Owned>,
S::Owned: AsRef<S>,
{
let boxed = Box::from(s.to_owned());
let mut flex: FlexStr<'_, S, R> = FlexStr::from_boxed(boxed);
assert!(flex.is_boxed());
assert!(flex.try_zeroize());
assert!(flex.is_inlined());
assert!(StringLike::is_empty(&flex));
}
pub fn test_zeroize_ref_counted_shared<S, R>(rc: R)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
R: RefCountedMut<S> + Clone,
{
let mut flex: FlexStr<'_, S, R> = FlexStr::from_ref_counted(rc);
assert!(flex.is_ref_counted());
let flex2 = flex.clone();
assert!(flex2.is_ref_counted());
assert!(!flex.try_zeroize());
assert!(flex.is_ref_counted());
assert_eq!(flex.as_ref_type(), flex2.as_ref_type());
}
pub fn test_zeroize_inline_bytes_cleared<S>(s: &S)
where
S: ?Sized + StringToFromBytes + fmt::Debug + PartialEq,
{
let mut inline = InlineFlexStr::try_from_type(s).expect("test input should be small enough");
assert!(
!S::self_as_raw_bytes(s).is_empty(),
"test input must be non-empty"
);
let ptr = &inline as *const InlineFlexStr<S> as *const u8;
let size = core::mem::size_of::<InlineFlexStr<S>>();
inline.zeroize();
let bytes = unsafe { core::slice::from_raw_parts(ptr, size) };
assert!(
bytes.iter().all(|&b| b == 0),
"all bytes of InlineFlexStr should be zero after zeroize, got: {bytes:?}"
);
}