Skip to content

Commit

Permalink
[sw/silicon_creator] Use EXPECT_DEATH in unit tests
Browse files Browse the repository at this point in the history
Fixes lowRISC#10549

Signed-off-by: Alphan Ulusoy <[email protected]>
  • Loading branch information
alphan committed Feb 11, 2022
1 parent 4810dd7 commit 4907768
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
29 changes: 14 additions & 15 deletions sw/device/silicon_creator/lib/base/sec_mmio_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,54 +173,53 @@ TEST_F(SecMmioTest, CheckCount) {
EXPECT_EQ(ctx_->check_count, 2);
}

// Negative test cases trigger assertions, which are caugth by `ASSERT_DEATH`
// calls. All test cases use lambda functions to wrap expectations and work
// around issue google/googletest#1004.
// Negative test cases trigger assertions, which are caugth by `EXPECT_DEATH`
// calls.
class SecMmioDeathTest : public SecMmioTest {};

TEST_F(SecMmioDeathTest, Read32OrDieSimulatedFault) {
ASSERT_DEATH(
[] {
EXPECT_DEATH(
{
EXPECT_ABS_READ32(0, 0x12345678);
EXPECT_ABS_READ32(0, 0);
sec_mmio_read32(0);
}(),
},
"");
}

TEST_F(SecMmioDeathTest, Write32SimulatedFault) {
ASSERT_DEATH(
[] {
EXPECT_DEATH(
{
EXPECT_ABS_WRITE32(0, 0x12345678);
EXPECT_ABS_READ32(0, 0);
sec_mmio_write32(0, 0x12345678);
}(),
},
"");
}

TEST_F(SecMmioDeathTest, CheckValuesSimulatedFault) {
ASSERT_DEATH(
[] {
EXPECT_DEATH(
{
EXPECT_ABS_WRITE32(0, 0x12345678);
EXPECT_ABS_READ32(0, 0x12345678);
sec_mmio_write32(0, 0x12345678);

EXPECT_ABS_READ32(0, 0);
sec_mmio_check_values(/*rnd_offset=*/0);
}(),
},
"");
}

TEST_F(SecMmioDeathTest, CheckCountWriteMismatch) {
// The developer forgot to increment the write counter, or an attacker
// glitched the sec write operation.
ASSERT_DEATH(
[] {
EXPECT_DEATH(
{
EXPECT_ABS_WRITE32(0, 0x12345678);
EXPECT_ABS_READ32(0, 0x12345678);
sec_mmio_write32(0, 0x12345678);
sec_mmio_check_counters(/*expected_check_count=*/0);
}(),
},
"");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class LifecycleDeathTest : public LifecycleTest,
public testing::WithParamInterface<uint32_t> {};

TEST_P(LifecycleDeathTest, InvalidState) {
ASSERT_DEATH(
EXPECT_DEATH(
{
EXPECT_SEC_READ32(base_ + LC_CTRL_LC_STATE_REG_OFFSET, GetParam());
lifecycle_state_get();
Expand Down
2 changes: 1 addition & 1 deletion sw/device/silicon_creator/lib/shutdown_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ TEST_F(ShutdownTest, InitializeManufacturing) {
class ShutdownDeathTest : public ShutdownTest {};

TEST_F(ShutdownDeathTest, InitializeInvalid) {
ASSERT_DEATH(
EXPECT_DEATH(
{
SetupOtpReads();
shutdown_init(static_cast<lifecycle_state_t>(0));
Expand Down
4 changes: 2 additions & 2 deletions sw/device/silicon_creator/lib/sigverify_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ INSTANTIATE_TEST_SUITE_P(NonTestOperationalStates, SigverifyInNonTestStates,
class SigverifyInNonTestStatesDeathTest : public SigverifyInLcState {};

TEST_P(SigverifyInNonTestStatesDeathTest, BadOtpValue) {
ASSERT_DEATH(
EXPECT_DEATH(
{
EXPECT_CALL(
otp_,
Expand Down Expand Up @@ -222,7 +222,7 @@ TEST_F(SigverifyInTestStates, BadSignatureIbex) {
class SigverifyBadLcStateDeathTest : public SigverifyInLcState {};

TEST_F(SigverifyBadLcStateDeathTest, BadLcState) {
ASSERT_DEATH(
EXPECT_DEATH(
{
uint32_t flash_exec = 0;
sigverify_rsa_verify(&kSignature, &key_, &kTestDigest,
Expand Down
4 changes: 2 additions & 2 deletions sw/device/silicon_creator/mask_rom/sigverify_keys_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ TEST_P(BadKeyIdTypeDeathTest, BadKeyType) {
};
const sigverify_rsa_key_t *key;

ASSERT_DEATH(
EXPECT_DEATH(
{
ExpectKeysPtrGet(keys);
sigverify_rsa_key_get(key_id, GetParam(), &key);
Expand All @@ -206,7 +206,7 @@ TEST_P(NonOperationalStateDeathTest, BadKey) {
std::tie(key_index, lc_state) = GetParam();
const sigverify_rsa_key_t *key;

ASSERT_DEATH(
EXPECT_DEATH(
{
ExpectKeysPtrGet(kMockKeys);
sigverify_rsa_key_get(
Expand Down

0 comments on commit 4907768

Please sign in to comment.