Skip to content

Commit 9e7cf70

Browse files
committed
Clear the buffer after allocation in the test module
Declare test class as the friend class to access the private functions for testing Signed-off-by: Lo,Chin-Ran <chin-ran.lo@nxp.com>
1 parent e4b076b commit 9e7cf70

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

src/wifipaf/WiFiPAFEndPoint.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class DLL_EXPORT WiFiPAFEndPoint
5555
{
5656
friend class WiFiPAFLayer;
5757
friend class WiFiPAFEndPointPool;
58+
friend class TestWiFiPAFLayer;
5859

5960
public:
6061
typedef uint64_t AlignT;
@@ -88,11 +89,6 @@ class DLL_EXPORT WiFiPAFEndPoint
8889
WiFiPAFEndPoint() = default;
8990
~WiFiPAFEndPoint() = default;
9091

91-
void DoClose(uint8_t flags, CHIP_ERROR err);
92-
CHIP_ERROR HandleConnectComplete();
93-
CHIP_ERROR DriveStandAloneAck();
94-
CHIP_ERROR DoSendStandAloneAck();
95-
9692
private:
9793
CHIP_ERROR _Receive(PacketBufferHandle && data);
9894
enum class PktDirect_t : uint8_t
@@ -144,17 +140,21 @@ class DLL_EXPORT WiFiPAFEndPoint
144140
SequenceNumber_t mReceiveWindowMaxSize;
145141

146142
CHIP_ERROR Init(WiFiPAFLayer * WiFiPafLayer, WiFiPAFSession & SessionInfo);
143+
void DoClose(uint8_t flags, CHIP_ERROR err);
147144
bool IsConnected(uint8_t state) const;
148145

149146
// Transmit path:
150147
CHIP_ERROR DriveSending();
148+
CHIP_ERROR DriveStandAloneAck();
151149
bool PrepareNextFragment(PacketBufferHandle && data, bool & sentAck);
152150
CHIP_ERROR SendNextMessage();
153151
CHIP_ERROR ContinueMessageSend();
152+
CHIP_ERROR DoSendStandAloneAck();
154153
CHIP_ERROR SendCharacteristic(PacketBufferHandle && buf);
155154
CHIP_ERROR SendWrite(PacketBufferHandle && buf);
156155

157156
// Receive path:
157+
CHIP_ERROR HandleConnectComplete();
158158
CHIP_ERROR HandleSendConfirmationReceived(bool result);
159159
CHIP_ERROR HandleHandshakeConfirmationReceived();
160160
CHIP_ERROR HandleFragmentConfirmationReceived(bool result);

src/wifipaf/WiFiPAFLayer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ enum class PafInfoAccess
160160
class DLL_EXPORT WiFiPAFLayer
161161
{
162162
friend class WiFiPAFEndPoint;
163+
friend class TestWiFiPAFLayer;
163164

164165
public:
165166
State mAppState = State::kNotReady;
@@ -188,12 +189,12 @@ class DLL_EXPORT WiFiPAFLayer
188189
static WiFiPAFTransportProtocolVersion
189190
GetHighestSupportedProtocolVersion(const PAFTransportCapabilitiesRequestMessage & reqMsg);
190191

191-
void InitialPafInfo();
192192
CHIP_ERROR AddPafSession(PafInfoAccess accType, WiFiPAFSession & SessionInfo);
193193
CHIP_ERROR RmPafSession(PafInfoAccess accType, WiFiPAFSession & SessionInfo);
194194
WiFiPAFSession * GetPAFInfo(PafInfoAccess accType, WiFiPAFSession & SessionInfo);
195195

196196
private:
197+
void InitialPafInfo();
197198
void CleanPafInfo(WiFiPAFSession & SessionInfo);
198199
WiFiPAFSession mPafInfoVect[WIFIPAF_LAYER_NUM_PAF_ENDPOINTS];
199200
chip::System::Layer * mSystemLayer;

src/wifipaf/tests/TestWiFiPAFLayer.cpp

+22-9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class TestWiFiPAFLayer : public WiFiPAFLayer, private WiFiPAFLayerDelegate, publ
5959
{
6060
ASSERT_EQ(Init(&DeviceLayer::SystemLayer()), CHIP_NO_ERROR);
6161
mWiFiPAFTransport = this;
62+
InitialPafInfo();
6263
}
6364

6465
void TearDown() override
@@ -70,6 +71,15 @@ class TestWiFiPAFLayer : public WiFiPAFLayer, private WiFiPAFLayerDelegate, publ
7071
CHIP_ERROR WiFiPAFMessageReceived(WiFiPAFSession & RxInfo, System::PacketBufferHandle && msg) override { return CHIP_NO_ERROR; }
7172
CHIP_ERROR WiFiPAFMessageSend(WiFiPAFSession & TxInfo, System::PacketBufferHandle && msg) override { return CHIP_NO_ERROR; }
7273
CHIP_ERROR WiFiPAFCloseSession(WiFiPAFSession & SessionInfo) override { return CHIP_NO_ERROR; }
74+
static constexpr size_t kTestPacketLength = 100;
75+
76+
void SetEndPoint(WiFiPAFEndPoint * pEndPoint) { mEndPoint = pEndPoint; }
77+
void EpDoClose(uint8_t flags, CHIP_ERROR err) { return mEndPoint->DoClose(flags, err); }
78+
CHIP_ERROR EpDriveStandAloneAck() { return mEndPoint->DriveStandAloneAck(); }
79+
CHIP_ERROR EpDoSendStandAloneAck() { return mEndPoint->DoSendStandAloneAck(); }
80+
81+
private:
82+
WiFiPAFEndPoint * mEndPoint;
7383
};
7484

7585
TEST_F(TestWiFiPAFLayer, CheckWiFiPAFTransportCapabilitiesRequestMessage)
@@ -115,7 +125,6 @@ TEST_F(TestWiFiPAFLayer, CheckWiFiPAFTransportCapabilitiesResponseMessage)
115125

116126
TEST_F(TestWiFiPAFLayer, CheckPafSession)
117127
{
118-
InitialPafInfo();
119128
// Add the 1st session by giving node_id, discriminator
120129
WiFiPAF::WiFiPAFSession sessionInfo = { .role = kWiFiPafRole_Subscriber, .nodeId = 0x1, .discriminator = 0xF01 };
121130
EXPECT_EQ(AddPafSession(PafInfoAccess::kAccNodeInfo, sessionInfo), CHIP_NO_ERROR);
@@ -180,6 +189,7 @@ TEST_F(TestWiFiPAFLayer, CheckRunAsCommissioner)
180189
WiFiPAFEndPoint * newEndPoint = nullptr;
181190
EXPECT_EQ(NewEndPoint(&newEndPoint, sessionInfo, sessionInfo.role), CHIP_NO_ERROR);
182191
EXPECT_NE(newEndPoint, nullptr);
192+
SetEndPoint(newEndPoint);
183193
newEndPoint->mState = WiFiPAFEndPoint::kState_Ready;
184194
SetWiFiPAFState(State::kInitialized);
185195
EXPECT_EQ(GetWiFiPAFState(), State::kInitialized);
@@ -201,8 +211,9 @@ TEST_F(TestWiFiPAFLayer, CheckRunAsCommissioner)
201211
EXPECT_EQ(OnWiFiPAFMessageReceived(sessionInfo, std::move(packetCapResp)), true);
202212

203213
// Send a packet
204-
auto buf = System::PacketBufferHandle::New(100);
205-
buf->SetDataLength(100);
214+
auto buf = System::PacketBufferHandle::New(kTestPacketLength);
215+
buf->SetDataLength(kTestPacketLength);
216+
memset(buf->Start(), 0, buf->DataLength());
206217
EXPECT_EQ(SendMessage(sessionInfo, std::move(buf)), CHIP_NO_ERROR);
207218
EXPECT_EQ(HandleWriteConfirmed(sessionInfo, true), CHIP_NO_ERROR);
208219

@@ -220,12 +231,12 @@ TEST_F(TestWiFiPAFLayer, CheckRunAsCommissioner)
220231
EXPECT_EQ(packet_rx->DataLength(), static_cast<size_t>(5));
221232
EXPECT_EQ(newEndPoint->Receive(std::move(packet_rx)), CHIP_NO_ERROR);
222233

223-
EXPECT_EQ(newEndPoint->DriveStandAloneAck(), CHIP_NO_ERROR);
224-
EXPECT_EQ(newEndPoint->DoSendStandAloneAck(), CHIP_NO_ERROR);
234+
EXPECT_EQ(EpDriveStandAloneAck(), CHIP_NO_ERROR);
235+
EXPECT_EQ(EpDoSendStandAloneAck(), CHIP_NO_ERROR);
225236

226237
// Close the session
227238
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_NO_ERROR);
228-
newEndPoint->DoClose(kWiFiPAFCloseFlag_AbortTransmission, WIFIPAF_ERROR_APP_CLOSED_CONNECTION);
239+
EpDoClose(kWiFiPAFCloseFlag_AbortTransmission, WIFIPAF_ERROR_APP_CLOSED_CONNECTION);
229240
}
230241

231242
TEST_F(TestWiFiPAFLayer, CheckRunAsCommissionee)
@@ -242,6 +253,7 @@ TEST_F(TestWiFiPAFLayer, CheckRunAsCommissionee)
242253
WiFiPAFEndPoint * newEndPoint = nullptr;
243254
EXPECT_EQ(NewEndPoint(&newEndPoint, sessionInfo, sessionInfo.role), CHIP_NO_ERROR);
244255
EXPECT_NE(newEndPoint, nullptr);
256+
SetEndPoint(newEndPoint);
245257
newEndPoint->mState = WiFiPAFEndPoint::kState_Ready;
246258

247259
EXPECT_EQ(newEndPoint->StartConnect(), CHIP_NO_ERROR);
@@ -261,8 +273,9 @@ TEST_F(TestWiFiPAFLayer, CheckRunAsCommissionee)
261273
EXPECT_EQ(HandleWriteConfirmed(sessionInfo, true), CHIP_NO_ERROR);
262274

263275
// Send a packet
264-
auto buf = System::PacketBufferHandle::New(100);
265-
buf->SetDataLength(100);
276+
auto buf = System::PacketBufferHandle::New(kTestPacketLength);
277+
buf->SetDataLength(kTestPacketLength);
278+
memset(buf->Start(), 0, buf->DataLength());
266279
EXPECT_EQ(SendMessage(sessionInfo, std::move(buf)), CHIP_NO_ERROR);
267280
EXPECT_EQ(HandleWriteConfirmed(sessionInfo, true), CHIP_NO_ERROR);
268281

@@ -281,7 +294,7 @@ TEST_F(TestWiFiPAFLayer, CheckRunAsCommissionee)
281294

282295
// Close the session
283296
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_NO_ERROR);
284-
newEndPoint->DoClose(kWiFiPAFCloseFlag_AbortTransmission, WIFIPAF_ERROR_APP_CLOSED_CONNECTION);
297+
EpDoClose(kWiFiPAFCloseFlag_AbortTransmission, WIFIPAF_ERROR_APP_CLOSED_CONNECTION);
285298
}
286299
}; // namespace WiFiPAF
287300
}; // namespace chip

src/wifipaf/tests/TestWiFiPAFTP.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ TEST_F(TestWiFiPAFTP, CheckLogState)
8787

8888
TEST_F(TestWiFiPAFTP, CheckSendSingle)
8989
{
90-
auto buf = System::PacketBufferHandle::New(100);
91-
buf->SetDataLength(100);
90+
constexpr size_t kShortPacketLength = 100;
91+
auto buf = System::PacketBufferHandle::New(kShortPacketLength);
92+
buf->SetDataLength(kShortPacketLength);
93+
memset(buf->Start(), 0, buf->DataLength());
9294
mRxNextSeqNum = 1;
9395
EXPECT_TRUE(HandleCharacteristicSend(buf.Retain(), true));
9496
EXPECT_EQ(TxState(), kState_Complete);
@@ -98,8 +100,11 @@ TEST_F(TestWiFiPAFTP, CheckSendSingle)
98100

99101
TEST_F(TestWiFiPAFTP, CheckSendMultiple)
100102
{
101-
auto buf = System::PacketBufferHandle::New(500);
102-
buf->SetDataLength(500);
103+
constexpr size_t kLongPacketLength = 500;
104+
auto buf = System::PacketBufferHandle::New(kLongPacketLength);
105+
ASSERT_FALSE(buf.IsNull());
106+
buf->SetDataLength(kLongPacketLength);
107+
memset(buf->Start(), 0, buf->DataLength());
103108
EXPECT_TRUE(HandleCharacteristicSend(buf.Retain(), false));
104109

105110
EXPECT_EQ(TxState(), kState_InProgress);

0 commit comments

Comments
 (0)