use super::{Data, LazyNode, Metrics, Node, Payload, Property, ROOT_NAME};
use crate::metrics::{BlockMetrics, BlockStatus};
use anyhow::{bail, format_err, Error};
use fuchsia_inspect::reader as ireader;
use fuchsia_inspect::reader::snapshot::ScannedBlock;
use inspect_format::constants::MIN_ORDER_SIZE;
use inspect_format::{ArrayFormat, BlockIndex, BlockType, LinkNodeDisposition, PropertyFormat};
use std::cmp::min;
use std::collections::{HashMap, HashSet};
use zx::Vmo;
#[derive(Debug)]
pub struct Scanner {
nodes: HashMap<BlockIndex, ScannedNode>,
names: HashMap<BlockIndex, ScannedName>,
properties: HashMap<BlockIndex, ScannedProperty>,
extents: HashMap<BlockIndex, ScannedExtent>,
final_dereferenced_strings: HashMap<BlockIndex, String>,
final_nodes: HashMap<BlockIndex, Node>,
final_properties: HashMap<BlockIndex, Property>,
metrics: Metrics,
child_trees: Option<HashMap<String, LazyNode>>,
}
impl TryFrom<&[u8]> for Scanner {
type Error = Error;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let scanner = Scanner::new(None);
scanner.scan(ireader::snapshot::Snapshot::try_from(bytes.to_vec())?, bytes)
}
}
impl TryFrom<&Vmo> for Scanner {
type Error = Error;
fn try_from(vmo: &Vmo) -> Result<Self, Self::Error> {
let scanner = Scanner::new(None);
scanner.scan(ireader::snapshot::Snapshot::try_from(vmo)?, &vmo_as_buffer(vmo)?)
}
}
impl TryFrom<LazyNode> for Scanner {
type Error = Error;
fn try_from(mut vmo_tree: LazyNode) -> Result<Self, Self::Error> {
let snapshot = ireader::snapshot::Snapshot::try_from(vmo_tree.vmo())?;
let buffer = vmo_as_buffer(vmo_tree.vmo())?;
let scanner = Scanner::new(vmo_tree.take_children());
scanner.scan(snapshot, &buffer)
}
}
fn vmo_as_buffer(vmo: &Vmo) -> Result<Vec<u8>, Error> {
let size = vmo.get_size()?;
let mut buffer = vec![0u8; size as usize];
vmo.read(&mut buffer[..], 0)?;
Ok(buffer)
}
fn low_bits(number: u8, n_bits: usize) -> u8 {
let n_bits = min(n_bits, 8);
let mask = !(0xff_u16 << n_bits) as u8;
number & mask
}
fn high_bits(number: u8, n_bits: usize) -> u8 {
let n_bits = min(n_bits, 8);
let mask = !(0xff_u16 >> n_bits) as u8;
number & mask
}
const BITS_PER_BYTE: usize = 8;
fn order_to_size(order: u8) -> usize {
MIN_ORDER_SIZE << (order as usize)
}
fn check_zero_bits(
buffer: &[u8],
block: &ScannedBlock<'_>,
start: usize,
end: usize,
) -> Result<(), Error> {
if end < start {
return Err(format_err!("End must be >= start"));
}
let bits_in_block = order_to_size(block.order()) * BITS_PER_BYTE;
if start > bits_in_block - 1 {
return Ok(());
}
let end = min(end, bits_in_block - 1);
let block_offset = usize::from(block.index()) * MIN_ORDER_SIZE;
let low_byte = start / BITS_PER_BYTE;
let high_byte = end / BITS_PER_BYTE;
let bottom_bits = high_bits(buffer[low_byte + block_offset], 8 - (start % 8));
let top_bits = low_bits(buffer[high_byte + block_offset], (end % 8) + 1);
if low_byte == high_byte {
match bottom_bits & top_bits {
0 => return Ok(()),
nonzero => bail!(
"Bits {}...{} of block type {} at {} have nonzero value {}",
start,
end,
block.block_type_or()?,
block.index(),
nonzero
),
}
}
if bottom_bits != 0 {
bail!(
"Non-zero value {} for bits {}.. of block type {} at {}",
bottom_bits,
start,
block.block_type_or()?,
block.index()
);
}
if top_bits != 0 {
bail!(
"Non-zero value {} for bits ..{} of block type {} at {}",
top_bits,
end,
block.block_type_or()?,
block.index()
);
}
for byte in low_byte + 1..high_byte {
if buffer[byte + block_offset] != 0 {
bail!(
"Non-zero value {} for byte {} of block type {} at {}",
buffer[byte],
byte,
block.block_type_or()?,
block.index()
);
}
}
Ok(())
}
impl Scanner {
fn new(child_trees: Option<HashMap<String, LazyNode>>) -> Scanner {
let mut ret = Scanner {
nodes: HashMap::new(),
names: HashMap::new(),
properties: HashMap::new(),
extents: HashMap::new(),
final_dereferenced_strings: HashMap::new(),
metrics: Metrics::new(),
final_nodes: HashMap::new(),
final_properties: HashMap::new(),
child_trees,
};
ret.nodes.insert(
BlockIndex::ROOT,
ScannedNode {
validated: true,
parent: BlockIndex::ROOT,
name: BlockIndex::ROOT,
children: HashSet::new(),
properties: HashSet::new(),
metrics: None,
},
);
ret
}
fn scan(mut self, snapshot: ireader::snapshot::Snapshot, buffer: &[u8]) -> Result<Self, Error> {
let mut link_blocks: Vec<ScannedBlock<'_>> = Vec::new();
let mut string_references: Vec<ScannedStringReference> = Vec::new();
for block in snapshot.scan() {
match block.block_type_or() {
Ok(BlockType::Free) => self.process_free(block)?,
Ok(BlockType::Reserved) => self.process_reserved(block)?,
Ok(BlockType::Header) => self.process_header(block)?,
Ok(BlockType::NodeValue) => self.process_node(block)?,
Ok(BlockType::IntValue)
| Ok(BlockType::UintValue)
| Ok(BlockType::DoubleValue)
| Ok(BlockType::ArrayValue)
| Ok(BlockType::BufferValue)
| Ok(BlockType::BoolValue) => self.process_property(block, buffer)?,
Ok(BlockType::LinkValue) => link_blocks.push(block),
Ok(BlockType::Extent) => self.process_extent(block, buffer)?,
Ok(BlockType::Name) => self.process_name(block, buffer)?,
Ok(BlockType::Tombstone) => self.process_tombstone(block)?,
Ok(BlockType::StringReference) => {
string_references.push(self.process_string_reference(block)?);
}
Err(error) => return Err(format_err!("Failed to read block type: {:?}", error)),
}
}
for block in string_references.drain(..) {
let index = block.index;
let dereferenced = self.expand_string_reference(block)?;
self.final_dereferenced_strings.insert(index, dereferenced);
}
for block in link_blocks.into_iter() {
self.process_property(block, buffer)?
}
let (mut new_nodes, mut new_properties) = self.make_valid_node_tree(BlockIndex::ROOT)?;
for (node, id) in new_nodes.drain(..) {
self.final_nodes.insert(id, node);
}
for (property, id) in new_properties.drain(..) {
self.final_properties.insert(id, property);
}
self.record_unused_metrics();
Ok(self)
}
pub fn data(self) -> Data {
Data::build(self.final_nodes, self.final_properties)
}
pub fn metrics(self) -> Metrics {
self.metrics
}
fn record_unused_metrics(&mut self) {
for (_, node) in self.nodes.drain() {
if let Some(metrics) = node.metrics {
self.metrics.record(&metrics, BlockStatus::NotUsed);
}
}
for (_, name) in self.names.drain() {
self.metrics.record(&name.metrics, BlockStatus::NotUsed);
}
for (_, property) in self.properties.drain() {
self.metrics.record(&property.metrics, BlockStatus::NotUsed);
}
for (_, extent) in self.extents.drain() {
self.metrics.record(&extent.metrics, BlockStatus::NotUsed);
}
}
fn use_node(&mut self, node_id: BlockIndex) -> Result<ScannedNode, Error> {
let mut node =
self.nodes.remove(&node_id).ok_or(format_err!("No node at index {}", node_id))?;
match node.metrics {
None => {
if node_id != BlockIndex::ROOT {
return Err(format_err!("Invalid node (no metrics) at index {}", node_id));
}
}
Some(metrics) => {
self.metrics.record(&metrics, BlockStatus::Used);
node.metrics = Some(metrics); }
}
Ok(node)
}
fn use_property(&mut self, property_id: BlockIndex) -> Result<ScannedProperty, Error> {
let property = self
.properties
.remove(&property_id)
.ok_or(format_err!("No property at index {}", property_id))?;
self.metrics.record(&property.metrics, BlockStatus::Used);
Ok(property)
}
fn use_owned_name(&mut self, name_id: BlockIndex) -> Result<String, Error> {
match self.names.remove(&name_id) {
Some(name) => {
self.metrics.record(&name.metrics, BlockStatus::Used);
Ok(name.name)
}
None => match self.final_dereferenced_strings.get(&name_id) {
Some(value) => {
Ok(value.clone())
}
None => Err(format_err!("No string at index {}", name_id)),
},
}
}
fn lookup_name_or_string_reference(&mut self, name_id: BlockIndex) -> Result<String, Error> {
match self.names.get(&name_id) {
Some(name) => {
self.metrics.record(&name.metrics, BlockStatus::Used);
Ok(name.name.clone())
}
None => match self.final_dereferenced_strings.get(&name_id) {
Some(value) => {
Ok(value.clone())
}
None => Err(format_err!("No string at index {}", name_id)),
},
}
}
fn process_free(&mut self, block: ScannedBlock<'_>) -> Result<(), Error> {
self.metrics.process(block)?;
Ok(())
}
fn process_header(&mut self, block: ScannedBlock<'_>) -> Result<(), Error> {
self.metrics.process(block)?;
Ok(())
}
fn process_tombstone(&mut self, block: ScannedBlock<'_>) -> Result<(), Error> {
self.metrics.process(block)?;
Ok(())
}
fn process_reserved(&mut self, block: ScannedBlock<'_>) -> Result<(), Error> {
self.metrics.process(block)?;
Ok(())
}
fn process_extent(&mut self, block: ScannedBlock<'_>, buffer: &[u8]) -> Result<(), Error> {
check_zero_bits(buffer, &block, 40, 63)?;
self.extents.insert(
block.index(),
ScannedExtent {
next: block.next_extent()?,
data: block.extent_contents()?.to_vec(),
metrics: Metrics::analyze(block)?,
},
);
Ok(())
}
fn process_name(&mut self, block: ScannedBlock<'_>, buffer: &[u8]) -> Result<(), Error> {
check_zero_bits(buffer, &block, 28, 63)?;
self.names.insert(
block.index(),
ScannedName {
name: block.name_contents()?.to_string(),
metrics: Metrics::analyze(block)?,
},
);
Ok(())
}
fn process_node(&mut self, block: ScannedBlock<'_>) -> Result<(), Error> {
let parent = block.parent_index()?;
let id = block.index();
let name = block.name_index()?;
let mut node;
let metrics = Some(Metrics::analyze(block)?);
if let Some(placeholder) = self.nodes.remove(&id) {
node = placeholder;
node.validated = true;
node.parent = parent;
node.name = name;
node.metrics = metrics;
} else {
node = ScannedNode {
validated: true,
name,
parent,
children: HashSet::new(),
properties: HashSet::new(),
metrics,
}
}
self.nodes.insert(id, node);
self.add_to_parent(parent, id, |node| &mut node.children);
Ok(())
}
fn add_to_parent<F: FnOnce(&mut ScannedNode) -> &mut HashSet<BlockIndex>>(
&mut self,
parent: BlockIndex,
id: BlockIndex,
get_the_hashset: F, ) {
self.nodes.entry(parent).or_insert_with(|| ScannedNode {
validated: false,
name: BlockIndex::EMPTY,
parent: BlockIndex::ROOT,
children: HashSet::new(),
properties: HashSet::new(),
metrics: None,
});
if let Some(parent_node) = self.nodes.get_mut(&parent) {
get_the_hashset(parent_node).insert(id);
}
}
fn build_scanned_payload(
&mut self,
block: &ScannedBlock<'_>,
block_type: BlockType,
) -> Result<ScannedPayload, Error> {
Ok(match block_type {
BlockType::IntValue => ScannedPayload::Int(block.int_value()?),
BlockType::UintValue => ScannedPayload::Uint(block.uint_value()?),
BlockType::DoubleValue => ScannedPayload::Double(block.double_value()?),
BlockType::BoolValue => ScannedPayload::Bool(block.bool_value()?),
BlockType::BufferValue => {
let format = block.property_format()?;
let length = block.total_length()?;
let link = block.property_extent_index()?;
match format {
PropertyFormat::String => ScannedPayload::String { length, link },
PropertyFormat::Bytes => ScannedPayload::Bytes { length, link },
}
}
BlockType::ArrayValue => {
let entry_type = block.array_entry_type()?;
let slots = block.array_slots()?;
match entry_type {
BlockType::IntValue => {
let array_format = block.array_format()?;
let numbers: Result<Vec<i64>, _> =
(0..slots).map(|i| block.array_get_int_slot(i)).collect();
ScannedPayload::IntArray(numbers?, array_format)
}
BlockType::UintValue => {
let array_format = block.array_format()?;
let numbers: Result<Vec<u64>, _> =
(0..slots).map(|i| block.array_get_uint_slot(i)).collect();
ScannedPayload::UintArray(numbers?, array_format)
}
BlockType::DoubleValue => {
let array_format = block.array_format()?;
let numbers: Result<Vec<f64>, _> =
(0..slots).map(|i| block.array_get_double_slot(i)).collect();
ScannedPayload::DoubleArray(numbers?, array_format)
}
BlockType::StringReference => {
let indexes: Result<Vec<BlockIndex>, _> =
(0..slots).map(|i| block.array_get_string_index_slot(i)).collect();
ScannedPayload::StringArray(indexes?)
}
illegal_type => {
return Err(format_err!(
"No way I should see {:?} for ArrayEntryType",
illegal_type
))
}
}
}
BlockType::LinkValue => {
let child_name =
self.lookup_name_or_string_reference(block.link_content_index()?).ok().ok_or(
format_err!("Child name not found for LinkValue block {}.", block.index()),
)?;
let child_trees = self
.child_trees
.as_mut()
.ok_or(format_err!("LinkValue encountered without child tree."))?;
let child_tree = child_trees.remove(&child_name).ok_or(format_err!(
"Lazy node not found for LinkValue block {} with name {}.",
block.index(),
child_name
))?;
ScannedPayload::Link {
disposition: block.link_node_disposition()?,
scanned_tree: Box::new(Scanner::try_from(child_tree)?),
}
}
illegal_type => {
return Err(format_err!("No way I should see {:?} for BlockType", illegal_type))
}
})
}
fn process_string_reference(
&mut self,
block: ScannedBlock<'_>,
) -> Result<ScannedStringReference, Error> {
let scanned = ScannedStringReference {
index: block.index(),
value: block.inline_string_reference()?.to_vec(),
length: block.total_length()?,
next_extent: block.next_extent()?,
};
self.metrics.process(block)?;
Ok(scanned)
}
fn process_property(&mut self, block: ScannedBlock<'_>, buffer: &[u8]) -> Result<(), Error> {
if block.block_type_or()? == BlockType::ArrayValue {
check_zero_bits(buffer, &block, 80, 127)?;
}
let id = block.index();
let parent = block.parent_index()?;
let block_type = block.block_type_or()?;
let payload = self.build_scanned_payload(&block, block_type)?;
let property = ScannedProperty {
name: block.name_index()?,
parent,
payload,
metrics: Metrics::analyze(block)?,
};
self.properties.insert(id, property);
self.add_to_parent(parent, id, |node| &mut node.properties);
Ok(())
}
#[allow(clippy::type_complexity)]
fn make_valid_node_tree(
&mut self,
id: BlockIndex,
) -> Result<(Vec<(Node, BlockIndex)>, Vec<(Property, BlockIndex)>), Error> {
let scanned_node = self.use_node(id)?;
if !scanned_node.validated {
return Err(format_err!("No node at {}", id));
}
let mut nodes_in_tree = vec![];
let mut properties_under = vec![];
for node_id in scanned_node.children.iter() {
let (mut nodes_of, mut properties_of) = self.make_valid_node_tree(*node_id)?;
nodes_in_tree.append(&mut nodes_of);
properties_under.append(&mut properties_of);
}
for property_id in scanned_node.properties.iter() {
properties_under.push((self.make_valid_property(*property_id)?, *property_id));
}
let name = if id == BlockIndex::ROOT {
ROOT_NAME.to_owned()
} else {
self.use_owned_name(scanned_node.name)?
};
let this_node = Node {
name,
parent: scanned_node.parent,
children: scanned_node.children.clone(),
properties: scanned_node.properties.clone(),
};
nodes_in_tree.push((this_node, id));
Ok((nodes_in_tree, properties_under))
}
fn make_valid_property(&mut self, id: BlockIndex) -> Result<Property, Error> {
let scanned_property = self.use_property(id)?;
let name = self.use_owned_name(scanned_property.name)?;
let payload = self.make_valid_payload(scanned_property.payload)?;
Ok(Property { id, name, parent: scanned_property.parent, payload })
}
fn make_valid_payload(&mut self, payload: ScannedPayload) -> Result<Payload, Error> {
Ok(match payload {
ScannedPayload::Int(data) => Payload::Int(data),
ScannedPayload::Uint(data) => Payload::Uint(data),
ScannedPayload::Double(data) => Payload::Double(data),
ScannedPayload::Bool(data) => Payload::Bool(data),
ScannedPayload::IntArray(data, format) => Payload::IntArray(data, format),
ScannedPayload::UintArray(data, format) => Payload::UintArray(data, format),
ScannedPayload::DoubleArray(data, format) => Payload::DoubleArray(data, format),
ScannedPayload::StringArray(indexes) => Payload::StringArray(
indexes
.iter()
.map(|i| {
if *i == BlockIndex::EMPTY {
return "".into();
}
self.final_dereferenced_strings.get(i).unwrap().clone()
})
.collect(),
),
ScannedPayload::Bytes { length, link } => {
Payload::Bytes(self.make_valid_vector(length, link)?)
}
ScannedPayload::String { length, link } => Payload::String(
std::str::from_utf8(&self.make_valid_vector(length, link)?)?.to_owned(),
),
ScannedPayload::Link { disposition, scanned_tree } => {
Payload::Link { disposition, parsed_data: scanned_tree.data() }
}
})
}
fn expand_string_reference(
&mut self,
mut block: ScannedStringReference,
) -> Result<String, Error> {
let length_of_inlined = block.value.len();
if block.next_extent != BlockIndex::EMPTY {
block.value.append(
&mut self.make_valid_vector(block.length - length_of_inlined, block.next_extent)?,
);
}
Ok(String::from_utf8(block.value)?)
}
fn make_valid_vector(&mut self, length: usize, link: BlockIndex) -> Result<Vec<u8>, Error> {
let mut dest = vec![];
let mut length_remaining = length;
let mut next_link = link;
while length_remaining > 0 {
let mut extent =
self.extents.remove(&next_link).ok_or(format_err!("No extent at {}", next_link))?;
let copy_len = min(extent.data.len(), length_remaining);
extent.metrics.set_data_bytes(copy_len);
self.metrics.record(&extent.metrics, BlockStatus::Used);
dest.extend_from_slice(&extent.data[..copy_len]);
length_remaining -= copy_len;
next_link = extent.next;
}
Ok(dest)
}
}
#[derive(Debug)]
struct ScannedNode {
validated: bool,
name: BlockIndex,
parent: BlockIndex,
children: HashSet<BlockIndex>,
properties: HashSet<BlockIndex>,
metrics: Option<BlockMetrics>,
}
#[derive(Debug)]
struct ScannedProperty {
name: BlockIndex,
parent: BlockIndex,
payload: ScannedPayload,
metrics: BlockMetrics,
}
#[derive(Debug)]
struct ScannedStringReference {
index: BlockIndex,
value: Vec<u8>,
length: usize,
next_extent: BlockIndex,
}
#[derive(Debug)]
struct ScannedName {
name: String,
metrics: BlockMetrics,
}
#[derive(Debug)]
struct ScannedExtent {
next: BlockIndex,
data: Vec<u8>,
metrics: BlockMetrics,
}
#[derive(Debug)]
enum ScannedPayload {
String { length: usize, link: BlockIndex },
Bytes { length: usize, link: BlockIndex },
Int(i64),
Uint(u64),
Double(f64),
Bool(bool),
IntArray(Vec<i64>, ArrayFormat),
UintArray(Vec<u64>, ArrayFormat),
DoubleArray(Vec<f64>, ArrayFormat),
StringArray(Vec<BlockIndex>),
Link { disposition: LinkNodeDisposition, scanned_tree: Box<Scanner> },
}
#[cfg(test)]
mod tests {
use super::*;
use crate::*;
use fidl_diagnostics_validate::*;
use fuchsia_inspect::reader::snapshot::BackingBuffer;
use inspect_format::{constants, Block, HeaderFields, PayloadFields, ReadBytes};
use num_traits::ToPrimitive;
const MAX_BLOCK_BITS: usize = constants::MAX_ORDER_SIZE * BITS_PER_BYTE;
fn copy_into(source: &[u8], dest: &mut [u8], offset: usize) {
dest[offset..offset + source.len()].copy_from_slice(source);
}
fn try_byte(
buffer: &mut [u8],
(index, offset): (usize, usize),
value: u8,
predicted: Option<&str>,
) {
let location = index * 16 + offset;
let previous = buffer[location];
buffer[location] = value;
let actual = data::Scanner::try_from(buffer as &[u8]).map(|d| d.data().to_string());
if predicted.is_none() {
if actual.is_err() {
println!(
"With ({},{}) -> {}, got expected error {:?}",
index, offset, value, actual
);
} else {
println!(
"BAD: With ({},{}) -> {}, expected error but got string {:?}",
index,
offset,
value,
actual.as_ref().unwrap()
);
}
} else if actual.is_err() {
println!(
"BAD: With ({},{}) -> {}, got unexpected error {:?}",
index, offset, value, actual
);
} else if actual.as_ref().ok().map(|s| &s[..]) == predicted {
println!(
"With ({},{}) -> {}, got expected string {:?}",
index,
offset,
value,
predicted.unwrap()
);
} else {
println!(
"BAD: With ({},{}) -> {}, expected string {:?} but got {:?}",
index,
offset,
value,
predicted.unwrap(),
actual.as_ref().unwrap()
);
println!("Raw data: {:?}", data::Scanner::try_from(buffer as &[u8]))
}
assert_eq!(predicted, actual.as_ref().ok().map(|s| &s[..]));
buffer[location] = previous;
}
fn put_header<T: ReadBytes>(header: &Block<&mut T>, buffer: &mut [u8], index: usize) {
copy_into(&HeaderFields::value(header).to_le_bytes(), buffer, index * 16);
}
fn put_payload<T: ReadBytes>(payload: &Block<&mut T>, buffer: &mut [u8], index: usize) {
copy_into(&PayloadFields::value(payload).to_le_bytes(), buffer, index * 16 + 8);
}
#[fuchsia::test]
fn test_scanning_string_reference() {
let mut buffer = [0u8; 4096];
const NODE: BlockIndex = BlockIndex::new(3);
const NUMBER_NAME: BlockIndex = BlockIndex::new(4);
const NUMBER_EXTENT: BlockIndex = BlockIndex::new(5);
let mut container = [0u8; 16];
let mut header = Block::new(&mut container, BlockIndex::EMPTY);
HeaderFields::set_order(&mut header, 0);
HeaderFields::set_block_type(&mut header, BlockType::Header.to_u8().unwrap());
HeaderFields::set_header_magic(&mut header, constants::HEADER_MAGIC_NUMBER);
HeaderFields::set_header_version(&mut header, constants::HEADER_VERSION_NUMBER);
put_header(&header, &mut buffer, (*BlockIndex::HEADER).try_into().unwrap());
HeaderFields::set_order(&mut header, 0);
HeaderFields::set_block_type(&mut header, BlockType::NodeValue.to_u8().unwrap());
HeaderFields::set_value_name_index(&mut header, *NUMBER_NAME);
HeaderFields::set_value_parent_index(&mut header, *BlockIndex::HEADER);
put_header(&header, &mut buffer, (*NODE).try_into().unwrap());
HeaderFields::set_order(&mut header, 0);
HeaderFields::set_block_type(&mut header, BlockType::StringReference.to_u8().unwrap());
HeaderFields::set_extent_next_index(&mut header, *NUMBER_EXTENT);
put_header(&header, &mut buffer, (*NUMBER_NAME).try_into().unwrap());
copy_into(&[6, 0, 0, 0], &mut buffer, (*NUMBER_NAME * 16 + 8).try_into().unwrap());
copy_into(b"numb", &mut buffer, (*NUMBER_NAME * 16 + 12).try_into().unwrap());
let mut container = [0u8; 16];
let mut number_extent = Block::new(&mut container, BlockIndex::EMPTY);
HeaderFields::set_order(&mut number_extent, 0);
HeaderFields::set_block_type(&mut number_extent, BlockType::Extent.to_u8().unwrap());
HeaderFields::set_extent_next_index(&mut number_extent, 0);
put_header(&number_extent, &mut buffer, (*NUMBER_EXTENT).try_into().unwrap());
copy_into(b"er", &mut buffer, (*NUMBER_EXTENT * 16 + 8).try_into().unwrap());
try_byte(&mut buffer, (16, 0), 0, Some("root ->\n> number ->"));
}
#[fuchsia::test]
fn test_scanning_logic() {
let mut buffer = [0u8; 4096];
const HEADER: usize = 0;
{
let mut container = [0u8; 16];
let mut header = Block::new(&mut container, BlockIndex::EMPTY);
HeaderFields::set_order(&mut header, 0);
HeaderFields::set_block_type(&mut header, BlockType::Header.to_u8().unwrap());
HeaderFields::set_header_magic(&mut header, constants::HEADER_MAGIC_NUMBER);
HeaderFields::set_header_version(&mut header, constants::HEADER_VERSION_NUMBER);
put_header(&header, &mut buffer, HEADER);
}
const ROOT: usize = 1;
{
let mut container = [0u8; 16];
let mut header = Block::new(&mut container, BlockIndex::EMPTY);
HeaderFields::set_order(&mut header, 0);
HeaderFields::set_block_type(&mut header, BlockType::NodeValue.to_u8().unwrap());
HeaderFields::set_value_name_index(&mut header, 2);
HeaderFields::set_value_parent_index(&mut header, 0);
put_header(&header, &mut buffer, ROOT);
}
const ROOT_NAME: usize = 2;
{
let mut container = [0u8; 16];
let mut header = Block::new(&mut container, BlockIndex::EMPTY);
HeaderFields::set_order(&mut header, 0);
HeaderFields::set_block_type(&mut header, BlockType::Name.to_u8().unwrap());
HeaderFields::set_name_length(&mut header, 4);
put_header(&header, &mut buffer, ROOT_NAME);
}
copy_into(b"node", &mut buffer, ROOT_NAME * 16 + 8);
try_byte(&mut buffer, (16, 0), 0, Some("root ->\n> node ->"));
try_byte(&mut buffer, (HEADER, 7), 0, None);
try_byte(&mut buffer, (ROOT, 1), 1, Some("root ->"));
try_byte(&mut buffer, (ROOT, 5), 1, None);
try_byte(&mut buffer, (HEADER, 8), 1, None);
try_byte(&mut buffer, (HEADER, 8), 2, Some("root ->\n> node ->"));
const NUMBER: usize = 4;
let mut container = [0u8; 16];
let mut number_header = Block::new(&mut container, BlockIndex::EMPTY);
HeaderFields::set_order(&mut number_header, 0);
HeaderFields::set_block_type(&mut number_header, BlockType::IntValue.to_u8().unwrap());
HeaderFields::set_value_name_index(&mut number_header, 3);
HeaderFields::set_value_parent_index(&mut number_header, 1);
put_header(&number_header, &mut buffer, NUMBER);
const NUMBER_NAME: usize = 3;
{
let mut container = [0u8; 16];
let mut header = Block::new(&mut container, BlockIndex::EMPTY);
HeaderFields::set_order(&mut header, 0);
HeaderFields::set_block_type(&mut header, BlockType::Name.to_u8().unwrap());
HeaderFields::set_name_length(&mut header, 6);
put_header(&header, &mut buffer, NUMBER_NAME);
copy_into(b"number", &mut buffer, NUMBER_NAME * 16 + 8);
}
try_byte(&mut buffer, (HEADER, 8), 2, Some("root ->\n> node ->\n> > number: Int(0)"));
try_byte(&mut buffer, (NUMBER, 1), 5, Some("root ->\n> node ->\n> > number: Uint(0)"));
try_byte(&mut buffer, (NUMBER, 1), 6, Some("root ->\n> node ->\n> > number: Double(0.0)"));
try_byte(&mut buffer, (NUMBER, 1), 7, Some("root ->\n> node ->\n> > number: String(\"\")"));
try_byte(&mut buffer, (NUMBER, 1), 0xb0, None);
try_byte(&mut buffer, (NUMBER, 1), 0xf, None);
HeaderFields::set_order(&mut number_header, 2);
HeaderFields::set_block_type(&mut number_header, BlockType::ArrayValue.to_u8().unwrap());
put_header(&number_header, &mut buffer, NUMBER);
try_byte(&mut buffer, (128, 0), 0, None);
try_byte(
&mut buffer,
(NUMBER, 8),
0x04,
Some("root ->\n> node ->\n> > number: IntArray([], Default)"),
);
try_byte(
&mut buffer,
(NUMBER, 8),
0x05,
Some("root ->\n> node ->\n> > number: UintArray([], Default)"),
);
try_byte(
&mut buffer,
(NUMBER, 8),
0x06,
Some("root ->\n> node ->\n> > number: DoubleArray([], Default)"),
);
try_byte(
&mut buffer,
(NUMBER, 8),
0x14,
Some("root ->\n> node ->\n> > number: IntArray([], LinearHistogram)"),
);
try_byte(
&mut buffer,
(NUMBER, 8),
0x24,
Some("root ->\n> node ->\n> > number: IntArray([], ExponentialHistogram)"),
);
try_byte(&mut buffer, (NUMBER, 8), 0x34, None);
try_byte(&mut buffer, (NUMBER, 8), BlockType::ArrayValue.to_u8().unwrap(), None);
buffer[NUMBER * 16 + 8] = 4; buffer[NUMBER * 16 + 9] = 2; try_byte(
&mut buffer,
(NUMBER, 16),
42,
Some("root ->\n> node ->\n> > number: IntArray([42, 0], Default)"),
);
try_byte(
&mut buffer,
(NUMBER, 24),
42,
Some("root ->\n> node ->\n> > number: IntArray([0, 42], Default)"),
);
}
#[fuchsia::test]
async fn test_to_string_order() -> Result<(), Error> {
let int0 = Property {
name: "int0".into(),
id: 2.into(),
parent: 1.into(),
payload: Payload::Int(0),
}
.to_string("");
let int1_struct = Property {
name: "int1".into(),
id: 2.into(),
parent: 1.into(),
payload: Payload::Int(1),
};
let int1 = int1_struct.to_string("");
assert_ne!(int0, int1);
let uint0 = Property {
name: "uint0".into(),
id: 2.into(),
parent: 1.into(),
payload: Payload::Uint(0),
}
.to_string("");
assert_ne!(int0, uint0);
let int0_different_name = Property {
name: "int0_different_name".into(),
id: 2.into(),
parent: 1.into(),
payload: Payload::Int(0),
}
.to_string("");
assert_ne!(int0, int0_different_name);
let uint0_different_ids = Property {
name: "uint0".into(),
id: 3.into(),
parent: 4.into(),
payload: Payload::Uint(0),
}
.to_string("");
assert_eq!(uint0, uint0_different_ids);
let int1_different_prefix = int1_struct.to_string("foo");
assert_ne!(int1, int1_different_prefix);
let mut puppet1 = puppet::tests::local_incomplete_puppet().await?;
let mut child1_action = create_node!(parent:0, id:1, name:"child1");
let mut child2_action = create_node!(parent:0, id:2, name:"child2");
let mut property1_action =
create_numeric_property!(parent:0, id:1, name:"prop1", value: Value::IntT(1));
let mut property2_action =
create_numeric_property!(parent:0, id:2, name:"prop2", value: Value::IntT(2));
puppet1.apply(&mut child1_action).await?;
puppet1.apply(&mut child2_action).await?;
let mut puppet2 = puppet::tests::local_incomplete_puppet().await?;
puppet2.apply(&mut child2_action).await?;
puppet2.apply(&mut child1_action).await?;
assert_eq!(puppet1.read_data().await?.to_string(), puppet2.read_data().await?.to_string());
puppet1.apply(&mut property1_action).await?;
puppet1.apply(&mut property2_action).await?;
puppet2.apply(&mut property2_action).await?;
puppet2.apply(&mut property1_action).await?;
assert_eq!(puppet1.read_data().await?.to_string(), puppet2.read_data().await?.to_string());
puppet1 = puppet::tests::local_incomplete_puppet().await?;
puppet2 = puppet::tests::local_incomplete_puppet().await?;
let mut subchild2_action = create_node!(parent:1, id:2, name:"child2");
puppet1.apply(&mut child1_action).await?;
puppet2.apply(&mut child1_action).await?;
puppet1.apply(&mut child2_action).await?;
puppet2.apply(&mut subchild2_action).await?;
assert_ne!(puppet1.read_data().await?.to_string(), puppet2.read_data().await?.to_string());
let mut subproperty2_action =
create_numeric_property!(parent:1, id:2, name:"prop2", value: Value::IntT(1));
puppet1.apply(&mut child1_action).await?;
puppet2.apply(&mut child1_action).await?;
puppet1.apply(&mut property2_action).await?;
puppet2.apply(&mut subproperty2_action).await?;
Ok(())
}
#[fuchsia::test]
fn test_bit_ops() -> Result<(), Error> {
assert_eq!(low_bits(0xff, 3), 7);
assert_eq!(low_bits(0x04, 3), 4);
assert_eq!(low_bits(0xf8, 3), 0);
assert_eq!(low_bits(0xab, 99), 0xab);
assert_eq!(low_bits(0xff, 0), 0);
assert_eq!(high_bits(0xff, 3), 0xe0);
assert_eq!(high_bits(0x20, 3), 0x20);
assert_eq!(high_bits(0x1f, 3), 0);
assert_eq!(high_bits(0xab, 99), 0xab);
assert_eq!(high_bits(0xff, 0), 0);
Ok(())
}
#[fuchsia::test]
fn test_zero_bits() -> Result<(), Error> {
let mut buffer = [0u8; 48];
for byte in buffer.iter_mut().take(16) {
*byte = 0xff;
}
for byte in buffer.iter_mut().skip(32) {
*byte = 0xff;
}
{
let backing_buffer = BackingBuffer::from(buffer.to_vec());
let block = Block::new(&backing_buffer, 1.into());
assert!(check_zero_bits(&buffer, &block, 1, 0).is_err());
assert!(check_zero_bits(&buffer, &block, 0, 0).is_ok());
assert!(check_zero_bits(&buffer, &block, 0, MAX_BLOCK_BITS).is_ok());
}
buffer[1 + 16] = 1;
{
let backing_buffer = BackingBuffer::from(buffer.to_vec());
let block = Block::new(&backing_buffer, 1.into());
assert!(check_zero_bits(&buffer, &block, 8, 8).is_err());
assert!(check_zero_bits(&buffer, &block, 8, MAX_BLOCK_BITS).is_err());
assert!(check_zero_bits(&buffer, &block, 9, MAX_BLOCK_BITS).is_ok());
}
buffer[2 + 16] = 0x80;
{
let backing_buffer = BackingBuffer::from(buffer.to_vec());
let block = Block::new(&backing_buffer, 1.into());
assert!(check_zero_bits(&buffer, &block, 9, MAX_BLOCK_BITS).is_err());
assert!(check_zero_bits(&buffer, &block, 9, 22).is_ok());
assert!(check_zero_bits(&buffer, &block, 24, MAX_BLOCK_BITS).is_ok());
assert!(check_zero_bits(&buffer, &block, 24, 63).is_ok());
}
buffer[2 + 16] = 0x20;
{
let backing_buffer = BackingBuffer::from(buffer.to_vec());
let block = Block::new(&backing_buffer, 1.into());
assert!(check_zero_bits(&buffer, &block, 16, 20).is_ok());
assert!(check_zero_bits(&buffer, &block, 21, 21).is_err());
assert!(check_zero_bits(&buffer, &block, 22, 63).is_ok());
}
buffer[7 + 16] = 0x80;
{
let backing_buffer = BackingBuffer::from(buffer.to_vec());
let block = Block::new(&backing_buffer, 1.into());
assert!(check_zero_bits(&buffer, &block, 22, 63).is_err());
assert!(check_zero_bits(&buffer, &block, 22, 62).is_ok());
}
buffer[3 + 16] = 0x10;
{
let backing_buffer = BackingBuffer::from(buffer.to_vec());
let block = Block::new(&backing_buffer, 1.into());
assert!(check_zero_bits(&buffer, &block, 22, 62).is_err());
}
buffer[3 + 16] = 0;
buffer[4 + 16] = 0x10;
{
let backing_buffer = BackingBuffer::from(buffer.to_vec());
let block = Block::new(&backing_buffer, 1.into());
assert!(check_zero_bits(&buffer, &block, 22, 62).is_err());
}
buffer[4 + 16] = 0;
buffer[5 + 16] = 0x10;
{
let backing_buffer = BackingBuffer::from(buffer.to_vec());
let block = Block::new(&backing_buffer, 1.into());
assert!(check_zero_bits(&buffer, &block, 22, 62).is_err());
}
buffer[5 + 16] = 0;
buffer[6 + 16] = 0x10;
{
let backing_buffer = BackingBuffer::from(buffer.to_vec());
let block = Block::new(&backing_buffer, 1.into());
assert!(check_zero_bits(&buffer, &block, 22, 62).is_err());
}
buffer[1 + 16] = 0x81;
{
let backing_buffer = BackingBuffer::from(buffer.to_vec());
let block = Block::new(&backing_buffer, 1.into());
assert!(check_zero_bits(&buffer, &block, 9, 14).is_ok());
assert!(check_zero_bits(&buffer, &block, 8, 14).is_err());
assert!(check_zero_bits(&buffer, &block, 9, 15).is_err());
}
Ok(())
}
#[fuchsia::test]
fn test_reserved_fields() {
let mut buffer = [0u8; 4096];
const HEADER: usize = 0;
{
let mut container = [0u8; 16];
let mut header = Block::new(&mut container, BlockIndex::EMPTY);
HeaderFields::set_order(&mut header, 0);
HeaderFields::set_block_type(&mut header, BlockType::Header.to_u8().unwrap());
HeaderFields::set_header_magic(&mut header, constants::HEADER_MAGIC_NUMBER);
HeaderFields::set_header_version(&mut header, constants::HEADER_VERSION_NUMBER);
put_header(&header, &mut buffer, HEADER);
}
const VALUE: usize = 1;
let mut container = [0u8; 16];
let mut value_header = Block::new(&mut container, BlockIndex::EMPTY);
HeaderFields::set_order(&mut value_header, 0);
HeaderFields::set_block_type(&mut value_header, BlockType::NodeValue.to_u8().unwrap());
HeaderFields::set_value_name_index(&mut value_header, 2);
HeaderFields::set_value_parent_index(&mut value_header, 0);
put_header(&value_header, &mut buffer, VALUE);
const VALUE_NAME: usize = 2;
{
let mut buf = [0; 16];
let mut header = Block::new(&mut buf, 0.into());
HeaderFields::set_order(&mut header, 0);
HeaderFields::set_block_type(&mut header, BlockType::Name.to_u8().unwrap());
HeaderFields::set_name_length(&mut header, 5);
put_header(&header, &mut buffer, VALUE_NAME);
}
copy_into(b"value", &mut buffer, VALUE_NAME * 16 + 8);
const EXTENT: usize = 3;
{
let mut container = [0u8; 16];
let mut header = Block::new(&mut container, BlockIndex::EMPTY);
HeaderFields::set_order(&mut header, 0);
HeaderFields::set_block_type(&mut header, BlockType::Extent.to_u8().unwrap());
HeaderFields::set_extent_next_index(&mut header, 0);
put_header(&header, &mut buffer, EXTENT);
}
try_byte(&mut buffer, (16, 0), 0, Some("root ->\n> value ->"));
try_byte(&mut buffer, (6, 7), 42, Some("root ->\n> value ->"));
try_byte(&mut buffer, (VALUE_NAME, 7), 42, None);
try_byte(&mut buffer, (EXTENT, 6), 42, None);
HeaderFields::set_block_type(&mut value_header, BlockType::ArrayValue.to_u8().unwrap());
put_header(&value_header, &mut buffer, VALUE);
{
let mut container = [0u8; 16];
let mut array_subheader = Block::new(&mut container, BlockIndex::EMPTY);
PayloadFields::set_array_entry_type(
&mut array_subheader,
BlockType::IntValue.to_u8().unwrap(),
);
PayloadFields::set_array_flags(
&mut array_subheader,
ArrayFormat::Default.to_u8().unwrap(),
);
put_payload(&array_subheader, &mut buffer, VALUE);
}
try_byte(&mut buffer, (16, 0), 0, Some("root ->\n> value: IntArray([], Default)"));
try_byte(&mut buffer, (VALUE, 12), 42, None);
HeaderFields::set_block_type(&mut value_header, BlockType::IntValue.to_u8().unwrap());
put_header(&value_header, &mut buffer, VALUE);
try_byte(&mut buffer, (VALUE, 12), 42, Some("root ->\n> value: Int(180388626436)"));
}
}