Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix proptest
Browse files Browse the repository at this point in the history
dr-bonez committed Nov 9, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 906ff8f commit 28e77b7
Showing 2 changed files with 23 additions and 11 deletions.
21 changes: 17 additions & 4 deletions backend/src/s9pk/merkle_archive/test.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ use crate::s9pk::merkle_archive::{Entry, EntryContents, MerkleArchive};
#[instrument]
fn test(files: Vec<(PathBuf, String)>) -> Result<(), Error> {
let mut root = DirectoryContents::<Arc<[u8]>>::new();
let mut check_set = BTreeMap::new();
let mut check_set = BTreeMap::<PathBuf, String>::new();
for (path, content) in files {
if let Err(e) = root.insert_path(
&path,
@@ -24,6 +24,20 @@ fn test(files: Vec<(PathBuf, String)>) -> Result<(), Error> {
) {
eprintln!("failed to insert file at {path:?}: {e}");
} else {
let mut remaining = check_set.split_off(&path);
while {
if let Some((p, s)) = remaining.pop_first() {
if !p.starts_with(&path) {
remaining.insert(p, s);
false
} else {
true
}
} else {
false
}
} {}
check_set.append(&mut remaining);
check_set.insert(path.clone(), content);
}
}
@@ -74,7 +88,6 @@ fn test(files: Vec<(PathBuf, String)>) -> Result<(), Error> {
proptest::proptest! {
#[test]
fn property_test(files: Vec<(PathBuf, String)>) {
// TODO: make unique
let files: Vec<(PathBuf, String)> = files.into_iter().filter(|(p, _)| p.file_name().is_some() && p.iter().all(|s| s.to_str().is_some())).collect();
if let Err(e) = test(files.clone()) {
panic!("{e}\nInput: {files:#?}\n{e:?}");
@@ -105,8 +118,8 @@ fn test_example_2() {
#[test]
fn test_example_3() {
if let Err(e) = test(vec![
(Path::new("b/").into(), "𑦪".into()),
(Path::new("a/c/0").into(), "·".into()),
(Path::new("b/a").into(), "𑦪".into()),
(Path::new("a/c/a").into(), "·".into()),
]) {
panic!("{e}\n{e:?}");
}
13 changes: 6 additions & 7 deletions backend/src/s9pk/merkle_archive/varint.rs
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@ use crate::prelude::*;
/// Most-significant byte, == 0x80
pub const MSB: u8 = 0b1000_0000;

const MAX_STR_LEN: u64 = 1024 * 1024; // 1 MiB

pub fn serialized_varint_size(n: u64) -> u64 {
VarInt::required_space(n) as u64
}
@@ -89,13 +91,10 @@ pub async fn deserialize_varint<R: AsyncRead + Unpin>(r: &mut R) -> Result<u64,
pub async fn deserialize_varstring<R: AsyncRead + Unpin>(r: &mut R) -> Result<String, Error> {
use tokio::io::AsyncReadExt;

let len = deserialize_varint(r).await?;
if len > 10000000 {
panic!("absurd len")
}
let mut buf = vec![0u8; len as usize];
r.read_exact(&mut buf).await?;
Ok(String::from_utf8(buf)?)
let len = std::cmp::min(deserialize_varint(r).await?, MAX_STR_LEN);
let mut res = String::with_capacity(len as usize);
r.take(len).read_to_string(&mut res).await?;
Ok(res)
}

#[cfg(test)]

0 comments on commit 28e77b7

Please sign in to comment.