From 89584645c38d4fef82ae25dc3a78b065639b3239 Mon Sep 17 00:00:00 2001 From: Katherine Watson Date: Mon, 17 Jun 2024 18:56:00 -0700 Subject: [PATCH] Migrate PublicBox to above tests --- secret-memory/src/public.rs | 212 ++++++++++++++++++------------------ 1 file changed, 106 insertions(+), 106 deletions(-) diff --git a/secret-memory/src/public.rs b/secret-memory/src/public.rs index 6a93613..333886e 100644 --- a/secret-memory/src/public.rs +++ b/secret-memory/src/public.rs @@ -172,112 +172,6 @@ impl StoreValueB64Writer for Public { } } -#[cfg(test)] -mod tests { - - #[cfg(test)] - mod tests { - use crate::Public; - use rosenpass_util::{ - b64::b64_encode, - file::{ - fopen_w, LoadValue, LoadValueB64, StoreValue, StoreValueB64, StoreValueB64Writer, - Visibility, - }, - }; - use std::{fs, os::unix::fs::PermissionsExt}; - use tempfile::tempdir; - - /// test loading a public from an example file, and then storing it again in a different file - #[test] - fn test_public_load_store() { - const N: usize = 100; - - // Generate original random bytes - let original_bytes: [u8; N] = [rand::random(); N]; - - // Create a temporary directory - let temp_dir = tempdir().unwrap(); - - // Store the original public to an example file in the temporary directory - let example_file = temp_dir.path().join("example_file"); - std::fs::write(example_file.clone(), &original_bytes).unwrap(); - - // Load the public from the example file - - let loaded_public = Public::load(&example_file).unwrap(); - - // Check that the loaded public matches the original bytes - assert_eq!(&loaded_public.value, &original_bytes); - - // Store the loaded public to a different file in the temporary directory - let new_file = temp_dir.path().join("new_file"); - loaded_public.store(&new_file).unwrap(); - - // Read the contents of the new file - let new_file_contents = fs::read(&new_file).unwrap(); - - // Read the contents of the original file - let original_file_contents = fs::read(&example_file).unwrap(); - - // Check that the contents of the new file match the original file - assert_eq!(new_file_contents, original_file_contents); - } - - /// test loading a base64 encoded public from an example file, and then storing it again in a different file - #[test] - fn test_public_load_store_base64() { - const N: usize = 100; - // Generate original random bytes - let original_bytes: [u8; N] = [rand::random(); N]; - // Create a temporary directory - let temp_dir = tempdir().unwrap(); - let example_file = temp_dir.path().join("example_file"); - let mut encoded_public = [0u8; N * 2]; - let encoded_public = b64_encode(&original_bytes, &mut encoded_public).unwrap(); - std::fs::write(&example_file, encoded_public).unwrap(); - - // Load the public from the example file - let loaded_public = Public::load_b64::<{ N * 2 }, _>(&example_file).unwrap(); - // Check that the loaded public matches the original bytes - assert_eq!(&loaded_public.value, &original_bytes); - - // Store the loaded public to a different file in the temporary directory - let new_file = temp_dir.path().join("new_file"); - loaded_public.store_b64::<{ N * 2 }, _>(&new_file).unwrap(); - - // Read the contents of the new file - let new_file_contents = fs::read(&new_file).unwrap(); - // Read the contents of the original file - let original_file_contents = fs::read(&example_file).unwrap(); - // Check that the contents of the new file match the original file - assert_eq!(new_file_contents, original_file_contents); - - //Check new file permissions are public - let metadata = fs::metadata(&new_file).unwrap(); - assert_eq!(metadata.permissions().mode() & 0o000777, 0o644); - - // Store the loaded public to a different file in the temporary directory for a second time - let new_file = temp_dir.path().join("new_file_writer"); - let new_file_writer = fopen_w(new_file.clone(), Visibility::Public).unwrap(); - loaded_public - .store_b64_writer::<{ N * 2 }, _>(&new_file_writer) - .unwrap(); - - // Read the contents of the new file - let new_file_contents = fs::read(&new_file).unwrap(); - // Read the contents of the original file - let original_file_contents = fs::read(&example_file).unwrap(); - // Check that the contents of the new file match the original file - assert_eq!(new_file_contents, original_file_contents); - - //Check new file permissions are public - let metadata = fs::metadata(&new_file).unwrap(); - assert_eq!(metadata.permissions().mode() & 0o000777, 0o644); - } - } -} - #[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] #[repr(transparent)] pub struct PublicBox { @@ -426,3 +320,109 @@ impl StoreValueB64Writer for PublicBox { Ok(()) } } + +#[cfg(test)] +mod tests { + + #[cfg(test)] + mod tests { + use crate::Public; + use rosenpass_util::{ + b64::b64_encode, + file::{ + fopen_w, LoadValue, LoadValueB64, StoreValue, StoreValueB64, StoreValueB64Writer, + Visibility, + }, + }; + use std::{fs, os::unix::fs::PermissionsExt}; + use tempfile::tempdir; + + /// test loading a public from an example file, and then storing it again in a different file + #[test] + fn test_public_load_store() { + const N: usize = 100; + + // Generate original random bytes + let original_bytes: [u8; N] = [rand::random(); N]; + + // Create a temporary directory + let temp_dir = tempdir().unwrap(); + + // Store the original public to an example file in the temporary directory + let example_file = temp_dir.path().join("example_file"); + std::fs::write(example_file.clone(), &original_bytes).unwrap(); + + // Load the public from the example file + + let loaded_public = Public::load(&example_file).unwrap(); + + // Check that the loaded public matches the original bytes + assert_eq!(&loaded_public.value, &original_bytes); + + // Store the loaded public to a different file in the temporary directory + let new_file = temp_dir.path().join("new_file"); + loaded_public.store(&new_file).unwrap(); + + // Read the contents of the new file + let new_file_contents = fs::read(&new_file).unwrap(); + + // Read the contents of the original file + let original_file_contents = fs::read(&example_file).unwrap(); + + // Check that the contents of the new file match the original file + assert_eq!(new_file_contents, original_file_contents); + } + + /// test loading a base64 encoded public from an example file, and then storing it again in a different file + #[test] + fn test_public_load_store_base64() { + const N: usize = 100; + // Generate original random bytes + let original_bytes: [u8; N] = [rand::random(); N]; + // Create a temporary directory + let temp_dir = tempdir().unwrap(); + let example_file = temp_dir.path().join("example_file"); + let mut encoded_public = [0u8; N * 2]; + let encoded_public = b64_encode(&original_bytes, &mut encoded_public).unwrap(); + std::fs::write(&example_file, encoded_public).unwrap(); + + // Load the public from the example file + let loaded_public = Public::load_b64::<{ N * 2 }, _>(&example_file).unwrap(); + // Check that the loaded public matches the original bytes + assert_eq!(&loaded_public.value, &original_bytes); + + // Store the loaded public to a different file in the temporary directory + let new_file = temp_dir.path().join("new_file"); + loaded_public.store_b64::<{ N * 2 }, _>(&new_file).unwrap(); + + // Read the contents of the new file + let new_file_contents = fs::read(&new_file).unwrap(); + // Read the contents of the original file + let original_file_contents = fs::read(&example_file).unwrap(); + // Check that the contents of the new file match the original file + assert_eq!(new_file_contents, original_file_contents); + + //Check new file permissions are public + let metadata = fs::metadata(&new_file).unwrap(); + assert_eq!(metadata.permissions().mode() & 0o000777, 0o644); + + // Store the loaded public to a different file in the temporary directory for a second time + let new_file = temp_dir.path().join("new_file_writer"); + let new_file_writer = fopen_w(new_file.clone(), Visibility::Public).unwrap(); + loaded_public + .store_b64_writer::<{ N * 2 }, _>(&new_file_writer) + .unwrap(); + + // Read the contents of the new file + let new_file_contents = fs::read(&new_file).unwrap(); + // Read the contents of the original file + let original_file_contents = fs::read(&example_file).unwrap(); + // Check that the contents of the new file match the original file + assert_eq!(new_file_contents, original_file_contents); + + //Check new file permissions are public + let metadata = fs::metadata(&new_file).unwrap(); + assert_eq!(metadata.permissions().mode() & 0o000777, 0o644); + } + } +}