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