Skip to content

Commit

Permalink
Move TestContext to a scoped variable (project-chip#18486)
Browse files Browse the repository at this point in the history
* Move TestContext to scoped

* Resolve comment: rename gLoopback -> loopback
  • Loading branch information
kghost authored May 17, 2022
1 parent 37adcd4 commit d79a0e3
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 197 deletions.
3 changes: 1 addition & 2 deletions src/app/tests/TestReadInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ class TestContext : public chip::Test::AppContext
chip::MonotonicallyIncreasingCounter<chip::EventNumber> mEventCounter;
};

TestContext sContext;

class TestEventGenerator : public chip::app::EventLoggingDelegate
{
public:
Expand Down Expand Up @@ -2622,6 +2620,7 @@ nlTestSuite sSuite =

int TestReadInteraction()
{
TestContext sContext;
nlTestRunner(&sSuite, &sContext);
return (nlTestRunnerStats(&sSuite));
}
Expand Down
4 changes: 2 additions & 2 deletions src/messaging/tests/TestExchangeMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ enum : uint8_t
kMsgType_TEST2 = 2,
};

TestContext sContext;

class MockAppDelegate : public UnsolicitedMessageHandler, public ExchangeDelegate
{
public:
Expand Down Expand Up @@ -262,6 +260,8 @@ nlTestSuite sSuite =
*/
int TestExchangeMgr()
{
TestContext sContext;

// Run test suit against one context
nlTestRunner(&sSuite, &sContext);

Expand Down
305 changes: 159 additions & 146 deletions src/messaging/tests/TestReliableMessageProtocol.cpp

Large diffs are not rendered by default.

34 changes: 18 additions & 16 deletions src/protocols/secure_channel/tests/TestCASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ using namespace chip::Protocols;
using TestContext = Test::LoopbackMessagingContext;

namespace {
TestContext sContext;

auto & gLoopback = sContext.GetLoopback();

FabricTable gCommissionerFabrics;
FabricIndex gCommissionerFabricIndex;
GroupDataProviderImpl gCommissionerGroupDataProvider;
Expand Down Expand Up @@ -227,28 +223,29 @@ void CASE_SecurePairingStartTest(nlTestSuite * inSuite, void * inContext)
pairing.EstablishSession(sessionManager, fabric, Node01_01, context, nullptr, &delegate) == CHIP_NO_ERROR);
ctx.DrainAndServiceIO();

NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 1);
auto & loopback = ctx.GetLoopback();
NL_TEST_ASSERT(inSuite, loopback.mSentMessageCount == 1);

// Clear pending packet in CRMP
ReliableMessageMgr * rm = ctx.GetExchangeManager().GetReliableMessageMgr();
ReliableMessageContext * rc = context->GetReliableMessageContext();
rm->ClearRetransTable(rc);

gLoopback.mMessageSendError = CHIP_ERROR_BAD_REQUEST;
loopback.mMessageSendError = CHIP_ERROR_BAD_REQUEST;

CASESession pairing1;
pairing1.SetGroupDataProvider(&gCommissionerGroupDataProvider);

gLoopback.mSentMessageCount = 0;
gLoopback.mMessageSendError = CHIP_ERROR_BAD_REQUEST;
ExchangeContext * context1 = ctx.NewUnauthenticatedExchangeToBob(&pairing1);
loopback.mSentMessageCount = 0;
loopback.mMessageSendError = CHIP_ERROR_BAD_REQUEST;
ExchangeContext * context1 = ctx.NewUnauthenticatedExchangeToBob(&pairing1);

NL_TEST_ASSERT(inSuite,
pairing1.EstablishSession(sessionManager, fabric, Node01_01, context1, nullptr, &delegate) ==
CHIP_ERROR_BAD_REQUEST);
ctx.DrainAndServiceIO();

gLoopback.mMessageSendError = CHIP_NO_ERROR;
loopback.mMessageSendError = CHIP_NO_ERROR;
}

void CASE_SecurePairingHandshakeTestCommon(nlTestSuite * inSuite, void * inContext, SessionManager & sessionManager,
Expand All @@ -264,7 +261,8 @@ void CASE_SecurePairingHandshakeTestCommon(nlTestSuite * inSuite, void * inConte
ReliableMessageProtocolConfig nonSleepyCommissionerRmpConfig(System::Clock::Milliseconds32(5000),
System::Clock::Milliseconds32(300));

gLoopback.mSentMessageCount = 0;
auto & loopback = ctx.GetLoopback();
loopback.mSentMessageCount = 0;

NL_TEST_ASSERT(inSuite,
ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::CASE_Sigma1,
Expand All @@ -285,7 +283,7 @@ void CASE_SecurePairingHandshakeTestCommon(nlTestSuite * inSuite, void * inConte
MakeOptional(nonSleepyCommissionerRmpConfig)) == CHIP_NO_ERROR);
ctx.DrainAndServiceIO();

NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 5);
NL_TEST_ASSERT(inSuite, loopback.mSentMessageCount == 5);
NL_TEST_ASSERT(inSuite, delegateAccessory.mNumPairingComplete == 1);
NL_TEST_ASSERT(inSuite, delegateCommissioner.mNumPairingComplete == 1);
NL_TEST_ASSERT(inSuite, pairingAccessory.GetRemoteMRPConfig().mIdleRetransTimeout == System::Clock::Milliseconds32(5000));
Expand Down Expand Up @@ -317,7 +315,8 @@ void CASE_SecurePairingHandshakeServerTest(nlTestSuite * inSuite, void * inConte

TestContext & ctx = *reinterpret_cast<TestContext *>(inContext);

gLoopback.mSentMessageCount = 0;
auto & loopback = ctx.GetLoopback();
loopback.mSentMessageCount = 0;

// Use the same session manager on both CASE client and server sides to validate that both
// components may work simultaneously on a single device.
Expand All @@ -336,7 +335,7 @@ void CASE_SecurePairingHandshakeServerTest(nlTestSuite * inSuite, void * inConte
nullptr, &delegateCommissioner) == CHIP_NO_ERROR);
ctx.DrainAndServiceIO();

NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 5);
NL_TEST_ASSERT(inSuite, loopback.mSentMessageCount == 5);
NL_TEST_ASSERT(inSuite, delegateCommissioner.mNumPairingComplete == 1);

// Validate that secure session is created
Expand Down Expand Up @@ -740,11 +739,12 @@ static void CASE_SessionResumptionStorage(nlTestSuite * inSuite, void * inContex
},
};

auto & loopback = ctx.GetLoopback();
for (size_t i = 0; i < sizeof(testVectors) / sizeof(testVectors[0]); ++i)
{
auto * pairingCommissioner = chip::Platform::New<CASESession>();
pairingCommissioner->SetGroupDataProvider(&gCommissionerGroupDataProvider);
gLoopback.mSentMessageCount = 0;
loopback.mSentMessageCount = 0;
NL_TEST_ASSERT(inSuite,
gPairingServer.ListenForSessionEstablishment(&ctx.GetExchangeManager(), &ctx.GetSecureSessionManager(),
&gDeviceFabrics, &testVectors[i].responderStorage,
Expand All @@ -755,7 +755,7 @@ static void CASE_SessionResumptionStorage(nlTestSuite * inSuite, void * inContex
&testVectors[i].initiatorStorage, &delegateCommissioner);
ctx.DrainAndServiceIO();
NL_TEST_ASSERT(inSuite, establishmentReturnVal == CHIP_NO_ERROR);
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == testVectors[i].expectedSentMessageCount);
NL_TEST_ASSERT(inSuite, loopback.mSentMessageCount == testVectors[i].expectedSentMessageCount);
NL_TEST_ASSERT(inSuite, delegateCommissioner.mNumPairingComplete == i + 1);
SessionHolder & holder = delegateCommissioner.GetSessionHolder();
NL_TEST_ASSERT(inSuite, bool(holder));
Expand Down Expand Up @@ -847,6 +847,8 @@ int CASE_TestSecurePairing_Teardown(void * inContext)
*/
int TestCASESession()
{
TestContext sContext;

// Run test suit against one context
nlTestRunner(&sSuite, &sContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ using namespace chip::Messaging;
using namespace chip::Protocols;

using TestContext = chip::Test::LoopbackMessagingContext;
TestContext sContext;

const char PAYLOAD[] = "Hello!";

Expand Down Expand Up @@ -169,6 +168,8 @@ int Finalize(void * aContext)
*/
int TestMessageCounterManager()
{
TestContext sContext;

// Run test suit against one context
nlTestRunner(&sSuite, &sContext);

Expand Down
65 changes: 39 additions & 26 deletions src/protocols/secure_channel/tests/TestPASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ class PASETestLoopbackTransportDelegate : public Test::LoopbackTransportDelegate

using TestContext = chip::Test::LoopbackMessagingContext;

TestContext sContext;
auto & gLoopback = sContext.GetLoopback();

class TestSecurePairingDelegate : public SessionEstablishmentDelegate
{
public:
Expand Down Expand Up @@ -120,7 +117,8 @@ void SecurePairingWaitTest(nlTestSuite * inSuite, void * inContext)

NL_TEST_ASSERT(inSuite, pairing.GetSecureSessionType() == SecureSession::Type::kPASE);

gLoopback.Reset();
auto & loopback = ctx.GetLoopback();
loopback.Reset();

NL_TEST_ASSERT(inSuite,
pairing.WaitForPairing(sessionManager, sTestSpake2p01_PASEVerifier, sTestSpake2p01_IterationCount,
Expand Down Expand Up @@ -158,30 +156,31 @@ void SecurePairingStartTest(nlTestSuite * inSuite, void * inContext)
TestSecurePairingDelegate delegate;
PASESession pairing;

gLoopback.Reset();
auto & loopback = ctx.GetLoopback();
loopback.Reset();

ExchangeContext * context = ctx.NewUnauthenticatedExchangeToBob(&pairing);

NL_TEST_ASSERT(inSuite,
pairing.Pair(sessionManager, sTestSpake2p01_PinCode, Optional<ReliableMessageProtocolConfig>::Missing(), nullptr,
nullptr) != CHIP_NO_ERROR);

gLoopback.Reset();
loopback.Reset();
NL_TEST_ASSERT(inSuite,
pairing.Pair(sessionManager, sTestSpake2p01_PinCode, Optional<ReliableMessageProtocolConfig>::Missing(), context,
&delegate) == CHIP_NO_ERROR);
ctx.DrainAndServiceIO();

NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount == 1);
NL_TEST_ASSERT(inSuite, loopback.mSentMessageCount == 1);

// Clear pending packet in CRMP
ReliableMessageMgr * rm = ctx.GetExchangeManager().GetReliableMessageMgr();
ReliableMessageContext * rc = context->GetReliableMessageContext();
rm->ClearRetransTable(rc);

gLoopback.Reset();
gLoopback.mSentMessageCount = 0;
gLoopback.mMessageSendError = CHIP_ERROR_BAD_REQUEST;
loopback.Reset();
loopback.mSentMessageCount = 0;
loopback.mMessageSendError = CHIP_ERROR_BAD_REQUEST;

PASESession pairing1;
ExchangeContext * context1 = ctx.NewUnauthenticatedExchangeToBob(&pairing1);
Expand All @@ -190,7 +189,7 @@ void SecurePairingStartTest(nlTestSuite * inSuite, void * inContext)
context1, &delegate) == CHIP_ERROR_BAD_REQUEST);
ctx.DrainAndServiceIO();

gLoopback.mMessageSendError = CHIP_NO_ERROR;
loopback.mMessageSendError = CHIP_NO_ERROR;
}

void SecurePairingHandshakeTestCommon(nlTestSuite * inSuite, void * inContext, SessionManager & sessionManager,
Expand All @@ -205,12 +204,13 @@ void SecurePairingHandshakeTestCommon(nlTestSuite * inSuite, void * inContext, S
PASESession pairingAccessory;

PASETestLoopbackTransportDelegate delegate;
gLoopback.SetLoopbackTransportDelegate(&delegate);
gLoopback.mSentMessageCount = 0;
auto & loopback = ctx.GetLoopback();
loopback.SetLoopbackTransportDelegate(&delegate);
loopback.mSentMessageCount = 0;

ExchangeContext * contextCommissioner = ctx.NewUnauthenticatedExchangeToBob(&pairingCommissioner);

if (gLoopback.mNumMessagesToDrop != 0)
if (loopback.mNumMessagesToDrop != 0)
{
ReliableMessageMgr * rm = ctx.GetExchangeManager().GetReliableMessageMgr();
ReliableMessageContext * rc = contextCommissioner->GetReliableMessageContext();
Expand Down Expand Up @@ -250,7 +250,7 @@ void SecurePairingHandshakeTestCommon(nlTestSuite * inSuite, void * inContext, S
// via piggybacked acks. So we cannot check for a specific value of mSentMessageCount.
// Let's make sure atleast number is >= than the minimum messages required to complete the
// handshake.
NL_TEST_ASSERT(inSuite, gLoopback.mSentMessageCount >= 5);
NL_TEST_ASSERT(inSuite, loopback.mSentMessageCount >= 5);
NL_TEST_ASSERT(inSuite, delegateAccessory.mNumPairingComplete == 1);
NL_TEST_ASSERT(inSuite, delegateCommissioner.mNumPairingComplete == 1);

Expand All @@ -274,15 +274,17 @@ void SecurePairingHandshakeTestCommon(nlTestSuite * inSuite, void * inContext, S
mrpAccessoryConfig.Value().mActiveRetransTimeout);
}

gLoopback.SetLoopbackTransportDelegate(nullptr);
loopback.SetLoopbackTransportDelegate(nullptr);
}

void SecurePairingHandshakeTest(nlTestSuite * inSuite, void * inContext)
{
SessionManager sessionManager;
TestSecurePairingDelegate delegateCommissioner;
PASESession pairingCommissioner;
gLoopback.Reset();
TestContext & ctx = *reinterpret_cast<TestContext *>(inContext);
auto & loopback = ctx.GetLoopback();
loopback.Reset();
SecurePairingHandshakeTestCommon(inSuite, inContext, sessionManager, pairingCommissioner,
Optional<ReliableMessageProtocolConfig>::Missing(),
Optional<ReliableMessageProtocolConfig>::Missing(), delegateCommissioner);
Expand All @@ -293,7 +295,9 @@ void SecurePairingHandshakeWithCommissionerMRPTest(nlTestSuite * inSuite, void *
SessionManager sessionManager;
TestSecurePairingDelegate delegateCommissioner;
PASESession pairingCommissioner;
gLoopback.Reset();
TestContext & ctx = *reinterpret_cast<TestContext *>(inContext);
auto & loopback = ctx.GetLoopback();
loopback.Reset();
ReliableMessageProtocolConfig config(1000_ms32, 10000_ms32);
SecurePairingHandshakeTestCommon(inSuite, inContext, sessionManager, pairingCommissioner,
Optional<ReliableMessageProtocolConfig>::Value(config),
Expand All @@ -305,7 +309,9 @@ void SecurePairingHandshakeWithDeviceMRPTest(nlTestSuite * inSuite, void * inCon
SessionManager sessionManager;
TestSecurePairingDelegate delegateCommissioner;
PASESession pairingCommissioner;
gLoopback.Reset();
TestContext & ctx = *reinterpret_cast<TestContext *>(inContext);
auto & loopback = ctx.GetLoopback();
loopback.Reset();
ReliableMessageProtocolConfig config(1000_ms32, 10000_ms32);
SecurePairingHandshakeTestCommon(inSuite, inContext, sessionManager, pairingCommissioner,
Optional<ReliableMessageProtocolConfig>::Missing(),
Expand All @@ -317,7 +323,9 @@ void SecurePairingHandshakeWithAllMRPTest(nlTestSuite * inSuite, void * inContex
SessionManager sessionManager;
TestSecurePairingDelegate delegateCommissioner;
PASESession pairingCommissioner;
gLoopback.Reset();
TestContext & ctx = *reinterpret_cast<TestContext *>(inContext);
auto & loopback = ctx.GetLoopback();
loopback.Reset();
ReliableMessageProtocolConfig commissionerConfig(1000_ms32, 10000_ms32);
ReliableMessageProtocolConfig deviceConfig(2000_ms32, 7000_ms32);
SecurePairingHandshakeTestCommon(inSuite, inContext, sessionManager, pairingCommissioner,
Expand All @@ -330,13 +338,15 @@ void SecurePairingHandshakeWithPacketLossTest(nlTestSuite * inSuite, void * inCo
SessionManager sessionManager;
TestSecurePairingDelegate delegateCommissioner;
PASESession pairingCommissioner;
gLoopback.Reset();
gLoopback.mNumMessagesToDrop = 2;
TestContext & ctx = *reinterpret_cast<TestContext *>(inContext);
auto & loopback = ctx.GetLoopback();
loopback.Reset();
loopback.mNumMessagesToDrop = 2;
SecurePairingHandshakeTestCommon(inSuite, inContext, sessionManager, pairingCommissioner,
Optional<ReliableMessageProtocolConfig>::Missing(),
Optional<ReliableMessageProtocolConfig>::Missing(), delegateCommissioner);
NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2);
NL_TEST_ASSERT(inSuite, gLoopback.mNumMessagesToDrop == 0);
NL_TEST_ASSERT(inSuite, loopback.mDroppedMessageCount == 2);
NL_TEST_ASSERT(inSuite, loopback.mNumMessagesToDrop == 0);
}

void SecurePairingFailedHandshake(nlTestSuite * inSuite, void * inContext)
Expand All @@ -350,8 +360,9 @@ void SecurePairingFailedHandshake(nlTestSuite * inSuite, void * inContext)
TestSecurePairingDelegate delegateAccessory;
PASESession pairingAccessory;

gLoopback.Reset();
gLoopback.mSentMessageCount = 0;
auto & loopback = ctx.GetLoopback();
loopback.Reset();
loopback.mSentMessageCount = 0;

ExchangeContext * contextCommissioner = ctx.NewUnauthenticatedExchangeToBob(&pairingCommissioner);

Expand Down Expand Up @@ -471,6 +482,8 @@ int TestSecurePairing_Teardown(void * inContext)
*/
int TestPASESession()
{
TestContext sContext;

// Run test suit against one context
nlTestRunner(&sSuite, &sContext);

Expand Down
3 changes: 2 additions & 1 deletion src/transport/raw/tests/TestTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ constexpr NodeId kDestinationNodeId = 111222333;
constexpr uint32_t kMessageCounter = 18;

using TestContext = chip::Test::IOContext;
TestContext sContext;

const char PAYLOAD[] = "Hello!";

Expand Down Expand Up @@ -503,6 +502,8 @@ static int Finalize(void * aContext)

int TestTCP()
{
TestContext sContext;

// Run test suit against one context
nlTestRunner(&sSuite, &sContext);

Expand Down
3 changes: 2 additions & 1 deletion src/transport/raw/tests/TestUDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ constexpr NodeId kDestinationNodeId = 111222333;
constexpr uint32_t kMessageCounter = 18;

using TestContext = chip::Test::IOContext;
TestContext sContext;

const char PAYLOAD[] = "Hello!";
int ReceiveHandlerCallCount = 0;
Expand Down Expand Up @@ -211,6 +210,8 @@ static int Finalize(void * aContext)

int TestUDP()
{
TestContext sContext;

// Run test suit against one context
nlTestRunner(&sSuite, &sContext);

Expand Down
Loading

0 comments on commit d79a0e3

Please sign in to comment.