Skip to content

Commit 79b2b7c

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

File tree

5 files changed

+301
-9
lines changed

5 files changed

+301
-9
lines changed

src/wifipaf/WiFiPAFEndPoint.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ 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();
9195
private:
9296
CHIP_ERROR _Receive(PacketBufferHandle && data);
9397
enum class PktDirect_t : uint8_t
@@ -140,20 +144,16 @@ class DLL_EXPORT WiFiPAFEndPoint
140144

141145
CHIP_ERROR Init(WiFiPAFLayer * WiFiPafLayer, WiFiPAFSession & SessionInfo);
142146
bool IsConnected(uint8_t state) const;
143-
void DoClose(uint8_t flags, CHIP_ERROR err);
144147

145148
// Transmit path:
146149
CHIP_ERROR DriveSending();
147-
CHIP_ERROR DriveStandAloneAck();
148150
bool PrepareNextFragment(PacketBufferHandle && data, bool & sentAck);
149151
CHIP_ERROR SendNextMessage();
150152
CHIP_ERROR ContinueMessageSend();
151-
CHIP_ERROR DoSendStandAloneAck();
152153
CHIP_ERROR SendCharacteristic(PacketBufferHandle && buf);
153154
CHIP_ERROR SendWrite(PacketBufferHandle && buf);
154155

155156
// Receive path:
156-
CHIP_ERROR HandleConnectComplete();
157157
CHIP_ERROR HandleSendConfirmationReceived(bool result);
158158
CHIP_ERROR HandleHandshakeConfirmationReceived();
159159
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

+110-4
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,8 +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,
120+
WiFiPAF::WiFiPAFSession sessionInfo = { .role = kWiFiPafRole_Subscriber,
119121
.nodeId = 0x1,
120122
.discriminator = 0xF01 };
121123
EXPECT_EQ(AddPafSession(PafInfoAccess::kAccNodeInfo, sessionInfo), CHIP_NO_ERROR);
@@ -161,11 +163,14 @@ TEST_F(TestWiFiPAFLayer, CheckPafSession)
161163
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_NO_ERROR);
162164
sessionInfo.id = 0x2;
163165
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_NO_ERROR);
166+
167+
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccNodeInfo, sessionInfo), CHIP_ERROR_NOT_IMPLEMENTED);
168+
EXPECT_EQ(RmPafSession(PafInfoAccess::kAccSessionId, sessionInfo), CHIP_ERROR_NOT_FOUND);
164169
}
165170

166-
TEST_F(TestWiFiPAFLayer, CheckNewEndpoint)
171+
TEST_F(TestWiFiPAFLayer, CheckRunAsCommissioner)
167172
{
168-
WiFiPAFSession SessionInfo = {
173+
WiFiPAFSession sessionInfo = {
169174
.role = kWiFiPafRole_Subscriber,
170175
.id = 1,
171176
.peer_id = 1,
@@ -175,8 +180,109 @@ TEST_F(TestWiFiPAFLayer, CheckNewEndpoint)
175180
};
176181

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

src/wifipaf/tests/TestWiFiPAFTP.cpp

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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
57+
{
58+
ASSERT_EQ(Init(nullptr, false), CHIP_NO_ERROR);
59+
}
60+
61+
void TearDown() override
62+
{
63+
}
64+
};
65+
66+
TEST_F(TestWiFiPAFTP, CheckLogState)
67+
{
68+
LogState();
69+
TakeTxPacket();
70+
TakeRxPacket();
71+
72+
PacketBufferHandle AckToSend = System::PacketBufferHandle::New(kTransferProtocolStandaloneAckHeaderSize);
73+
EXPECT_EQ(EncodeStandAloneAck(AckToSend), CHIP_NO_ERROR);
74+
75+
EXPECT_NE(GetAndIncrementNextTxSeqNum(), 0);
76+
EXPECT_NE(GetAndRecordRxAckSeqNum(), 0);
77+
HasUnackedData();
78+
79+
80+
uint16_t FragSize = 300;
81+
SetTxFragmentSize(FragSize);
82+
SetRxFragmentSize(FragSize);
83+
EXPECT_EQ(GetTxFragmentSize(), FragSize);
84+
EXPECT_EQ(GetRxFragmentSize(), FragSize);
85+
86+
EXPECT_NE(GetRxNextSeqNum(), 0);
87+
EXPECT_NE(GetLastReceivedSequenceNumber(), 0);
88+
EXPECT_NE(GetNewestUnackedSentSequenceNumber(), 0);
89+
EXPECT_EQ(ExpectingAck(), true);
90+
ClearRxPacket();
91+
ClearTxPacket();
92+
}
93+
94+
TEST_F(TestWiFiPAFTP, CheckSendSingle)
95+
{
96+
auto buf = System::PacketBufferHandle::New(100);
97+
buf->SetDataLength(100);
98+
mRxNextSeqNum = 1;
99+
EXPECT_TRUE(HandleCharacteristicSend(buf.Retain(), true));
100+
EXPECT_EQ(TxState(), kState_Complete);
101+
TakeTxPacket();
102+
EXPECT_EQ(TxState(), kState_Idle);
103+
}
104+
105+
TEST_F(TestWiFiPAFTP, CheckSendMultiple)
106+
{
107+
auto buf = System::PacketBufferHandle::New(500);
108+
buf->SetDataLength(500);
109+
EXPECT_TRUE(HandleCharacteristicSend(buf.Retain(), false));
110+
111+
EXPECT_EQ(TxState(), kState_InProgress);
112+
EXPECT_TRUE(HandleCharacteristicSend(nullptr, false));
113+
EXPECT_EQ(TxState(), kState_Complete);
114+
TakeTxPacket();
115+
EXPECT_EQ(TxState(), kState_Idle);
116+
}
117+
118+
TEST_F(TestWiFiPAFTP, CheckRecv)
119+
{
120+
SequenceNumber_t receivedAck = 0;
121+
bool didReceiveAck = false;
122+
123+
// Receive a packet
124+
constexpr uint8_t packetData0[] = {
125+
to_underlying(HeaderFlags::kStartMessage) | to_underlying(HeaderFlags::kEndMessage),
126+
0x01,
127+
0x01,
128+
0x00,
129+
0xff, // payload
130+
};
131+
132+
auto packet0 = System::PacketBufferHandle::NewWithData(packetData0, sizeof(packetData0));
133+
EXPECT_EQ(packet0->DataLength(), static_cast<size_t>(5));
134+
135+
receivedAck = 0;
136+
didReceiveAck = false;
137+
EXPECT_EQ(HandleCharacteristicReceived(std::move(packet0), receivedAck, didReceiveAck), CHIP_NO_ERROR);
138+
EXPECT_EQ(RxState(), kState_Complete);
139+
TakeRxPacket();
140+
EXPECT_EQ(RxState(), kState_Idle);
141+
}
142+
143+
TEST_F(TestWiFiPAFTP, CheckAckRecv)
144+
{
145+
constexpr uint8_t packetData0[] = {
146+
to_underlying(HeaderFlags::kStartMessage) | to_underlying(HeaderFlags::kEndMessage) | to_underlying(HeaderFlags::kFragmentAck),
147+
0x01,
148+
0x01,
149+
0x00,
150+
0x00, // payload
151+
};
152+
153+
auto packet0 = System::PacketBufferHandle::NewWithData(packetData0, sizeof(packetData0));
154+
155+
EXPECT_EQ(packet0->DataLength(), static_cast<size_t>(5));
156+
157+
SequenceNumber_t receivedAck = 0;
158+
bool didReceiveAck = true;
159+
mExpectingAck = true;
160+
EXPECT_EQ(HandleCharacteristicReceived(std::move(packet0), receivedAck, didReceiveAck), CHIP_NO_ERROR);
161+
EXPECT_EQ(RxState(), kState_Idle);
162+
}
163+
164+
TEST_F(TestWiFiPAFTP, CheckErrorRecv)
165+
{
166+
SequenceNumber_t receivedAck = 0;
167+
bool didReceiveAck = false;
168+
169+
// Null-buffer
170+
auto nullbuf = System::PacketBufferHandle::New(0);
171+
EXPECT_NE(HandleCharacteristicReceived(std::move(nullbuf), receivedAck, didReceiveAck), CHIP_NO_ERROR);
172+
173+
// Reveived Invalid packet
174+
constexpr uint8_t packetData_invalid_ack[] = {
175+
to_underlying(HeaderFlags::kFragmentAck) | to_underlying(HeaderFlags::kEndMessage),
176+
0xf0,
177+
0x01,
178+
0x00,
179+
0xff, // payload
180+
};
181+
auto packet_invalid_ack = System::PacketBufferHandle::NewWithData(packetData_invalid_ack, sizeof(packetData_invalid_ack));
182+
EXPECT_EQ(HandleCharacteristicReceived(std::move(packet_invalid_ack), receivedAck, didReceiveAck), CHIP_NO_ERROR);
183+
}
184+
}; // namespace WiFiPAF
185+
}; // namespace chip

0 commit comments

Comments
 (0)