Skip to content

Commit e4b076b

Browse files
committed
Add more unit-test cases
Signed-off-by: Lo,Chin-Ran <chin-ran.lo@nxp.com>
1 parent aa77173 commit e4b076b

File tree

5 files changed

+298
-11
lines changed

5 files changed

+298
-11
lines changed

src/wifipaf/WiFiPAFEndPoint.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class DLL_EXPORT WiFiPAFEndPoint
8888
WiFiPAFEndPoint() = default;
8989
~WiFiPAFEndPoint() = default;
9090

91+
void DoClose(uint8_t flags, CHIP_ERROR err);
92+
CHIP_ERROR HandleConnectComplete();
93+
CHIP_ERROR DriveStandAloneAck();
94+
CHIP_ERROR DoSendStandAloneAck();
95+
9196
private:
9297
CHIP_ERROR _Receive(PacketBufferHandle && data);
9398
enum class PktDirect_t : uint8_t
@@ -140,20 +145,16 @@ class DLL_EXPORT WiFiPAFEndPoint
140145

141146
CHIP_ERROR Init(WiFiPAFLayer * WiFiPafLayer, WiFiPAFSession & SessionInfo);
142147
bool IsConnected(uint8_t state) const;
143-
void DoClose(uint8_t flags, CHIP_ERROR err);
144148

145149
// Transmit path:
146150
CHIP_ERROR DriveSending();
147-
CHIP_ERROR DriveStandAloneAck();
148151
bool PrepareNextFragment(PacketBufferHandle && data, bool & sentAck);
149152
CHIP_ERROR SendNextMessage();
150153
CHIP_ERROR ContinueMessageSend();
151-
CHIP_ERROR DoSendStandAloneAck();
152154
CHIP_ERROR SendCharacteristic(PacketBufferHandle && buf);
153155
CHIP_ERROR SendWrite(PacketBufferHandle && buf);
154156

155157
// Receive path:
156-
CHIP_ERROR HandleConnectComplete();
157158
CHIP_ERROR HandleSendConfirmationReceived(bool result);
158159
CHIP_ERROR HandleHandshakeConfirmationReceived();
159160
CHIP_ERROR HandleFragmentConfirmationReceived(bool result);

src/wifipaf/WiFiPAFLayer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ class DLL_EXPORT WiFiPAFLayer
188188
static WiFiPAFTransportProtocolVersion
189189
GetHighestSupportedProtocolVersion(const PAFTransportCapabilitiesRequestMessage & reqMsg);
190190

191+
void InitialPafInfo();
191192
CHIP_ERROR AddPafSession(PafInfoAccess accType, WiFiPAFSession & SessionInfo);
192193
CHIP_ERROR RmPafSession(PafInfoAccess accType, WiFiPAFSession & SessionInfo);
193194
WiFiPAFSession * GetPAFInfo(PafInfoAccess accType, WiFiPAFSession & SessionInfo);
194195

195196
private:
196-
void InitialPafInfo();
197197
void CleanPafInfo(WiFiPAFSession & SessionInfo);
198198
WiFiPAFSession mPafInfoVect[WIFIPAF_LAYER_NUM_PAF_ENDPOINTS];
199199
chip::System::Layer * mSystemLayer;

src/wifipaf/tests/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ chip_test_suite("tests") {
2323
test_sources = [
2424
"TestWiFiPAFErrorStr.cpp",
2525
"TestWiFiPAFLayer.cpp",
26+
"TestWiFiPAFTP.cpp",
2627
]
2728

2829
cflags = [ "-Wconversion" ]

src/wifipaf/tests/TestWiFiPAFLayer.cpp

+111-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <system/SystemLayer.h>
3434
#include <system/SystemPacketBuffer.h>
3535

36+
#include <wifipaf/WiFiPAFError.h>
3637
#include <wifipaf/WiFiPAFLayer.h>
3738
#include <wifipaf/WiFiPAFLayerDelegate.h>
3839

@@ -114,10 +115,9 @@ TEST_F(TestWiFiPAFLayer, CheckWiFiPAFTransportCapabilitiesResponseMessage)
114115

115116
TEST_F(TestWiFiPAFLayer, CheckPafSession)
116117
{
118+
InitialPafInfo();
117119
// Add the 1st session by giving node_id, discriminator
118-
WiFiPAF::WiFiPAFSession sessionInfo = { .role = WiFiPAF::WiFiPafRole::kWiFiPafRole_Subscriber,
119-
.nodeId = 0x1,
120-
.discriminator = 0xF01 };
120+
WiFiPAF::WiFiPAFSession sessionInfo = { .role = kWiFiPafRole_Subscriber, .nodeId = 0x1, .discriminator = 0xF01 };
121121
EXPECT_EQ(AddPafSession(PafInfoAccess::kAccNodeInfo, sessionInfo), CHIP_NO_ERROR);
122122

123123
// Add the 2nd session
@@ -161,11 +161,14 @@ TEST_F(TestWiFiPAFLayer, CheckPafSession)
161161
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_NO_ERROR);
162162
sessionInfo.id = 0x2;
163163
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_NO_ERROR);
164+
165+
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccNodeInfo, sessionInfo), CHIP_ERROR_NOT_IMPLEMENTED);
166+
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_ERROR_NOT_FOUND);
164167
}
165168

166-
TEST_F(TestWiFiPAFLayer, CheckNewEndpoint)
169+
TEST_F(TestWiFiPAFLayer, CheckRunAsCommissioner)
167170
{
168-
WiFiPAFSession SessionInfo = {
171+
WiFiPAFSession sessionInfo = {
169172
.role = kWiFiPafRole_Subscriber,
170173
.id = 1,
171174
.peer_id = 1,
@@ -175,8 +178,110 @@ TEST_F(TestWiFiPAFLayer, CheckNewEndpoint)
175178
};
176179

177180
WiFiPAFEndPoint * newEndPoint = nullptr;
178-
EXPECT_EQ(NewEndPoint(&newEndPoint, SessionInfo, SessionInfo.role), CHIP_NO_ERROR);
181+
EXPECT_EQ(NewEndPoint(&newEndPoint, sessionInfo, sessionInfo.role), CHIP_NO_ERROR);
182+
EXPECT_NE(newEndPoint, nullptr);
183+
newEndPoint->mState = WiFiPAFEndPoint::kState_Ready;
184+
SetWiFiPAFState(State::kInitialized);
185+
EXPECT_EQ(GetWiFiPAFState(), State::kInitialized);
186+
187+
EXPECT_EQ(newEndPoint->StartConnect(), CHIP_NO_ERROR);
188+
EXPECT_EQ(AddPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_NO_ERROR);
189+
newEndPoint->mState = WiFiPAFEndPoint::kState_Connected;
190+
191+
// Send the capability request packet
192+
constexpr uint8_t bufCapReq[] = { 0x65, 0x6c, 0x04, 0x00, 0x00, 0x00, 0x5e, 0x01, 0x06 };
193+
auto packetCapReq = System::PacketBufferHandle::NewWithData(bufCapReq, sizeof(bufCapReq));
194+
EXPECT_EQ(SendMessage(sessionInfo, std::move(packetCapReq)), CHIP_NO_ERROR);
195+
EXPECT_EQ(HandleWriteConfirmed(sessionInfo, true), CHIP_NO_ERROR);
196+
197+
// Receive the capability response packet
198+
constexpr uint8_t bufCapResp[] = { 0x65, 0x6c, 0x04, 0x5b, 0x01, 0x06 };
199+
auto packetCapResp = System::PacketBufferHandle::NewWithData(bufCapResp, sizeof(bufCapResp));
200+
newEndPoint->mState = WiFiPAFEndPoint::kState_Connecting;
201+
EXPECT_EQ(OnWiFiPAFMessageReceived(sessionInfo, std::move(packetCapResp)), true);
202+
203+
// Send a packet
204+
auto buf = System::PacketBufferHandle::New(100);
205+
buf->SetDataLength(100);
206+
EXPECT_EQ(SendMessage(sessionInfo, std::move(buf)), CHIP_NO_ERROR);
207+
EXPECT_EQ(HandleWriteConfirmed(sessionInfo, true), CHIP_NO_ERROR);
208+
209+
constexpr uint8_t buf_rx[] = {
210+
to_underlying(WiFiPAFTP::HeaderFlags::kStartMessage) | to_underlying(WiFiPAFTP::HeaderFlags::kEndMessage) |
211+
to_underlying(WiFiPAFTP::HeaderFlags::kFragmentAck),
212+
0x01,
213+
0x01,
214+
0x00,
215+
0x00, // payload
216+
};
217+
218+
// Receive a pcaket
219+
auto packet_rx = System::PacketBufferHandle::NewWithData(buf_rx, sizeof(buf_rx));
220+
EXPECT_EQ(packet_rx->DataLength(), static_cast<size_t>(5));
221+
EXPECT_EQ(newEndPoint->Receive(std::move(packet_rx)), CHIP_NO_ERROR);
222+
223+
EXPECT_EQ(newEndPoint->DriveStandAloneAck(), CHIP_NO_ERROR);
224+
EXPECT_EQ(newEndPoint->DoSendStandAloneAck(), CHIP_NO_ERROR);
225+
226+
// Close the session
227+
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_NO_ERROR);
228+
newEndPoint->DoClose(kWiFiPAFCloseFlag_AbortTransmission, WIFIPAF_ERROR_APP_CLOSED_CONNECTION);
229+
}
230+
231+
TEST_F(TestWiFiPAFLayer, CheckRunAsCommissionee)
232+
{
233+
WiFiPAFSession sessionInfo = {
234+
.role = kWiFiPafRole_Publisher,
235+
.id = 1,
236+
.peer_id = 1,
237+
.peer_addr = { 0xd0, 0x17, 0x69, 0xee, 0x7f, 0x3c },
238+
.nodeId = 1,
239+
.discriminator = 0xF00,
240+
};
241+
242+
WiFiPAFEndPoint * newEndPoint = nullptr;
243+
EXPECT_EQ(NewEndPoint(&newEndPoint, sessionInfo, sessionInfo.role), CHIP_NO_ERROR);
179244
EXPECT_NE(newEndPoint, nullptr);
245+
newEndPoint->mState = WiFiPAFEndPoint::kState_Ready;
246+
247+
EXPECT_EQ(newEndPoint->StartConnect(), CHIP_NO_ERROR);
248+
EXPECT_EQ(AddPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_NO_ERROR);
249+
250+
newEndPoint->mState = WiFiPAFEndPoint::kState_Ready;
251+
252+
// Receive the Capability_Request packet
253+
constexpr uint8_t bufCapReq[] = { 0x65, 0x6c, 0x04, 0x00, 0x00, 0x00, 0x5e, 0x01, 0x06 };
254+
auto packetCapReq = System::PacketBufferHandle::NewWithData(bufCapReq, sizeof(bufCapReq));
255+
EXPECT_EQ(OnWiFiPAFMessageReceived(sessionInfo, std::move(packetCapReq)), true);
256+
257+
// Reply the Capability Response packet
258+
constexpr uint8_t bufCapResp[] = { 0x65, 0x6c, 0x04, 0x5b, 0x01, 0x06 };
259+
auto packetCapResp = System::PacketBufferHandle::NewWithData(bufCapResp, sizeof(bufCapResp));
260+
EXPECT_EQ(SendMessage(sessionInfo, std::move(packetCapResp)), CHIP_NO_ERROR);
261+
EXPECT_EQ(HandleWriteConfirmed(sessionInfo, true), CHIP_NO_ERROR);
262+
263+
// Send a packet
264+
auto buf = System::PacketBufferHandle::New(100);
265+
buf->SetDataLength(100);
266+
EXPECT_EQ(SendMessage(sessionInfo, std::move(buf)), CHIP_NO_ERROR);
267+
EXPECT_EQ(HandleWriteConfirmed(sessionInfo, true), CHIP_NO_ERROR);
268+
269+
// Receive a packet
270+
constexpr uint8_t buf_rx[] = {
271+
to_underlying(WiFiPAFTP::HeaderFlags::kStartMessage) | to_underlying(WiFiPAFTP::HeaderFlags::kEndMessage) |
272+
to_underlying(WiFiPAFTP::HeaderFlags::kFragmentAck),
273+
0x01,
274+
0x01,
275+
0x00,
276+
0x00, // payload
277+
};
278+
auto packet_rx = System::PacketBufferHandle::NewWithData(buf_rx, sizeof(buf_rx));
279+
EXPECT_EQ(packet_rx->DataLength(), static_cast<size_t>(5));
280+
EXPECT_EQ(newEndPoint->Receive(std::move(packet_rx)), CHIP_NO_ERROR);
281+
282+
// Close the session
283+
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_NO_ERROR);
284+
newEndPoint->DoClose(kWiFiPAFCloseFlag_AbortTransmission, WIFIPAF_ERROR_APP_CLOSED_CONNECTION);
180285
}
181286
}; // namespace WiFiPAF
182287
}; // namespace chip

src/wifipaf/tests/TestWiFiPAFTP.cpp

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
*
3+
* Copyright (c) 2025 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+
#include <cstdint>
19+
#include <cstring>
20+
#include <string>
21+
#include <type_traits>
22+
#include <utility>
23+
24+
#include <pw_unit_test/framework.h>
25+
26+
#include <lib/core/CHIPError.h>
27+
#include <lib/core/StringBuilderAdapters.h>
28+
#include <lib/support/CHIPMem.h>
29+
#include <lib/support/Span.h>
30+
#include <lib/support/TypeTraits.h>
31+
#include <lib/support/logging/CHIPLogging.h>
32+
#include <platform/CHIPDeviceLayer.h>
33+
#include <system/SystemLayer.h>
34+
#include <system/SystemPacketBuffer.h>
35+
36+
#include <wifipaf/WiFiPAFTP.h>
37+
38+
namespace chip {
39+
namespace WiFiPAF {
40+
41+
class TestWiFiPAFTP : public WiFiPAFTP, public ::testing::Test
42+
{
43+
public:
44+
static void SetUpTestSuite()
45+
{
46+
ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR);
47+
ASSERT_EQ(DeviceLayer::SystemLayer().Init(), CHIP_NO_ERROR);
48+
}
49+
50+
static void TearDownTestSuite()
51+
{
52+
DeviceLayer::SystemLayer().Shutdown();
53+
chip::Platform::MemoryShutdown();
54+
}
55+
56+
void SetUp() override { ASSERT_EQ(Init(nullptr, false), CHIP_NO_ERROR); }
57+
58+
void TearDown() override {}
59+
};
60+
61+
TEST_F(TestWiFiPAFTP, CheckLogState)
62+
{
63+
LogState();
64+
TakeTxPacket();
65+
TakeRxPacket();
66+
67+
PacketBufferHandle AckToSend = System::PacketBufferHandle::New(kTransferProtocolStandaloneAckHeaderSize);
68+
EXPECT_EQ(EncodeStandAloneAck(AckToSend), CHIP_NO_ERROR);
69+
70+
EXPECT_NE(GetAndIncrementNextTxSeqNum(), 0);
71+
EXPECT_NE(GetAndRecordRxAckSeqNum(), 0);
72+
HasUnackedData();
73+
74+
uint16_t FragSize = 300;
75+
SetTxFragmentSize(FragSize);
76+
SetRxFragmentSize(FragSize);
77+
EXPECT_EQ(GetTxFragmentSize(), FragSize);
78+
EXPECT_EQ(GetRxFragmentSize(), FragSize);
79+
80+
EXPECT_NE(GetRxNextSeqNum(), 0);
81+
EXPECT_NE(GetLastReceivedSequenceNumber(), 0);
82+
EXPECT_NE(GetNewestUnackedSentSequenceNumber(), 0);
83+
EXPECT_EQ(ExpectingAck(), true);
84+
ClearRxPacket();
85+
ClearTxPacket();
86+
}
87+
88+
TEST_F(TestWiFiPAFTP, CheckSendSingle)
89+
{
90+
auto buf = System::PacketBufferHandle::New(100);
91+
buf->SetDataLength(100);
92+
mRxNextSeqNum = 1;
93+
EXPECT_TRUE(HandleCharacteristicSend(buf.Retain(), true));
94+
EXPECT_EQ(TxState(), kState_Complete);
95+
TakeTxPacket();
96+
EXPECT_EQ(TxState(), kState_Idle);
97+
}
98+
99+
TEST_F(TestWiFiPAFTP, CheckSendMultiple)
100+
{
101+
auto buf = System::PacketBufferHandle::New(500);
102+
buf->SetDataLength(500);
103+
EXPECT_TRUE(HandleCharacteristicSend(buf.Retain(), false));
104+
105+
EXPECT_EQ(TxState(), kState_InProgress);
106+
EXPECT_TRUE(HandleCharacteristicSend(nullptr, false));
107+
EXPECT_EQ(TxState(), kState_Complete);
108+
TakeTxPacket();
109+
EXPECT_EQ(TxState(), kState_Idle);
110+
}
111+
112+
TEST_F(TestWiFiPAFTP, CheckRecv)
113+
{
114+
SequenceNumber_t receivedAck = 0;
115+
bool didReceiveAck = false;
116+
117+
// Receive a packet
118+
constexpr uint8_t packetData0[] = {
119+
to_underlying(HeaderFlags::kStartMessage) | to_underlying(HeaderFlags::kEndMessage),
120+
0x01,
121+
0x01,
122+
0x00,
123+
0xff, // payload
124+
};
125+
126+
auto packet0 = System::PacketBufferHandle::NewWithData(packetData0, sizeof(packetData0));
127+
EXPECT_EQ(packet0->DataLength(), static_cast<size_t>(5));
128+
129+
receivedAck = 0;
130+
didReceiveAck = false;
131+
EXPECT_EQ(HandleCharacteristicReceived(std::move(packet0), receivedAck, didReceiveAck), CHIP_NO_ERROR);
132+
EXPECT_EQ(RxState(), kState_Complete);
133+
TakeRxPacket();
134+
EXPECT_EQ(RxState(), kState_Idle);
135+
}
136+
137+
TEST_F(TestWiFiPAFTP, CheckAckRecv)
138+
{
139+
constexpr uint8_t packetData0[] = {
140+
to_underlying(HeaderFlags::kStartMessage) | to_underlying(HeaderFlags::kEndMessage) |
141+
to_underlying(HeaderFlags::kFragmentAck),
142+
0x01,
143+
0x01,
144+
0x00,
145+
0x00, // payload
146+
};
147+
148+
auto packet0 = System::PacketBufferHandle::NewWithData(packetData0, sizeof(packetData0));
149+
150+
EXPECT_EQ(packet0->DataLength(), static_cast<size_t>(5));
151+
152+
SequenceNumber_t receivedAck = 0;
153+
bool didReceiveAck = true;
154+
mExpectingAck = true;
155+
EXPECT_EQ(HandleCharacteristicReceived(std::move(packet0), receivedAck, didReceiveAck), CHIP_NO_ERROR);
156+
EXPECT_EQ(RxState(), kState_Idle);
157+
}
158+
159+
TEST_F(TestWiFiPAFTP, CheckErrorRecv)
160+
{
161+
SequenceNumber_t receivedAck = 0;
162+
bool didReceiveAck = false;
163+
164+
// Null-buffer
165+
auto nullbuf = System::PacketBufferHandle::New(0);
166+
EXPECT_NE(HandleCharacteristicReceived(std::move(nullbuf), receivedAck, didReceiveAck), CHIP_NO_ERROR);
167+
168+
// Reveived Invalid packet
169+
constexpr uint8_t packetData_invalid_ack[] = {
170+
to_underlying(HeaderFlags::kFragmentAck) | to_underlying(HeaderFlags::kEndMessage),
171+
0xf0,
172+
0x01,
173+
0x00,
174+
0xff, // payload
175+
};
176+
auto packet_invalid_ack = System::PacketBufferHandle::NewWithData(packetData_invalid_ack, sizeof(packetData_invalid_ack));
177+
EXPECT_EQ(HandleCharacteristicReceived(std::move(packet_invalid_ack), receivedAck, didReceiveAck), CHIP_NO_ERROR);
178+
}
179+
}; // namespace WiFiPAF
180+
}; // namespace chip

0 commit comments

Comments
 (0)