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]: {name} tried to use {service}, which was not declared in its sandbox",
41 )));
42 }
43 results
44 }
45}
4647#[cfg(test)]
48mod tests {
49use super::*;
50use crate::metrics::fetch::TextFetcher;
5152#[fuchsia::test]
53fn test_sandbox_errors() {
54let 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<_>>();
6263// 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.
69let 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();
7778let empty_diagnostics_vec = Vec::new();
7980let mut inputs = FileDataFetcher::new(&empty_diagnostics_vec);
81 inputs.klog = &fetcher;
82assert_eq!(SandboxErrorsPlugin {}.run(&inputs).warnings, expected_warnings);
8384let mut inputs = FileDataFetcher::new(&empty_diagnostics_vec);
85 inputs.syslog = &fetcher;
86assert_eq!(SandboxErrorsPlugin {}.run(&inputs).warnings, expected_warnings);
87 }
88}