Skip to main content

fidl_fuchsia_pkg_ext/
measure.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 fidl_fuchsia_pkg as fidl;
6
7/// FIDL types that can have their in-line and out-of-line message byte payload size measured.
8pub trait Measurable {
9    /// Determine the message byte count for this instance.
10    fn measure(&self) -> usize;
11}
12
13impl Measurable for fidl::BlobId {
14    fn measure(&self) -> usize {
15        measure_fuchsia_pkg::Measurable::measure(self).num_bytes
16    }
17}
18
19impl Measurable for fidl::BlobInfo {
20    fn measure(&self) -> usize {
21        measure_fuchsia_pkg::Measurable::measure(self).num_bytes
22    }
23}
24
25impl Measurable for fidl::PackageIndexEntry {
26    fn measure(&self) -> usize {
27        measure_fuchsia_pkg::Measurable::measure(self).num_bytes
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34    use proptest::prelude::*;
35
36    /// Truncates `s` to be at most `max_len` bytes.
37    fn truncate_str(s: &str, max_len: usize) -> &str {
38        let index = s.floor_char_boundary(max_len);
39        &s[..index]
40    }
41
42    prop_compose! {
43        fn arb_package_index_entry()(
44            url in ".{0,2048}",
45            blob_id: crate::BlobId,
46        ) -> fidl::PackageIndexEntry {
47            // The regex accepts up to 2048 Unicode code points, but the max
48            // length from FIDL is in terms of bytes, not code points.
49            let url = truncate_str(&url, 2048).to_owned();
50            fidl::PackageIndexEntry {
51                package_url: fidl::PackageUrl { url, },
52                meta_far_blob_id: blob_id.into(),
53            }
54        }
55    }
56
57    proptest! {
58        #![proptest_config(ProptestConfig{
59            // Disable persistence to avoid the warning for not running in the
60            // source code directory (since we're running on a Fuchsia target)
61            failure_persistence: None,
62            .. ProptestConfig::default()
63        })]
64
65        #[test]
66        fn blob_id_size_is_as_bytes_size(item: crate::BlobId) {
67            let item: fidl::BlobId = item.into();
68
69            let expected = std::mem::size_of_val(&item);
70            let actual = item.measure();
71            prop_assert_eq!(expected, actual);
72        }
73
74        #[test]
75        fn blob_info_size_is_as_bytes_size(item: crate::BlobInfo) {
76            let item: fidl::BlobInfo = item.into();
77
78            let expected = std::mem::size_of_val(&item);
79            let actual = item.measure();
80            prop_assert_eq!(expected, actual);
81        }
82
83        #[test]
84        fn package_index_entry_size_is_fidl_encoded_size(
85            item in arb_package_index_entry()
86        ) {
87            let actual = item.measure();
88            let (bytes, _) =
89                ::fidl::standalone_encode_value(&item).unwrap();
90            let expected = bytes.len();
91            prop_assert_eq!(expected, actual);
92        }
93    }
94}