fuchsia_triage/plugins/
sandbox_errors.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 super::helpers::analyze_logs;
6use super::Plugin;
7use crate::act::Action;
8use crate::metrics::fetch::FileDataFetcher;
9use regex::Regex;
10use std::collections::BTreeSet;
11
12pub struct SandboxErrorsPlugin();
13
14impl Plugin for SandboxErrorsPlugin {
15    fn name(&self) -> &'static str {
16        "sandbox_errors"
17    }
18
19    fn display_name(&self) -> &'static str {
20        "Sandbox Errors"
21    }
22
23    fn run_structured(&self, inputs: &FileDataFetcher<'_>) -> Vec<Action> {
24        let mut results = Vec::new();
25
26        let mut error_tuples: BTreeSet<(String, String)> = BTreeSet::new();
27        let err_ref = &mut error_tuples;
28
29        let re = Regex::new(r"`([^`]+)` is not allowed to connect to `([^`]+)` because this service is not present in the component's sandbox")
30            .expect("regex compilation");
31        analyze_logs(inputs, re, |mut pattern_match| {
32            err_ref.insert((
33                <&str>::from(pattern_match.remove(1)).to_string(),
34                <&str>::from(pattern_match.remove(1)).to_string(),
35            ));
36        });
37
38        for (name, service) in error_tuples.iter() {
39            results.push(Action::new_synthetic_warning(format!(
40                "[WARNING]: {name} tried to use {service}, which was not declared in its sandbox",
41            )));
42        }
43        results
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use crate::metrics::fetch::TextFetcher;
51
52    #[fuchsia::test]
53    fn test_sandbox_errors() {
54        let expected_warnings: Vec<String> = vec![
55            "[WARNING]: my_component tried to use fuchsia.example.Id, which was not declared in its sandbox",
56            "[WARNING]: my_component tried to use fuchsia.example.Test, which was not declared in its sandbox",
57            "[WARNING]: test tried to use fuchsia.example.Test, which was not declared in its sandbox",
58        ]
59        .into_iter()
60        .map(|s| s.to_string())
61        .collect::<Vec<_>>();
62
63        // Test these cases:
64        // - Sandbox failure
65        // - Duplicate failure (suppressed).
66        // - Same component, different service (not duplicate)
67        // - Different component, same service (not duplicate)
68        // - Unrelated log line.
69        let fetcher: TextFetcher = r#"
70[100.100] `my_component` is not allowed to connect to `fuchsia.example.Test` because this service is not present in the component's sandbox
71[110.100] `my_component` is not allowed to connect to `fuchsia.example.Test` because this service is not present in the component's sandbox
72[120.100] `my_component` is not allowed to connect to `fuchsia.example.Id` because this service is not present in the component's sandbox
73[130.100] `test` is not allowed to connect to `fuchsia.example.Test` because this service is not present in the component's sandbox
74[140.100] `test` is not allowed to connect to `component 2`
75"#
76        .into();
77
78        let empty_diagnostics_vec = Vec::new();
79
80        let mut inputs = FileDataFetcher::new(&empty_diagnostics_vec);
81        inputs.klog = &fetcher;
82        assert_eq!(SandboxErrorsPlugin {}.run(&inputs).warnings, expected_warnings);
83
84        let mut inputs = FileDataFetcher::new(&empty_diagnostics_vec);
85        inputs.syslog = &fetcher;
86        assert_eq!(SandboxErrorsPlugin {}.run(&inputs).warnings, expected_warnings);
87    }
88}