Skip to content

Commit

Permalink
Fix test code build issues when INET_CONFIG_TCP_ENDPOINT is set to false
Browse files Browse the repository at this point in the history
  • Loading branch information
pidarped committed Aug 15, 2024
1 parent 6cdd274 commit b4d2da5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 22 deletions.
12 changes: 11 additions & 1 deletion src/inet/tests/TestInetEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ TEST_F(TestInetEndPoint, TestInetEndPointInternal)

// EndPoint
UDPEndPoint * testUDPEP = nullptr;
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
TCPEndPoint * testTCPEP1 = nullptr;
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
PacketBufferHandle buf = PacketBufferHandle::New(PacketBuffer::kMaxSize);

// init all the EndPoints
Expand All @@ -266,10 +268,12 @@ TEST_F(TestInetEndPoint, TestInetEndPointInternal)
ASSERT_EQ(err, CHIP_NO_ERROR);
EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(System::Stats::kInetLayer_NumUDPEps, 1));

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
SYSTEM_STATS_RESET(System::Stats::kInetLayer_NumTCPEps);
err = gTCP.NewEndPoint(&testTCPEP1);
ASSERT_EQ(err, CHIP_NO_ERROR);
EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(System::Stats::kInetLayer_NumUDPEps, 1));
EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(System::Stats::kInetLayer_NumTCPEps, 1));
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

err = InterfaceId::Null().GetLinkLocalAddr(&addr);

Expand Down Expand Up @@ -323,6 +327,7 @@ TEST_F(TestInetEndPoint, TestInetEndPointInternal)
testUDPEP->Free();
EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(System::Stats::kInetLayer_NumUDPEps, 0));

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
// TcpEndPoint special cases to cover the error branch
err = testTCPEP1->GetPeerInfo(nullptr, nullptr);
EXPECT_EQ(err, CHIP_ERROR_INCORRECT_STATE);
Expand Down Expand Up @@ -362,14 +367,17 @@ TEST_F(TestInetEndPoint, TestInetEndPointInternal)
testTCPEP1->Free();
EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(System::Stats::kInetLayer_NumTCPEps, 0));
EXPECT_TRUE(SYSTEM_STATS_TEST_HIGH_WATER_MARK(System::Stats::kInetLayer_NumTCPEps, 1));
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
}

#if !CHIP_SYSTEM_CONFIG_POOL_USE_HEAP
// Test the Inet resource limitations.
TEST_F(TestInetEndPoint, TestInetEndPointLimit)
{
UDPEndPoint * testUDPEP[INET_CONFIG_NUM_UDP_ENDPOINTS + 1] = { nullptr };
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
TCPEndPoint * testTCPEP[INET_CONFIG_NUM_TCP_ENDPOINTS + 1] = { nullptr };
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

CHIP_ERROR err = CHIP_NO_ERROR;

Expand All @@ -388,6 +396,7 @@ TEST_F(TestInetEndPoint, TestInetEndPointLimit)
const int udpHighWaterMark = udpCount;
EXPECT_TRUE(SYSTEM_STATS_TEST_HIGH_WATER_MARK(System::Stats::kInetLayer_NumUDPEps, udpHighWaterMark));

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
int tcpCount = 0;
SYSTEM_STATS_RESET(System::Stats::kInetLayer_NumTCPEps);
for (int i = INET_CONFIG_NUM_TCP_ENDPOINTS; i >= 0; --i)
Expand All @@ -402,6 +411,7 @@ TEST_F(TestInetEndPoint, TestInetEndPointLimit)
}
const int tcpHighWaterMark = tcpCount;
EXPECT_TRUE(SYSTEM_STATS_TEST_HIGH_WATER_MARK(System::Stats::kInetLayer_NumTCPEps, tcpHighWaterMark));
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

#if CHIP_SYSTEM_CONFIG_NUM_TIMERS
// Verify same aComplete and aAppState args do not exhaust timer pool
Expand Down
19 changes: 18 additions & 1 deletion src/inet/tests/inet-layer-test-tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ static const uint32_t kExpectedTxSizeDefault = kExpectedRxSizeDefault;

static const uint32_t kOptFlagsDefault = (kOptFlagUseIPv6 | kOptFlagUseUDPIP);

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
static TCPEndPoint * sTCPIPEndPoint = nullptr; // Used for connect/send/receive
static TCPEndPoint * sTCPIPListenEndPoint = nullptr; // Used for accept/listen
static const uint16_t kTCPPort = kUDPPort;
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

static UDPEndPoint * sUDPIPEndPoint = nullptr;

static const uint16_t kTCPPort = kUDPPort;
// clang-format off
static TestState sTestState =
{
Expand Down Expand Up @@ -184,6 +187,7 @@ static OptionSet * sToolOptionSets[] =
namespace chip {
namespace Inet {

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
class TCPTest
{
public:
Expand All @@ -193,6 +197,7 @@ class TCPTest
return endPoint->mState == TCPEndPoint::State::kConnected || endPoint->mState == TCPEndPoint::State::kReceiveShutdown;
}
};
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

} // namespace Inet
} // namespace chip
Expand Down Expand Up @@ -502,6 +507,7 @@ static bool HandleDataReceived(const PacketBufferHandle & aBuffer, bool aCheckBu

// TCP Endpoint Callbacks

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
void HandleTCPConnectionComplete(TCPEndPoint * aEndPoint, CHIP_ERROR aError)
{
CHIP_ERROR lStatus;
Expand Down Expand Up @@ -636,6 +642,7 @@ static void HandleTCPConnectionReceived(TCPEndPoint * aListenEndPoint, TCPEndPoi

sTCPIPEndPoint = aConnectEndPoint;
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

// UDP Endpoint Callbacks

Expand Down Expand Up @@ -673,11 +680,13 @@ static bool IsTransportReadyForSend()
return (sUDPIPEndPoint != nullptr);
}

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
if ((gOptFlags & kOptFlagUseTCPIP) == kOptFlagUseTCPIP)
{
return (sTCPIPEndPoint != nullptr) && (sTCPIPEndPoint->PendingSendLength() == 0) &&
TCPTest::StateIsConnectedOrReceiveShutdown(sTCPIPEndPoint);
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

return false;
}
Expand All @@ -686,6 +695,7 @@ static CHIP_ERROR PrepareTransportForSend()
{
CHIP_ERROR lStatus = CHIP_NO_ERROR;

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
if (gOptFlags & kOptFlagUseTCPIP)
{
if (sTCPIPEndPoint == nullptr)
Expand All @@ -702,6 +712,7 @@ static CHIP_ERROR PrepareTransportForSend()
INET_FAIL_ERROR(lStatus, "TCPEndPoint::Connect failed");
}
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

return (lStatus);
}
Expand All @@ -722,6 +733,7 @@ static CHIP_ERROR DriveSendForDestination(const IPAddress & aAddress, uint16_t a

ReturnErrorOnFailure(sUDPIPEndPoint->SendTo(aAddress, kUDPPort, std::move(lBuffer)));
}
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
else if ((gOptFlags & kOptFlagUseTCPIP) == kOptFlagUseTCPIP)
{
const uint32_t lFirstValue = sTestState.mStats.mTransmit.mActual;
Expand All @@ -737,6 +749,7 @@ static CHIP_ERROR DriveSendForDestination(const IPAddress & aAddress, uint16_t a

ReturnErrorOnFailure(sTCPIPEndPoint->Send(std::move(lBuffer)));
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -838,6 +851,7 @@ static void StartTest()
lStatus = sUDPIPEndPoint->Listen(HandleUDPMessageReceived, HandleUDPReceiveError);
INET_FAIL_ERROR(lStatus, "UDPEndPoint::Listen failed");
}
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
else if (gOptFlags & kOptFlagUseTCPIP)
{
const uint16_t lConnectionBacklogMax = 1;
Expand All @@ -855,6 +869,7 @@ static void StartTest()
lStatus = sTCPIPListenEndPoint->Listen(lConnectionBacklogMax);
INET_FAIL_ERROR(lStatus, "TCPEndPoint::Listen failed");
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
}

if (Common::IsReceiver())
Expand All @@ -870,6 +885,7 @@ static void CleanupTest()

// Release the resources associated with the allocated end points.

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
if (sTCPIPEndPoint != nullptr)
{
sTCPIPEndPoint->Close();
Expand All @@ -881,6 +897,7 @@ static void CleanupTest()
sTCPIPListenEndPoint->Shutdown();
sTCPIPListenEndPoint->Free();
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

if (sUDPIPEndPoint != nullptr)
{
Expand Down
41 changes: 27 additions & 14 deletions src/messaging/tests/echo/echo_requester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,27 @@ constexpr chip::FabricIndex gFabricIndex = 0;
chip::Protocols::Echo::EchoClient gEchoClient;

chip::TransportMgr<chip::Transport::UDP> gUDPManager;
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
chip::TransportMgr<chip::Transport::TCP<kMaxTcpActiveConnectionCount, kMaxTcpPendingPackets>> gTCPManager;
chip::Inet::IPAddress gDestAddr;
chip::SessionHolder gSession;

chip::Transport::AppTCPConnectionCallbackCtxt gAppTCPConnCbCtxt;
chip::Transport::ActiveTCPConnectionState * gActiveTCPConnState = nullptr;

// The last time a CHIP Echo was attempted to be sent.
chip::System::Clock::Timestamp gLastEchoTime = chip::System::Clock::kZero;
static void HandleConnectionAttemptComplete(chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
static void HandleConnectionClosed(chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr);

// True, if client is still connecting to the server, false otherwise.
static bool gClientConInProgress = false;

// True, once client connection to server is established.
static bool gClientConEstablished = false;
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

chip::Inet::IPAddress gDestAddr;
chip::SessionHolder gSession;

// The last time a CHIP Echo was attempted to be sent.
chip::System::Clock::Timestamp gLastEchoTime = chip::System::Clock::kZero;

// The handle to the TCP connection to the peer.
// static chip::Transport::ActiveTCPConnectionState * gCon = nullptr;
Expand All @@ -95,9 +101,6 @@ uint64_t gTCPConnAttemptCount = 0;
CHIP_ERROR SendEchoRequest();
void EchoTimerHandler(chip::System::Layer * systemLayer, void * appState);

static void HandleConnectionAttemptComplete(chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
static void HandleConnectionClosed(chip::Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr);

void Shutdown()
{
chip::DeviceLayer::SystemLayer().CancelTimer(EchoTimerHandler, nullptr);
Expand Down Expand Up @@ -170,7 +173,7 @@ CHIP_ERROR SendEchoRequest()
return err;
}

CHIP_ERROR EstablishSecureSession(chip::Transport::ActiveTCPConnectionState * conn = nullptr)
CHIP_ERROR EstablishSecureSession()
{
char peerAddrBuf[chip::Transport::PeerAddress::kMaxToStringSize];
chip::Transport::PeerAddress peerAddr;
Expand All @@ -195,18 +198,21 @@ CHIP_ERROR EstablishSecureSession(chip::Transport::ActiveTCPConnectionState * co
}
else
{
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
if (gUseTCP)
{
printf("Associating secure session with connection %p\n", conn);
gSession.Get().Value()->AsSecureSession()->SetTCPConnection(conn);
printf("Associating secure session with connection %p\n", gActiveTCPConnState);
gSession.Get().Value()->AsSecureSession()->SetTCPConnection(gActiveTCPConnState);
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

printf("Successfully established secure session with peer at %s\n", peerAddrBuf);
}

return err;
}

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
void CloseConnection()
{
char peerAddrBuf[chip::Transport::PeerAddress::kMaxToStringSize];
Expand All @@ -225,7 +231,7 @@ void HandleConnectionAttemptComplete(chip::Transport::ActiveTCPConnectionState *
{
chip::DeviceLayer::PlatformMgr().StopEventLoopTask();

if (err != CHIP_NO_ERROR)
if (err != CHIP_NO_ERROR || conn != gActiveTCPConnState)
{
printf("Connection FAILED with err: %s\n", chip::ErrorStr(err));

Expand All @@ -235,7 +241,7 @@ void HandleConnectionAttemptComplete(chip::Transport::ActiveTCPConnectionState *
return;
}

err = EstablishSecureSession(conn);
err = EstablishSecureSession();
if (err != CHIP_NO_ERROR)
{
printf("Secure session FAILED with err: %s\n", chip::ErrorStr(err));
Expand Down Expand Up @@ -281,6 +287,7 @@ void EstablishTCPConnection()

gClientConInProgress = true;
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

void HandleEchoResponseReceived(chip::Messaging::ExchangeContext * ec, chip::System::PacketBufferHandle && payload)
{
Expand Down Expand Up @@ -332,6 +339,7 @@ int main(int argc, char * argv[])

InitializeChip();

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
if (gUseTCP)
{
err = gTCPManager.Init(chip::Transport::TcpListenParameters(chip::DeviceLayer::TCPEndPointManager())
Expand All @@ -348,6 +356,7 @@ int main(int argc, char * argv[])
gAppTCPConnCbCtxt.connClosedCb = HandleConnectionClosed;
}
else
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
{
err = gUDPManager.Init(chip::Transport::UdpListenParameters(chip::DeviceLayer::UDPEndPointManager())
.SetAddressType(chip::Inet::IPAddressType::kIPv6)
Expand All @@ -365,13 +374,14 @@ int main(int argc, char * argv[])
err = gMessageCounterManager.Init(&gExchangeManager);
SuccessOrExit(err);

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
if (gUseTCP)
{

while (!gClientConEstablished)
{
// For TCP transport, attempt to establish the connection to the CHIP echo responder.
// On Connection completion, call EstablishSecureSession(conn);
// On Connection completion, call EstablishSecureSession();
EstablishTCPConnection();

chip::DeviceLayer::PlatformMgr().RunEventLoop();
Expand All @@ -383,9 +393,10 @@ int main(int argc, char * argv[])
}
}
else
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
{
// Start the CHIP session to the CHIP echo responder.
err = EstablishSecureSession(nullptr);
err = EstablishSecureSession();
SuccessOrExit(err);
}

Expand All @@ -402,11 +413,13 @@ int main(int argc, char * argv[])

gUDPManager.Close();

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
if (gUseTCP)
{
gTCPManager.TCPDisconnect(chip::Transport::PeerAddress::TCP(gDestAddr));
}
gTCPManager.Close();
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

Shutdown();

Expand Down
4 changes: 4 additions & 0 deletions src/messaging/tests/echo/echo_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ namespace {
// The EchoServer object.
chip::Protocols::Echo::EchoServer gEchoServer;
chip::TransportMgr<chip::Transport::UDP> gUDPManager;
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
chip::TransportMgr<chip::Transport::TCP<kMaxTcpActiveConnectionCount, kMaxTcpPendingPackets>> gTCPManager;
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
chip::SessionHolder gSession;

// Callback handler when a CHIP EchoRequest is received.
Expand Down Expand Up @@ -85,13 +87,15 @@ int main(int argc, char * argv[])

if (useTCP)
{
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
err = gTCPManager.Init(chip::Transport::TcpListenParameters(chip::DeviceLayer::TCPEndPointManager())
.SetAddressType(chip::Inet::IPAddressType::kIPv6));
SuccessOrExit(err);

err = gSessionManager.Init(&chip::DeviceLayer::SystemLayer(), &gTCPManager, &gMessageCounterManager, &gStorage,
&gFabricTable, gSessionKeystore);
SuccessOrExit(err);
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
}
else
{
Expand Down
Loading

0 comments on commit b4d2da5

Please sign in to comment.