Skip to content

Commit c298c3c

Browse files
committed
Refactored TestTCP and TestUDP to simplify them. Pulled out the places where test needs access to private members and put those in a TestAccess class. Moved TCPBaseTestAccess into an H file so it can be used by other tests in the future.
1 parent 062e063 commit c298c3c

File tree

7 files changed

+373
-298
lines changed

7 files changed

+373
-298
lines changed

src/inet/TCPEndPoint.h

-5
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838

3939
namespace chip {
4040

41-
namespace Transport {
42-
class TCPTest;
43-
};
44-
4541
namespace Inet {
4642

4743
class TCPTest;
@@ -529,7 +525,6 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis<TCPEndPoint>
529525
constexpr static size_t kMaxReceiveMessageSize = System::PacketBuffer::kMaxSizeWithoutReserve;
530526

531527
protected:
532-
friend class ::chip::Transport::TCPTest;
533528
friend class TCPTest;
534529

535530
TCPEndPoint(EndPointManager<TCPEndPoint> & endPointManager) :

src/transport/raw/TCP.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
namespace chip {
4242
namespace Transport {
4343

44+
// Forward declaration of friend class for test access.
45+
template <size_t kActiveConnectionsSize, size_t kPendingPacketSize> class TCPBaseTestAccess;
46+
4447
/** Defines listening parameters for setting up a TCP transport */
4548
class TcpListenParameters
4649
{
@@ -200,7 +203,8 @@ class DLL_EXPORT TCPBase : public Base
200203
void CloseActiveConnections();
201204

202205
private:
203-
friend class TCPTest;
206+
// Allow tests to access private members.
207+
template <size_t kActiveConnectionsSize, size_t kPendingPacketSize> friend class TCPBaseTestAccess;
204208

205209
/**
206210
* Allocate an unused connection from the pool
@@ -330,7 +334,6 @@ class TCP : public TCPBase
330334
~TCP() override { mPendingPackets.ReleaseAll(); }
331335

332336
private:
333-
friend class TCPTest;
334337
ActiveTCPConnectionState mConnectionsBuffer[kActiveConnectionsSize];
335338
PoolImpl<PendingPacket, kPendingPacketSize, ObjectPoolMem::kInline, PendingPacketPoolType::Interface> mPendingPackets;
336339
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
20+
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
21+
#include <transport/raw/TCP.h>
22+
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
23+
24+
using namespace chip::Inet;
25+
26+
namespace chip {
27+
namespace Transport {
28+
/**
29+
* @brief Class acts as an accessor to private members of the TCPBase class without needing to give
30+
* friend access to each individual test.
31+
*/
32+
template <size_t kActiveConnectionsSize, size_t kPendingPacketSize>
33+
class TCPBaseTestAccess
34+
{
35+
public:
36+
using TCPImpl = Transport::TCP<kActiveConnectionsSize, kPendingPacketSize>;
37+
38+
static void * FindActiveConnection(TCPImpl & tcp, Transport::PeerAddress & peerAddress)
39+
{
40+
return tcp.FindActiveConnection(peerAddress);
41+
}
42+
static TCPEndPoint * GetEndpoint(void * state) { return static_cast<ActiveTCPConnectionState *>(state)->mEndPoint; }
43+
44+
static CHIP_ERROR ProcessReceivedBuffer(TCPImpl & tcp, TCPEndPoint * endPoint, const PeerAddress & peerAddress,
45+
System::PacketBufferHandle && buffer)
46+
{
47+
return tcp.ProcessReceivedBuffer(endPoint, peerAddress, std::move(buffer));
48+
}
49+
};
50+
} // namespace Transport
51+
} // namespace chip

src/transport/raw/tests/TestMessageHeader.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323
*
2424
*/
2525

26+
#include <gtest/gtest.h>
2627
#include <lib/core/ErrorStr.h>
2728
#include <lib/support/CHIPMem.h>
2829
#include <lib/support/CodeUtils.h>
2930
#include <protocols/Protocols.h>
3031
#include <transport/raw/MessageHeader.h>
3132

32-
#include <gtest/gtest.h>
33-
3433
namespace {
3534

3635
using namespace chip;

src/transport/raw/tests/TestPeerAddress.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
2121
#include <stdint.h>
2222
#include <string.h>
2323

24+
#include <gtest/gtest.h>
2425
#include <inet/IPAddress.h>
2526
#include <lib/core/DataModelTypes.h>
2627
#include <lib/core/PeerId.h>
2728
#include <transport/raw/PeerAddress.h>
2829

29-
#include <gtest/gtest.h>
30-
3130
namespace {
3231

3332
using namespace chip;

0 commit comments

Comments
 (0)