Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2ac7a4c2 authored by Dennis Shen's avatar Dennis Shen
Browse files

aconfig: add flag info read rust api

Bug: b/321077378
Test: atest aconfig_storage_read_api.test; atest
aconfig_storage_read_api.test.rust

Change-Id: I382ac0145c5d91827952b3ddb01cabefd1539854
parent eff5363e
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -91,9 +91,9 @@ impl FlagInfoHeader {
/// bit field for flag info
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FlagInfoBit {
    IsSticky = 0,
    IsReadWrite = 1,
    HasOverride = 2,
    IsSticky = 1 << 0,
    IsReadWrite = 1 << 1,
    HasOverride = 1 << 2,
}

/// Flag info node struct
@@ -108,9 +108,9 @@ impl fmt::Debug for FlagInfoNode {
        writeln!(
            f,
            "sticky: {}, readwrite: {}, override: {}",
            self.attributes & (FlagInfoBit::IsSticky as u8),
            self.attributes & (FlagInfoBit::IsReadWrite as u8),
            self.attributes & (FlagInfoBit::HasOverride as u8),
            self.attributes & (FlagInfoBit::IsSticky as u8) != 0,
            self.attributes & (FlagInfoBit::IsReadWrite as u8) != 0,
            self.attributes & (FlagInfoBit::HasOverride as u8) != 0,
        )?;
        Ok(())
    }
+1 −1
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ use std::fs::File;
use std::hash::{Hash, Hasher};
use std::io::Read;

pub use crate::flag_info::{FlagInfoHeader, FlagInfoList, FlagInfoNode};
pub use crate::flag_info::{FlagInfoHeader, FlagInfoList, FlagInfoNode, FlagInfoBit};
pub use crate::flag_table::{FlagTable, FlagTableHeader, FlagTableNode};
pub use crate::flag_value::{FlagValueHeader, FlagValueList};
pub use crate::package_table::{PackageTable, PackageTableHeader, PackageTableNode};
+1 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ rust_test_host {
        "tests/package.map",
        "tests/flag.map",
        "tests/flag.val",
        "tests/flag.info",
    ],
}

+115 −0
Original line number Diff line number Diff line
/*
 * 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.
 */

//! flag value query module defines the flag value file read from mapped bytes

use crate::{AconfigStorageError, FILE_VERSION};
use aconfig_storage_file::{flag_info::FlagInfoHeader, read_u8_from_bytes};
use anyhow::anyhow;

/// Get flag attribute bitfield
pub fn find_boolean_flag_attribute(
    buf: &[u8],
    flag_offset: u32,
) -> Result<u8, AconfigStorageError> {
    let interpreted_header = FlagInfoHeader::from_bytes(buf)?;
    if interpreted_header.version > crate::FILE_VERSION {
        return Err(AconfigStorageError::HigherStorageFileVersion(anyhow!(
            "Cannot read storage file with a higher version of {} with lib version {}",
            interpreted_header.version,
            FILE_VERSION
        )));
    }

    let mut head = (interpreted_header.boolean_flag_offset + flag_offset) as usize;

    if head >= interpreted_header.file_size as usize {
        return Err(AconfigStorageError::InvalidStorageFileOffset(anyhow!(
            "Flag info offset goes beyond the end of the file."
        )));
    }

    let val = read_u8_from_bytes(buf, &mut head)?;
    Ok(val)
}

#[cfg(test)]
mod tests {
    use super::*;
    use aconfig_storage_file::{test_utils::create_test_flag_info_list, FlagInfoBit};

    #[test]
    // this test point locks down query if flag is sticky
    fn test_is_flag_sticky() {
        let flag_info_list = create_test_flag_info_list().into_bytes();
        for offset in 0..8 {
            let attribute = find_boolean_flag_attribute(&flag_info_list[..], offset).unwrap();
            assert_eq!((attribute & FlagInfoBit::IsSticky as u8) != 0u8, false);
        }
    }

    #[test]
    // this test point locks down query if flag is readwrite
    fn test_is_flag_readwrite() {
        let flag_info_list = create_test_flag_info_list().into_bytes();
        let baseline: Vec<bool> = vec![true, false, true, false, false, false, false, false];
        for offset in 0..8 {
            let attribute = find_boolean_flag_attribute(&flag_info_list[..], offset).unwrap();
            assert_eq!(
                (attribute & FlagInfoBit::IsReadWrite as u8) != 0u8,
                baseline[offset as usize]
            );
        }
    }

    #[test]
    // this test point locks down query if flag has override
    fn test_flag_has_override() {
        let flag_info_list = create_test_flag_info_list().into_bytes();
        for offset in 0..8 {
            let attribute = find_boolean_flag_attribute(&flag_info_list[..], offset).unwrap();
            assert_eq!((attribute & FlagInfoBit::HasOverride as u8) != 0u8, false);
        }
    }

    #[test]
    // this test point locks down query beyond the end of boolean section
    fn test_boolean_out_of_range() {
        let flag_info_list = create_test_flag_info_list().into_bytes();
        let error = find_boolean_flag_attribute(&flag_info_list[..], 8).unwrap_err();
        assert_eq!(
            format!("{:?}", error),
            "InvalidStorageFileOffset(Flag info offset goes beyond the end of the file.)"
        );
    }

    #[test]
    // this test point locks down query error when file has a higher version
    fn test_higher_version_storage_file() {
        let mut info_list = create_test_flag_info_list();
        info_list.header.version = crate::FILE_VERSION + 1;
        let flag_info = info_list.into_bytes();
        let error = find_boolean_flag_attribute(&flag_info[..], 4).unwrap_err();
        assert_eq!(
            format!("{:?}", error),
            format!(
                "HigherStorageFileVersion(Cannot read storage file with a higher version of {} with lib version {})",
                crate::FILE_VERSION + 1,
                crate::FILE_VERSION
            )
        );
    }
}
+2 −15
Original line number Diff line number Diff line
@@ -48,26 +48,13 @@ pub fn find_boolean_flag_value(buf: &[u8], flag_offset: u32) -> Result<bool, Aco
#[cfg(test)]
mod tests {
    use super::*;
    use aconfig_storage_file::{FlagValueList, StorageFileType};

    pub fn create_test_flag_value_list() -> FlagValueList {
        let header = FlagValueHeader {
            version: crate::FILE_VERSION,
            container: String::from("system"),
            file_type: StorageFileType::FlagVal as u8,
            file_size: 35,
            num_flags: 8,
            boolean_value_offset: 27,
        };
        let booleans: Vec<bool> = vec![false, true, false, false, true, true, false, true];
        FlagValueList { header, booleans }
    }
    use aconfig_storage_file::test_utils::create_test_flag_value_list;

    #[test]
    // this test point locks down flag value query
    fn test_flag_value_query() {
        let flag_value_list = create_test_flag_value_list().into_bytes();
        let baseline: Vec<bool> = vec![false, true, false, false, true, true, false, true];
        let baseline: Vec<bool> = vec![false, true, true, false, true, true, true, true];
        for (offset, expected_value) in baseline.into_iter().enumerate() {
            let flag_value = find_boolean_flag_value(&flag_value_list[..], offset as u32).unwrap();
            assert_eq!(flag_value, expected_value);
Loading