component_debug/
route.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::{format_err, Result};
use fidl_fuchsia_sys2 as fsys;
use moniker::{ExtendedMoniker, Moniker};
use prettytable::format::consts::FORMAT_CLEAN;
use prettytable::{cell, row, Table};
use std::fmt;

const SUCCESS_SUMMARY: &'static str = "Success";
const VOID_SUMMARY: &'static str = "Routed from void";

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// Analytical information about a capability.
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug)]
pub struct RouteReport {
    pub decl_type: DeclType,
    pub capability: String,
    pub error_summary: Option<String>,
    pub source_moniker: Option<String>,
    pub service_instances: Option<Vec<ServiceInstance>>,
    pub outcome: RouteOutcome,
}

#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq)]
pub struct ServiceInstance {
    pub instance_name: String,
    pub child_name: String,
    pub child_instance_name: String,
}

impl TryFrom<fsys::ServiceInstance> for ServiceInstance {
    type Error = anyhow::Error;

    fn try_from(value: fsys::ServiceInstance) -> std::result::Result<Self, Self::Error> {
        Ok(Self {
            instance_name: value
                .instance_name
                .ok_or_else(|| format_err!("missing instance_name"))?,
            child_name: value.child_name.ok_or_else(|| format_err!("missing child_name"))?,
            child_instance_name: value
                .child_instance_name
                .ok_or_else(|| format_err!("missing child_instance_name"))?,
        })
    }
}

impl TryFrom<fsys::RouteReport> for RouteReport {
    type Error = anyhow::Error;

    fn try_from(report: fsys::RouteReport) -> Result<Self> {
        let decl_type =
            report.decl_type.ok_or_else(|| format_err!("missing decl type"))?.try_into()?;
        let capability = report.capability.ok_or_else(|| format_err!("missing capability name"))?;
        let error_summary = if let Some(error) = report.error { error.summary } else { None };
        let source_moniker = report.source_moniker;
        let service_instances = report
            .service_instances
            .map(|s| s.into_iter().map(|s| s.try_into()).collect())
            .transpose()?;
        let outcome = match report.outcome {
            Some(o) => o.try_into()?,
            None => {
                // Backward compatibility. `outcome` may be missing if the client (e.g., ffx)
                // is built at a later version than the target.
                if error_summary.is_some() {
                    RouteOutcome::Failed
                } else {
                    RouteOutcome::Success
                }
            }
        };
        Ok(RouteReport {
            decl_type,
            capability,
            error_summary,
            source_moniker,
            service_instances,
            outcome,
        })
    }
}

#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq)]
pub enum DeclType {
    Use,
    Expose,
}

impl fmt::Display for DeclType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            DeclType::Use => "use",
            DeclType::Expose => "expose",
        };
        write!(f, "{}", s)
    }
}

impl TryFrom<fsys::DeclType> for DeclType {
    type Error = anyhow::Error;

    fn try_from(value: fsys::DeclType) -> std::result::Result<Self, Self::Error> {
        match value {
            fsys::DeclType::Use => Ok(DeclType::Use),
            fsys::DeclType::Expose => Ok(DeclType::Expose),
            _ => Err(format_err!("unknown decl type")),
        }
    }
}

#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq)]
pub enum RouteOutcome {
    Success,
    Void,
    Failed,
}

impl TryFrom<fsys::RouteOutcome> for RouteOutcome {
    type Error = anyhow::Error;

    fn try_from(value: fsys::RouteOutcome) -> std::result::Result<Self, Self::Error> {
        match value {
            fsys::RouteOutcome::Success => Ok(RouteOutcome::Success),
            fsys::RouteOutcome::Void => Ok(RouteOutcome::Void),
            fsys::RouteOutcome::Failed => Ok(RouteOutcome::Failed),
            _ => Err(format_err!("unknown route outcome")),
        }
    }
}

/// Call `RouteValidator/Route` with `moniker` and `targets`.
pub async fn route(
    route_validator: &fsys::RouteValidatorProxy,
    moniker: Moniker,
    targets: Vec<fsys::RouteTarget>,
) -> Result<Vec<RouteReport>> {
    let reports = match route_validator.route(&moniker.to_string(), &targets).await? {
        Ok(reports) => reports,
        Err(e) => {
            return Err(format_err!(
                "Component manager returned an unexpected error during routing: {:?}\n\
                 The state of the component instance may have changed.\n\
                 Please report this to the Component Framework team.",
                e
            ));
        }
    };

    reports.into_iter().map(|r| r.try_into()).collect()
}

/// Construct a table of routes from the given route reports.
pub fn create_table(reports: Vec<RouteReport>) -> Table {
    let mut table = Table::new();
    table.set_format(*FORMAT_CLEAN);

    let mut first = true;
    for report in reports {
        if first {
            first = false;
        } else {
            table.add_empty_row();
        }
        add_report(report, &mut table);
    }
    table
}

fn add_report(report: RouteReport, table: &mut Table) {
    table
        .add_row(row!(r->"Capability: ", &format!("{} ({})", report.capability, report.decl_type)));
    let (mark, summary) = match report.outcome {
        RouteOutcome::Success => {
            let mark = ansi_term::Color::Green.paint("[✓]");
            (mark, SUCCESS_SUMMARY)
        }
        RouteOutcome::Void => {
            let mark = ansi_term::Color::Yellow.paint("[~]");
            (mark, VOID_SUMMARY)
        }
        RouteOutcome::Failed => {
            let mark = ansi_term::Color::Red.paint("[✗]");
            let summary = report
                .error_summary
                .as_ref()
                .map(|s| s.as_str())
                .unwrap_or("Missing error summary. This is a bug.");
            (mark, summary)
        }
    };
    table.add_row(row!(r->"Result: ", &format!("{} {}", mark, summary)));
    if let Some(source_moniker) = report.source_moniker {
        let source_moniker = match ExtendedMoniker::parse_str(&source_moniker) {
            Ok(m) => m.to_string(),
            Err(e) => format!("<invalid moniker>: {}: {}", e, source_moniker),
        };
        table.add_row(row!(r->"Source: ", source_moniker));
    }
    if let Some(service_instances) = report.service_instances {
        let mut service_table = Table::new();
        let mut format = *FORMAT_CLEAN;
        format.padding(0, 0);
        service_table.set_format(format);
        let mut first = true;
        for service_instance in service_instances {
            if first {
                first = false;
            } else {
                service_table.add_empty_row();
            }
            service_table.add_row(row!(r->"Name: ", &service_instance.instance_name));
            service_table.add_row(row!(r->"Source child: ", &service_instance.child_name));
            service_table.add_row(row!(r->"Name in child: ",
                &service_instance.child_instance_name));
        }
        table.add_row(row!(r->"Service instances: ", service_table));
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use assert_matches::assert_matches;
    use fidl::endpoints;
    use fuchsia_async as fasync;
    use futures::TryStreamExt;

    fn route_validator(
        expected_moniker: &'static str,
        expected_targets: Vec<fsys::RouteTarget>,
        reports: Vec<fsys::RouteReport>,
    ) -> fsys::RouteValidatorProxy {
        let (route_validator, mut stream) =
            endpoints::create_proxy_and_stream::<fsys::RouteValidatorMarker>();
        fasync::Task::local(async move {
            match stream.try_next().await.unwrap().unwrap() {
                fsys::RouteValidatorRequest::Validate { .. } => {
                    panic!("unexpected Validate request");
                }
                fsys::RouteValidatorRequest::Route { moniker, targets, responder } => {
                    assert_eq!(
                        Moniker::parse_str(expected_moniker).unwrap(),
                        Moniker::parse_str(&moniker).unwrap()
                    );
                    assert_eq!(expected_targets, targets);
                    responder.send(Ok(&reports)).unwrap();
                }
            }
        })
        .detach();
        route_validator
    }

    #[fuchsia::test]
    async fn test_errors() {
        let targets =
            vec![fsys::RouteTarget { decl_type: fsys::DeclType::Use, name: "fuchsia.foo".into() }];
        let validator = route_validator(
            "/test",
            targets.clone(),
            vec![fsys::RouteReport {
                capability: Some("fuchsia.foo.bar".into()),
                decl_type: Some(fsys::DeclType::Use),
                error: Some(fsys::RouteError {
                    summary: Some("Access denied".into()),
                    ..Default::default()
                }),
                // Test inference of Failed
                outcome: None,
                ..Default::default()
            }],
        );

        let mut reports =
            route(&validator, Moniker::parse_str("./test").unwrap(), targets).await.unwrap();
        assert_eq!(reports.len(), 1);

        let report = reports.remove(0);
        assert_matches!(
            report,
            RouteReport {
                capability,
                decl_type: DeclType::Use,
                error_summary: Some(s),
                source_moniker: None,
                service_instances: None,
                outcome: RouteOutcome::Failed,
            } if capability == "fuchsia.foo.bar" && s == "Access denied"
        );
    }

    #[fuchsia::test]
    async fn test_no_errors() {
        let targets =
            vec![fsys::RouteTarget { decl_type: fsys::DeclType::Use, name: "fuchsia.foo".into() }];
        let validator = route_validator(
            "/test",
            targets.clone(),
            vec![
                fsys::RouteReport {
                    capability: Some("fuchsia.foo.bar".into()),
                    decl_type: Some(fsys::DeclType::Use),
                    source_moniker: Some("<component manager>".into()),
                    error: None,
                    outcome: Some(fsys::RouteOutcome::Void),
                    ..Default::default()
                },
                fsys::RouteReport {
                    capability: Some("fuchsia.foo.baz".into()),
                    decl_type: Some(fsys::DeclType::Expose),
                    source_moniker: Some("/test/src".into()),
                    service_instances: Some(vec![
                        fsys::ServiceInstance {
                            instance_name: Some("1234abcd".into()),
                            child_name: Some("a".into()),
                            child_instance_name: Some("default".into()),
                            ..Default::default()
                        },
                        fsys::ServiceInstance {
                            instance_name: Some("abcd1234".into()),
                            child_name: Some("b".into()),
                            child_instance_name: Some("other".into()),
                            ..Default::default()
                        },
                    ]),
                    error: None,
                    // Test inference of Success
                    outcome: None,
                    ..Default::default()
                },
            ],
        );

        let mut reports =
            route(&validator, Moniker::parse_str("./test").unwrap(), targets).await.unwrap();
        assert_eq!(reports.len(), 2);

        let report = reports.remove(0);
        assert_matches!(
            report,
            RouteReport {
                capability,
                decl_type: DeclType::Use,
                error_summary: None,
                source_moniker: Some(m),
                service_instances: None,
                outcome: RouteOutcome::Void,
            } if capability == "fuchsia.foo.bar" && m == "<component manager>"
        );

        let report = reports.remove(0);
        assert_matches!(
            report,
            RouteReport {
                capability,
                decl_type: DeclType::Expose,
                error_summary: None,
                source_moniker: Some(m),
                service_instances: Some(v),
                outcome: RouteOutcome::Success,
            } if capability == "fuchsia.foo.baz" && m == "/test/src"
                && v == vec![
                    ServiceInstance {
                        instance_name: "1234abcd".into(),
                        child_name: "a".into(),
                        child_instance_name: "default".into(),
                    },
                    ServiceInstance {
                        instance_name: "abcd1234".into(),
                        child_name: "b".into(),
                        child_instance_name: "other".into(),
                    },
                ]
        );
    }

    #[fuchsia::test]
    async fn test_no_routes() {
        let validator = route_validator("/test", vec![], vec![]);

        let reports =
            route(&validator, Moniker::parse_str("./test").unwrap(), vec![]).await.unwrap();
        assert!(reports.is_empty());
    }

    #[fuchsia::test]
    async fn test_parse_error() {
        let validator = route_validator(
            "/test",
            vec![],
            vec![
                // Don't set any fields
                fsys::RouteReport::default(),
            ],
        );

        let result = route(&validator, Moniker::parse_str("./test").unwrap(), vec![]).await;
        assert_matches!(result, Err(_));
    }
}