1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 use anyhow::{bail, ensure, Context, Result};
18 use itertools::Itertools;
19 use protobuf::Message;
20 use std::collections::HashMap;
21 use std::io::Read;
22 use std::path::PathBuf;
23 
24 use crate::codegen::cpp::generate_cpp_code;
25 use crate::codegen::java::generate_java_code;
26 use crate::codegen::rust::generate_rust_code;
27 use crate::codegen::CodegenMode;
28 use crate::dump::{DumpFormat, DumpPredicate};
29 use crate::storage::generate_storage_file;
30 use aconfig_protos::{
31     ParsedFlagExt, ProtoFlagMetadata, ProtoFlagPermission, ProtoFlagState, ProtoParsedFlag,
32     ProtoParsedFlags, ProtoTracepoint,
33 };
34 use aconfig_storage_file::StorageFileType;
35 
36 pub struct Input {
37     pub source: String,
38     pub reader: Box<dyn Read>,
39 }
40 
41 impl Input {
try_parse_flags(&mut self) -> Result<ProtoParsedFlags>42     fn try_parse_flags(&mut self) -> Result<ProtoParsedFlags> {
43         let mut buffer = Vec::new();
44         self.reader
45             .read_to_end(&mut buffer)
46             .with_context(|| format!("failed to read {}", self.source))?;
47         aconfig_protos::parsed_flags::try_from_binary_proto(&buffer)
48             .with_context(|| self.error_context())
49     }
50 
error_context(&self) -> String51     fn error_context(&self) -> String {
52         format!("failed to parse {}", self.source)
53     }
54 }
55 
56 pub struct OutputFile {
57     pub path: PathBuf, // relative to some root directory only main knows about
58     pub contents: Vec<u8>,
59 }
60 
61 pub const DEFAULT_FLAG_STATE: ProtoFlagState = ProtoFlagState::DISABLED;
62 pub const DEFAULT_FLAG_PERMISSION: ProtoFlagPermission = ProtoFlagPermission::READ_WRITE;
63 
parse_flags( package: &str, container: Option<&str>, declarations: Vec<Input>, values: Vec<Input>, default_permission: ProtoFlagPermission, ) -> Result<Vec<u8>>64 pub fn parse_flags(
65     package: &str,
66     container: Option<&str>,
67     declarations: Vec<Input>,
68     values: Vec<Input>,
69     default_permission: ProtoFlagPermission,
70 ) -> Result<Vec<u8>> {
71     let mut parsed_flags = ProtoParsedFlags::new();
72 
73     for mut input in declarations {
74         let mut contents = String::new();
75         input
76             .reader
77             .read_to_string(&mut contents)
78             .with_context(|| format!("failed to read {}", input.source))?;
79 
80         let flag_declarations = aconfig_protos::flag_declarations::try_from_text_proto(&contents)
81             .with_context(|| input.error_context())?;
82         ensure!(
83             package == flag_declarations.package(),
84             "failed to parse {}: expected package {}, got {}",
85             input.source,
86             package,
87             flag_declarations.package()
88         );
89         if let Some(c) = container {
90             ensure!(
91                 c == flag_declarations.container(),
92                 "failed to parse {}: expected container {}, got {}",
93                 input.source,
94                 c,
95                 flag_declarations.container()
96             );
97         }
98         for mut flag_declaration in flag_declarations.flag.into_iter() {
99             aconfig_protos::flag_declaration::verify_fields(&flag_declaration)
100                 .with_context(|| input.error_context())?;
101 
102             // create ParsedFlag using FlagDeclaration and default values
103             let mut parsed_flag = ProtoParsedFlag::new();
104             if let Some(c) = container {
105                 parsed_flag.set_container(c.to_string());
106             }
107             parsed_flag.set_package(package.to_string());
108             parsed_flag.set_name(flag_declaration.take_name());
109             parsed_flag.set_namespace(flag_declaration.take_namespace());
110             parsed_flag.set_description(flag_declaration.take_description());
111             parsed_flag.bug.append(&mut flag_declaration.bug);
112             parsed_flag.set_state(DEFAULT_FLAG_STATE);
113             let flag_permission = if flag_declaration.is_fixed_read_only() {
114                 ProtoFlagPermission::READ_ONLY
115             } else {
116                 default_permission
117             };
118             parsed_flag.set_permission(flag_permission);
119             parsed_flag.set_is_fixed_read_only(flag_declaration.is_fixed_read_only());
120             parsed_flag.set_is_exported(flag_declaration.is_exported());
121             let mut tracepoint = ProtoTracepoint::new();
122             tracepoint.set_source(input.source.clone());
123             tracepoint.set_state(DEFAULT_FLAG_STATE);
124             tracepoint.set_permission(flag_permission);
125             parsed_flag.trace.push(tracepoint);
126 
127             let mut metadata = ProtoFlagMetadata::new();
128             let purpose = flag_declaration.metadata.purpose();
129             metadata.set_purpose(purpose);
130             parsed_flag.metadata = Some(metadata).into();
131 
132             // verify ParsedFlag looks reasonable
133             aconfig_protos::parsed_flag::verify_fields(&parsed_flag)?;
134 
135             // verify ParsedFlag can be added
136             ensure!(
137                 parsed_flags.parsed_flag.iter().all(|other| other.name() != parsed_flag.name()),
138                 "failed to declare flag {} from {}: flag already declared",
139                 parsed_flag.name(),
140                 input.source
141             );
142 
143             // add ParsedFlag to ParsedFlags
144             parsed_flags.parsed_flag.push(parsed_flag);
145         }
146     }
147 
148     for mut input in values {
149         let mut contents = String::new();
150         input
151             .reader
152             .read_to_string(&mut contents)
153             .with_context(|| format!("failed to read {}", input.source))?;
154         let flag_values = aconfig_protos::flag_values::try_from_text_proto(&contents)
155             .with_context(|| input.error_context())?;
156         for flag_value in flag_values.flag_value.into_iter() {
157             aconfig_protos::flag_value::verify_fields(&flag_value)
158                 .with_context(|| input.error_context())?;
159 
160             let Some(parsed_flag) = parsed_flags
161                 .parsed_flag
162                 .iter_mut()
163                 .find(|pf| pf.package() == flag_value.package() && pf.name() == flag_value.name())
164             else {
165                 // (silently) skip unknown flags
166                 continue;
167             };
168 
169             ensure!(
170                 !parsed_flag.is_fixed_read_only()
171                     || flag_value.permission() == ProtoFlagPermission::READ_ONLY,
172                 "failed to set permission of flag {}, since this flag is fixed read only flag",
173                 flag_value.name()
174             );
175 
176             parsed_flag.set_state(flag_value.state());
177             parsed_flag.set_permission(flag_value.permission());
178             let mut tracepoint = ProtoTracepoint::new();
179             tracepoint.set_source(input.source.clone());
180             tracepoint.set_state(flag_value.state());
181             tracepoint.set_permission(flag_value.permission());
182             parsed_flag.trace.push(tracepoint);
183         }
184     }
185 
186     // Create a sorted parsed_flags
187     aconfig_protos::parsed_flags::sort_parsed_flags(&mut parsed_flags);
188     aconfig_protos::parsed_flags::verify_fields(&parsed_flags)?;
189     let mut output = Vec::new();
190     parsed_flags.write_to_vec(&mut output)?;
191     Ok(output)
192 }
193 
create_java_lib(mut input: Input, codegen_mode: CodegenMode) -> Result<Vec<OutputFile>>194 pub fn create_java_lib(mut input: Input, codegen_mode: CodegenMode) -> Result<Vec<OutputFile>> {
195     let parsed_flags = input.try_parse_flags()?;
196     let modified_parsed_flags = modify_parsed_flags_based_on_mode(parsed_flags, codegen_mode)?;
197     let Some(package) = find_unique_package(&modified_parsed_flags) else {
198         bail!("no parsed flags, or the parsed flags use different packages");
199     };
200     let package = package.to_string();
201     let _flag_ids = assign_flag_ids(&package, modified_parsed_flags.iter())?;
202     generate_java_code(&package, modified_parsed_flags.into_iter(), codegen_mode)
203 }
204 
create_cpp_lib( mut input: Input, codegen_mode: CodegenMode, allow_instrumentation: bool, ) -> Result<Vec<OutputFile>>205 pub fn create_cpp_lib(
206     mut input: Input,
207     codegen_mode: CodegenMode,
208     allow_instrumentation: bool,
209 ) -> Result<Vec<OutputFile>> {
210     // TODO(327420679): Enable export mode for native flag library
211     ensure!(
212         codegen_mode != CodegenMode::Exported,
213         "Exported mode for generated c/c++ flag library is disabled"
214     );
215     let parsed_flags = input.try_parse_flags()?;
216     let modified_parsed_flags = modify_parsed_flags_based_on_mode(parsed_flags, codegen_mode)?;
217     let Some(package) = find_unique_package(&modified_parsed_flags) else {
218         bail!("no parsed flags, or the parsed flags use different packages");
219     };
220     let package = package.to_string();
221     let flag_ids = assign_flag_ids(&package, modified_parsed_flags.iter())?;
222     generate_cpp_code(
223         &package,
224         modified_parsed_flags.into_iter(),
225         codegen_mode,
226         flag_ids,
227         allow_instrumentation,
228     )
229 }
230 
create_rust_lib( mut input: Input, codegen_mode: CodegenMode, allow_instrumentation: bool, ) -> Result<OutputFile>231 pub fn create_rust_lib(
232     mut input: Input,
233     codegen_mode: CodegenMode,
234     allow_instrumentation: bool,
235 ) -> Result<OutputFile> {
236     // // TODO(327420679): Enable export mode for native flag library
237     ensure!(
238         codegen_mode != CodegenMode::Exported,
239         "Exported mode for generated rust flag library is disabled"
240     );
241     let parsed_flags = input.try_parse_flags()?;
242     let modified_parsed_flags = modify_parsed_flags_based_on_mode(parsed_flags, codegen_mode)?;
243     let Some(package) = find_unique_package(&modified_parsed_flags) else {
244         bail!("no parsed flags, or the parsed flags use different packages");
245     };
246     let package = package.to_string();
247     let flag_ids = assign_flag_ids(&package, modified_parsed_flags.iter())?;
248     generate_rust_code(
249         &package,
250         flag_ids,
251         modified_parsed_flags.into_iter(),
252         codegen_mode,
253         allow_instrumentation,
254     )
255 }
256 
create_storage( caches: Vec<Input>, container: &str, file: &StorageFileType, ) -> Result<Vec<u8>>257 pub fn create_storage(
258     caches: Vec<Input>,
259     container: &str,
260     file: &StorageFileType,
261 ) -> Result<Vec<u8>> {
262     let parsed_flags_vec: Vec<ProtoParsedFlags> =
263         caches.into_iter().map(|mut input| input.try_parse_flags()).collect::<Result<Vec<_>>>()?;
264     generate_storage_file(container, parsed_flags_vec.iter(), file)
265 }
266 
create_device_config_defaults(mut input: Input) -> Result<Vec<u8>>267 pub fn create_device_config_defaults(mut input: Input) -> Result<Vec<u8>> {
268     let parsed_flags = input.try_parse_flags()?;
269     let mut output = Vec::new();
270     for parsed_flag in parsed_flags
271         .parsed_flag
272         .into_iter()
273         .filter(|pf| pf.permission() == ProtoFlagPermission::READ_WRITE)
274     {
275         let line = format!(
276             "{}:{}={}\n",
277             parsed_flag.namespace(),
278             parsed_flag.fully_qualified_name(),
279             match parsed_flag.state() {
280                 ProtoFlagState::ENABLED => "enabled",
281                 ProtoFlagState::DISABLED => "disabled",
282             }
283         );
284         output.extend_from_slice(line.as_bytes());
285     }
286     Ok(output)
287 }
288 
create_device_config_sysprops(mut input: Input) -> Result<Vec<u8>>289 pub fn create_device_config_sysprops(mut input: Input) -> Result<Vec<u8>> {
290     let parsed_flags = input.try_parse_flags()?;
291     let mut output = Vec::new();
292     for parsed_flag in parsed_flags
293         .parsed_flag
294         .into_iter()
295         .filter(|pf| pf.permission() == ProtoFlagPermission::READ_WRITE)
296     {
297         let line = format!(
298             "persist.device_config.{}={}\n",
299             parsed_flag.fully_qualified_name(),
300             match parsed_flag.state() {
301                 ProtoFlagState::ENABLED => "true",
302                 ProtoFlagState::DISABLED => "false",
303             }
304         );
305         output.extend_from_slice(line.as_bytes());
306     }
307     Ok(output)
308 }
309 
dump_parsed_flags( mut input: Vec<Input>, format: DumpFormat, filters: &[&str], dedup: bool, ) -> Result<Vec<u8>>310 pub fn dump_parsed_flags(
311     mut input: Vec<Input>,
312     format: DumpFormat,
313     filters: &[&str],
314     dedup: bool,
315 ) -> Result<Vec<u8>> {
316     let individually_parsed_flags: Result<Vec<ProtoParsedFlags>> =
317         input.iter_mut().map(|i| i.try_parse_flags()).collect();
318     let parsed_flags: ProtoParsedFlags =
319         aconfig_protos::parsed_flags::merge(individually_parsed_flags?, dedup)?;
320     let filters: Vec<Box<DumpPredicate>> = if filters.is_empty() {
321         vec![Box::new(|_| true)]
322     } else {
323         filters
324             .iter()
325             .map(|f| crate::dump::create_filter_predicate(f))
326             .collect::<Result<Vec<_>>>()?
327     };
328     crate::dump::dump_parsed_flags(
329         parsed_flags.parsed_flag.into_iter().filter(|flag| filters.iter().any(|p| p(flag))),
330         format,
331     )
332 }
333 
find_unique_package(parsed_flags: &[ProtoParsedFlag]) -> Option<&str>334 fn find_unique_package(parsed_flags: &[ProtoParsedFlag]) -> Option<&str> {
335     let package = parsed_flags.first().map(|pf| pf.package())?;
336     if parsed_flags.iter().any(|pf| pf.package() != package) {
337         return None;
338     }
339     Some(package)
340 }
341 
modify_parsed_flags_based_on_mode( parsed_flags: ProtoParsedFlags, codegen_mode: CodegenMode, ) -> Result<Vec<ProtoParsedFlag>>342 pub fn modify_parsed_flags_based_on_mode(
343     parsed_flags: ProtoParsedFlags,
344     codegen_mode: CodegenMode,
345 ) -> Result<Vec<ProtoParsedFlag>> {
346     fn exported_mode_flag_modifier(mut parsed_flag: ProtoParsedFlag) -> ProtoParsedFlag {
347         parsed_flag.set_state(ProtoFlagState::DISABLED);
348         parsed_flag.set_permission(ProtoFlagPermission::READ_WRITE);
349         parsed_flag.set_is_fixed_read_only(false);
350         parsed_flag
351     }
352 
353     fn force_read_only_mode_flag_modifier(mut parsed_flag: ProtoParsedFlag) -> ProtoParsedFlag {
354         parsed_flag.set_permission(ProtoFlagPermission::READ_ONLY);
355         parsed_flag
356     }
357 
358     let modified_parsed_flags: Vec<_> = match codegen_mode {
359         CodegenMode::Exported => parsed_flags
360             .parsed_flag
361             .into_iter()
362             .filter(|pf| pf.is_exported())
363             .map(exported_mode_flag_modifier)
364             .collect(),
365         CodegenMode::ForceReadOnly => parsed_flags
366             .parsed_flag
367             .into_iter()
368             .filter(|pf| !pf.is_exported())
369             .map(force_read_only_mode_flag_modifier)
370             .collect(),
371         CodegenMode::Production | CodegenMode::Test => {
372             parsed_flags.parsed_flag.into_iter().collect()
373         }
374     };
375     if modified_parsed_flags.is_empty() {
376         bail!("{codegen_mode} library contains no {codegen_mode} flags");
377     }
378 
379     Ok(modified_parsed_flags)
380 }
381 
assign_flag_ids<'a, I>(package: &str, parsed_flags_iter: I) -> Result<HashMap<String, u16>> where I: Iterator<Item = &'a ProtoParsedFlag> + Clone,382 pub fn assign_flag_ids<'a, I>(package: &str, parsed_flags_iter: I) -> Result<HashMap<String, u16>>
383 where
384     I: Iterator<Item = &'a ProtoParsedFlag> + Clone,
385 {
386     assert!(parsed_flags_iter.clone().tuple_windows().all(|(a, b)| a.name() <= b.name()));
387     let mut flag_ids = HashMap::new();
388     for (id_to_assign, pf) in (0_u32..).zip(parsed_flags_iter) {
389         if package != pf.package() {
390             return Err(anyhow::anyhow!("encountered a flag not in current package"));
391         }
392 
393         // put a cap on how many flags a package can contain to 65535
394         if id_to_assign > u16::MAX as u32 {
395             return Err(anyhow::anyhow!("the number of flags in a package cannot exceed 65535"));
396         }
397 
398         flag_ids.insert(pf.name().to_string(), id_to_assign as u16);
399     }
400     Ok(flag_ids)
401 }
402 
403 #[cfg(test)]
404 mod tests {
405     use super::*;
406     use aconfig_protos::ProtoFlagPurpose;
407 
408     #[test]
test_parse_flags()409     fn test_parse_flags() {
410         let parsed_flags = crate::test::parse_test_flags(); // calls parse_flags
411         aconfig_protos::parsed_flags::verify_fields(&parsed_flags).unwrap();
412 
413         let enabled_ro =
414             parsed_flags.parsed_flag.iter().find(|pf| pf.name() == "enabled_ro").unwrap();
415         assert!(aconfig_protos::parsed_flag::verify_fields(enabled_ro).is_ok());
416         assert_eq!("com.android.aconfig.test", enabled_ro.package());
417         assert_eq!("enabled_ro", enabled_ro.name());
418         assert_eq!("This flag is ENABLED + READ_ONLY", enabled_ro.description());
419         assert_eq!(ProtoFlagState::ENABLED, enabled_ro.state());
420         assert_eq!(ProtoFlagPermission::READ_ONLY, enabled_ro.permission());
421         assert_eq!(ProtoFlagPurpose::PURPOSE_BUGFIX, enabled_ro.metadata.purpose());
422         assert_eq!(3, enabled_ro.trace.len());
423         assert!(!enabled_ro.is_fixed_read_only());
424         assert_eq!("tests/test.aconfig", enabled_ro.trace[0].source());
425         assert_eq!(ProtoFlagState::DISABLED, enabled_ro.trace[0].state());
426         assert_eq!(ProtoFlagPermission::READ_WRITE, enabled_ro.trace[0].permission());
427         assert_eq!("tests/first.values", enabled_ro.trace[1].source());
428         assert_eq!(ProtoFlagState::DISABLED, enabled_ro.trace[1].state());
429         assert_eq!(ProtoFlagPermission::READ_WRITE, enabled_ro.trace[1].permission());
430         assert_eq!("tests/second.values", enabled_ro.trace[2].source());
431         assert_eq!(ProtoFlagState::ENABLED, enabled_ro.trace[2].state());
432         assert_eq!(ProtoFlagPermission::READ_ONLY, enabled_ro.trace[2].permission());
433 
434         assert_eq!(9, parsed_flags.parsed_flag.len());
435         for pf in parsed_flags.parsed_flag.iter() {
436             if pf.name().starts_with("enabled_fixed_ro") {
437                 continue;
438             }
439             let first = pf.trace.first().unwrap();
440             assert_eq!(DEFAULT_FLAG_STATE, first.state());
441             assert_eq!(DEFAULT_FLAG_PERMISSION, first.permission());
442 
443             let last = pf.trace.last().unwrap();
444             assert_eq!(pf.state(), last.state());
445             assert_eq!(pf.permission(), last.permission());
446         }
447 
448         let enabled_fixed_ro =
449             parsed_flags.parsed_flag.iter().find(|pf| pf.name() == "enabled_fixed_ro").unwrap();
450         assert!(enabled_fixed_ro.is_fixed_read_only());
451         assert_eq!(ProtoFlagState::ENABLED, enabled_fixed_ro.state());
452         assert_eq!(ProtoFlagPermission::READ_ONLY, enabled_fixed_ro.permission());
453         assert_eq!(2, enabled_fixed_ro.trace.len());
454         assert_eq!(ProtoFlagPermission::READ_ONLY, enabled_fixed_ro.trace[0].permission());
455         assert_eq!(ProtoFlagPermission::READ_ONLY, enabled_fixed_ro.trace[1].permission());
456     }
457 
458     #[test]
test_parse_flags_setting_default()459     fn test_parse_flags_setting_default() {
460         let first_flag = r#"
461         package: "com.first"
462         flag {
463             name: "first"
464             namespace: "first_ns"
465             description: "This is the description of the first flag."
466             bug: "123"
467         }
468         "#;
469         let declaration =
470             vec![Input { source: "momery".to_string(), reader: Box::new(first_flag.as_bytes()) }];
471         let value: Vec<Input> = vec![];
472 
473         let flags_bytes = crate::commands::parse_flags(
474             "com.first",
475             None,
476             declaration,
477             value,
478             ProtoFlagPermission::READ_ONLY,
479         )
480         .unwrap();
481         let parsed_flags =
482             aconfig_protos::parsed_flags::try_from_binary_proto(&flags_bytes).unwrap();
483         assert_eq!(1, parsed_flags.parsed_flag.len());
484         let parsed_flag = parsed_flags.parsed_flag.first().unwrap();
485         assert_eq!(ProtoFlagState::DISABLED, parsed_flag.state());
486         assert_eq!(ProtoFlagPermission::READ_ONLY, parsed_flag.permission());
487     }
488 
489     #[test]
test_parse_flags_package_mismatch_between_declaration_and_command_line()490     fn test_parse_flags_package_mismatch_between_declaration_and_command_line() {
491         let first_flag = r#"
492         package: "com.declaration.package"
493         container: "first.container"
494         flag {
495             name: "first"
496             namespace: "first_ns"
497             description: "This is the description of the first flag."
498             bug: "123"
499         }
500         "#;
501         let declaration =
502             vec![Input { source: "memory".to_string(), reader: Box::new(first_flag.as_bytes()) }];
503 
504         let value: Vec<Input> = vec![];
505 
506         let error = crate::commands::parse_flags(
507             "com.argument.package",
508             Some("first.container"),
509             declaration,
510             value,
511             ProtoFlagPermission::READ_WRITE,
512         )
513         .unwrap_err();
514         assert_eq!(
515             format!("{:?}", error),
516             "failed to parse memory: expected package com.argument.package, got com.declaration.package"
517         );
518     }
519 
520     #[test]
test_parse_flags_container_mismatch_between_declaration_and_command_line()521     fn test_parse_flags_container_mismatch_between_declaration_and_command_line() {
522         let first_flag = r#"
523         package: "com.first"
524         container: "declaration.container"
525         flag {
526             name: "first"
527             namespace: "first_ns"
528             description: "This is the description of the first flag."
529             bug: "123"
530         }
531         "#;
532         let declaration =
533             vec![Input { source: "memory".to_string(), reader: Box::new(first_flag.as_bytes()) }];
534 
535         let value: Vec<Input> = vec![];
536 
537         let error = crate::commands::parse_flags(
538             "com.first",
539             Some("argument.container"),
540             declaration,
541             value,
542             ProtoFlagPermission::READ_WRITE,
543         )
544         .unwrap_err();
545         assert_eq!(
546             format!("{:?}", error),
547             "failed to parse memory: expected container argument.container, got declaration.container"
548         );
549     }
550 
551     #[test]
test_parse_flags_override_fixed_read_only()552     fn test_parse_flags_override_fixed_read_only() {
553         let first_flag = r#"
554         package: "com.first"
555         container: "com.first.container"
556         flag {
557             name: "first"
558             namespace: "first_ns"
559             description: "This is the description of the first flag."
560             bug: "123"
561             is_fixed_read_only: true
562         }
563         "#;
564         let declaration =
565             vec![Input { source: "memory".to_string(), reader: Box::new(first_flag.as_bytes()) }];
566 
567         let first_flag_value = r#"
568         flag_value {
569             package: "com.first"
570             name: "first"
571             state: DISABLED
572             permission: READ_WRITE
573         }
574         "#;
575         let value = vec![Input {
576             source: "memory".to_string(),
577             reader: Box::new(first_flag_value.as_bytes()),
578         }];
579         let error = crate::commands::parse_flags(
580             "com.first",
581             Some("com.first.container"),
582             declaration,
583             value,
584             ProtoFlagPermission::READ_WRITE,
585         )
586         .unwrap_err();
587         assert_eq!(
588             format!("{:?}", error),
589             "failed to set permission of flag first, since this flag is fixed read only flag"
590         );
591     }
592 
593     #[test]
test_parse_flags_metadata()594     fn test_parse_flags_metadata() {
595         let metadata_flag = r#"
596         package: "com.first"
597         flag {
598             name: "first"
599             namespace: "first_ns"
600             description: "This is the description of this feature flag."
601             bug: "123"
602             metadata {
603                 purpose: PURPOSE_FEATURE
604             }
605         }
606         "#;
607         let declaration = vec![Input {
608             source: "memory".to_string(),
609             reader: Box::new(metadata_flag.as_bytes()),
610         }];
611         let value: Vec<Input> = vec![];
612 
613         let flags_bytes = crate::commands::parse_flags(
614             "com.first",
615             None,
616             declaration,
617             value,
618             ProtoFlagPermission::READ_ONLY,
619         )
620         .unwrap();
621         let parsed_flags =
622             aconfig_protos::parsed_flags::try_from_binary_proto(&flags_bytes).unwrap();
623         assert_eq!(1, parsed_flags.parsed_flag.len());
624         let parsed_flag = parsed_flags.parsed_flag.first().unwrap();
625         assert_eq!(ProtoFlagPurpose::PURPOSE_FEATURE, parsed_flag.metadata.purpose());
626     }
627 
628     #[test]
test_create_device_config_defaults()629     fn test_create_device_config_defaults() {
630         let input = parse_test_flags_as_input();
631         let bytes = create_device_config_defaults(input).unwrap();
632         let text = std::str::from_utf8(&bytes).unwrap();
633         assert_eq!("aconfig_test:com.android.aconfig.test.disabled_rw=disabled\naconfig_test:com.android.aconfig.test.disabled_rw_exported=disabled\nother_namespace:com.android.aconfig.test.disabled_rw_in_other_namespace=disabled\naconfig_test:com.android.aconfig.test.enabled_rw=enabled\n", text);
634     }
635 
636     #[test]
test_create_device_config_sysprops()637     fn test_create_device_config_sysprops() {
638         let input = parse_test_flags_as_input();
639         let bytes = create_device_config_sysprops(input).unwrap();
640         let text = std::str::from_utf8(&bytes).unwrap();
641         assert_eq!("persist.device_config.com.android.aconfig.test.disabled_rw=false\npersist.device_config.com.android.aconfig.test.disabled_rw_exported=false\npersist.device_config.com.android.aconfig.test.disabled_rw_in_other_namespace=false\npersist.device_config.com.android.aconfig.test.enabled_rw=true\n", text);
642     }
643 
644     #[test]
test_dump()645     fn test_dump() {
646         let input = parse_test_flags_as_input();
647         let bytes = dump_parsed_flags(
648             vec![input],
649             DumpFormat::Custom("{fully_qualified_name}".to_string()),
650             &[],
651             false,
652         )
653         .unwrap();
654         let text = std::str::from_utf8(&bytes).unwrap();
655         assert!(text.contains("com.android.aconfig.test.disabled_ro"));
656     }
657 
658     #[test]
test_dump_textproto_format_dedup()659     fn test_dump_textproto_format_dedup() {
660         let input = parse_test_flags_as_input();
661         let input2 = parse_test_flags_as_input();
662         let bytes =
663             dump_parsed_flags(vec![input, input2], DumpFormat::Textproto, &[], true).unwrap();
664         let text = std::str::from_utf8(&bytes).unwrap();
665         assert_eq!(
666             None,
667             crate::test::first_significant_code_diff(
668                 crate::test::TEST_FLAGS_TEXTPROTO.trim(),
669                 text.trim()
670             )
671         );
672     }
673 
parse_test_flags_as_input() -> Input674     fn parse_test_flags_as_input() -> Input {
675         let parsed_flags = crate::test::parse_test_flags();
676         let binary_proto = parsed_flags.write_to_bytes().unwrap();
677         let cursor = std::io::Cursor::new(binary_proto);
678         let reader = Box::new(cursor);
679         Input { source: "test.data".to_string(), reader }
680     }
681 
682     #[test]
test_modify_parsed_flags_based_on_mode_prod()683     fn test_modify_parsed_flags_based_on_mode_prod() {
684         let parsed_flags = crate::test::parse_test_flags();
685         let p_parsed_flags =
686             modify_parsed_flags_based_on_mode(parsed_flags.clone(), CodegenMode::Production)
687                 .unwrap();
688         assert_eq!(parsed_flags.parsed_flag.len(), p_parsed_flags.len());
689         for (i, item) in p_parsed_flags.iter().enumerate() {
690             assert!(parsed_flags.parsed_flag[i].eq(item));
691         }
692     }
693 
694     #[test]
test_modify_parsed_flags_based_on_mode_exported()695     fn test_modify_parsed_flags_based_on_mode_exported() {
696         let parsed_flags = crate::test::parse_test_flags();
697         let p_parsed_flags =
698             modify_parsed_flags_based_on_mode(parsed_flags, CodegenMode::Exported).unwrap();
699         assert_eq!(3, p_parsed_flags.len());
700         for flag in p_parsed_flags.iter() {
701             assert_eq!(ProtoFlagState::DISABLED, flag.state());
702             assert_eq!(ProtoFlagPermission::READ_WRITE, flag.permission());
703             assert!(!flag.is_fixed_read_only());
704             assert!(flag.is_exported());
705         }
706 
707         let mut parsed_flags = crate::test::parse_test_flags();
708         parsed_flags.parsed_flag.retain(|pf| !pf.is_exported());
709         let error =
710             modify_parsed_flags_based_on_mode(parsed_flags, CodegenMode::Exported).unwrap_err();
711         assert_eq!("exported library contains no exported flags", format!("{:?}", error));
712     }
713 
714     #[test]
test_assign_flag_ids()715     fn test_assign_flag_ids() {
716         let parsed_flags = crate::test::parse_test_flags();
717         let package = find_unique_package(&parsed_flags.parsed_flag).unwrap().to_string();
718         let flag_ids = assign_flag_ids(&package, parsed_flags.parsed_flag.iter()).unwrap();
719         let expected_flag_ids = HashMap::from([
720             (String::from("disabled_ro"), 0_u16),
721             (String::from("disabled_rw"), 1_u16),
722             (String::from("disabled_rw_exported"), 2_u16),
723             (String::from("disabled_rw_in_other_namespace"), 3_u16),
724             (String::from("enabled_fixed_ro"), 4_u16),
725             (String::from("enabled_fixed_ro_exported"), 5_u16),
726             (String::from("enabled_ro"), 6_u16),
727             (String::from("enabled_ro_exported"), 7_u16),
728             (String::from("enabled_rw"), 8_u16),
729         ]);
730         assert_eq!(flag_ids, expected_flag_ids);
731     }
732 
733     #[test]
test_modify_parsed_flags_based_on_mode_force_read_only()734     fn test_modify_parsed_flags_based_on_mode_force_read_only() {
735         let parsed_flags = crate::test::parse_test_flags();
736         let p_parsed_flags =
737             modify_parsed_flags_based_on_mode(parsed_flags.clone(), CodegenMode::ForceReadOnly)
738                 .unwrap();
739         assert_eq!(6, p_parsed_flags.len());
740         for pf in p_parsed_flags {
741             assert_eq!(ProtoFlagPermission::READ_ONLY, pf.permission());
742         }
743 
744         let mut parsed_flags = crate::test::parse_test_flags();
745         parsed_flags.parsed_flag.retain_mut(|pf| pf.is_exported());
746         let error = modify_parsed_flags_based_on_mode(parsed_flags, CodegenMode::ForceReadOnly)
747             .unwrap_err();
748         assert_eq!(
749             "force-read-only library contains no force-read-only flags",
750             format!("{:?}", error)
751         );
752     }
753 }
754