Skip to content

Commit

Permalink
Make test setup functions terminate on error (project-chip#33002)
Browse files Browse the repository at this point in the history
* Make test setup functions terminate on error

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
mbknust and restyled-commits authored May 6, 2024
1 parent bdd33bd commit 3c5b92c
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 146 deletions.
4 changes: 2 additions & 2 deletions src/app/icd/server/tests/TestICDManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class TestICDManager : public ::testing::Test
ASSERT_NE(pMessagingContext, nullptr);
}

ASSERT_EQ(pMessagingContext->SetUpTestSuite(), CHIP_NO_ERROR);
pMessagingContext->SetUpTestSuite();
ASSERT_EQ(chip::DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR);

DeviceLayer::SetSystemLayerForTesting(&(pMessagingContext->GetSystemLayer()));
Expand Down Expand Up @@ -198,7 +198,7 @@ class TestICDManager : public ::testing::Test
// Performs setup for each individual test in the test suite
void SetUp() override
{
EXPECT_EQ(pMessagingContext->SetUp(), CHIP_NO_ERROR);
pMessagingContext->SetUp();

mICDStateObserver.ResetAll();
mICDManager.RegisterObserver(&mICDStateObserver);
Expand Down
33 changes: 15 additions & 18 deletions src/app/tests/AppTestContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ chip::Access::AccessControl gPermissiveAccessControl;
namespace chip {
namespace Test {

CHIP_ERROR AppContext::SetUpTestSuite()
void AppContext::SetUpTestSuite()
{
CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit((err = LoopbackMessagingContext::SetUpTestSuite()) == CHIP_NO_ERROR,
ChipLogError(AppServer, "SetUpTestSuite lo messaging context failed: %" CHIP_ERROR_FORMAT, err.Format()));
VerifyOrExit((err = chip::DeviceLayer::PlatformMgr().InitChipStack()) == CHIP_NO_ERROR,
ChipLogError(AppServer, "Init CHIP stack failed: %" CHIP_ERROR_FORMAT, err.Format()));
exit:
return err;
LoopbackMessagingContext::SetUpTestSuite();
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = chip::DeviceLayer::PlatformMgr().InitChipStack()) == CHIP_NO_ERROR, AppServer,
"Init CHIP stack failed: %" CHIP_ERROR_FORMAT, err.Format());
}

void AppContext::TearDownTestSuite()
Expand All @@ -55,20 +53,19 @@ void AppContext::TearDownTestSuite()
LoopbackMessagingContext::TearDownTestSuite();
}

CHIP_ERROR AppContext::SetUp()
void AppContext::SetUp()
{
CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit((err = LoopbackMessagingContext::SetUp()) == CHIP_NO_ERROR,
ChipLogError(AppServer, "SetUp lo messaging context failed: %" CHIP_ERROR_FORMAT, err.Format()));
VerifyOrExit((err = app::InteractionModelEngine::GetInstance()->Init(
&GetExchangeManager(), &GetFabricTable(), app::reporting::GetDefaultReportScheduler())) == CHIP_NO_ERROR,
ChipLogError(AppServer, "Init InteractionModelEngine failed: %" CHIP_ERROR_FORMAT, err.Format()));
LoopbackMessagingContext::SetUp();
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), &GetFabricTable(),
app::reporting::GetDefaultReportScheduler())) ==
CHIP_NO_ERROR,
AppServer, "Init InteractionModelEngine failed: %" CHIP_ERROR_FORMAT, err.Format());
Access::SetAccessControl(gPermissiveAccessControl);
VerifyOrExit((err = Access::GetAccessControl().Init(chip::Access::Examples::GetPermissiveAccessControlDelegate(),
gDeviceTypeResolver)) == CHIP_NO_ERROR,
ChipLogError(AppServer, "Init AccessControl failed: %" CHIP_ERROR_FORMAT, err.Format()));
exit:
return err;
VerifyOrDieWithMsg((err = Access::GetAccessControl().Init(chip::Access::Examples::GetPermissiveAccessControlDelegate(),
gDeviceTypeResolver)) == CHIP_NO_ERROR,
AppServer, "Init AccessControl failed: %" CHIP_ERROR_FORMAT, err.Format());
}

void AppContext::TearDown()
Expand Down
4 changes: 2 additions & 2 deletions src/app/tests/AppTestContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class AppContext : public LoopbackMessagingContext
{
public:
// Performs shared setup for all tests in the test suite
CHIP_ERROR SetUpTestSuite() override;
void SetUpTestSuite() override;
// Performs shared teardown for all tests in the test suite
void TearDownTestSuite() override;
// Performs setup for each individual test in the test suite
CHIP_ERROR SetUp() override;
void SetUp() override;
// Performs teardown for each individual test in the test suite
void TearDown() override;
};
Expand Down
5 changes: 2 additions & 3 deletions src/app/tests/TestAclAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ class TestAccessContext : public chip::Test::AppContext
{
public:
// Performs setup for each individual test in the test suite
CHIP_ERROR SetUp() override
void SetUp() override
{
ReturnErrorOnFailure(chip::Test::AppContext::SetUp());
chip::Test::AppContext::SetUp();
Access::GetAccessControl().Finish();
Access::GetAccessControl().Init(GetTestAccessControlDelegate(), gDeviceTypeResolver);
return CHIP_NO_ERROR;
}
};

Expand Down
12 changes: 5 additions & 7 deletions src/app/tests/TestAclEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,25 @@ class TestContext : public chip::Test::AppContext
{
public:
// Performs setup for each individual test in the test suite
CHIP_ERROR SetUp() override
void SetUp() override
{
const chip::app::LogStorageResources logStorageResources[] = {
{ &gDebugEventBuffer[0], sizeof(gDebugEventBuffer), chip::app::PriorityLevel::Debug },
{ &gInfoEventBuffer[0], sizeof(gInfoEventBuffer), chip::app::PriorityLevel::Info },
{ &gCritEventBuffer[0], sizeof(gCritEventBuffer), chip::app::PriorityLevel::Critical },
};

ReturnErrorOnFailure(chip::Test::AppContext::SetUp());
chip::Test::AppContext::SetUp();

CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit((err = mEventCounter.Init(0)) == CHIP_NO_ERROR,
ChipLogError(AppServer, "Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format()));
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = mEventCounter.Init(0)) == CHIP_NO_ERROR, AppServer,
"Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format());
chip::app::EventManagement::CreateEventManagement(&GetExchangeManager(), ArraySize(logStorageResources),
gCircularEventBuffer, logStorageResources, &mEventCounter);

Access::GetAccessControl().Finish();
Access::GetAccessControl().Init(GetTestAccessControlDelegate(), gDeviceTypeResolver);

exit:
return err;
}

// Performs teardown for each individual test in the test suite
Expand Down
12 changes: 5 additions & 7 deletions src/app/tests/TestEventLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,22 @@ class TestContext : public chip::Test::AppContext
{
public:
// Performs setup for each individual test in the test suite
CHIP_ERROR SetUp() override
void SetUp() override
{
const chip::app::LogStorageResources logStorageResources[] = {
{ &gDebugEventBuffer[0], sizeof(gDebugEventBuffer), chip::app::PriorityLevel::Debug },
{ &gInfoEventBuffer[0], sizeof(gInfoEventBuffer), chip::app::PriorityLevel::Info },
{ &gCritEventBuffer[0], sizeof(gCritEventBuffer), chip::app::PriorityLevel::Critical },
};

ReturnErrorOnFailure(chip::Test::AppContext::SetUp());
chip::Test::AppContext::SetUp();

CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit((err = mEventCounter.Init(0)) == CHIP_NO_ERROR,
ChipLogError(AppServer, "Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format()));
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = mEventCounter.Init(0)) == CHIP_NO_ERROR, AppServer,
"Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format());
chip::app::EventManagement::CreateEventManagement(&GetExchangeManager(), ArraySize(logStorageResources),
gCircularEventBuffer, logStorageResources, &mEventCounter);

exit:
return err;
}

// Performs teardown for each individual test in the test suite
Expand Down
17 changes: 7 additions & 10 deletions src/app/tests/TestEventLoggingNoUTCTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ class TestContext : public chip::Test::AppContext
{
public:
// Performs shared setup for all tests in the test suite
CHIP_ERROR SetUpTestSuite() override
void SetUpTestSuite() override
{
ReturnErrorOnFailure(chip::Test::AppContext::SetUpTestSuite());
chip::Test::AppContext::SetUpTestSuite();
mClock.Emplace(chip::System::SystemClock());
return CHIP_NO_ERROR;
}

// Performs shared teardown for all tests in the test suite
Expand All @@ -99,24 +98,22 @@ class TestContext : public chip::Test::AppContext
}

// Performs setup for each individual test in the test suite
CHIP_ERROR SetUp() override
void SetUp() override
{
const chip::app::LogStorageResources logStorageResources[] = {
{ &gDebugEventBuffer[0], sizeof(gDebugEventBuffer), chip::app::PriorityLevel::Debug },
{ &gInfoEventBuffer[0], sizeof(gInfoEventBuffer), chip::app::PriorityLevel::Info },
{ &gCritEventBuffer[0], sizeof(gCritEventBuffer), chip::app::PriorityLevel::Critical },
};

ReturnErrorOnFailure(chip::Test::AppContext::SetUp());
chip::Test::AppContext::SetUp();

CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit((err = mEventCounter.Init(0)) == CHIP_NO_ERROR,
ChipLogError(AppServer, "Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format()));
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = mEventCounter.Init(0)) == CHIP_NO_ERROR, AppServer,
"Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format());
chip::app::EventManagement::CreateEventManagement(&GetExchangeManager(), ArraySize(logStorageResources),
gCircularEventBuffer, logStorageResources, &mEventCounter);

exit:
return err;
}

// Performs teardown for each individual test in the test suite
Expand Down
12 changes: 5 additions & 7 deletions src/app/tests/TestEventOverflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,22 @@ class TestContext : public chip::Test::AppContext
{
public:
// Performs setup for each individual test in the test suite
CHIP_ERROR SetUp() override
void SetUp() override
{
const chip::app::LogStorageResources logStorageResources[] = {
{ &gDebugEventBuffer[0], sizeof(gDebugEventBuffer), chip::app::PriorityLevel::Debug },
{ &gInfoEventBuffer[0], sizeof(gInfoEventBuffer), chip::app::PriorityLevel::Info },
{ &gCritEventBuffer[0], sizeof(gCritEventBuffer), chip::app::PriorityLevel::Critical },
};

ReturnErrorOnFailure(chip::Test::AppContext::SetUp());
chip::Test::AppContext::SetUp();

CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit((err = mEventCounter.Init(0)) == CHIP_NO_ERROR,
ChipLogError(AppServer, "Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format()));
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = mEventCounter.Init(0)) == CHIP_NO_ERROR, AppServer,
"Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format());
chip::app::EventManagement::CreateEventManagement(&GetExchangeManager(), ArraySize(logStorageResources),
gCircularEventBuffer, logStorageResources, &mEventCounter);

exit:
return err;
}

// Performs teardown for each individual test in the test suite
Expand Down
12 changes: 5 additions & 7 deletions src/app/tests/TestFabricScopedEventLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,22 @@ class TestContext : public chip::Test::AppContext
{
public:
// Performs setup for each individual test in the test suite
CHIP_ERROR SetUp() override
void SetUp() override
{
const chip::app::LogStorageResources logStorageResources[] = {
{ &gDebugEventBuffer[0], sizeof(gDebugEventBuffer), chip::app::PriorityLevel::Debug },
{ &gInfoEventBuffer[0], sizeof(gInfoEventBuffer), chip::app::PriorityLevel::Info },
{ &gCritEventBuffer[0], sizeof(gCritEventBuffer), chip::app::PriorityLevel::Critical },
};

ReturnErrorOnFailure(chip::Test::AppContext::SetUp());
chip::Test::AppContext::SetUp();

CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit((err = mEventCounter.Init(0)) == CHIP_NO_ERROR,
ChipLogError(AppServer, "Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format()));
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = mEventCounter.Init(0)) == CHIP_NO_ERROR, AppServer,
"Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format());
chip::app::EventManagement::CreateEventManagement(&GetExchangeManager(), ArraySize(logStorageResources),
gCircularEventBuffer, logStorageResources, &mEventCounter);

exit:
return err;
}

// Performs teardown for each individual test in the test suite
Expand Down
18 changes: 6 additions & 12 deletions src/app/tests/TestReadInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class TestContext : public chip::Test::AppContext
{
public:
// Performs shared setup for all tests in the test suite
CHIP_ERROR SetUpTestSuite() override
void SetUpTestSuite() override
{
ReturnErrorOnFailure(chip::Test::AppContext::SetUpTestSuite());
chip::Test::AppContext::SetUpTestSuite();
gRealClock = &chip::System::SystemClock();
chip::System::Clock::Internal::SetSystemClockForTesting(&gMockClock);

Expand All @@ -94,8 +94,6 @@ class TestContext : public chip::Test::AppContext
{
gReportScheduler = chip::app::reporting::GetDefaultReportScheduler();
}

return CHIP_NO_ERROR;
}

static int nlTestSetUpTestSuite_Sync(void * context)
Expand All @@ -112,24 +110,20 @@ class TestContext : public chip::Test::AppContext
}

// Performs setup for each individual test in the test suite
CHIP_ERROR SetUp() override
void SetUp() override
{
const chip::app::LogStorageResources logStorageResources[] = {
{ &gDebugEventBuffer[0], sizeof(gDebugEventBuffer), chip::app::PriorityLevel::Debug },
{ &gInfoEventBuffer[0], sizeof(gInfoEventBuffer), chip::app::PriorityLevel::Info },
{ &gCritEventBuffer[0], sizeof(gCritEventBuffer), chip::app::PriorityLevel::Critical },
};

ReturnErrorOnFailure(chip::Test::AppContext::SetUp());
chip::Test::AppContext::SetUp();

CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit((err = mEventCounter.Init(0)) == CHIP_NO_ERROR,
ChipLogError(AppServer, "Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format()));
// TODO: change to ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDie(mEventCounter.Init(0) == CHIP_NO_ERROR);
chip::app::EventManagement::CreateEventManagement(&GetExchangeManager(), ArraySize(logStorageResources),
gCircularEventBuffer, logStorageResources, &mEventCounter);

exit:
return err;
}

// Performs teardown for each individual test in the test suite
Expand Down
13 changes: 6 additions & 7 deletions src/app/tests/TestWriteInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,21 @@ class TestContext : public chip::Test::AppContext
{
public:
// Performs setup for each individual test in the test suite
CHIP_ERROR SetUp() override
void SetUp() override
{
ReturnErrorOnFailure(chip::Test::AppContext::SetUp());
chip::Test::AppContext::SetUp();

gTestStorage.ClearStorage();
gGroupsProvider.SetStorageDelegate(&gTestStorage);
gGroupsProvider.SetSessionKeystore(&gSessionKeystore);
ReturnErrorOnFailure(gGroupsProvider.Init());
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDie(gGroupsProvider.Init() == CHIP_NO_ERROR);
chip::Credentials::SetGroupDataProvider(&gGroupsProvider);

uint8_t buf[sizeof(chip::CompressedFabricId)];
chip::MutableByteSpan span(buf);
ReturnErrorOnFailure(GetBobFabric()->GetCompressedFabricIdBytes(span));
ReturnErrorOnFailure(chip::GroupTesting::InitData(&gGroupsProvider, GetBobFabricIndex(), span));

return CHIP_NO_ERROR;
VerifyOrDie(GetBobFabric()->GetCompressedFabricIdBytes(span) == CHIP_NO_ERROR);
VerifyOrDie(chip::GroupTesting::InitData(&gGroupsProvider, GetBobFabricIndex(), span) == CHIP_NO_ERROR);
}

// Performs teardown for each individual test in the test suite
Expand Down
12 changes: 5 additions & 7 deletions src/controller/tests/TestEventCaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,22 @@ class TestContext : public chip::Test::AppContext
{
public:
// Performs setup for each individual test in the test suite
CHIP_ERROR SetUp() override
void SetUp() override
{
const chip::app::LogStorageResources logStorageResources[] = {
{ &gDebugEventBuffer[0], sizeof(gDebugEventBuffer), chip::app::PriorityLevel::Debug },
{ &gInfoEventBuffer[0], sizeof(gInfoEventBuffer), chip::app::PriorityLevel::Info },
{ &gCritEventBuffer[0], sizeof(gCritEventBuffer), chip::app::PriorityLevel::Critical },
};

ReturnErrorOnFailure(chip::Test::AppContext::SetUp());
chip::Test::AppContext::SetUp();

CHIP_ERROR err = CHIP_NO_ERROR;
VerifyOrExit((err = mEventCounter.Init(0)) == CHIP_NO_ERROR,
ChipLogError(AppServer, "Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format()));
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
VerifyOrDieWithMsg((err = mEventCounter.Init(0)) == CHIP_NO_ERROR, AppServer,
"Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format());
chip::app::EventManagement::CreateEventManagement(&GetExchangeManager(), ArraySize(logStorageResources),
gCircularEventBuffer, logStorageResources, &mEventCounter);

exit:
return err;
}

// Performs teardown for each individual test in the test suite
Expand Down
Loading

0 comments on commit 3c5b92c

Please sign in to comment.