Skip to content

zeroize: Support stablized 'alloc' crate #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions zeroize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@
)]
#![doc(html_root_url = "https://docs.rs/zeroize/0.7.0")]

#[cfg(all(feature = "alloc", not(feature = "std")))]
#[allow(unused_imports)] // rustc bug?
#[macro_use]
extern crate alloc;

#[cfg(any(feature = "std", test))]
#[cfg_attr(test, macro_use)]
extern crate std;
Expand Down
110 changes: 57 additions & 53 deletions zeroize/tests/zeroize_derive.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,60 @@
//! Integration tests for `zeroize_derive` proc macros

use zeroize::Zeroize;

#[derive(Zeroize)]
struct ZeroizableTupleStruct([u8; 3]);

#[test]
fn derive_tuple_struct_test() {
let mut value = ZeroizableTupleStruct([1, 2, 3]);
value.zeroize();
assert_eq!(&value.0, &[0, 0, 0])
}

#[derive(Zeroize)]
struct ZeroizableStruct {
string: String,
vec: Vec<u8>,
bytearray: [u8; 3],
number: usize,
boolean: bool,
}

#[test]
fn derive_struct_test() {
let mut value = ZeroizableStruct {
string: String::from("Hello, world!"),
vec: vec![1, 2, 3],
bytearray: [4, 5, 6],
number: 42,
boolean: true,
};

value.zeroize();

assert!(value.string.is_empty());
assert!(value.vec.is_empty());
assert_eq!(&value.bytearray, &[0, 0, 0]);
assert_eq!(value.number, 0);
assert!(!value.boolean);
}

/// Test that the custom macro actually derived `Drop` for `ZeroizableStruct`
trait Droppable: Drop {}
impl Droppable for ZeroizableStruct {}

/// Test that this successfully disables deriving a drop handler by defining
/// a custom one which should conflict if the custom derive did too
#[allow(dead_code)]
#[derive(Zeroize)]
#[zeroize(no_drop)]
struct ZeroizeNoDropStruct([u8; 3]);

impl Drop for ZeroizeNoDropStruct {
fn drop(&mut self) {}
#[cfg(feature = "zeroize_derive")]
mod custom_derive_tests {
use zeroize::Zeroize;

#[derive(Zeroize)]
struct ZeroizableTupleStruct([u8; 3]);

#[test]
fn derive_tuple_struct_test() {
let mut value = ZeroizableTupleStruct([1, 2, 3]);
value.zeroize();
assert_eq!(&value.0, &[0, 0, 0])
}

#[derive(Zeroize)]
struct ZeroizableStruct {
string: String,
vec: Vec<u8>,
bytearray: [u8; 3],
number: usize,
boolean: bool,
}

#[test]
fn derive_struct_test() {
let mut value = ZeroizableStruct {
string: String::from("Hello, world!"),
vec: vec![1, 2, 3],
bytearray: [4, 5, 6],
number: 42,
boolean: true,
};

value.zeroize();

assert!(value.string.is_empty());
assert!(value.vec.is_empty());
assert_eq!(&value.bytearray, &[0, 0, 0]);
assert_eq!(value.number, 0);
assert!(!value.boolean);
}

/// Test that the custom macro actually derived `Drop` for `ZeroizableStruct`
trait Droppable: Drop {}

impl Droppable for ZeroizableStruct {}

/// Test that this successfully disables deriving a drop handler by defining
/// a custom one which should conflict if the custom derive did too
#[allow(dead_code)]
#[derive(Zeroize)]
#[zeroize(no_drop)]
struct ZeroizeNoDropStruct([u8; 3]);

impl Drop for ZeroizeNoDropStruct {
fn drop(&mut self) {}
}
}