Skip to content

Commit

Permalink
Refactored transport/raw/tests to more cleanly handle access to priva…
Browse files Browse the repository at this point in the history
…te members of TCPBase (#33306)

* 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.

* Clang formatting.

* Added TCPBaseTestAccess.h to BUILD.gn

* Minor change to fix failing test

* Reordered the includes to match the standard "Related header, C system headers, C++ standard library headers, other libraries' headers, your project's headers"
  • Loading branch information
feasel0 authored May 6, 2024
1 parent bf73003 commit 16e220b
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 305 deletions.
5 changes: 0 additions & 5 deletions src/inet/TCPEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@

namespace chip {

namespace Transport {
class TCPTest;
};

namespace Inet {

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

protected:
friend class ::chip::Transport::TCPTest;
friend class TCPTest;

TCPEndPoint(EndPointManager<TCPEndPoint> & endPointManager) :
Expand Down
9 changes: 7 additions & 2 deletions src/transport/raw/TCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
namespace chip {
namespace Transport {

// Forward declaration of friend class for test access.
template <size_t kActiveConnectionsSize, size_t kPendingPacketSize>
class TCPBaseTestAccess;

/** Defines listening parameters for setting up a TCP transport */
class TcpListenParameters
{
Expand Down Expand Up @@ -200,7 +204,9 @@ class DLL_EXPORT TCPBase : public Base
void CloseActiveConnections();

private:
friend class TCPTest;
// Allow tests to access private members.
template <size_t kActiveConnectionsSize, size_t kPendingPacketSize>
friend class TCPBaseTestAccess;

/**
* Allocate an unused connection from the pool
Expand Down Expand Up @@ -330,7 +336,6 @@ class TCP : public TCPBase
~TCP() override { mPendingPackets.ReleaseAll(); }

private:
friend class TCPTest;
ActiveTCPConnectionState mConnectionsBuffer[kActiveConnectionsSize];
PoolImpl<PendingPacket, kPendingPacketSize, ObjectPoolMem::kInline, PendingPacketPoolType::Interface> mPendingPackets;
};
Expand Down
2 changes: 2 additions & 0 deletions src/transport/raw/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ chip_test_suite("tests") {
test_sources += [ "TestTCP.cpp" ]
}

sources = [ "TCPBaseTestAccess.h" ]

public_deps = [
":helpers",
"${chip_root}/src/inet/tests:helpers",
Expand Down
49 changes: 49 additions & 0 deletions src/transport/raw/tests/TCPBaseTestAccess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
#include <transport/raw/TCP.h>
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

namespace chip {
namespace Transport {
/**
* @brief Class acts as an accessor to private members of the TCPBase class without needing to give
* friend access to each individual test.
*/
template <size_t kActiveConnectionsSize, size_t kPendingPacketSize>
class TCPBaseTestAccess
{
public:
using TCPImpl = Transport::TCP<kActiveConnectionsSize, kPendingPacketSize>;

static void * FindActiveConnection(TCPImpl & tcp, Transport::PeerAddress & peerAddress)
{
return tcp.FindActiveConnection(peerAddress);
}
static Inet::TCPEndPoint * GetEndpoint(void * state) { return static_cast<ActiveTCPConnectionState *>(state)->mEndPoint; }

static CHIP_ERROR ProcessReceivedBuffer(TCPImpl & tcp, Inet::TCPEndPoint * endPoint, const PeerAddress & peerAddress,
System::PacketBufferHandle && buffer)
{
return tcp.ProcessReceivedBuffer(endPoint, peerAddress, std::move(buffer));
}
};
} // namespace Transport
} // namespace chip
4 changes: 2 additions & 2 deletions src/transport/raw/tests/TestMessageHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
*
*/

#include <gtest/gtest.h>

#include <lib/core/ErrorStr.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/CodeUtils.h>
#include <protocols/Protocols.h>
#include <transport/raw/MessageHeader.h>

#include <gtest/gtest.h>

namespace {

using namespace chip;
Expand Down
4 changes: 2 additions & 2 deletions src/transport/raw/tests/TestPeerAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
#include <stdint.h>
#include <string.h>

#include <gtest/gtest.h>

#include <inet/IPAddress.h>
#include <lib/core/DataModelTypes.h>
#include <lib/core/PeerId.h>
#include <transport/raw/PeerAddress.h>

#include <gtest/gtest.h>

namespace {

using namespace chip;
Expand Down
Loading

0 comments on commit 16e220b

Please sign in to comment.