1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! # Reading inspect data
//!
//! Provides an API for reading inspect data from a given [`Inspector`][Inspector], VMO, byte
//! vectors, etc.
//!
//! ## Concepts
//!
//! ### Diagnostics hierarchy
//!
//! Represents the inspect VMO as a regular tree of data. The API ensures that this structure
//! always contains the lazy values loaded as well.
//!
//! ### Partial node hierarchy
//!
//! Represents the inspect VMO as a regular tree of data, but unlike the Diagnostics Hierarchy, it
//! won't contain the lazy values loaded. An API is provided for converting this to a diagnostics
//! hierarchy ([`Into<DiagnosticsHierarchy>`](impl-Into<DiagnosticsHierarchy<String>>)), but keep
//! in mind that the resulting diagnostics hierarchy won't contain any of the lazy values.
//!
//! ## Example usage
//!
//! ```rust
//! use fuchsia_inspect::{Inspector, reader};
//!
//! let inspector = Inspector::default();
//! // ...
//! let hierarchy = reader::read(&inspector)?;
//! ```

use crate::reader::snapshot::{ScannedBlock, Snapshot};
use diagnostics_hierarchy::*;
use inspect_format::{BlockIndex, BlockType};
use maplit::btreemap;
use std::borrow::Cow;
use std::collections::BTreeMap;

pub use crate::reader::error::ReaderError;
pub use crate::reader::readable_tree::ReadableTree;
pub use crate::reader::tree_reader::{read, read_with_timeout};
pub use diagnostics_hierarchy::{ArrayContent, ArrayFormat, DiagnosticsHierarchy, Property};
pub use inspect_format::LinkNodeDisposition;

mod error;
mod readable_tree;
pub mod snapshot;
mod tree_reader;

/// A partial node hierarchy represents a node in an inspect tree without
/// the linked (lazy) nodes expanded.
/// Usually a client would prefer to use a `DiagnosticsHierarchy` to get the full
/// inspect tree.
#[derive(Clone, Debug, PartialEq)]
pub struct PartialNodeHierarchy {
    /// The name of this node.
    pub(crate) name: String,

    /// The properties for the node.
    pub(crate) properties: Vec<Property>,

    /// The children of this node.
    pub(crate) children: Vec<PartialNodeHierarchy>,

    /// Links this node hierarchy haven't expanded yet.
    pub(crate) links: Vec<LinkValue>,
}

/// A lazy node in a hierarchy.
#[derive(Debug, PartialEq, Clone)]
pub(crate) struct LinkValue {
    /// The name of the link.
    pub name: String,

    /// The content of the link.
    pub content: String,

    /// The disposition of the link in the hierarchy when evaluated.
    pub disposition: LinkNodeDisposition,
}

impl PartialNodeHierarchy {
    /// Creates an `PartialNodeHierarchy` with the given `name`, `properties` and `children`
    pub fn new(
        name: impl Into<String>,
        properties: Vec<Property>,
        children: Vec<PartialNodeHierarchy>,
    ) -> Self {
        Self { name: name.into(), properties, children, links: vec![] }
    }

    /// Creates an empty `PartialNodeHierarchy`
    pub fn empty() -> Self {
        PartialNodeHierarchy::new("", vec![], vec![])
    }

    /// Whether the partial hierarchy is complete or not. A complete node hierarchy
    /// has all the links loaded into it.
    pub fn is_complete(&self) -> bool {
        self.links.is_empty()
    }
}

/// Transforms the partial hierarchy into a `DiagnosticsHierarchy`. If the node hierarchy had
/// unexpanded links, those will appear as missing values.
impl From<PartialNodeHierarchy> for DiagnosticsHierarchy {
    fn from(partial: PartialNodeHierarchy) -> DiagnosticsHierarchy {
        DiagnosticsHierarchy {
            name: partial.name,
            children: partial.children.into_iter().map(|child| child.into()).collect(),
            properties: partial.properties,
            missing: partial
                .links
                .into_iter()
                .map(|link_value| MissingValue {
                    reason: MissingValueReason::LinkNeverExpanded,
                    name: link_value.name,
                })
                .collect(),
        }
    }
}

impl DiagnosticsHierarchyGetter<String> for PartialNodeHierarchy {
    fn get_diagnostics_hierarchy(&self) -> Cow<'_, DiagnosticsHierarchy> {
        let hierarchy: DiagnosticsHierarchy = self.clone().into();
        if !hierarchy.missing.is_empty() {
            panic!(
                "Missing links: {:?}",
                hierarchy
                    .missing
                    .iter()
                    .map(|missing| {
                        format!("(name:{:?}, reason:{:?})", missing.name, missing.reason)
                    })
                    .collect::<Vec<_>>()
                    .join(", ")
            );
        }
        Cow::Owned(hierarchy)
    }
}

impl TryFrom<Snapshot> for PartialNodeHierarchy {
    type Error = ReaderError;

    fn try_from(snapshot: Snapshot) -> Result<Self, Self::Error> {
        read_snapshot(&snapshot)
    }
}

#[cfg(target_os = "fuchsia")]
impl TryFrom<&zx::Vmo> for PartialNodeHierarchy {
    type Error = ReaderError;

    fn try_from(vmo: &zx::Vmo) -> Result<Self, Self::Error> {
        let snapshot = Snapshot::try_from(vmo)?;
        read_snapshot(&snapshot)
    }
}

impl TryFrom<Vec<u8>> for PartialNodeHierarchy {
    type Error = ReaderError;

    fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
        let snapshot = Snapshot::try_from(bytes)?;
        read_snapshot(&snapshot)
    }
}

/// Read the blocks in the snapshot as a node hierarchy.
fn read_snapshot(snapshot: &Snapshot) -> Result<PartialNodeHierarchy, ReaderError> {
    let result = scan_blocks(snapshot)?;
    result.reduce()
}

fn scan_blocks(snapshot: &Snapshot) -> Result<ScanResult<'_>, ReaderError> {
    let mut result = ScanResult::new(snapshot);
    for block in snapshot.scan() {
        if block.index() == BlockIndex::ROOT
            && block.block_type_or().map_err(ReaderError::VmoFormat)? != BlockType::Header
        {
            return Err(ReaderError::MissingHeader);
        }
        match block.block_type_or().map_err(ReaderError::VmoFormat)? {
            BlockType::NodeValue => {
                result.parse_node(&block)?;
            }
            BlockType::IntValue | BlockType::UintValue | BlockType::DoubleValue => {
                result.parse_numeric_property(&block)?;
            }
            BlockType::BoolValue => {
                result.parse_bool_property(&block)?;
            }
            BlockType::ArrayValue => {
                result.parse_array_property(&block)?;
            }
            BlockType::BufferValue => {
                result.parse_property(&block)?;
            }
            BlockType::LinkValue => {
                result.parse_link(&block)?;
            }
            _ => {}
        }
    }
    Ok(result)
}

/// Result of scanning a snapshot before aggregating hierarchies.
struct ScanResult<'a> {
    /// All the nodes found while scanning the snapshot.
    /// Scanned nodes NodeHierarchies won't have their children filled.
    parsed_nodes: BTreeMap<BlockIndex, ScannedNode>,

    /// A snapshot of the Inspect VMO tree.
    snapshot: &'a Snapshot,
}

/// A scanned node in the Inspect VMO tree.
#[derive(Debug)]
struct ScannedNode {
    /// The node hierarchy with properties and children nodes filled.
    partial_hierarchy: PartialNodeHierarchy,

    /// The number of children nodes this node has.
    child_nodes_count: usize,

    /// The index of the parent node of this node.
    parent_index: BlockIndex,

    /// True only if this node was intialized. Uninitialized nodes will be ignored.
    initialized: bool,
}

impl ScannedNode {
    fn new() -> Self {
        ScannedNode {
            partial_hierarchy: PartialNodeHierarchy::empty(),
            child_nodes_count: 0,
            parent_index: BlockIndex::EMPTY,
            initialized: false,
        }
    }

    /// Sets the name and parent index of the node.
    fn initialize(&mut self, name: String, parent_index: BlockIndex) {
        self.partial_hierarchy.name = name;
        self.parent_index = parent_index;
        self.initialized = true;
    }

    /// A scanned node is considered complete if the number of children in the
    /// hierarchy is the same as the number of children counted while scanning.
    fn is_complete(&self) -> bool {
        self.partial_hierarchy.children.len() == self.child_nodes_count
    }

    /// A scanned node is considered initialized if a NodeValue was parsed for it.
    fn is_initialized(&self) -> bool {
        self.initialized
    }
}

macro_rules! get_or_create_scanned_node {
    ($map:expr, $key:expr) => {
        $map.entry($key).or_insert(ScannedNode::new())
    };
}

impl<'a> ScanResult<'a> {
    fn new(snapshot: &'a Snapshot) -> Self {
        let mut root_node = ScannedNode::new();
        root_node.initialize("root".to_string(), BlockIndex::ROOT);
        let parsed_nodes = btreemap!(
            BlockIndex::ROOT => root_node,
        );
        ScanResult { snapshot, parsed_nodes }
    }

    fn reduce(self) -> Result<PartialNodeHierarchy, ReaderError> {
        // Stack of nodes that have been found that are complete.
        let mut complete_nodes = Vec::<ScannedNode>::new();

        // Maps a block index to the node there. These nodes are still not
        // complete.
        let mut pending_nodes = BTreeMap::<BlockIndex, ScannedNode>::new();

        let mut uninitialized_nodes = std::collections::BTreeSet::new();

        // Split the parsed_nodes into complete nodes and pending nodes.
        for (index, scanned_node) in self.parsed_nodes.into_iter() {
            if !scanned_node.is_initialized() {
                // Skip all nodes that were not initialized.
                uninitialized_nodes.insert(index);
                continue;
            }
            if scanned_node.is_complete() {
                if index == BlockIndex::ROOT {
                    return Ok(scanned_node.partial_hierarchy);
                }
                complete_nodes.push(scanned_node);
            } else {
                pending_nodes.insert(index, scanned_node);
            }
        }

        // Build a valid hierarchy by attaching completed nodes to their parent.
        // Once the parent is complete, it's added to the stack and we recurse
        // until the root is found (parent index = 0).
        while let Some(scanned_node) = complete_nodes.pop() {
            if uninitialized_nodes.contains(&scanned_node.parent_index) {
                // Skip children of initialized nodes. These nodes were implicitly unlinked due to
                // tombstoning.
                continue;
            }
            {
                // Add the current node to the parent hierarchy.
                let parent_node = pending_nodes
                    .get_mut(&scanned_node.parent_index)
                    .ok_or(ReaderError::ParentIndexNotFound(scanned_node.parent_index))?;
                parent_node.partial_hierarchy.children.push(scanned_node.partial_hierarchy);
            }
            if pending_nodes
                .get(&scanned_node.parent_index)
                .ok_or(ReaderError::ParentIndexNotFound(scanned_node.parent_index))?
                .is_complete()
            {
                // Safety: if pending_nodes did not contain scanned_node.parent_index,
                // we would've returned above with ParentIndexNotFound
                let parent_node = pending_nodes.remove(&scanned_node.parent_index).unwrap();
                if scanned_node.parent_index == BlockIndex::ROOT {
                    return Ok(parent_node.partial_hierarchy);
                }
                complete_nodes.push(parent_node);
            }
        }

        Err(ReaderError::MalformedTree)
    }

    pub fn get_name(&self, index: BlockIndex) -> Option<String> {
        self.snapshot.get_name(index)
    }

    fn parse_node(&mut self, block: &ScannedBlock<'_>) -> Result<(), ReaderError> {
        let name_index = block.name_index()?;
        let name = self.get_name(name_index).ok_or(ReaderError::ParseName(name_index))?;
        let parent_index = block.parent_index()?;
        get_or_create_scanned_node!(self.parsed_nodes, block.index())
            .initialize(name, parent_index);
        if parent_index != block.index() {
            get_or_create_scanned_node!(self.parsed_nodes, parent_index).child_nodes_count += 1;
        }
        Ok(())
    }

    fn push_property(
        &mut self,
        block: &ScannedBlock<'_>,
        property: Property,
    ) -> Result<(), ReaderError> {
        let parent_index = block.parent_index()?;
        let parent = get_or_create_scanned_node!(self.parsed_nodes, parent_index);
        parent.partial_hierarchy.properties.push(property);
        Ok(())
    }

    fn parse_numeric_property(&mut self, block: &ScannedBlock<'_>) -> Result<(), ReaderError> {
        self.push_property(block, self.snapshot.parse_numeric_property(block)?)?;
        Ok(())
    }

    fn parse_bool_property(&mut self, block: &ScannedBlock<'_>) -> Result<(), ReaderError> {
        self.push_property(block, self.snapshot.parse_bool_property(block)?)?;
        Ok(())
    }

    fn parse_array_property(&mut self, block: &ScannedBlock<'_>) -> Result<(), ReaderError> {
        self.push_property(block, self.snapshot.parse_array_property(block)?)?;
        Ok(())
    }

    fn parse_property(&mut self, block: &ScannedBlock<'_>) -> Result<(), ReaderError> {
        self.push_property(block, self.snapshot.parse_property(block)?)?;
        Ok(())
    }

    fn parse_link(&mut self, block: &ScannedBlock<'_>) -> Result<(), ReaderError> {
        let parent_index = block.parent_index()?;
        let parent = get_or_create_scanned_node!(self.parsed_nodes, parent_index);
        parent.partial_hierarchy.links.push(self.snapshot.parse_link(block)?);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::private::InspectTypeInternal;
    use crate::writer::testing_utils::GetBlockExt;
    use crate::{ArrayProperty, HistogramProperty, Inspector, StringReference};
    use anyhow::Error;
    use diagnostics_assertions::{assert_data_tree, assert_json_diff};
    use futures::prelude::*;
    use inspect_format::{constants, BlockContainer, CopyBytes, PayloadFields};

    #[fuchsia::test]
    async fn test_load_string_reference() {
        let inspector = Inspector::default();
        let root = inspector.root();

        let name_value = StringReference::from("abc");
        let longer_name_value = StringReference::from("abcdefg");

        let child = root.create_child(&name_value);
        child.record_int(&name_value, 5);

        root.record_bool(&longer_name_value, false);

        let result = read(&inspector).await.unwrap();
        assert_json_diff!(result, root: {
            abc: {
                abc: 5i64,
            },
            abcdefg: false,
        });
    }

    #[fuchsia::test]
    async fn read_string_array() {
        let inspector = Inspector::default();
        let root = inspector.root();

        let zero = (0..3000).map(|_| '0').collect::<String>();
        let one: String = "1".into();
        let two: String = "two".into();
        let three: String = "three three three".into();
        let four: String = "fourth".into();

        let array = root.create_string_array("array", 5);
        array.set(0, &zero);
        array.set(1, &one);
        array.set(2, &two);
        array.set(3, &three);
        array.set(4, &four);

        let result = read(&inspector).await.unwrap();
        assert_json_diff!(result, root: {
            "array": vec![zero, one, two, three, four],
        });
    }

    #[fuchsia::test]
    async fn read_unset_string_array() {
        let inspector = Inspector::default();
        let root = inspector.root();

        let zero = (0..3000).map(|_| '0').collect::<String>();
        let one: String = "1".into();
        let four: String = "fourth".into();

        let array = root.create_string_array("array", 5);
        array.set(0, &zero);
        array.set(1, &one);
        array.set(4, &four);

        let result = read(&inspector).await.unwrap();
        assert_json_diff!(result, root: {
            "array": vec![zero, one, "".into(), "".into(), four],
        });
    }

    #[fuchsia::test]
    async fn read_vmo() {
        let inspector = Inspector::default();
        let root = inspector.root();
        let _root_int = root.create_int("int-root", 3);
        let root_double_array = root.create_double_array("property-double-array", 5);
        let double_array_data = vec![-1.2, 2.3, 3.4, 4.5, -5.6];
        for (i, x) in double_array_data.iter().enumerate() {
            root_double_array.set(i, *x);
        }

        let child1 = root.create_child("child-1");
        let _child1_uint = child1.create_uint("property-uint", 10);
        let _child1_double = child1.create_double("property-double", -3.4);
        let _child1_bool = child1.create_bool("property-bool", true);

        let chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
        let string_data = chars.iter().cycle().take(6000).collect::<String>();
        let _string_prop = child1.create_string("property-string", &string_data);

        let child1_int_array = child1.create_int_linear_histogram(
            "property-int-array",
            LinearHistogramParams { floor: 1, step_size: 2, buckets: 3 },
        );
        for x in [-1, 2, 3, 5, 8].iter() {
            child1_int_array.insert(*x);
        }

        let child2 = root.create_child("child-2");
        let _child2_double = child2.create_double("property-double", 5.8);
        let _child2_bool = child2.create_bool("property-bool", false);

        let child3 = child1.create_child("child-1-1");
        let _child3_int = child3.create_int("property-int", -9);
        let bytes_data = (0u8..=9u8).cycle().take(5000).collect::<Vec<u8>>();
        let _bytes_prop = child3.create_bytes("property-bytes", &bytes_data);

        let child3_uint_array = child3.create_uint_exponential_histogram(
            "property-uint-array",
            ExponentialHistogramParams {
                floor: 1,
                initial_step: 1,
                step_multiplier: 2,
                buckets: 5,
            },
        );
        for x in [1, 2, 3, 4].iter() {
            child3_uint_array.insert(*x);
        }

        let result = read(&inspector).await.unwrap();

        assert_data_tree!(result, root: {
            "int-root": 3i64,
            "property-double-array": double_array_data,
            "child-1": {
                "property-uint": 10u64,
                "property-double": -3.4,
                "property-bool": true,
                "property-string": string_data,
                "property-int-array": LinearHistogram {
                    floor: 1i64,
                    step: 2,
                    counts: vec![1, 1, 1, 1, 1],
                    indexes: None,
                    size: 5
                },
                "child-1-1": {
                    "property-int": -9i64,
                    "property-bytes": bytes_data,
                    "property-uint-array": ExponentialHistogram {
                        floor: 1u64,
                        initial_step: 1,
                        step_multiplier: 2,
                        counts: vec![1, 1, 2],
                        indexes: Some(vec![1, 2, 3]),
                        size: 7
                    },
                }
            },
            "child-2": {
                "property-double": 5.8,
                "property-bool": false,
            }
        })
    }

    #[fuchsia::test]
    async fn siblings_with_same_name() {
        let inspector = Inspector::default();

        let foo: StringReference = "foo".into();

        inspector.root().record_int("foo", 0);
        inspector.root().record_int("foo", 1);
        inspector.root().record_int(&foo, 2);
        inspector.root().record_int(&foo, 3);

        let dh = read(&inspector).await.unwrap();
        assert_eq!(dh.properties.len(), 4);
        for i in 0..dh.properties.len() {
            match &dh.properties[i] {
                Property::Int(n, v) => {
                    assert_eq!(n, "foo");
                    assert_eq!(*v, i as i64);
                }
                _ => panic!("We only record int properties"),
            }
        }
    }

    #[fuchsia::test]
    fn tombstone_reads() {
        let inspector = Inspector::default();
        let node1 = inspector.root().create_child("child1");
        let node2 = node1.create_child("child2");
        let node3 = node2.create_child("child3");
        let prop1 = node1.create_string("val", "test");
        let prop2 = node2.create_string("val", "test");
        let prop3 = node3.create_string("val", "test");

        assert_json_diff!(inspector,
            root: {
                child1: {
                    val: "test",
                    child2: {
                        val: "test",
                        child3: {
                            val: "test",
                        }
                    }
                }
            }
        );

        std::mem::drop(node3);
        assert_json_diff!(inspector,
            root: {
                child1: {
                    val: "test",
                    child2: {
                        val: "test",
                    }
                }
            }
        );

        std::mem::drop(node2);
        assert_json_diff!(inspector,
            root: {
                child1: {
                    val: "test",
                }
            }
        );

        // Recreate the nodes. Ensure that the old properties are not picked up.
        let node2 = node1.create_child("child2");
        let _node3 = node2.create_child("child3");
        assert_json_diff!(inspector,
            root: {
                child1: {
                    val: "test",
                    child2: {
                        child3: {}
                    }
                }
            }
        );

        // Delete out of order, leaving 3 dangling.
        std::mem::drop(node2);
        assert_json_diff!(inspector,
            root: {
                child1: {
                    val: "test",
                }
            }
        );

        std::mem::drop(node1);
        assert_json_diff!(inspector,
            root: {
            }
        );

        std::mem::drop(prop3);
        assert_json_diff!(inspector,
            root: {
            }
        );

        std::mem::drop(prop2);
        assert_json_diff!(inspector,
            root: {
            }
        );

        std::mem::drop(prop1);
        assert_json_diff!(inspector,
            root: {
            }
        );
    }

    #[fuchsia::test]
    async fn from_invalid_utf8_string() {
        // Creates a perfectly normal Inspector with a perfectly normal string
        // property with a perfectly normal value.
        let inspector = Inspector::default();
        let root = inspector.root();
        let prop = root.create_string("property", "hello world");

        // Now we will excavate the bytes that comprise the string property, then mess with them on
        // purpose to produce an invalid UTF8 string in the property.
        let vmo = inspector.vmo().await.unwrap();
        let snapshot = Snapshot::try_from(&vmo).expect("getting snapshot");
        let block = snapshot.get_block(prop.block_index().unwrap()).expect("getting block");

        // The first byte of the actual property string is at this byte offset in the VMO.
        let byte_offset = constants::MIN_ORDER_SIZE
            * (*block.property_extent_index().unwrap() as usize)
            + constants::HEADER_SIZE_BYTES;

        // Get the raw VMO bytes to mess with.
        let vmo_size = BlockContainer::len(&vmo);
        let mut buf = vec![0u8; vmo_size];
        vmo.copy_bytes(&mut buf[..]);

        // Mess up the first byte of the string property value such that the byte is an invalid
        // UTF8 character.  Then build a new node hierarchy based off those bytes, see if invalid
        // string is converted into a valid UTF8 string with some information lost.
        buf[byte_offset] = 0xFE;
        let hierarchy: DiagnosticsHierarchy = PartialNodeHierarchy::try_from(Snapshot::build(&buf))
            .expect("creating node hierarchy")
            .into();

        assert_json_diff!(hierarchy, root: {
            property: "\u{FFFD}ello world",
        });
    }

    #[fuchsia::test]
    async fn test_invalid_array_slots() -> Result<(), Error> {
        let inspector = Inspector::default();
        let root = inspector.root();
        let array = root.create_int_array("int-array", 3);

        // Mess up with the block slots by setting them to a too big number.
        array.get_block_mut(|array_block| {
            PayloadFields::set_array_slots_count(array_block, 255);
        });

        let vmo = inspector.vmo().await.unwrap();
        let vmo_size = BlockContainer::len(&vmo);

        let mut buf = vec![0u8; vmo_size];
        vmo.copy_bytes(&mut buf[..]);

        assert!(PartialNodeHierarchy::try_from(Snapshot::build(&buf)).is_err());

        Ok(())
    }

    #[fuchsia::test]
    async fn lazy_nodes() -> Result<(), Error> {
        let inspector = Inspector::default();
        inspector.root().record_int("int", 3);
        let child = inspector.root().create_child("child");
        child.record_double("double", 1.5);
        inspector.root().record_lazy_child("lazy", || {
            async move {
                let inspector = Inspector::default();
                inspector.root().record_uint("uint", 5);
                inspector.root().record_lazy_values("nested-lazy-values", || {
                    async move {
                        let inspector = Inspector::default();
                        inspector.root().record_string("string", "test");
                        let child = inspector.root().create_child("nested-lazy-child");
                        let array = child.create_int_array("array", 3);
                        array.set(0, 1);
                        child.record(array);
                        inspector.root().record(child);
                        Ok(inspector)
                    }
                    .boxed()
                });
                Ok(inspector)
            }
            .boxed()
        });

        inspector.root().record_lazy_values("lazy-values", || {
            async move {
                let inspector = Inspector::default();
                let child = inspector.root().create_child("lazy-child-1");
                child.record_string("test", "testing");
                inspector.root().record(child);
                inspector.root().record_uint("some-uint", 3);
                inspector.root().record_lazy_values("nested-lazy-values", || {
                    async move {
                        let inspector = Inspector::default();
                        inspector.root().record_int("lazy-int", -3);
                        let child = inspector.root().create_child("one-more-child");
                        child.record_double("lazy-double", 4.3);
                        inspector.root().record(child);
                        Ok(inspector)
                    }
                    .boxed()
                });
                inspector.root().record_lazy_child("nested-lazy-child", || {
                    async move {
                        let inspector = Inspector::default();
                        // This will go out of scope and is not recorded, so it shouldn't appear.
                        let _double = inspector.root().create_double("double", -1.2);
                        Ok(inspector)
                    }
                    .boxed()
                });
                Ok(inspector)
            }
            .boxed()
        });

        let hierarchy = read(&inspector).await?;
        assert_json_diff!(hierarchy, root: {
            int: 3i64,
            child: {
                double: 1.5,
            },
            lazy: {
                uint: 5u64,
                string: "test",
                "nested-lazy-child": {
                    array: vec![1i64, 0, 0],
                }
            },
            "some-uint": 3u64,
            "lazy-child-1": {
                test: "testing",
            },
            "lazy-int": -3i64,
            "one-more-child": {
                "lazy-double": 4.3,
            },
            "nested-lazy-child": {
            }
        });

        Ok(())
    }

    #[fuchsia::test]
    fn test_matching_with_inspector() {
        let inspector = Inspector::default();
        assert_json_diff!(inspector, root: {});
    }

    #[fuchsia::test]
    fn test_matching_with_partial() {
        let propreties = vec![Property::String("sub".to_string(), "sub_value".to_string())];
        let partial = PartialNodeHierarchy::new("root", propreties, vec![]);
        assert_json_diff!(partial, root: {
            sub: "sub_value",
        });
    }

    #[fuchsia::test]
    #[should_panic]
    fn test_missing_values_with_partial() {
        let mut partial = PartialNodeHierarchy::new("root", vec![], vec![]);
        partial.links = vec![LinkValue {
            name: "missing-link".to_string(),
            content: "missing-link-404".to_string(),
            disposition: LinkNodeDisposition::Child,
        }];
        assert_json_diff!(partial, root: {});
    }

    #[fuchsia::test]
    fn test_matching_with_expression_as_key() {
        let properties = vec![Property::String("sub".to_string(), "sub_value".to_string())];
        let partial = PartialNodeHierarchy::new("root", properties, vec![]);
        let value = || "sub_value";
        let key = || "sub".to_string();
        assert_json_diff!(partial, root: {
            key() => value(),
        });
    }
}