|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2025 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * @file |
| 21 | + * This file implements an E2E test for the SessionManager/Transport/UnauthenticatedSession |
| 22 | + * TCP connection. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <errno.h> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +#include <pw_unit_test/framework.h> |
| 29 | + |
| 30 | +#include <credentials/PersistentStorageOpCertStore.h> |
| 31 | +#include <crypto/DefaultSessionKeystore.h> |
| 32 | +#include <crypto/PersistentStorageOperationalKeystore.h> |
| 33 | +#include <crypto/RandUtils.h> |
| 34 | +#include <lib/core/CHIPCore.h> |
| 35 | +#include <lib/support/CHIPMem.h> |
| 36 | +#include <lib/support/CodeUtils.h> |
| 37 | +#include <lib/support/TestPersistentStorageDelegate.h> |
| 38 | +#include <lib/support/UnitTestUtils.h> |
| 39 | +#include <lib/support/tests/ExtraPwTestMacros.h> |
| 40 | +#include <platform/CHIPDeviceLayer.h> |
| 41 | +#include <protocols/secure_channel/MessageCounterManager.h> |
| 42 | +#include <system/SystemLayer.h> |
| 43 | +#include <system/SystemPacketBuffer.h> |
| 44 | +#include <transport/SessionManager.h> |
| 45 | +#include <transport/TransportMgr.h> |
| 46 | +#include <transport/raw/TCP.h> |
| 47 | +#include <transport/raw/tests/NetworkTestHelpers.h> |
| 48 | + |
| 49 | +using namespace chip; |
| 50 | +using namespace chip::Inet; |
| 51 | +using namespace chip::Transport; |
| 52 | +using namespace chip::Testing; |
| 53 | + |
| 54 | +namespace { |
| 55 | + |
| 56 | +// Use only 2 active connections (1 outgoing, 1 incoming) to easily detect leaks (exhaustion) |
| 57 | +constexpr size_t kMaxTcpActiveConnectionCount = 2; |
| 58 | +constexpr size_t kMaxTcpPendingPackets = 4; |
| 59 | +constexpr int kMaxPortBindRetries = 100; |
| 60 | + |
| 61 | +using TCPImpl = Transport::TCP<kMaxTcpActiveConnectionCount, kMaxTcpPendingPackets>; |
| 62 | + |
| 63 | +uint16_t GetRandomPort() |
| 64 | +{ |
| 65 | + return static_cast<uint16_t>(CHIP_PORT + chip::Crypto::GetRandU16() % 1000); |
| 66 | +} |
| 67 | + |
| 68 | +CHIP_ERROR RetryPortSetup(uint16_t & outPort, std::function<CHIP_ERROR(uint16_t)> setupFn) |
| 69 | +{ |
| 70 | + CHIP_ERROR err; |
| 71 | + for (int retryCount = 0; retryCount < kMaxPortBindRetries; retryCount++) |
| 72 | + { |
| 73 | + uint16_t port = GetRandomPort(); |
| 74 | + |
| 75 | + err = setupFn(port); |
| 76 | + if (err == CHIP_ERROR_POSIX(EADDRINUSE)) |
| 77 | + { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + outPort = (err == CHIP_NO_ERROR) ? port : 0; |
| 82 | + return err; |
| 83 | + } |
| 84 | + return err; |
| 85 | +} |
| 86 | + |
| 87 | +class TestTCPConnection : public ::testing::Test, public SessionConnectionDelegate, public SessionMessageDelegate |
| 88 | +{ |
| 89 | +public: |
| 90 | + static void SetUpTestSuite() |
| 91 | + { |
| 92 | + if (mIOContext == nullptr) |
| 93 | + { |
| 94 | + mIOContext = new IOContext(); |
| 95 | + ASSERT_NE(mIOContext, nullptr); |
| 96 | + } |
| 97 | + EXPECT_SUCCESS(mIOContext->Init()); |
| 98 | + } |
| 99 | + static void TearDownTestSuite() |
| 100 | + { |
| 101 | + if (mIOContext != nullptr) |
| 102 | + { |
| 103 | + mIOContext->Shutdown(); |
| 104 | + delete mIOContext; |
| 105 | + mIOContext = nullptr; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + void SetUp() override |
| 110 | + { |
| 111 | + // Ignore Init return value, we check correctness later/implicitly |
| 112 | + (void) mTransportMgrBase.Init(&mTCP); |
| 113 | + mTransportMgrBase.SetSessionManager(&mSessionManager); |
| 114 | + |
| 115 | + mSessionManager.SetConnectionDelegate(this); |
| 116 | + mSessionManager.SetMessageDelegate(this); |
| 117 | + } |
| 118 | + |
| 119 | + void TearDown() override |
| 120 | + { |
| 121 | + mSessionManager.Shutdown(); |
| 122 | + mTransportMgrBase.Close(); |
| 123 | + mTCP.Close(); |
| 124 | + mFabricTable.Shutdown(); |
| 125 | + mOpKeyStore.Finish(); |
| 126 | + mOpCertStore.Finish(); |
| 127 | + } |
| 128 | + |
| 129 | + // SessionConnectionDelegate |
| 130 | + void OnTCPConnectionClosed(const Transport::ActiveTCPConnectionState & conn, const SessionHandle & session, |
| 131 | + CHIP_ERROR conErr) override |
| 132 | + {} |
| 133 | + |
| 134 | + bool OnTCPConnectionAttemptComplete(Transport::ActiveTCPConnectionHandle & conn, CHIP_ERROR conErr) override { return true; } |
| 135 | + |
| 136 | + // SessionMessageDelegate |
| 137 | + void OnMessageReceived(const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, const SessionHandle & session, |
| 138 | + DuplicateMessage isDuplicate, System::PacketBufferHandle && msgBuf) override |
| 139 | + {} |
| 140 | + |
| 141 | +protected: |
| 142 | + static IOContext * mIOContext; |
| 143 | + TCPImpl mTCP; |
| 144 | + TransportMgrBase mTransportMgrBase; |
| 145 | + SessionManager mSessionManager; |
| 146 | + secure_channel::MessageCounterManager mMessageCounterManager; |
| 147 | + chip::TestPersistentStorageDelegate mDeviceStorage; |
| 148 | + chip::Crypto::DefaultSessionKeystore mSessionKeystore; |
| 149 | + chip::PersistentStorageOperationalKeystore mOpKeyStore; |
| 150 | + chip::Credentials::PersistentStorageOpCertStore mOpCertStore; |
| 151 | + FabricTable mFabricTable; |
| 152 | + |
| 153 | + CHIP_ERROR InitTCP(uint16_t & outPort) |
| 154 | + { |
| 155 | + return RetryPortSetup(outPort, [&](uint16_t port) { |
| 156 | + return mTCP.Init(Transport::TcpListenParameters(mIOContext->GetTCPEndPointManager()) |
| 157 | + .SetAddressType(IPAddressType::kIPv6) |
| 158 | + .SetListenPort(port) |
| 159 | + .SetServerListenEnabled(true)); |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + CHIP_ERROR InitSessionManager() |
| 164 | + { |
| 165 | + ReturnErrorOnFailure(mOpKeyStore.Init(&mDeviceStorage)); |
| 166 | + ReturnErrorOnFailure(mOpCertStore.Init(&mDeviceStorage)); |
| 167 | + |
| 168 | + chip::FabricTable::InitParams initParams; |
| 169 | + initParams.storage = &mDeviceStorage; |
| 170 | + initParams.operationalKeystore = &mOpKeyStore; |
| 171 | + initParams.opCertStore = &mOpCertStore; |
| 172 | + |
| 173 | + ReturnErrorOnFailure(mFabricTable.Init(initParams)); |
| 174 | + |
| 175 | + return mSessionManager.Init(&mIOContext->GetSystemLayer(), &mTransportMgrBase, &mMessageCounterManager, &mDeviceStorage, |
| 176 | + &mFabricTable, mSessionKeystore); |
| 177 | + } |
| 178 | +}; |
| 179 | + |
| 180 | +IOContext * TestTCPConnection::mIOContext = nullptr; |
| 181 | + |
| 182 | +TEST_F(TestTCPConnection, TestUnauthenticatedSessionReleaseOnConnectionClose) |
| 183 | +{ |
| 184 | + uint16_t port; |
| 185 | + EXPECT_SUCCESS(InitTCP(port)); |
| 186 | + EXPECT_SUCCESS(InitSessionManager()); |
| 187 | + |
| 188 | + IPAddress addr; |
| 189 | + IPAddress::FromString("::1", addr); |
| 190 | + |
| 191 | + // Connect to self (loopback) |
| 192 | + Transport::PeerAddress peerAddr = Transport::PeerAddress::TCP(addr, port); |
| 193 | + |
| 194 | + // 1. Establish initial connection (Leaked Candidate) |
| 195 | + ActiveTCPConnectionHandle connHandle; |
| 196 | + EXPECT_SUCCESS(mTCP.TCPConnect(peerAddr, nullptr, connHandle)); |
| 197 | + |
| 198 | + mIOContext->DriveIOUntil(chip::System::Clock::Seconds16(5), |
| 199 | + [&]() { return !connHandle.IsNull() && connHandle->IsConnected(); }); |
| 200 | + ASSERT_FALSE(connHandle.IsNull()); |
| 201 | + ASSERT_TRUE(connHandle->IsConnected()); |
| 202 | + |
| 203 | + // 2. Create UnauthenticatedSession by simulating an incoming message |
| 204 | + PayloadHeader header; |
| 205 | + uint8_t payloadBuffer[64]; |
| 206 | + uint16_t encodeLen; |
| 207 | + header.SetMessageType(Protocols::Id(VendorId::Common, 1221), 112).SetExchangeID(2233).SetInitiator(true); |
| 208 | + EXPECT_SUCCESS(header.Encode(payloadBuffer, &encodeLen)); |
| 209 | + |
| 210 | + auto msgBuf = System::PacketBufferHandle::NewWithData(payloadBuffer, encodeLen); |
| 211 | + PacketHeader packetHeader; |
| 212 | + packetHeader.SetSourceNodeId(1234).SetMessageCounter(1); |
| 213 | + packetHeader.SetSessionType(Header::SessionType::kUnicastSession); // Unauthenticated |
| 214 | + |
| 215 | + EXPECT_SUCCESS(packetHeader.EncodeBeforeData(msgBuf)); |
| 216 | + |
| 217 | + { |
| 218 | + Transport::MessageTransportContext ctxt; |
| 219 | + ctxt.conn = connHandle; |
| 220 | + |
| 221 | + mSessionManager.OnMessageReceived(peerAddr, std::move(msgBuf), &ctxt); |
| 222 | + } |
| 223 | + |
| 224 | + // Close TCP connection & release local handle |
| 225 | + connHandle->ForceDisconnect(); |
| 226 | + connHandle.Release(); |
| 227 | + |
| 228 | + // Drive IO to process TCP closure |
| 229 | + mIOContext->DriveIOUntil(chip::System::Clock::Seconds16(1), [&]() { return false; }); |
| 230 | + |
| 231 | + // 5. Try to connect again to make sure there's no leak |
| 232 | + EXPECT_SUCCESS(mTCP.TCPConnect(peerAddr, nullptr, connHandle)); |
| 233 | +} |
| 234 | + |
| 235 | +} // namespace |
0 commit comments