xref: /aosp_15_r20/build/make/tools/aconfig/aconfig_storage_read_api/src/flag_table_query.rs (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
1 /*
2  * Copyright (C) 2024 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 //! flag table query module defines the flag table file read from mapped bytes
18 
19 use crate::AconfigStorageError;
20 use aconfig_storage_file::{
21     flag_table::FlagTableHeader, flag_table::FlagTableNode, read_u32_from_bytes, StoredFlagType,
22     MAX_SUPPORTED_FILE_VERSION,
23 };
24 use anyhow::anyhow;
25 
26 /// Flag table query return
27 #[derive(PartialEq, Debug)]
28 pub struct FlagReadContext {
29     pub flag_type: StoredFlagType,
30     pub flag_index: u16,
31 }
32 
33 /// Query flag read context: flag type and within package flag index
find_flag_read_context( buf: &[u8], package_id: u32, flag: &str, ) -> Result<Option<FlagReadContext>, AconfigStorageError>34 pub fn find_flag_read_context(
35     buf: &[u8],
36     package_id: u32,
37     flag: &str,
38 ) -> Result<Option<FlagReadContext>, AconfigStorageError> {
39     let interpreted_header = FlagTableHeader::from_bytes(buf)?;
40     if interpreted_header.version > MAX_SUPPORTED_FILE_VERSION {
41         return Err(AconfigStorageError::HigherStorageFileVersion(anyhow!(
42             "Cannot read storage file with a higher version of {} with lib version {}",
43             interpreted_header.version,
44             MAX_SUPPORTED_FILE_VERSION
45         )));
46     }
47 
48     let num_buckets = (interpreted_header.node_offset - interpreted_header.bucket_offset) / 4;
49     let bucket_index = FlagTableNode::find_bucket_index(package_id, flag, num_buckets);
50 
51     let mut pos = (interpreted_header.bucket_offset + 4 * bucket_index) as usize;
52     let mut flag_node_offset = read_u32_from_bytes(buf, &mut pos)? as usize;
53     if flag_node_offset < interpreted_header.node_offset as usize
54         || flag_node_offset >= interpreted_header.file_size as usize
55     {
56         return Ok(None);
57     }
58 
59     loop {
60         let interpreted_node = FlagTableNode::from_bytes(&buf[flag_node_offset..])?;
61         if interpreted_node.package_id == package_id && interpreted_node.flag_name == flag {
62             return Ok(Some(FlagReadContext {
63                 flag_type: interpreted_node.flag_type,
64                 flag_index: interpreted_node.flag_index,
65             }));
66         }
67         match interpreted_node.next_offset {
68             Some(offset) => flag_node_offset = offset as usize,
69             None => return Ok(None),
70         }
71     }
72 }
73 
74 #[cfg(test)]
75 mod tests {
76     use super::*;
77     use aconfig_storage_file::{test_utils::create_test_flag_table, DEFAULT_FILE_VERSION};
78 
79     #[test]
80     // this test point locks down table query
test_flag_query()81     fn test_flag_query() {
82         let flag_table = create_test_flag_table(DEFAULT_FILE_VERSION).into_bytes();
83         let baseline = vec![
84             (0, "enabled_ro", StoredFlagType::ReadOnlyBoolean, 1u16),
85             (0, "enabled_rw", StoredFlagType::ReadWriteBoolean, 2u16),
86             (2, "enabled_rw", StoredFlagType::ReadWriteBoolean, 1u16),
87             (1, "disabled_rw", StoredFlagType::ReadWriteBoolean, 0u16),
88             (1, "enabled_fixed_ro", StoredFlagType::FixedReadOnlyBoolean, 1u16),
89             (1, "enabled_ro", StoredFlagType::ReadOnlyBoolean, 2u16),
90             (2, "enabled_fixed_ro", StoredFlagType::FixedReadOnlyBoolean, 0u16),
91             (0, "disabled_rw", StoredFlagType::ReadWriteBoolean, 0u16),
92         ];
93         for (package_id, flag_name, flag_type, flag_index) in baseline.into_iter() {
94             let flag_context =
95                 find_flag_read_context(&flag_table[..], package_id, flag_name).unwrap().unwrap();
96             assert_eq!(flag_context.flag_type, flag_type);
97             assert_eq!(flag_context.flag_index, flag_index);
98         }
99     }
100 
101     #[test]
102     // this test point locks down table query of a non exist flag
test_not_existed_flag_query()103     fn test_not_existed_flag_query() {
104         let flag_table = create_test_flag_table(DEFAULT_FILE_VERSION).into_bytes();
105         let flag_context = find_flag_read_context(&flag_table[..], 1, "disabled_fixed_ro").unwrap();
106         assert_eq!(flag_context, None);
107         let flag_context = find_flag_read_context(&flag_table[..], 2, "disabled_rw").unwrap();
108         assert_eq!(flag_context, None);
109     }
110 
111     #[test]
112     // this test point locks down query error when file has a higher version
test_higher_version_storage_file()113     fn test_higher_version_storage_file() {
114         let mut table = create_test_flag_table(DEFAULT_FILE_VERSION);
115         table.header.version = MAX_SUPPORTED_FILE_VERSION + 1;
116         let flag_table = table.into_bytes();
117         let error = find_flag_read_context(&flag_table[..], 0, "enabled_ro").unwrap_err();
118         assert_eq!(
119             format!("{:?}", error),
120             format!(
121                 "HigherStorageFileVersion(Cannot read storage file with a higher version of {} with lib version {})",
122                 MAX_SUPPORTED_FILE_VERSION + 1,
123                 MAX_SUPPORTED_FILE_VERSION
124             )
125         );
126     }
127 }
128