mirror of
https://github.com/rosenpass/rosenpass.git
synced 2026-02-28 06:23:08 -08:00
Migrate PublicBox to above tests
This commit is contained in:
committed by
kat watson
parent
3286e49370
commit
89584645c3
@@ -172,112 +172,6 @@ impl<const N: usize> StoreValueB64Writer for Public<N> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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)]
|
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct PublicBox<const N: usize> {
|
pub struct PublicBox<const N: usize> {
|
||||||
@@ -426,3 +320,109 @@ impl<const N: usize> StoreValueB64Writer for PublicBox<N> {
|
|||||||
Ok(())
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user