Dennis Shen | 0d1c562 | 2023-12-01 21:04:29 +0000 | [diff] [blame^] | 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::Result; |
| 18 | use std::collections::{HashMap, HashSet}; |
| 19 | |
| 20 | use crate::commands::OutputFile; |
| 21 | use crate::protos::{ProtoParsedFlag, ProtoParsedFlags}; |
| 22 | |
| 23 | pub struct FlagPackage<'a> { |
| 24 | pub package_name: &'a str, |
| 25 | pub package_id: u32, |
| 26 | pub flag_names: HashSet<&'a str>, |
| 27 | pub boolean_flags: Vec<&'a ProtoParsedFlag>, |
| 28 | pub boolean_offset: u32, |
| 29 | } |
| 30 | |
| 31 | impl<'a> FlagPackage<'a> { |
| 32 | fn new(package_name: &'a str, package_id: u32) -> Self { |
| 33 | FlagPackage { |
| 34 | package_name, |
| 35 | package_id, |
| 36 | flag_names: HashSet::new(), |
| 37 | boolean_flags: vec![], |
| 38 | boolean_offset: 0, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn insert(&mut self, pf: &'a ProtoParsedFlag) { |
| 43 | if self.flag_names.insert(pf.name()) { |
| 44 | self.boolean_flags.push(pf); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub fn group_flags_by_package<'a, I>(parsed_flags_vec_iter: I) -> Vec<FlagPackage<'a>> |
| 50 | where |
| 51 | I: Iterator<Item = &'a ProtoParsedFlags>, |
| 52 | { |
| 53 | // group flags by package |
| 54 | let mut packages: Vec<FlagPackage<'a>> = Vec::new(); |
| 55 | let mut package_index: HashMap<&'a str, usize> = HashMap::new(); |
| 56 | for parsed_flags in parsed_flags_vec_iter { |
| 57 | for parsed_flag in parsed_flags.parsed_flag.iter() { |
| 58 | let index = *(package_index.entry(parsed_flag.package()).or_insert(packages.len())); |
| 59 | if index == packages.len() { |
| 60 | packages.push(FlagPackage::new(parsed_flag.package(), index as u32)); |
| 61 | } |
| 62 | packages[index].insert(parsed_flag); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // calculate package flag value start offset, in flag value file, each boolean |
| 67 | // is stored as two bytes, the first byte will be the flag value. the second |
| 68 | // byte is flag info byte, which is a bitmask to indicate the status of a flag |
| 69 | let mut boolean_offset = 0; |
| 70 | for p in packages.iter_mut() { |
| 71 | p.boolean_offset = boolean_offset; |
| 72 | boolean_offset += 2 * p.boolean_flags.len() as u32; |
| 73 | } |
| 74 | |
| 75 | packages |
| 76 | } |
| 77 | |
| 78 | pub fn generate_storage_files<'a, I>( |
| 79 | _containser: &str, |
| 80 | parsed_flags_vec_iter: I, |
| 81 | ) -> Result<Vec<OutputFile>> |
| 82 | where |
| 83 | I: Iterator<Item = &'a ProtoParsedFlags>, |
| 84 | { |
| 85 | let _packages = group_flags_by_package(parsed_flags_vec_iter); |
| 86 | Ok(vec![]) |
| 87 | } |
| 88 | |
| 89 | #[cfg(test)] |
| 90 | mod tests { |
| 91 | use super::*; |
| 92 | use crate::Input; |
| 93 | |
| 94 | pub fn parse_all_test_flags() -> Vec<ProtoParsedFlags> { |
| 95 | let aconfig_files = [ |
| 96 | ( |
| 97 | "com.android.aconfig.storage.test_1", |
| 98 | "storage_test_1_part_1.aconfig", |
| 99 | include_bytes!("../../tests/storage_test_1_part_1.aconfig").as_slice(), |
| 100 | ), |
| 101 | ( |
| 102 | "com.android.aconfig.storage.test_1", |
| 103 | "storage_test_1_part_2.aconfig", |
| 104 | include_bytes!("../../tests/storage_test_1_part_2.aconfig").as_slice(), |
| 105 | ), |
| 106 | ( |
| 107 | "com.android.aconfig.storage.test_2", |
| 108 | "storage_test_2.aconfig", |
| 109 | include_bytes!("../../tests/storage_test_2.aconfig").as_slice(), |
| 110 | ), |
| 111 | ]; |
| 112 | |
| 113 | aconfig_files |
| 114 | .into_iter() |
| 115 | .map(|(pkg, file, content)| { |
| 116 | let bytes = crate::commands::parse_flags( |
| 117 | pkg, |
| 118 | Some("system"), |
| 119 | vec![Input { |
| 120 | source: format!("tests/{}", file).to_string(), |
| 121 | reader: Box::new(content), |
| 122 | }], |
| 123 | vec![], |
| 124 | crate::commands::DEFAULT_FLAG_PERMISSION, |
| 125 | ) |
| 126 | .unwrap(); |
| 127 | crate::protos::parsed_flags::try_from_binary_proto(&bytes).unwrap() |
| 128 | }) |
| 129 | .collect() |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn test_flag_package() { |
| 134 | let caches = parse_all_test_flags(); |
| 135 | let packages = group_flags_by_package(caches.iter()); |
| 136 | |
| 137 | for pkg in packages.iter() { |
| 138 | let pkg_name = pkg.package_name; |
| 139 | assert_eq!(pkg.flag_names.len(), pkg.boolean_flags.len()); |
| 140 | for pf in pkg.boolean_flags.iter() { |
| 141 | assert!(pkg.flag_names.contains(pf.name())); |
| 142 | assert_eq!(pf.package(), pkg_name); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | assert_eq!(packages.len(), 2); |
| 147 | |
| 148 | assert_eq!(packages[0].package_name, "com.android.aconfig.storage.test_1"); |
| 149 | assert_eq!(packages[0].package_id, 0); |
| 150 | assert_eq!(packages[0].flag_names.len(), 5); |
| 151 | assert!(packages[0].flag_names.contains("enabled_rw")); |
| 152 | assert!(packages[0].flag_names.contains("disabled_rw")); |
| 153 | assert!(packages[0].flag_names.contains("enabled_ro")); |
| 154 | assert!(packages[0].flag_names.contains("disabled_ro")); |
| 155 | assert!(packages[0].flag_names.contains("enabled_fixed_ro")); |
| 156 | assert_eq!(packages[0].boolean_offset, 0); |
| 157 | |
| 158 | assert_eq!(packages[1].package_name, "com.android.aconfig.storage.test_2"); |
| 159 | assert_eq!(packages[1].package_id, 1); |
| 160 | assert_eq!(packages[1].flag_names.len(), 3); |
| 161 | assert!(packages[1].flag_names.contains("enabled_ro")); |
| 162 | assert!(packages[1].flag_names.contains("disabled_ro")); |
| 163 | assert!(packages[1].flag_names.contains("enabled_fixed_ro")); |
| 164 | assert_eq!(packages[1].boolean_offset, 10); |
| 165 | } |
| 166 | } |