use std::sync::Arc;
use bytes::Bytes;
use super::super::dict::STEPS;
use crate::budget::DecodeBuf;
use crate::error::LimitKind;
use crate::limits::{Limits, Meter};
use crate::stream::Endian;
use crate::typetree::{NodeList, NodeRow, Tree};
use crate::walk::{ReadCtx, ReadOptions, RefTypes, WriteCtx, WriteRules, boost, python};
const N: u32 = 1 << 16;
fn write_steps(rows: Vec<NodeRow>, data: &[u8]) -> [(&'static str, u64); 2] {
let tree = Tree::compile(
NodeList::trusted(rows).unwrap().into_shared().unwrap(),
&Limits::default(),
)
.unwrap();
let data = Bytes::copy_from_slice(data);
let options = ReadOptions::default();
let meter = Meter::unlimited();
let ctx = ReadCtx {
endian: Endian::Little,
ref_types: RefTypes::List(&[]),
options: &options,
meter: &meter,
};
let limits = Limits::default();
let wctx = WriteCtx {
endian: Endian::Little,
ref_types: RefTypes::List(&[]),
rules: WriteRules::UnityPy,
limits: &limits,
};
["boost", "python"].map(|reader| {
let p = match reader {
"boost" => boost::read(&data, &tree, &ctx),
_ => python::read(&data, 0, &tree, &ctx),
}
.unwrap();
let mut out = DecodeBuf::new(&meter, 0, u64::MAX, LimitKind::InputMemory).unwrap();
STEPS.with(|s| s.set(0));
super::write(&p, &tree, &wctx, &mut out).unwrap();
(reader, STEPS.with(std::cell::Cell::get))
})
}
fn ints(n: u32) -> Vec<u8> {
(0..n).flat_map(u32::to_le_bytes).collect()
}
#[test]
fn a_wide_referenced_object_writes_in_linear_map_steps() {
let mut rows = vec![
NodeRow::new(0, "Base", "Base"),
NodeRow::new(1, "ReferencedObject", "o"),
];
for i in 0..N {
rows.push(NodeRow::new(2, "int", format!("c{i}").as_str()));
}
for (reader, steps) in write_steps(rows, &ints(N)) {
assert!(
steps <= 2 * u64::from(N),
"{reader}: {steps} map steps for {N} children"
);
}
}
#[test]
fn a_dict_keyed_by_another_node_writes_in_linear_map_steps() {
let mut rows = vec![NodeRow::new(0, "Base", "Base"), NodeRow::new(1, "Foo", "a")];
for i in 0..N {
rows.push(NodeRow::new(2, "int", format!("c{i}").as_str()));
}
rows.push(NodeRow::new(1, "Bar", "a"));
for i in (0..N).rev() {
rows.push(NodeRow::new(2, "int", format!("c{i}").as_str()));
}
for (reader, steps) in write_steps(rows, &ints(2 * N)) {
assert!(
steps <= 4 * u64::from(N),
"{reader}: {steps} map steps for {N} children"
);
}
}
#[test]
fn many_siblings_share_one_foreign_dict_in_linear_map_steps() {
const K: u32 = 1 << 12;
let mut rows = vec![NodeRow::new(0, "Base", "Base")];
for _ in 0..K {
rows.push(NodeRow::new(1, "Foo", "a"));
rows.push(NodeRow::new(2, "int", "zz"));
}
rows.push(NodeRow::new(1, "Bar", "a"));
for i in 0..K - 1 {
rows.push(NodeRow::new(2, "int", format!("c{i}").as_str()));
}
rows.push(NodeRow::new(2, "int", "zz"));
for (reader, steps) in write_steps(rows, &ints(2 * K)) {
assert!(
steps <= 4 * u64::from(2 * K),
"{reader}: {steps} map steps for {K} siblings and {K} entries"
);
}
}
#[test]
fn a_dict_inside_a_shared_dict_is_indexed_once() {
const K: u32 = 1 << 12;
let mut rows = vec![NodeRow::new(0, "Base", "Base")];
for _ in 0..K {
rows.push(NodeRow::new(1, "Foo", "a"));
rows.push(NodeRow::new(2, "X", "x"));
rows.push(NodeRow::new(3, "int", "zz"));
}
rows.push(NodeRow::new(1, "Bar", "a"));
rows.push(NodeRow::new(2, "Y", "x"));
for i in 0..K - 1 {
rows.push(NodeRow::new(3, "int", format!("c{i}").as_str()));
}
rows.push(NodeRow::new(3, "int", "zz"));
for (reader, steps) in write_steps(rows, &ints(2 * K)) {
assert!(
steps <= 4 * u64::from(2 * K),
"{reader}: {steps} map steps for {K} siblings and {K} entries"
);
}
}
#[test]
fn errors_no_recorded_write_reaches_name_their_exceptions() {
use crate::LimitError;
use crate::provider::NoTree;
use crate::walk::{ParseError, WriteError};
for why in [
NoTree::UnknownClass,
NoTree::NoEntry,
NoTree::NoReleaseRoot,
NoTree::NoVersion,
] {
let e = WriteError::NoTypeTree {
class_id: 1,
why: Some(why),
};
assert_eq!(e.exceptions(), [why.exception()], "{why:?}");
}
let limit = LimitError::refused(LimitKind::ValueDepth, 2, 1);
let charge = Meter::new(
0,
Arc::new(crate::budget::Unbounded),
crate::budget::Reservation::Input,
)
.charge(1)
.unwrap_err();
for e in [
WriteError::NoTypeTree {
class_id: 1,
why: None,
},
WriteError::Limit(limit),
WriteError::Charge(charge),
WriteError::Tree(Box::new(ParseError::NoTypeTree {
class_id: 1,
why: None,
})),
] {
assert!(e.exceptions().is_empty(), "{e:?}");
}
}