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]: {} tried to use {}, which was not declared in its sandbox",
41                name, service
42            )));
43        }
44        results
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use crate::metrics::fetch::TextFetcher;
52
53    #[fuchsia::test]
54    fn test_sandbox_errors() {
55        let expected_warnings: Vec<String> = vec![
56            "[WARNING]: my_component tried to use fuchsia.example.Id, which was not declared in its sandbox",
57            "[WARNING]: my_component tried to use fuchsia.example.Test, which was not declared in its sandbox",
58            "[WARNING]: test tried to use fuchsia.example.Test, which was not declared in its sandbox",
59        ]
60        .into_iter()
61        .map(|s| s.to_string())
62        .collect::<Vec<_>>();
63
64        // Test these cases:
65        // - Sandbox failure
66        // - Duplicate failure (suppressed).
67        // - Same component, different service (not duplicate)
68        // - Different component, same service (not duplicate)
69        // - Unrelated log line.
70        let fetcher: TextFetcher = r#"
71[100.100] `my_component` is not allowed to connect to `fuchsia.example.Test` because this service is not present in the component's sandbox
72[110.100] `my_component` is not allowed to connect to `fuchsia.example.Test` because this service is not present in the component's sandbox
73[120.100] `my_component` is not allowed to connect to `fuchsia.example.Id` because this service is not present in the component's sandbox
74[130.100] `test` is not allowed to connect to `fuchsia.example.Test` because this service is not present in the component's sandbox
75[140.100] `test` is not allowed to connect to `component 2`
76"#
77        .into();
78
79        let empty_diagnostics_vec = Vec::new();
80
81        let mut inputs = FileDataFetcher::new(&empty_diagnostics_vec);
82        inputs.klog = &fetcher;
83        assert_eq!(SandboxErrorsPlugin {}.run(&inputs).warnings, expected_warnings);
84
85        let mut inputs = FileDataFetcher::new(&empty_diagnostics_vec);
86        inputs.syslog = &fetcher;
87        assert_eq!(SandboxErrorsPlugin {}.run(&inputs).warnings, expected_warnings);
88    }
89}