1use crate::component_instance::{ComponentInstanceInterface, ExtendedInstanceInterface};
6use crate::error::ComponentInstanceError;
7use anyhow::Error;
8use clonable_error::ClonableError;
9use cm_graph::DependencyNode;
10use directed_graph::DirectedGraph;
11use std::sync::{Arc, LazyLock};
12use thiserror::Error;
13use url::Url;
14use version_history::AbiRevision;
15use {fidl_fuchsia_component_resolution as fresolution, fidl_fuchsia_io as fio, zx_status as zx};
16
17#[cfg(target_os = "fuchsia")]
18use cm_rust::{FidlIntoNative, NativeIntoFidl};
19
20const RELATIVE_URL_PREFIX: &str = "relative:///";
22static RELATIVE_URL_BASE: LazyLock<Url> =
25    LazyLock::new(|| Url::parse(RELATIVE_URL_PREFIX).unwrap());
26
27#[derive(Debug)]
31pub struct ResolvedComponent {
32    pub context_to_resolve_children: Option<ComponentResolutionContext>,
35    pub decl: cm_rust::ComponentDecl,
36    pub package: Option<ResolvedPackage>,
37    pub config_values: Option<cm_rust::ConfigValuesData>,
38    pub abi_revision: Option<AbiRevision>,
39    pub dependencies: DirectedGraph<DependencyNode>,
40}
41
42#[cfg(target_os = "fuchsia")]
46impl TryFrom<fresolution::Component> for ResolvedComponent {
47    type Error = ResolverError;
48
49    fn try_from(component: fresolution::Component) -> Result<Self, Self::Error> {
50        let decl_buffer: fidl_fuchsia_mem::Data =
51            component.decl.ok_or(ResolverError::RemoteInvalidData)?;
52        let mut dependencies = DirectedGraph::new();
53        let decl = read_and_validate_manifest(&decl_buffer, &mut dependencies)?;
54        let config_values = match &decl.config {
55            Some(config) => match config.value_source {
56                cm_rust::ConfigValueSource::PackagePath(_) => {
57                    Some(read_and_validate_config_values(
58                        &component.config_values.ok_or(ResolverError::RemoteInvalidData)?,
59                    )?)
60                }
61                cm_rust::ConfigValueSource::Capabilities(_) => None,
62            },
63            None => None,
64        };
65        let context_to_resolve_children = component.resolution_context.map(Into::into);
66        let abi_revision = component.abi_revision.map(Into::into);
67        Ok(ResolvedComponent {
68            context_to_resolve_children,
69            decl,
70            package: component.package.map(TryInto::try_into).transpose()?,
71            config_values,
72            abi_revision,
73            dependencies,
74        })
75    }
76}
77
78#[cfg(target_os = "fuchsia")]
79impl From<ResolvedComponent> for fresolution::Component {
80    fn from(component: ResolvedComponent) -> Self {
81        let ResolvedComponent {
82            context_to_resolve_children,
83            decl,
84            package,
85            config_values,
86            abi_revision,
87            dependencies: _,
88        } = component;
89        let decl_bytes = fidl::persist(&decl.native_into_fidl())
90            .expect("failed to serialize validated manifest");
91        let decl_vmo = fidl::Vmo::create(decl_bytes.len() as u64).expect("failed to create VMO");
92        decl_vmo.write(&decl_bytes, 0).expect("failed to write to VMO");
93        fresolution::Component {
94            url: None,
95            decl: Some(fidl_fuchsia_mem::Data::Buffer(fidl_fuchsia_mem::Buffer {
96                vmo: decl_vmo,
97                size: decl_bytes.len() as u64,
98            })),
99            package: package.map(|p| fresolution::Package {
100                url: Some(p.url),
101                directory: Some(p.directory),
102                ..Default::default()
103            }),
104            config_values: config_values.map(|config_values| {
105                let config_values_bytes = fidl::persist(&config_values.native_into_fidl())
106                    .expect("failed to serialize config values");
107                let config_values_vmo = fidl::Vmo::create(config_values_bytes.len() as u64)
108                    .expect("failed to create VMO");
109                config_values_vmo.write(&config_values_bytes, 0).expect("failed to write to VMO");
110                fidl_fuchsia_mem::Data::Buffer(fidl_fuchsia_mem::Buffer {
111                    vmo: config_values_vmo,
112                    size: config_values_bytes.len() as u64,
113                })
114            }),
115            resolution_context: context_to_resolve_children.map(Into::into),
116            abi_revision: abi_revision.map(Into::into),
117            ..Default::default()
118        }
119    }
120}
121
122#[cfg(target_os = "fuchsia")]
123pub fn read_and_validate_manifest(
124    data: &fidl_fuchsia_mem::Data,
125    dependencies: &mut DirectedGraph<DependencyNode>,
126) -> Result<cm_rust::ComponentDecl, ResolverError> {
127    let bytes = mem_util::bytes_from_data(data).map_err(ResolverError::manifest_invalid)?;
128    read_and_validate_manifest_bytes(&bytes, dependencies)
129}
130
131#[cfg(target_os = "fuchsia")]
132pub fn read_and_validate_manifest_bytes(
133    bytes: &[u8],
134    dependencies: &mut DirectedGraph<DependencyNode>,
135) -> Result<cm_rust::ComponentDecl, ResolverError> {
136    let component_decl: fidl_fuchsia_component_decl::Component =
137        fidl::unpersist(bytes).map_err(ResolverError::manifest_invalid)?;
138    cm_fidl_validator::validate(&component_decl, dependencies)
139        .map_err(ResolverError::manifest_invalid)?;
140    Ok(component_decl.fidl_into_native())
141}
142
143#[cfg(target_os = "fuchsia")]
144pub fn read_and_validate_config_values(
145    data: &fidl_fuchsia_mem::Data,
146) -> Result<cm_rust::ConfigValuesData, ResolverError> {
147    let bytes = mem_util::bytes_from_data(&data).map_err(ResolverError::config_values_invalid)?;
148    let values = fidl::unpersist(&bytes).map_err(ResolverError::fidl_error)?;
149    cm_fidl_validator::validate_values_data(&values)
150        .map_err(|e| ResolverError::config_values_invalid(e))?;
151    Ok(values.fidl_into_native())
152}
153
154#[derive(Debug)]
158pub struct ResolvedPackage {
159    pub url: String,
161    pub directory: fidl::endpoints::ClientEnd<fio::DirectoryMarker>,
163}
164
165impl TryFrom<fresolution::Package> for ResolvedPackage {
166    type Error = ResolverError;
167
168    fn try_from(package: fresolution::Package) -> Result<Self, Self::Error> {
169        Ok(ResolvedPackage {
170            url: package.url.ok_or(ResolverError::PackageUrlMissing)?,
171            directory: package.directory.ok_or(ResolverError::PackageDirectoryMissing)?,
172        })
173    }
174}
175
176#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
179pub struct ComponentResolutionContext {
180    pub bytes: Vec<u8>,
181}
182
183impl ComponentResolutionContext {
184    pub fn new(bytes: Vec<u8>) -> Self {
185        ComponentResolutionContext { bytes }
186    }
187}
188
189impl From<fresolution::Context> for ComponentResolutionContext {
190    fn from(context: fresolution::Context) -> Self {
191        ComponentResolutionContext { bytes: context.bytes }
192    }
193}
194
195impl From<&fresolution::Context> for ComponentResolutionContext {
196    fn from(context: &fresolution::Context) -> ComponentResolutionContext {
197        ComponentResolutionContext { bytes: context.bytes.clone() }
198    }
199}
200
201impl From<ComponentResolutionContext> for fresolution::Context {
202    fn from(context: ComponentResolutionContext) -> Self {
203        Self { bytes: context.bytes }
204    }
205}
206
207impl From<&ComponentResolutionContext> for fresolution::Context {
208    fn from(context: &ComponentResolutionContext) -> fresolution::Context {
209        Self { bytes: context.bytes.clone() }
210    }
211}
212
213impl<'a> From<&'a ComponentResolutionContext> for &'a [u8] {
214    fn from(context: &'a ComponentResolutionContext) -> &'a [u8] {
215        &context.bytes
216    }
217}
218
219#[derive(Debug, Clone, PartialEq, Eq)]
222struct ResolvedAncestorComponent {
223    pub address: ComponentAddress,
227    pub context_to_resolve_children: Option<ComponentResolutionContext>,
230}
231
232impl ResolvedAncestorComponent {
233    pub async fn direct_parent_of<C: ComponentInstanceInterface>(
235        component: &Arc<C>,
236    ) -> Result<Self, ResolverError> {
237        let parent_component = get_parent(component).await?;
238        let resolved_parent = parent_component.lock_resolved_state().await?;
239        Ok(Self {
240            address: resolved_parent.address().await?,
241            context_to_resolve_children: resolved_parent.context_to_resolve_children(),
242        })
243    }
244
245    pub async fn first_packaged_ancestor_of<C: ComponentInstanceInterface>(
247        component: &Arc<C>,
248    ) -> Result<Self, ResolverError> {
249        let mut parent_component = get_parent(component).await?;
250        loop {
251            {
254                let resolved_parent = parent_component.lock_resolved_state().await?;
255                let address = resolved_parent.address().await?;
256                if address.scheme() != "realm-builder" {
285                    return Ok(Self {
286                        address,
287                        context_to_resolve_children: resolved_parent.context_to_resolve_children(),
288                    });
289                }
290            }
291            parent_component = get_parent(&parent_component).await?;
292        }
293    }
294}
295
296async fn get_parent<C: ComponentInstanceInterface>(
297    component: &Arc<C>,
298) -> Result<Arc<C>, ResolverError> {
299    if let ExtendedInstanceInterface::Component(parent_component) =
300        component.try_get_parent().map_err(|err| {
301            ResolverError::no_parent_context(anyhow::format_err!(
302                "Component {} ({}) has no parent for context: {:?}.",
303                component.moniker(),
304                component.url(),
305                err,
306            ))
307        })?
308    {
309        Ok(parent_component)
310    } else {
311        Err(ResolverError::no_parent_context(anyhow::format_err!(
312            "Component {} ({}) has no parent for context.",
313            component.moniker(),
314            component.url(),
315        )))
316    }
317}
318
319#[derive(Debug, Clone, PartialEq, Eq)]
325pub enum ComponentAddress {
326    Absolute { url: Url },
328
329    RelativePath {
332        scheme: String,
335
336        url: Url,
339
340        context: ComponentResolutionContext,
346    },
347}
348
349impl ComponentAddress {
350    fn new_absolute(url: Url) -> Self {
352        Self::Absolute { url }
353    }
354
355    fn new_relative_path(
357        path: &str,
358        some_resource: Option<&str>,
359        scheme: &str,
360        context: ComponentResolutionContext,
361    ) -> Result<Self, ResolverError> {
362        let mut url = RELATIVE_URL_BASE.clone();
363        url.set_path(path);
364        url.set_fragment(some_resource);
365        Self::check_relative_url(&url)?;
366        Ok(Self::RelativePath { url, context, scheme: scheme.into() })
367    }
368
369    pub fn from_absolute_url(component_url: &cm_types::Url) -> Result<Self, ResolverError> {
372        match Url::parse(component_url.as_str()) {
373            Ok(url) => Ok(Self::new_absolute(url)),
374            Err(url::ParseError::RelativeUrlWithoutBase) => {
375                Err(ResolverError::RelativeUrlNotExpected(component_url.to_string()))
376            }
377            Err(err) => Err(ResolverError::malformed_url(err)),
378        }
379    }
380
381    fn parse_relative_url(component_url: &cm_types::Url) -> Result<Url, ResolverError> {
384        let component_url = component_url.as_str();
385        match Url::parse(component_url) {
386            Ok(_) => Err(ResolverError::malformed_url(anyhow::format_err!(
387                "Error parsing a relative URL given absolute URL '{}'.",
388                component_url,
389            ))),
390            Err(url::ParseError::RelativeUrlWithoutBase) => {
391                RELATIVE_URL_BASE.join(component_url).map_err(|err| {
392                    ResolverError::malformed_url(anyhow::format_err!(
393                        "Error parsing a relative component URL '{}': {:?}.",
394                        component_url,
395                        err
396                    ))
397                })
398            }
399            Err(err) => Err(ResolverError::malformed_url(anyhow::format_err!(
400                "Unexpected error while parsing a component URL '{}': {:?}.",
401                component_url,
402                err,
403            ))),
404        }
405    }
406
407    fn check_relative_url(url: &Url) -> Result<(), ResolverError> {
408        let truncated_url = url.as_str().strip_prefix(RELATIVE_URL_PREFIX).ok_or_else(|| {
409            ResolverError::malformed_url(anyhow::format_err!(
410                "Could not strip relative prefix from url. This is a bug. {}",
411                url
412            ))
413        })?;
414        let relative_url = RELATIVE_URL_BASE.make_relative(&url).ok_or_else(|| {
415            ResolverError::malformed_url(anyhow::format_err!(
416                "Could not make relative url. This is a bug. {}",
417                url
418            ))
419        })?;
420        if truncated_url != relative_url {
421            return Err(ResolverError::malformed_url(anyhow::format_err!(
422                "Relative url generated from url::Url did not match expectations. \
423                This is a bug. {}",
424                url
425            )));
426        }
427        Ok(())
428    }
429
430    fn relative_path(relative_url: &Url) -> &str {
435        let path = relative_url.path();
436        path.strip_prefix('/').unwrap_or(path)
437    }
438
439    pub async fn from_url<C: ComponentInstanceInterface>(
444        component_url: &cm_types::Url,
445        component: &Arc<C>,
446    ) -> Result<Self, ResolverError> {
447        Self::from(component_url, None, component).await
448    }
449
450    pub async fn from_url_and_context<C: ComponentInstanceInterface>(
454        component_url: &cm_types::Url,
455        context: ComponentResolutionContext,
456        component: &Arc<C>,
457    ) -> Result<Self, ResolverError> {
458        Self::from(component_url, Some(context), component).await
459    }
460
461    pub async fn from<C: ComponentInstanceInterface>(
466        component_url: &cm_types::Url,
467        context: Option<ComponentResolutionContext>,
468        component: &Arc<C>,
469    ) -> Result<Self, ResolverError> {
470        let result = Self::from_absolute_url(component_url);
471        if !matches!(result, Err(ResolverError::RelativeUrlNotExpected(_))) {
472            return result;
473        }
474        let relative_url = Self::parse_relative_url(component_url)?;
475        let relative_path = Self::relative_path(&relative_url);
476        if relative_url.fragment().is_none() && relative_path.is_empty() {
477            return Err(ResolverError::malformed_url(anyhow::format_err!("{}", component_url)));
478        }
479        if relative_url.query().is_some() {
480            return Err(ResolverError::malformed_url(anyhow::format_err!(
481                "Query strings are not allowed in relative component URLs: {}",
482                component_url
483            )));
484        }
485        if relative_path.is_empty() {
486            let resolved_parent = ResolvedAncestorComponent::direct_parent_of(component).await?;
489            resolved_parent.address.clone_with_new_resource(relative_url.fragment())
490        } else {
491            let resolved_ancestor =
500                ResolvedAncestorComponent::first_packaged_ancestor_of(component).await?;
501            let scheme = resolved_ancestor.address.scheme();
502            if let Some(context) = context {
503                Self::new_relative_path(relative_path, relative_url.fragment(), scheme, context)
504            } else {
505                let context = resolved_ancestor.context_to_resolve_children.clone().ok_or_else(|| {
506                        ResolverError::RelativeUrlMissingContext(format!(
507                            "Relative path component URL '{}' cannot be resolved because its ancestor did not provide a resolution context. The ancestor's component address is {:?}.",
508                             component_url, resolved_ancestor.address
509                        ))
510                    })?;
511                Self::new_relative_path(relative_path, relative_url.fragment(), scheme, context)
512            }
513        }
514    }
515
516    pub fn clone_with_new_resource(
519        &self,
520        some_resource: Option<&str>,
521    ) -> Result<Self, ResolverError> {
522        self.clone().consume_with_new_resource(some_resource)
523    }
524
525    pub fn consume_with_new_resource(
526        mut self,
527        some_resource: Option<&str>,
528    ) -> Result<Self, ResolverError> {
529        let url = match &mut self {
530            Self::Absolute { url } => url,
531            Self::RelativePath { url, .. } => url,
532        };
533        url.set_fragment(some_resource);
534        match self {
535            Self::Absolute { url } => Ok(Self::Absolute { url }),
536            Self::RelativePath { context, scheme, url } => {
537                Self::check_relative_url(&url)?;
538                Ok(Self::RelativePath { url, context, scheme })
539            }
540        }
541    }
542
543    pub fn is_absolute(&self) -> bool {
545        matches!(self, Self::Absolute { .. })
546    }
547
548    pub fn is_relative_path(&self) -> bool {
550        matches!(self, Self::RelativePath { .. })
551    }
552
553    pub fn context(&self) -> &ComponentResolutionContext {
558        if let Self::RelativePath { context, .. } = self {
559            &context
560        } else {
561            panic!("context() is only valid for `ComponentAddressKind::RelativePath");
562        }
563    }
564
565    pub fn scheme(&self) -> &str {
569        match self {
570            Self::Absolute { url } => url.scheme(),
571            Self::RelativePath { scheme, .. } => &scheme,
572        }
573    }
574
575    pub fn path(&self) -> &str {
577        match self {
578            Self::Absolute { url } => url.path(),
579            Self::RelativePath { url, .. } => Self::relative_path(&url),
580        }
581    }
582
583    pub fn query(&self) -> Option<&str> {
586        match self {
587            Self::Absolute { url } => url.query(),
588            Self::RelativePath { .. } => None,
589        }
590    }
591
592    pub fn resource(&self) -> Option<&str> {
594        match self {
595            Self::Absolute { url } => url.fragment(),
596            Self::RelativePath { url, .. } => url.fragment(),
597        }
598    }
599
600    pub fn url(&self) -> &str {
603        match self {
604            Self::Absolute { url } => url.as_str(),
605            Self::RelativePath { url, .. } => &url.as_str()[RELATIVE_URL_PREFIX.len()..],
606        }
607    }
608
609    pub fn to_url_and_context(&self) -> (&str, Option<&ComponentResolutionContext>) {
612        match self {
613            Self::Absolute { .. } => (self.url(), None),
614            Self::RelativePath { context, .. } => (self.url(), Some(context)),
615        }
616    }
617}
618
619#[derive(Debug, Error, Clone)]
621pub enum ResolverError {
622    #[error("an unexpected error occurred: {0}")]
623    Internal(#[source] ClonableError),
624    #[error("an IO error occurred: {0}")]
625    Io(#[source] ClonableError),
626    #[error("component manifest not found: {0}")]
627    ManifestNotFound(#[source] ClonableError),
628    #[error("package not found: {0}")]
629    PackageNotFound(#[source] ClonableError),
630    #[error("component manifest invalid: {0}")]
631    ManifestInvalid(#[source] ClonableError),
632    #[error("config values file invalid: {0}")]
633    ConfigValuesInvalid(#[source] ClonableError),
634    #[error("abi revision not found")]
635    AbiRevisionNotFound,
636    #[error("abi revision invalid: {0}")]
637    AbiRevisionInvalid(#[source] ClonableError),
638    #[error("failed to read config values: {0}")]
639    ConfigValuesIo(zx::Status),
640    #[error("scheme not registered")]
641    SchemeNotRegistered,
642    #[error("malformed url: {0}")]
643    MalformedUrl(#[source] ClonableError),
644    #[error("relative url requires a parent component with resolution context: {0}")]
645    NoParentContext(#[source] ClonableError),
646    #[error("package URL missing")]
647    PackageUrlMissing,
648    #[error("package directory handle missing")]
649    PackageDirectoryMissing,
650    #[error("a relative URL was not expected: {0}")]
651    RelativeUrlNotExpected(String),
652    #[error("failed to route resolver capability: {0}")]
653    RoutingError(#[source] ClonableError),
654    #[error("a context is required to resolve relative url: {0}")]
655    RelativeUrlMissingContext(String),
656    #[error("this component resolver does not resolve relative path component URLs: {0}")]
657    UnexpectedRelativePath(String),
658    #[error("the remote resolver returned invalid data")]
659    RemoteInvalidData,
660    #[error("an error occurred sending a FIDL request to the remote resolver: {0}")]
661    FidlError(#[source] ClonableError),
662}
663
664impl ResolverError {
665    pub fn as_zx_status(&self) -> zx::Status {
666        match self {
667            ResolverError::PackageNotFound(_)
668            | ResolverError::ManifestNotFound(_)
669            | ResolverError::ManifestInvalid(_)
670            | ResolverError::ConfigValuesInvalid(_)
671            | ResolverError::Io(_)
672            | ResolverError::ConfigValuesIo(_)
673            | ResolverError::AbiRevisionNotFound
674            | ResolverError::AbiRevisionInvalid(_)
675            | ResolverError::SchemeNotRegistered
676            | ResolverError::MalformedUrl(_)
677            | ResolverError::NoParentContext(_)
678            | ResolverError::RelativeUrlMissingContext(_)
679            | ResolverError::RemoteInvalidData
680            | ResolverError::PackageUrlMissing
681            | ResolverError::PackageDirectoryMissing
682            | ResolverError::UnexpectedRelativePath(_) => zx::Status::NOT_FOUND,
683
684            ResolverError::Internal(_)
685            | ResolverError::RelativeUrlNotExpected(_)
686            | ResolverError::RoutingError(_)
687            | ResolverError::FidlError(_) => zx::Status::INTERNAL,
688        }
689    }
690
691    pub fn internal(err: impl Into<Error>) -> Self {
692        Self::Internal(err.into().into())
693    }
694
695    pub fn io(err: impl Into<Error>) -> Self {
696        Self::Io(err.into().into())
697    }
698
699    pub fn manifest_not_found(err: impl Into<Error>) -> Self {
700        Self::ManifestNotFound(err.into().into())
701    }
702
703    pub fn package_not_found(err: impl Into<Error>) -> Self {
704        Self::PackageNotFound(err.into().into())
705    }
706
707    pub fn manifest_invalid(err: impl Into<Error>) -> Self {
708        Self::ManifestInvalid(err.into().into())
709    }
710
711    pub fn config_values_invalid(err: impl Into<Error>) -> Self {
712        Self::ConfigValuesInvalid(err.into().into())
713    }
714
715    pub fn abi_revision_invalid(err: impl Into<Error>) -> Self {
716        Self::AbiRevisionInvalid(err.into().into())
717    }
718
719    pub fn malformed_url(err: impl Into<Error>) -> Self {
720        Self::MalformedUrl(err.into().into())
721    }
722
723    pub fn no_parent_context(err: impl Into<Error>) -> Self {
724        Self::NoParentContext(err.into().into())
725    }
726
727    pub fn routing_error(err: impl Into<Error>) -> Self {
728        Self::RoutingError(err.into().into())
729    }
730
731    pub fn fidl_error(err: impl Into<Error>) -> Self {
732        Self::FidlError(err.into().into())
733    }
734}
735
736impl From<fresolution::ResolverError> for ResolverError {
737    fn from(err: fresolution::ResolverError) -> ResolverError {
738        match err {
739            fresolution::ResolverError::Internal => ResolverError::internal(RemoteError(err)),
740            fresolution::ResolverError::Io => ResolverError::io(RemoteError(err)),
741            fresolution::ResolverError::PackageNotFound
742            | fresolution::ResolverError::NoSpace
743            | fresolution::ResolverError::ResourceUnavailable
744            | fresolution::ResolverError::NotSupported => {
745                ResolverError::package_not_found(RemoteError(err))
746            }
747            fresolution::ResolverError::ManifestNotFound => {
748                ResolverError::manifest_not_found(RemoteError(err))
749            }
750            fresolution::ResolverError::InvalidArgs => {
751                ResolverError::malformed_url(RemoteError(err))
752            }
753            fresolution::ResolverError::InvalidManifest => {
754                ResolverError::ManifestInvalid(anyhow::Error::from(RemoteError(err)).into())
755            }
756            fresolution::ResolverError::ConfigValuesNotFound => {
757                ResolverError::ConfigValuesIo(zx::Status::NOT_FOUND)
758            }
759            fresolution::ResolverError::AbiRevisionNotFound => ResolverError::AbiRevisionNotFound,
760            fresolution::ResolverError::InvalidAbiRevision => {
761                ResolverError::abi_revision_invalid(RemoteError(err))
762            }
763        }
764    }
765}
766
767impl From<ResolverError> for fresolution::ResolverError {
768    fn from(err: ResolverError) -> fresolution::ResolverError {
769        match err {
770            ResolverError::Internal(_) => fresolution::ResolverError::Internal,
771            ResolverError::Io(_) => fresolution::ResolverError::Io,
772            ResolverError::ManifestNotFound(_) => fresolution::ResolverError::ManifestNotFound,
773            ResolverError::PackageNotFound(_) => fresolution::ResolverError::PackageNotFound,
774            ResolverError::ManifestInvalid(_) => fresolution::ResolverError::InvalidManifest,
775            ResolverError::ConfigValuesInvalid(_) => fresolution::ResolverError::InvalidManifest,
776            ResolverError::AbiRevisionNotFound => fresolution::ResolverError::AbiRevisionNotFound,
777            ResolverError::AbiRevisionInvalid(_) => fresolution::ResolverError::InvalidAbiRevision,
778            ResolverError::ConfigValuesIo(_) => fresolution::ResolverError::Io,
779            ResolverError::SchemeNotRegistered => fresolution::ResolverError::NotSupported,
780            ResolverError::MalformedUrl(_) => fresolution::ResolverError::InvalidArgs,
781            ResolverError::NoParentContext(_) => fresolution::ResolverError::Internal,
782            ResolverError::PackageUrlMissing => fresolution::ResolverError::PackageNotFound,
783            ResolverError::PackageDirectoryMissing => fresolution::ResolverError::PackageNotFound,
784            ResolverError::RelativeUrlNotExpected(_) => fresolution::ResolverError::InvalidArgs,
785            ResolverError::RoutingError(_) => fresolution::ResolverError::Internal,
786            ResolverError::RelativeUrlMissingContext(_) => fresolution::ResolverError::InvalidArgs,
787            ResolverError::UnexpectedRelativePath(_) => fresolution::ResolverError::InvalidArgs,
788            ResolverError::RemoteInvalidData => fresolution::ResolverError::InvalidManifest,
789            ResolverError::FidlError(_) => fresolution::ResolverError::Internal,
790        }
791    }
792}
793
794impl From<ComponentInstanceError> for ResolverError {
795    fn from(err: ComponentInstanceError) -> ResolverError {
796        use ComponentInstanceError::*;
797        match &err {
798            ComponentManagerInstanceUnavailable {}
799            | ComponentManagerInstanceUnexpected {}
800            | InstanceNotFound { .. }
801            | InstanceNotExecutable { .. }
802            | ResolveFailed { .. } => {
803                ResolverError::Internal(ClonableError::from(anyhow::format_err!("{:?}", err)))
804            }
805            NoAbsoluteUrl { .. } => ResolverError::NoParentContext(ClonableError::from(
806                anyhow::format_err!("{:?}", err),
807            )),
808            MalformedUrl { .. } => {
809                ResolverError::MalformedUrl(ClonableError::from(anyhow::format_err!("{:?}", err)))
810            }
811        }
812    }
813}
814
815#[derive(Error, Clone, Debug)]
816#[error("remote resolver responded with {0:?}")]
817struct RemoteError(fresolution::ResolverError);
818
819#[cfg(test)]
820mod tests {
821    use super::*;
822    use crate::bedrock::sandbox_construction::ComponentSandbox;
823    use crate::capability_source::{BuiltinCapabilities, NamespaceCapabilities};
824    use crate::component_instance::{ResolvedInstanceInterface, TopInstanceInterface};
825    use crate::policy::GlobalPolicyChecker;
826    use assert_matches::assert_matches;
827    use async_trait::async_trait;
828    use cm_rust::{CapabilityDecl, CollectionDecl, ExposeDecl, OfferDecl, UseDecl};
829    use cm_rust_testing::new_decl_from_json;
830    use cm_types::Name;
831    use fidl::endpoints::create_endpoints;
832    use moniker::{BorrowedChildName, ChildName, Moniker};
833    use serde_json::json;
834    use {fidl_fuchsia_component_decl as fdecl, fidl_fuchsia_mem as fmem};
835
836    fn from_absolute_url(url: &str) -> ComponentAddress {
837        ComponentAddress::from_absolute_url(&url.parse().unwrap()).unwrap()
838    }
839
840    fn parse_relative_url(url: &str) -> Url {
841        ComponentAddress::parse_relative_url(&url.parse().unwrap()).unwrap()
842    }
843
844    #[test]
845    fn test_resolved_package() {
846        let url = "some_url".to_string();
847        let (dir_client, _) = create_endpoints::<fio::DirectoryMarker>();
848        let fidl_package = fresolution::Package {
849            url: Some(url.clone()),
850            directory: Some(dir_client),
851            ..Default::default()
852        };
853        let resolved_package = ResolvedPackage::try_from(fidl_package).unwrap();
854        assert_eq!(resolved_package.url, url);
855    }
856
857    #[test]
858    fn test_component_address() {
859        let address = from_absolute_url("some-scheme://fuchsia.com/package#meta/comp.cm");
860        assert!(address.is_absolute());
861        assert_eq!(address.scheme(), "some-scheme");
862        assert_eq!(address.path(), "/package");
863        assert_eq!(address.query(), None);
864        assert_eq!(address.resource(), Some("meta/comp.cm"));
865        assert_eq!(address.url(), "some-scheme://fuchsia.com/package#meta/comp.cm");
866        assert_matches!(
867            address.to_url_and_context(),
868            ("some-scheme://fuchsia.com/package#meta/comp.cm", None)
869        );
870
871        let abs_address = ComponentAddress::new_absolute(
872            Url::parse("some-scheme://fuchsia.com/package#meta/comp.cm").unwrap(),
873        );
874        assert_eq!(abs_address, address);
875
876        assert_eq!(abs_address, address);
877        assert!(abs_address.is_absolute());
878        assert_eq!(abs_address.scheme(), "some-scheme");
879        assert_eq!(abs_address.path(), "/package");
880        assert_eq!(abs_address.query(), None);
881        assert_eq!(abs_address.resource(), Some("meta/comp.cm"));
882        assert_eq!(abs_address.url(), "some-scheme://fuchsia.com/package#meta/comp.cm");
883        assert_matches!(
884            abs_address.to_url_and_context(),
885            ("some-scheme://fuchsia.com/package#meta/comp.cm", None)
886        );
887
888        let cloned_address = abs_address.clone();
889        assert_eq!(abs_address, cloned_address);
890
891        let address2 = abs_address.clone_with_new_resource(Some("meta/other_comp.cm")).unwrap();
892        assert_ne!(address2, abs_address);
893        assert!(address2.is_absolute());
894        assert_eq!(address2.resource(), Some("meta/other_comp.cm"));
895        assert_eq!(address2.scheme(), "some-scheme");
896        assert_eq!(address2.path(), "/package");
897        assert_eq!(address2.query(), None);
898
899        let rel_address = ComponentAddress::new_relative_path(
900            "subpackage",
901            Some("meta/subcomp.cm"),
902            "some-scheme",
903            ComponentResolutionContext::new(vec![b'4', b'5', b'6']),
904        )
905        .unwrap();
906        if let ComponentAddress::RelativePath { ref context, .. } = rel_address {
907            assert_eq!(&context.bytes, &vec![b'4', b'5', b'6']);
908        }
909        assert!(rel_address.is_relative_path());
910        assert_eq!(rel_address.path(), "subpackage");
911        assert_eq!(rel_address.query(), None);
912        assert_eq!(rel_address.resource(), Some("meta/subcomp.cm"));
913        assert_eq!(&rel_address.context().bytes, &vec![b'4', b'5', b'6']);
914        assert_eq!(rel_address.url(), "subpackage#meta/subcomp.cm");
915        assert_eq!(
916            rel_address.to_url_and_context(),
917            (
918                "subpackage#meta/subcomp.cm",
919                Some(&ComponentResolutionContext::new(vec![b'4', b'5', b'6']))
920            )
921        );
922
923        let rel_address2 =
924            rel_address.clone_with_new_resource(Some("meta/other_subcomp.cm")).unwrap();
925        assert_ne!(rel_address2, rel_address);
926        assert!(rel_address2.is_relative_path());
927        assert_eq!(rel_address2.path(), "subpackage");
928        assert_eq!(rel_address2.query(), None);
929        assert_eq!(rel_address2.resource(), Some("meta/other_subcomp.cm"));
930        assert_eq!(&rel_address2.context().bytes, &vec![b'4', b'5', b'6']);
931        assert_eq!(rel_address2.url(), "subpackage#meta/other_subcomp.cm");
932        assert_eq!(
933            rel_address2.to_url_and_context(),
934            (
935                "subpackage#meta/other_subcomp.cm",
936                Some(&ComponentResolutionContext::new(vec![b'4', b'5', b'6']))
937            )
938        );
939
940        let address = from_absolute_url("base://b");
941        assert!(address.is_absolute());
942        assert_eq!(address.scheme(), "base");
943        assert_eq!(address.path(), "");
944        assert_eq!(address.query(), None);
945        assert_eq!(address.resource(), None);
946        assert_eq!(address.url(), "base://b");
947        assert_matches!(address.to_url_and_context(), ("base://b", None));
948
949        let address = from_absolute_url("fuchsia-boot:///#meta/root.cm");
950        assert!(address.is_absolute());
951        assert_eq!(address.scheme(), "fuchsia-boot");
952        assert_eq!(address.path(), "/");
953        assert_eq!(address.query(), None);
954        assert_eq!(address.resource(), Some("meta/root.cm"));
955        assert_eq!(address.url(), "fuchsia-boot:///#meta/root.cm");
956        assert_matches!(address.to_url_and_context(), ("fuchsia-boot:///#meta/root.cm", None));
957
958        let address = from_absolute_url("custom-resolver:my:special:path#meta/root.cm");
959        assert!(address.is_absolute());
960        assert_eq!(address.scheme(), "custom-resolver");
961        assert_eq!(address.path(), "my:special:path");
962        assert_eq!(address.query(), None);
963        assert_eq!(address.resource(), Some("meta/root.cm"));
964        assert_eq!(address.url(), "custom-resolver:my:special:path#meta/root.cm");
965        assert_matches!(
966            address.to_url_and_context(),
967            ("custom-resolver:my:special:path#meta/root.cm", None)
968        );
969
970        let address = from_absolute_url("cast:00000000");
971        assert!(address.is_absolute());
972        assert_eq!(address.scheme(), "cast");
973        assert_eq!(address.path(), "00000000");
974        assert_eq!(address.query(), None);
975        assert_eq!(address.resource(), None);
976        assert_eq!(address.url(), "cast:00000000");
977        assert_matches!(address.to_url_and_context(), ("cast:00000000", None));
978
979        let address = from_absolute_url("cast:00000000#meta/root.cm");
980        assert!(address.is_absolute());
981        assert_eq!(address.scheme(), "cast");
982        assert_eq!(address.path(), "00000000");
983        assert_eq!(address.query(), None);
984        assert_eq!(address.resource(), Some("meta/root.cm"));
985        assert_eq!(address.url(), "cast:00000000#meta/root.cm");
986        assert_matches!(address.to_url_and_context(), ("cast:00000000#meta/root.cm", None));
987
988        let address =
989            from_absolute_url("fuchsia-pkg://fuchsia.com/package?hash=cafe0123#meta/comp.cm");
990        assert!(address.is_absolute());
991        assert_eq!(address.scheme(), "fuchsia-pkg");
992        assert_eq!(address.path(), "/package");
993        assert_eq!(address.resource(), Some("meta/comp.cm"));
994        assert_eq!(address.query(), Some("hash=cafe0123"));
995        assert_eq!(address.url(), "fuchsia-pkg://fuchsia.com/package?hash=cafe0123#meta/comp.cm");
996        assert_matches!(
997            address.to_url_and_context(),
998            ("fuchsia-pkg://fuchsia.com/package?hash=cafe0123#meta/comp.cm", None)
999        );
1000    }
1001
1002    #[test]
1003    fn test_relative_path() {
1004        let url = Url::parse("relative:///package#fragment").unwrap();
1005        assert_eq!(url.path(), "/package");
1006        assert_eq!(ComponentAddress::relative_path(&url), "package");
1007
1008        let url = Url::parse("cast:00000000#fragment").unwrap();
1009        assert_eq!(url.path(), "00000000");
1010        assert_eq!(ComponentAddress::relative_path(&url), "00000000");
1011    }
1012
1013    #[test]
1014    fn test_parse_relative_url() {
1015        let relative_prefix_with_one_less_slash = Url::parse("relative://").unwrap();
1016        assert_eq!(relative_prefix_with_one_less_slash.scheme(), "relative");
1017        assert_eq!(relative_prefix_with_one_less_slash.host(), None);
1018        assert_eq!(relative_prefix_with_one_less_slash.path(), "");
1019
1020        assert_eq!(RELATIVE_URL_BASE.scheme(), "relative");
1021        assert_eq!(RELATIVE_URL_BASE.host(), None);
1022        assert_eq!(RELATIVE_URL_BASE.path(), "/");
1023
1024        let mut clone_relative_base = RELATIVE_URL_BASE.clone();
1025        assert_eq!(clone_relative_base.path(), "/");
1026        clone_relative_base.set_path("");
1027        assert_eq!(clone_relative_base.path(), "");
1028
1029        let mut clone_relative_base = RELATIVE_URL_BASE.clone();
1030        assert_eq!(clone_relative_base.path(), "/");
1031        clone_relative_base.set_path("some_path_no_initial_slash");
1032        assert_eq!(clone_relative_base.path(), "/some_path_no_initial_slash");
1033
1034        let clone_relative_base = RELATIVE_URL_BASE.clone();
1035        let joined = clone_relative_base.join("some_path_no_initial_slash").unwrap();
1036        assert_eq!(joined.path(), "/some_path_no_initial_slash");
1037
1038        let clone_relative_base = relative_prefix_with_one_less_slash.clone();
1039        let joined = clone_relative_base.join("some_path_no_initial_slash").unwrap();
1040        assert_eq!(joined.path(), "/some_path_no_initial_slash");
1042
1043        let relative_url = parse_relative_url("subpackage#meta/subcomp.cm");
1044        assert_eq!(relative_url.path(), "/subpackage");
1045        assert_eq!(relative_url.query(), None);
1046        assert_eq!(relative_url.fragment(), Some("meta/subcomp.cm"));
1047
1048        let relative_url = parse_relative_url("/subpackage#meta/subcomp.cm");
1049        assert_eq!(relative_url.path(), "/subpackage");
1050        assert_eq!(relative_url.query(), None);
1051        assert_eq!(relative_url.fragment(), Some("meta/subcomp.cm"));
1052
1053        let relative_url = parse_relative_url("//subpackage#meta/subcomp.cm");
1054        assert_eq!(relative_url.path(), "");
1055        assert_eq!(relative_url.host_str(), Some("subpackage"));
1056        assert_eq!(relative_url.query(), None);
1057        assert_eq!(relative_url.fragment(), Some("meta/subcomp.cm"));
1058
1059        let relative_url = parse_relative_url("///subpackage#meta/subcomp.cm");
1060        assert_eq!(relative_url.path(), "/subpackage");
1061        assert_eq!(relative_url.host_str(), None);
1062        assert_eq!(relative_url.query(), None);
1063        assert_eq!(relative_url.fragment(), Some("meta/subcomp.cm"));
1064
1065        let relative_url = parse_relative_url("fuchsia.com/subpackage#meta/subcomp.cm");
1066        assert_eq!(relative_url.path(), "/fuchsia.com/subpackage");
1067        assert_eq!(relative_url.query(), None);
1068        assert_eq!(relative_url.fragment(), Some("meta/subcomp.cm"));
1069
1070        let relative_url = parse_relative_url("//fuchsia.com/subpackage#meta/subcomp.cm");
1071        assert_eq!(relative_url.path(), "/subpackage");
1072        assert_eq!(relative_url.host_str(), Some("fuchsia.com"));
1073        assert_eq!(relative_url.query(), None);
1074        assert_eq!(relative_url.fragment(), Some("meta/subcomp.cm"));
1075
1076        assert_matches!(
1077            ComponentAddress::parse_relative_url(
1078                &"fuchsia-pkg://fuchsia.com/subpackage#meta/subcomp.cm".parse().unwrap()
1079            ),
1080            Err(ResolverError::MalformedUrl(..))
1081        );
1082
1083        let relative_url = parse_relative_url("#meta/peercomp.cm");
1084        assert_eq!(relative_url.path(), "/");
1085        assert_eq!(relative_url.query(), None);
1086        assert_eq!(relative_url.fragment(), Some("meta/peercomp.cm"));
1087
1088        let address = from_absolute_url("some-scheme://fuchsia.com/package#meta/comp.cm")
1089            .clone_with_new_resource(relative_url.fragment())
1090            .unwrap();
1091
1092        assert!(address.is_absolute());
1093        assert_eq!(address.scheme(), "some-scheme");
1094        assert_eq!(address.path(), "/package");
1095        assert_eq!(address.query(), None);
1096        assert_eq!(address.resource(), Some("meta/peercomp.cm"));
1097        assert_eq!(address.url(), "some-scheme://fuchsia.com/package#meta/peercomp.cm");
1098
1099        let address = from_absolute_url("cast:00000000")
1100            .clone_with_new_resource(relative_url.fragment())
1101            .unwrap();
1102
1103        assert!(address.is_absolute());
1104        assert_eq!(address.scheme(), "cast");
1105        assert_eq!(address.path(), "00000000");
1106        assert_eq!(address.query(), None);
1107        assert_eq!(address.resource(), Some("meta/peercomp.cm"));
1108        assert_eq!(address.url(), "cast:00000000#meta/peercomp.cm");
1109    }
1110
1111    static COMPONENT_DECL: LazyLock<cm_rust::ComponentDecl> = LazyLock::new(|| {
1112        new_decl_from_json(json!(
1113        {
1114            "include": [ "syslog/client.shard.cml" ],
1115            "program": {
1116                "runner": "elf",
1117                "binary": "bin/example",
1118            },
1119            "children": [
1120                {
1121                    "name": "logger",
1122                    "url": "fuchsia-pkg://fuchsia.com/logger/stable#meta/logger.cm",
1123                    "environment": "#env_one",
1124                },
1125            ],
1126            "collections": [
1127                {
1128                    "name": "modular",
1129                    "durability": "transient",
1130                },
1131            ],
1132            "capabilities": [
1133                {
1134                    "protocol": "fuchsia.logger.Log2",
1135                    "path": "/svc/fuchsia.logger.Log2",
1136                },
1137            ],
1138            "use": [
1139                {
1140                    "protocol": "fuchsia.fonts.LegacyProvider",
1141                },
1142            ],
1143            "environments": [
1144                {
1145                    "name": "env_one",
1146                    "extends": "none",
1147                    "__stop_timeout_ms": 1337,
1148                },
1149            ],
1150            "facets": {
1151                "author": "Fuchsia",
1152            }}))
1153        .expect("failed to construct manifest")
1154    });
1155
1156    #[fuchsia::test]
1157    fn test_read_and_validate_manifest() {
1158        let manifest = fmem::Data::Bytes(
1159            fidl::persist(&COMPONENT_DECL.clone().native_into_fidl())
1160                .expect("failed to encode manifest"),
1161        );
1162        let actual = read_and_validate_manifest(&manifest, &mut DirectedGraph::new())
1163            .expect("failed to decode manifest");
1164        assert_eq!(actual, COMPONENT_DECL.clone());
1165    }
1166
1167    #[fuchsia::test]
1168    async fn test_read_and_validate_config_values() {
1169        let fidl_config_values = fdecl::ConfigValuesData {
1170            values: Some(vec![
1171                fdecl::ConfigValueSpec {
1172                    value: Some(fdecl::ConfigValue::Single(fdecl::ConfigSingleValue::Bool(false))),
1173                    ..Default::default()
1174                },
1175                fdecl::ConfigValueSpec {
1176                    value: Some(fdecl::ConfigValue::Single(fdecl::ConfigSingleValue::Uint8(5))),
1177                    ..Default::default()
1178                },
1179                fdecl::ConfigValueSpec {
1180                    value: Some(fdecl::ConfigValue::Single(fdecl::ConfigSingleValue::String(
1181                        "hello!".to_string(),
1182                    ))),
1183                    ..Default::default()
1184                },
1185                fdecl::ConfigValueSpec {
1186                    value: Some(fdecl::ConfigValue::Vector(fdecl::ConfigVectorValue::BoolVector(
1187                        vec![true, false],
1188                    ))),
1189                    ..Default::default()
1190                },
1191                fdecl::ConfigValueSpec {
1192                    value: Some(fdecl::ConfigValue::Vector(
1193                        fdecl::ConfigVectorValue::StringVector(vec![
1194                            "hello!".to_string(),
1195                            "world!".to_string(),
1196                        ]),
1197                    )),
1198                    ..Default::default()
1199                },
1200            ]),
1201            checksum: Some(fdecl::ConfigChecksum::Sha256([0; 32])),
1202            ..Default::default()
1203        };
1204        let config_values = cm_rust::ConfigValuesData {
1205            values: Box::from([
1206                cm_rust::ConfigValueSpec {
1207                    value: cm_rust::ConfigValue::Single(cm_rust::ConfigSingleValue::Bool(false)),
1208                },
1209                cm_rust::ConfigValueSpec {
1210                    value: cm_rust::ConfigValue::Single(cm_rust::ConfigSingleValue::Uint8(5)),
1211                },
1212                cm_rust::ConfigValueSpec {
1213                    value: cm_rust::ConfigValue::Single(cm_rust::ConfigSingleValue::String(
1214                        "hello!".to_string(),
1215                    )),
1216                },
1217                cm_rust::ConfigValueSpec {
1218                    value: cm_rust::ConfigValue::Vector(cm_rust::ConfigVectorValue::BoolVector(
1219                        Box::from([true, false]),
1220                    )),
1221                },
1222                cm_rust::ConfigValueSpec {
1223                    value: cm_rust::ConfigValue::Vector(cm_rust::ConfigVectorValue::StringVector(
1224                        Box::from(["hello!".to_string(), "world!".to_string()]),
1225                    )),
1226                },
1227            ]),
1228            checksum: cm_rust::ConfigChecksum::Sha256([0; 32]),
1229        };
1230        let data = fmem::Data::Bytes(
1231            fidl::persist(&fidl_config_values).expect("failed to encode config values"),
1232        );
1233        let actual =
1234            read_and_validate_config_values(&data).expect("failed to decode config values");
1235        assert_eq!(actual, config_values);
1236    }
1237
1238    #[derive(Debug, Default, Clone)]
1239    struct MockTopInstance {
1240        namespace_capabilities: NamespaceCapabilities,
1241        builtin_capabilities: BuiltinCapabilities,
1242    }
1243
1244    impl TopInstanceInterface for MockTopInstance {
1245        fn namespace_capabilities(&self) -> &NamespaceCapabilities {
1246            &self.namespace_capabilities
1247        }
1248        fn builtin_capabilities(&self) -> &BuiltinCapabilities {
1249            &self.builtin_capabilities
1250        }
1251    }
1252
1253    #[derive(Clone)]
1254    struct MockComponentInstance {
1255        parent: Option<Box<MockComponentInstance>>,
1256        resolved_state: Option<MockResolvedState>,
1257        moniker: Moniker,
1258        address: cm_types::Url,
1259    }
1260    #[async_trait]
1261    impl ComponentInstanceInterface for MockComponentInstance {
1262        type TopInstance = MockTopInstance;
1263        fn moniker(&self) -> &Moniker {
1264            &self.moniker
1265        }
1266        fn url(&self) -> &cm_types::Url {
1267            &self.address
1268        }
1269        fn config_parent_overrides(&self) -> Option<&[cm_rust::ConfigOverride]> {
1270            unimplemented!()
1271        }
1272        fn policy_checker(&self) -> &GlobalPolicyChecker {
1273            unimplemented!()
1274        }
1275        fn component_id_index(&self) -> &component_id_index::Index {
1276            unimplemented!()
1277        }
1278        fn try_get_parent(
1279            &self,
1280        ) -> Result<ExtendedInstanceInterface<Self>, ComponentInstanceError> {
1281            if let Some(parent) = self.parent.as_ref() {
1282                Ok(ExtendedInstanceInterface::Component(Arc::new((**parent).clone())))
1283            } else {
1284                Ok(ExtendedInstanceInterface::AboveRoot(Arc::new(MockTopInstance::default())))
1285            }
1286        }
1287        async fn lock_resolved_state<'a>(
1288            self: &'a Arc<Self>,
1289        ) -> Result<Box<dyn ResolvedInstanceInterface<Component = Self> + 'a>, ComponentInstanceError>
1290        {
1291            Ok(Box::new(self.resolved_state.as_ref().unwrap()))
1292        }
1293        async fn component_sandbox(
1294            self: &Arc<Self>,
1295        ) -> Result<ComponentSandbox, ComponentInstanceError> {
1296            unimplemented!()
1297        }
1298    }
1299    impl MockComponentInstance {
1300        async fn component_address(self: &Arc<Self>) -> Result<ComponentAddress, ResolverError> {
1301            ComponentAddress::from_url(&self.address, self).await
1302        }
1303    }
1304
1305    #[derive(Clone)]
1306    struct MockResolvedState {
1307        address: Result<ComponentAddress, ResolverError>,
1308        context_to_resolve_children: Option<ComponentResolutionContext>,
1309    }
1310    #[async_trait]
1311    impl ResolvedInstanceInterface for MockResolvedState {
1312        type Component = MockComponentInstance;
1313        fn uses(&self) -> Box<[UseDecl]> {
1314            unimplemented!()
1315        }
1316        fn exposes(&self) -> Box<[ExposeDecl]> {
1317            unimplemented!()
1318        }
1319        fn offers(&self) -> Box<[OfferDecl]> {
1320            unimplemented!()
1321        }
1322        fn capabilities(&self) -> Box<[CapabilityDecl]> {
1323            unimplemented!()
1324        }
1325        fn collections(&self) -> Box<[CollectionDecl]> {
1326            unimplemented!()
1327        }
1328        fn get_child(&self, _moniker: &BorrowedChildName) -> Option<Arc<Self::Component>> {
1329            unimplemented!()
1330        }
1331        fn children_in_collection(
1332            &self,
1333            _collection: &Name,
1334        ) -> Vec<(ChildName, Arc<Self::Component>)> {
1335            unimplemented!()
1336        }
1337        async fn address(&self) -> Result<ComponentAddress, ResolverError> {
1338            self.address.clone()
1339        }
1340        fn context_to_resolve_children(&self) -> Option<ComponentResolutionContext> {
1341            self.context_to_resolve_children.clone()
1342        }
1343    }
1344
1345    #[fuchsia::test]
1346    async fn test_from_absolute_component_url_with_component_instance() -> Result<(), Error> {
1347        let url_str = "fuchsia-pkg://fuchsia.com/package#meta/comp.cm";
1348        let root = Arc::new(MockComponentInstance {
1349            parent: None,
1350            resolved_state: Some(MockResolvedState {
1351                address: Ok(ComponentAddress::new_absolute(Url::parse(url_str).unwrap())),
1352                context_to_resolve_children: None,
1353            }),
1354            moniker: Moniker::root(),
1355            address: cm_types::Url::new(url_str).unwrap(),
1356        });
1357
1358        let abs = root.component_address().await.unwrap();
1359        assert_matches!(abs, ComponentAddress::Absolute { .. });
1360        assert_eq!(abs.scheme(), "fuchsia-pkg");
1361        assert_eq!(abs.path(), "/package");
1362        assert_eq!(abs.resource(), Some("meta/comp.cm"));
1363        Ok(())
1364    }
1365
1366    #[fuchsia::test]
1367    async fn test_from_relative_path_component_url_with_component_instance() -> Result<(), Error> {
1368        let root_url_str = "fuchsia-pkg://fuchsia.com/package#meta/comp.cm";
1369        let root = MockComponentInstance {
1370            parent: None,
1371            resolved_state: Some(MockResolvedState {
1372                address: Ok(ComponentAddress::new_absolute(Url::parse(root_url_str).unwrap())),
1373                context_to_resolve_children: Some(ComponentResolutionContext::new(
1374                    "package_context".as_bytes().to_vec(),
1375                )),
1376            }),
1377            moniker: Moniker::root(),
1378            address: cm_types::Url::new(root_url_str).unwrap(),
1379        };
1380        let child = Arc::new(MockComponentInstance {
1381            parent: Some(Box::new(root)),
1382            resolved_state: None,
1383            moniker: "/child".try_into().unwrap(),
1384            address: cm_types::Url::new("subpackage#meta/subcomp.cm").unwrap(),
1385        });
1386
1387        let relpath = child.component_address().await.unwrap();
1388        assert_matches!(relpath, ComponentAddress::RelativePath { .. });
1389        assert_eq!(relpath.path(), "subpackage");
1390        assert_eq!(relpath.resource(), Some("meta/subcomp.cm"));
1391        assert_eq!(
1392            relpath.context(),
1393            &ComponentResolutionContext::new("package_context".as_bytes().to_vec())
1394        );
1395
1396        Ok(())
1397    }
1398
1399    #[fuchsia::test]
1400    async fn test_from_relative_path_component_url_with_cast_component_instance()
1401    -> Result<(), Error> {
1402        let root_url_str = "cast:00000000/package#meta/comp.cm";
1403        let root = MockComponentInstance {
1404            parent: None,
1405            resolved_state: Some(MockResolvedState {
1406                address: Ok(ComponentAddress::new_absolute(Url::parse(root_url_str).unwrap())),
1407                context_to_resolve_children: Some(ComponentResolutionContext::new(
1408                    "package_context".as_bytes().to_vec(),
1409                )),
1410            }),
1411            moniker: Moniker::root(),
1412            address: cm_types::Url::new(root_url_str).unwrap(),
1413        };
1414        let child = Arc::new(MockComponentInstance {
1415            parent: Some(Box::new(root)),
1416            resolved_state: None,
1417            moniker: "/child".try_into().unwrap(),
1418            address: cm_types::Url::new("subpackage#meta/subcomp.cm").unwrap(),
1419        });
1420
1421        let relpath = child.component_address().await.unwrap();
1422        assert_matches!(relpath, ComponentAddress::RelativePath { .. });
1423        assert_eq!(relpath.path(), "subpackage");
1424        assert_eq!(relpath.resource(), Some("meta/subcomp.cm"));
1425        assert_eq!(
1426            relpath.context(),
1427            &ComponentResolutionContext::new("package_context".as_bytes().to_vec())
1428        );
1429
1430        Ok(())
1431    }
1432}