Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Sep 14, 2023
1 parent 4d60da1 commit 63481d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
30 changes: 19 additions & 11 deletions crates/tests/bcrypt/tests/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use windows::{core::*, Win32::Security::Cryptography::*};
fn test() -> Result<()> {
unsafe {
let mut rng = Default::default();
BCryptOpenAlgorithmProvider(&mut rng, BCRYPT_RNG_ALGORITHM, None, Default::default())?;
BCryptOpenAlgorithmProvider(&mut rng, BCRYPT_RNG_ALGORITHM, None, Default::default())
.ok()?;

let mut des = Default::default();
BCryptOpenAlgorithmProvider(&mut des, BCRYPT_3DES_ALGORITHM, None, Default::default())?;
BCryptOpenAlgorithmProvider(&mut des, BCRYPT_3DES_ALGORITHM, None, Default::default())
.ok()?;

let mut object_len = [0; 4];
let mut bytes_copied = 0;
Expand All @@ -17,14 +19,15 @@ fn test() -> Result<()> {
Some(&mut object_len),
&mut bytes_copied,
0,
)?;
)
.ok()?;
let object_len = u32::from_le_bytes(object_len);

let mut shared_secret = vec![0; object_len as usize];
BCryptGenRandom(rng, &mut shared_secret, Default::default())?;
BCryptGenRandom(rng, &mut shared_secret, Default::default()).ok()?;

let mut encrypt_key = Default::default();
BCryptGenerateSymmetricKey(des, &mut encrypt_key, None, &shared_secret, 0)?;
BCryptGenerateSymmetricKey(des, &mut encrypt_key, None, &shared_secret, 0).ok()?;

let mut block_len = [0; 4];
BCryptGetProperty(
Expand All @@ -33,7 +36,8 @@ fn test() -> Result<()> {
Some(&mut block_len),
&mut bytes_copied,
0,
)?;
)
.ok()?;
let block_len = u32::from_le_bytes(block_len) as usize;

let send_message = "I ❤️ Rust";
Expand All @@ -50,7 +54,8 @@ fn test() -> Result<()> {
None,
&mut encrypted_len,
Default::default(),
)?;
)
.ok()?;

let mut encrypted = vec![0; encrypted_len as usize];
BCryptEncrypt(
Expand All @@ -61,10 +66,11 @@ fn test() -> Result<()> {
Some(&mut encrypted),
&mut encrypted_len,
Default::default(),
)?;
)
.ok()?;

let mut decrypt_key = Default::default();
BCryptGenerateSymmetricKey(des, &mut decrypt_key, None, &shared_secret, 0)?;
BCryptGenerateSymmetricKey(des, &mut decrypt_key, None, &shared_secret, 0).ok()?;

let mut decrypted_len = 0;
BCryptDecrypt(
Expand All @@ -75,7 +81,8 @@ fn test() -> Result<()> {
None,
&mut decrypted_len,
Default::default(),
)?;
)
.ok()?;

let mut decrypted = vec![0; decrypted_len as usize];
BCryptDecrypt(
Expand All @@ -86,7 +93,8 @@ fn test() -> Result<()> {
Some(&mut decrypted),
&mut decrypted_len,
Default::default(),
)?;
)
.ok()?;

let receive_message =
std::str::from_utf8(trim_null_end(&decrypted)).expect("Not a valid message");
Expand Down
5 changes: 3 additions & 2 deletions crates/tests/extensions/tests/ntstatus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ fn test() -> Result<()> {
w!("RNG"),
None,
BCRYPT_OPEN_ALGORITHM_PROVIDER_FLAGS::default(),
)?;
)
.ok()?;

let mut random = GUID::zeroed();
let bytes = std::slice::from_raw_parts_mut(
&mut random as *mut _ as *mut u8,
std::mem::size_of::<GUID>(),
);

BCryptGenRandom(provider, bytes, Default::default())?;
BCryptGenRandom(provider, bytes, Default::default()).ok()?;

assert_ne!(random, GUID::zeroed());
}
Expand Down

0 comments on commit 63481d2

Please sign in to comment.