xref: /aosp_15_r20/external/avb/rust/tests/tests.rs (revision d289c2ba6de359471b23d594623b906876bc48a0)
1 // Copyright 2023, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 mod cert_tests;
16 mod test_data;
17 mod test_ops;
18 mod verify_tests;
19 
20 use avb::{slot_verify, HashtreeErrorMode, SlotVerifyData, SlotVerifyFlags, SlotVerifyResult};
21 use std::{ffi::CString, fs};
22 use test_data::*;
23 use test_ops::{FakeVbmetaKey, TestOps};
24 
25 /// Initializes a `TestOps` object such that verification will succeed on `TEST_PARTITION_NAME`.
26 ///
27 /// This usually forms the basis of the `TestOps` objects used, with tests modifying the returned
28 /// object as needed for the individual test case.
build_test_ops_one_image_one_vbmeta<'a>() -> TestOps<'a>29 fn build_test_ops_one_image_one_vbmeta<'a>() -> TestOps<'a> {
30     let mut ops = TestOps::default();
31     ops.add_partition(TEST_PARTITION_NAME, fs::read(TEST_IMAGE_PATH).unwrap());
32     ops.add_partition("vbmeta", fs::read(TEST_VBMETA_PATH).unwrap());
33     ops.default_vbmeta_key = Some(FakeVbmetaKey::Avb {
34         public_key: fs::read(TEST_PUBLIC_KEY_PATH).unwrap(),
35         public_key_metadata: None,
36     });
37     ops.rollbacks.insert(TEST_VBMETA_ROLLBACK_LOCATION, Ok(0));
38     ops.unlock_state = Ok(false);
39     ops
40 }
41 
42 /// Calls `slot_verify()` using standard args for `build_test_ops_one_image_one_vbmeta()` setup.
verify_one_image_one_vbmeta<'a>( ops: &mut TestOps<'a>, ) -> SlotVerifyResult<'a, SlotVerifyData<'a>>43 fn verify_one_image_one_vbmeta<'a>(
44     ops: &mut TestOps<'a>,
45 ) -> SlotVerifyResult<'a, SlotVerifyData<'a>> {
46     slot_verify(
47         ops,
48         &[&CString::new(TEST_PARTITION_NAME).unwrap()],
49         None,
50         SlotVerifyFlags::AVB_SLOT_VERIFY_FLAGS_NONE,
51         HashtreeErrorMode::AVB_HASHTREE_ERROR_MODE_EIO,
52     )
53 }
54