aboutsummaryrefslogtreecommitdiff
path: root/tools/aconfig/aconfig_storage_file/src/protos.rs
blob: 8b862057e773b6f63af4cc3cd7651111a416490c (plain)
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
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// When building with the Android tool-chain
//
//   - an external crate `aconfig_storage_metadata_protos` will be generated
//   - the feature "cargo" will be disabled
//
// When building with cargo
//
//   - a local sub-module will be generated in OUT_DIR and included in this file
//   - the feature "cargo" will be enabled
//
// This module hides these differences from the rest of the codebase.

// ---- When building with the Android tool-chain ----
#[cfg(not(feature = "cargo"))]
mod auto_generated {
    pub use aconfig_storage_protos::aconfig_storage_metadata as ProtoStorage;
    pub use ProtoStorage::Storage_file_info as ProtoStorageFileInfo;
    pub use ProtoStorage::Storage_files as ProtoStorageFiles;
}

// ---- When building with cargo ----
#[cfg(feature = "cargo")]
mod auto_generated {
    // include! statements should be avoided (because they import file contents verbatim), but
    // because this is only used during local development, and only if using cargo instead of the
    // Android tool-chain, we allow it
    include!(concat!(env!("OUT_DIR"), "/aconfig_storage_protos/mod.rs"));
    pub use aconfig_storage_metadata::Storage_file_info as ProtoStorageFileInfo;
    pub use aconfig_storage_metadata::Storage_files as ProtoStorageFiles;
}

// ---- Common for both the Android tool-chain and cargo ----
pub use auto_generated::*;

use anyhow::Result;
use protobuf::Message;
use std::io::Write;
use tempfile::NamedTempFile;

pub mod storage_record_pb {
    use super::*;
    use anyhow::ensure;

    pub fn try_from_binary_proto(bytes: &[u8]) -> Result<ProtoStorageFiles> {
        let message: ProtoStorageFiles = protobuf::Message::parse_from_bytes(bytes)?;
        verify_fields(&message)?;
        Ok(message)
    }

    pub fn verify_fields(storage_files: &ProtoStorageFiles) -> Result<()> {
        for storage_file_info in storage_files.files.iter() {
            ensure!(
                !storage_file_info.package_map().is_empty(),
                "invalid storage file record: missing package map file for container {}",
                storage_file_info.container()
            );
            ensure!(
                !storage_file_info.flag_map().is_empty(),
                "invalid storage file record: missing flag map file for container {}",
                storage_file_info.container()
            );
            ensure!(
                !storage_file_info.flag_val().is_empty(),
                "invalid storage file record: missing flag val file for container {}",
                storage_file_info.container()
            );
        }
        Ok(())
    }

    pub fn get_binary_proto_from_text_proto(text_proto: &str) -> Result<Vec<u8>> {
        let storage_files: ProtoStorageFiles = protobuf::text_format::parse_from_str(text_proto)?;
        let mut binary_proto = Vec::new();
        storage_files.write_to_vec(&mut binary_proto)?;
        Ok(binary_proto)
    }

    pub fn write_proto_to_temp_file(text_proto: &str) -> Result<NamedTempFile> {
        let bytes = get_binary_proto_from_text_proto(text_proto).unwrap();
        let mut file = NamedTempFile::new()?;
        let _ = file.write_all(&bytes);
        Ok(file)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_storage_record_pb() {
        let text_proto = r#"
files {
    version: 0
    container: "system"
    package_map: "/system/etc/package.map"
    flag_map: "/system/etc/flag.map"
    flag_val: "/metadata/aconfig/system.val"
    timestamp: 12345
}
files {
    version: 1
    container: "product"
    package_map: "/product/etc/package.map"
    flag_map: "/product/etc/flag.map"
    flag_val: "/metadata/aconfig/product.val"
    timestamp: 54321
}
"#;
        let binary_proto_bytes =
            storage_record_pb::get_binary_proto_from_text_proto(text_proto).unwrap();
        let storage_files = storage_record_pb::try_from_binary_proto(&binary_proto_bytes).unwrap();
        assert_eq!(storage_files.files.len(), 2);
        let system_file = &storage_files.files[0];
        assert_eq!(system_file.version(), 0);
        assert_eq!(system_file.container(), "system");
        assert_eq!(system_file.package_map(), "/system/etc/package.map");
        assert_eq!(system_file.flag_map(), "/system/etc/flag.map");
        assert_eq!(system_file.flag_val(), "/metadata/aconfig/system.val");
        assert_eq!(system_file.timestamp(), 12345);
        let product_file = &storage_files.files[1];
        assert_eq!(product_file.version(), 1);
        assert_eq!(product_file.container(), "product");
        assert_eq!(product_file.package_map(), "/product/etc/package.map");
        assert_eq!(product_file.flag_map(), "/product/etc/flag.map");
        assert_eq!(product_file.flag_val(), "/metadata/aconfig/product.val");
        assert_eq!(product_file.timestamp(), 54321);
    }

    #[test]
    fn test_parse_invalid_storage_record_pb() {
        let text_proto = r#"
files {
    version: 0
    container: "system"
    package_map: ""
    flag_map: "/system/etc/flag.map"
    flag_val: "/metadata/aconfig/system.val"
    timestamp: 12345
}
"#;
        let binary_proto_bytes =
            storage_record_pb::get_binary_proto_from_text_proto(text_proto).unwrap();
        let err = storage_record_pb::try_from_binary_proto(&binary_proto_bytes).unwrap_err();
        assert_eq!(
            format!("{:?}", err),
            "invalid storage file record: missing package map file for container system"
        );

        let text_proto = r#"
files {
    version: 0
    container: "system"
    package_map: "/system/etc/package.map"
    flag_map: ""
    flag_val: "/metadata/aconfig/system.val"
    timestamp: 12345
}
"#;
        let binary_proto_bytes =
            storage_record_pb::get_binary_proto_from_text_proto(text_proto).unwrap();
        let err = storage_record_pb::try_from_binary_proto(&binary_proto_bytes).unwrap_err();
        assert_eq!(
            format!("{:?}", err),
            "invalid storage file record: missing flag map file for container system"
        );

        let text_proto = r#"
files {
    version: 0
    container: "system"
    package_map: "/system/etc/package.map"
    flag_map: "/system/etc/flag.map"
    flag_val: ""
    timestamp: 12345
}
"#;
        let binary_proto_bytes =
            storage_record_pb::get_binary_proto_from_text_proto(text_proto).unwrap();
        let err = storage_record_pb::try_from_binary_proto(&binary_proto_bytes).unwrap_err();
        assert_eq!(
            format!("{:?}", err),
            "invalid storage file record: missing flag val file for container system"
        );
    }
}