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.
45use super::helpers::analyze_logs;
6use super::Plugin;
7use crate::act::Action;
8use crate::metrics::fetch::FileDataFetcher;
9use regex::Regex;
10use std::collections::BTreeSet;
1112pub struct SandboxErrorsPlugin();
1314impl Plugin for SandboxErrorsPlugin {
15fn name(&self) -> &'static str {
16"sandbox_errors"
17}
1819fn display_name(&self) -> &'static str {
20"Sandbox Errors"
21}
2223fn run_structured(&self, inputs: &FileDataFetcher<'_>) -> Vec<Action> {
24let mut results = Vec::new();
2526let mut error_tuples: BTreeSet<(String, String)> = BTreeSet::new();
27let err_ref = &mut error_tuples;
2829let 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 });
3738for (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}
4748#[cfg(test)]
49mod tests {
50use super::*;
51use crate::metrics::fetch::TextFetcher;
5253#[fuchsia::test]
54fn test_sandbox_errors() {
55let 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<_>>();
6364// 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.
70let 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();
7879let empty_diagnostics_vec = Vec::new();
8081let mut inputs = FileDataFetcher::new(&empty_diagnostics_vec);
82 inputs.klog = &fetcher;
83assert_eq!(SandboxErrorsPlugin {}.run(&inputs).warnings, expected_warnings);
8485let mut inputs = FileDataFetcher::new(&empty_diagnostics_vec);
86 inputs.syslog = &fetcher;
87assert_eq!(SandboxErrorsPlugin {}.run(&inputs).warnings, expected_warnings);
88 }
89}