Skip to content

Commit

Permalink
fixed tests and patched up documentation
Browse files Browse the repository at this point in the history
fixed consolidated_test

...

...

...
  • Loading branch information
sokorototo committed Feb 10, 2025
1 parent 28a6e5c commit ecc4c94
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use std::{io::Cursor, fs::File};
use vach::prelude::*;

let mut leaves = [
Leaf::new(File::open("test_data/background.wav")?, "ambient"),
Leaf::new(File::open("background.wav")?, "ambient"),
Leaf::new(vec![12, 23, 34, 45, 56, 67, 78, 89, 10], "ftstep"),
Leaf::new(b"Fast-Acting Long-Lasting, *Bathroom Reader*" as &[u8], "hello")
];
Expand Down
2 changes: 1 addition & 1 deletion crates/vach/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use vach::prelude::*;
// collect leaves in a vector, or static buffer
let mut leaves = [
// Leaf::new(File::open("test_data/background.wav").unwrap(), "ambient"),
// Leaf::new(File::open("background.wav").unwrap(), "ambient"),
Leaf::new([12, 23, 34, 45, 56, 67, 78, 90, 69].as_slice(), "ftstep").compress(CompressMode::Always),
Leaf::new(b"Hello, Cassandra!".as_slice(), "hello")
];
Expand Down
35 changes: 17 additions & 18 deletions crates/vach/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,17 @@ fn leaves_from_dir<'a>(
let directory = fs::read_dir(path)?;

for file in directory {
let uri = file?.path();
let path = file?.path();

let v = uri
.iter()
.map(|u| String::from(u.to_str().unwrap()))
.collect::<Vec<String>>();
let v = path.iter().map(|u| u.to_string_lossy()).collect::<Vec<_>>();

if uri.is_file() {
let file = fs::File::open(uri)?;
if path.is_file() && path.extension().map(|s| s.to_str().unwrap()) != Some("vach") {
let file = fs::File::open(&path)?;
let id = v.last().unwrap();

let leaf = Leaf::new(file, format!("{}/{}", v.get(v.len() - 2).unwrap(), v.last().unwrap()));
let leaf = match template {
Some(t) => leaf.template(t),
None => leaf,
Some(t) => Leaf::new(file, id).template(t),
None => Leaf::new(file, id),
};

leaves.push(leaf);
Expand Down Expand Up @@ -175,13 +172,13 @@ fn fetch_with_signature() -> InternalResult {

// Load keypair
let keypair = &KEYPAIR[crate::SECRET_KEY_LENGTH..];
let vk = read_verifying_key(handle)?;
let vk = read_verifying_key(keypair)?;

// open archive
let target = File::open(SIGNED_TARGET)?;
let mut archive = Archive::with_key(target, &vk)?;

let resource = archive.fetch_mut("test_data/quicksort.wasm")?;
let resource = archive.fetch_mut("quicksort.wasm")?;
assert_eq!(resource.data.len(), 106537);

// The adjacent resource was flagged to not be signed
Expand Down Expand Up @@ -223,6 +220,7 @@ fn builder_with_encryption() -> InternalResult {

let template = Leaf::default().encrypt(true).compress(CompressMode::Never).sign(true);
let mut leaves = leaves_from_dir("test_data", Some(&template))?;

leaves.push(
Leaf::new(b"Snitches get stitches, iOS sucks" as &[u8], "stitches.snitches")
.sign(false)
Expand Down Expand Up @@ -255,7 +253,7 @@ fn fetch_from_encrypted() -> InternalResult {
let data = std::str::from_utf8(&not_signed.data).unwrap();
assert_eq!(data, "Snitches get stitches, iOS sucks");

let signed = archive.fetch_mut("test_data/quicksort.wasm")?;
let signed = archive.fetch_mut("quicksort.wasm")?;

assert_eq!(signed.data.len(), 106537);
assert!(signed.verified);
Expand All @@ -267,8 +265,8 @@ fn fetch_from_encrypted() -> InternalResult {

#[test]
#[cfg(all(feature = "builder", feature = "archive", feature = "crypto"))]
fn consolidated_example() -> InternalResult {
use crate::crypto_utils::gen_keypair;
fn consolidated_test() -> InternalResult {
use crate::crypto_utils::{gen_keypair, read_keypair};
use std::{io::Cursor, time::Instant};

let mut target = Cursor::new(Vec::<u8>::new());
Expand All @@ -279,7 +277,8 @@ fn consolidated_example() -> InternalResult {
let data_3 = b"Fast-Acting Long-Lasting, *Bathroom Reader*" as &[u8];

// Builder definition
let keypair_bytes = gen_keypair().to_keypair_bytes();
let keypair = gen_keypair();
let keypair_bytes = keypair.to_keypair_bytes();

let mut config = BuilderConfig::default();
config.load_keypair(keypair_bytes.as_slice()).unwrap();
Expand All @@ -300,8 +299,8 @@ fn consolidated_example() -> InternalResult {
println!("Building took: {:?}", then.elapsed());

// parse verifying key
let keypair = &KEYPAIR[crate::SECRET_KEY_LENGTH..];
let vk = read_verifying_key(handle)?;
let sk = read_keypair(keypair_bytes.as_slice())?;
let vk = sk.verifying_key();

// open archive
let then = Instant::now();
Expand Down
7 changes: 5 additions & 2 deletions crates/vach/src/writer/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ pub struct BuilderConfig {
impl BuilderConfig {
/// Setter for the [`num_threads`](BuilderConfig::num_threads) field
#[cfg(feature = "multithreaded")]
pub fn num_threads(mut self, num_threads: NonZeroUsize) -> Self {
self.num_threads = num_threads;
pub fn num_threads(mut self, num_threads: usize) -> Self {
if let Some(n) = NonZeroUsize::new(num_threads) {
self.num_threads = n;
}

self
}

Expand Down

0 comments on commit ecc4c94

Please sign in to comment.