Software APIs
rom_ext_boot_policy_unittest.cc
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include "sw/device/silicon_creator/rom_ext/rom_ext_boot_policy.h"
6 
7 #include "gtest/gtest.h"
8 #include "sw/device/silicon_creator/lib/mock_boot_data.h"
9 #include "sw/device/silicon_creator/lib/mock_manifest.h"
10 #include "sw/device/silicon_creator/rom_ext/mock_rom_ext_boot_policy_ptrs.h"
11 #include "sw/device/silicon_creator/testing/rom_test.h"
12 
13 namespace manifest_unittest {
14 namespace {
15 using ::testing::Return;
16 
18  protected:
19  rom_test::MockRomExtBootPolicyPtrs rom_ext_boot_policy_ptrs_;
20  rom_test::MockManifest mock_manifest_;
21  rom_test::MockBootData mock_boot_data_;
22 };
23 
24 // TODO(#21204): Refactor to use `manifest_check` from `lib/manifest.h`.
25 TEST_F(RomExtBootPolicyTest, DISABLED_ManifestCheck) {
27 
30 
32  EXPECT_CALL(mock_manifest_, Check(&manifest)).WillOnce(Return(kErrorOk));
33  EXPECT_EQ(rom_ext_boot_policy_manifest_check(&manifest, &boot_data),
34  kErrorOk);
35 
36  manifest.length = CHIP_BL0_SIZE_MAX >> 1;
37  EXPECT_CALL(mock_manifest_, Check(&manifest)).WillOnce(Return(kErrorOk));
38  EXPECT_EQ(rom_ext_boot_policy_manifest_check(&manifest, &boot_data),
39  kErrorOk);
40 
41  manifest.length = CHIP_BL0_SIZE_MAX;
42  EXPECT_CALL(mock_manifest_, Check(&manifest)).WillOnce(Return(kErrorOk));
43  EXPECT_EQ(rom_ext_boot_policy_manifest_check(&manifest, &boot_data),
44  kErrorOk);
45 }
46 
47 TEST_F(RomExtBootPolicyTest, ManifestCheckBadIdentifier) {
50 
51  EXPECT_EQ(rom_ext_boot_policy_manifest_check(&manifest, &boot_data),
52  kErrorBootPolicyBadIdentifier);
53 }
54 
55 TEST_F(RomExtBootPolicyTest, ManifestCheckBadLength) {
59 
61  EXPECT_EQ(rom_ext_boot_policy_manifest_check(&manifest, &boot_data),
62  kErrorBootPolicyBadLength);
63 
64  manifest.length = CHIP_BL0_SIZE_MAX + 1;
65  EXPECT_EQ(rom_ext_boot_policy_manifest_check(&manifest, &boot_data),
66  kErrorBootPolicyBadLength);
67 }
68 
69 TEST_F(RomExtBootPolicyTest, ManifestCheckBadBl0SecVer) {
72 
77 
78  EXPECT_EQ(rom_ext_boot_policy_manifest_check(&manifest, &boot_data),
79  kErrorBootPolicyRollback);
80 }
81 
83  uint32_t primary;
84 };
85 
87  : public RomExtBootPolicyTest,
88  public testing::WithParamInterface<ManifestOrderTestCase> {};
89 
90 TEST_P(ManifestOrderTest, ManifestsGet) {
91  manifest_t manifest_a{};
92  manifest_t manifest_b{};
93  manifest_a.security_version = 0;
94  manifest_b.security_version = 1;
95 
96  EXPECT_CALL(rom_ext_boot_policy_ptrs_, ManifestA)
97  .WillOnce(Return(&manifest_a));
98  EXPECT_CALL(rom_ext_boot_policy_ptrs_, ManifestB)
99  .WillOnce(Return(&manifest_b));
100 
102  if (GetParam().primary == kBootSlotA) {
103  boot_data.primary_bl0_slot = kBootSlotA;
104  } else {
105  boot_data.primary_bl0_slot = kBootSlotB;
106  }
107 
109  rom_ext_boot_policy_manifests_get(&boot_data);
110  if (GetParam().primary == kBootSlotA) {
111  EXPECT_EQ(res.ordered[0], &manifest_a);
112  EXPECT_EQ(res.ordered[1], &manifest_b);
113  } else {
114  EXPECT_EQ(res.ordered[0], &manifest_b);
115  EXPECT_EQ(res.ordered[1], &manifest_a);
116  }
117 }
118 
119 INSTANTIATE_TEST_SUITE_P(SecurityVersionCases, ManifestOrderTest,
120  testing::Values(
121  ManifestOrderTestCase{
122  .primary = kBootSlotA,
123  },
124  ManifestOrderTestCase{
125  .primary = kBootSlotB,
126  }));
127 } // namespace
128 } // namespace manifest_unittest