Skip to main content

reachability_core/
inspect.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::{LinkState, Proto};
6use fuchsia_inspect::Node;
7use fuchsia_inspect_contrib::inspect_log;
8use fuchsia_inspect_contrib::nodes::BoundedListNode;
9
10// Keep only the 50 most recent events.
11const INSPECT_LOG_WINDOW_SIZE: usize = 50;
12
13/// Maintains the Inspect information.
14pub(crate) struct InspectInfo {
15    _node: Node,
16    v4: BoundedListNode,
17    v6: BoundedListNode,
18}
19
20impl InspectInfo {
21    /// Create inspect node `id` rooted at `n`.
22    pub(crate) fn new(n: &Node, id: &str, name: &str) -> Self {
23        let node = n.create_child(id);
24        node.record_string("name", name);
25        let mut v4 = BoundedListNode::new(node.create_child("IPv4"), INSPECT_LOG_WINDOW_SIZE);
26        inspect_log!(v4, state: "None");
27        let mut v6 = BoundedListNode::new(node.create_child("IPv6"), INSPECT_LOG_WINDOW_SIZE);
28        inspect_log!(v6, state: "None");
29
30        InspectInfo { _node: node, v4, v6 }
31    }
32    pub(crate) fn log_link_state(&mut self, proto: Proto, link_state: LinkState) {
33        match proto {
34            Proto::IPv4 => inspect_log!(self.v4, state: format!("{:?}", link_state)),
35            Proto::IPv6 => inspect_log!(self.v6, state: format!("{:?}", link_state)),
36        }
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use diagnostics_assertions::assert_data_tree;
44    use fuchsia_inspect::Inspector;
45
46    #[test]
47    fn test_log_state() {
48        let mut executor = fuchsia_async::TestExecutor::new();
49        let inspector = Inspector::default();
50        let mut i = InspectInfo::new(inspector.root(), "id", "myname");
51        assert_data_tree!(@executor executor, inspector, root: contains {
52            id: {
53                name:"myname",
54                IPv4:{"0": contains {
55                    state: "None"
56                }
57                },
58                IPv6:{"0": contains {
59                    state: "None"
60                }
61                }
62            }
63        });
64
65        i.log_link_state(Proto::IPv4, LinkState::Internet);
66        assert_data_tree!(@executor executor, inspector, root: contains {
67            id: {
68                name:"myname",
69                IPv4:{"0": contains {
70                    state: "None"
71                },
72                "1": contains {
73                    state: "Internet"
74                }
75                },
76                IPv6:{"0": contains {
77                    state: "None"
78                }
79                }
80            }
81        });
82        i.log_link_state(Proto::IPv4, LinkState::Gateway);
83        i.log_link_state(Proto::IPv6, LinkState::Local);
84        assert_data_tree!(@executor executor, inspector, root: contains {
85            id: {
86                name:"myname",
87                IPv4:{"0": contains {
88                    state: "None"
89                },
90                "1": contains {
91                    state: "Internet"
92                },
93                "2": contains {
94                    state: "Gateway"
95                }
96                },
97                IPv6:{"0": contains {
98                    state: "None"
99                },
100                "1": contains {
101                    state: "Local"
102                }
103                }
104            }
105        });
106    }
107}