2525#include < lib/core/CHIPError.h>
2626#include < transport/raw/PeerAddress.h>
2727
28+ #include < memory>
29+
2830using namespace chip ;
2931using namespace chip ::Controller;
3032using PairerAccess = chip::Testing::SetUpCodePairerTestAccess;
3133
3234namespace {
3335
36+ // DeviceCommissioner is too large to embed in a test fixture (it exceeds
37+ // the pw_unit_test light backend's static memory pool). Heap-allocate it
38+ // along with the SetUpCodePairer and test accessor that depend on it.
3439class TestSetUpCodePairer : public ::testing::Test
3540{
3641public:
3742 static void SetUpTestSuite () { ASSERT_EQ (Platform::MemoryInit (), CHIP_NO_ERROR); }
3843 static void TearDownTestSuite () { Platform::MemoryShutdown (); }
3944
45+ void SetUp () override
46+ {
47+ mCommissioner = std::make_unique<DeviceCommissioner>();
48+ mPairer = std::make_unique<SetUpCodePairer>(mCommissioner .get ());
49+ mAccess = std::make_unique<PairerAccess>(mPairer .get ());
50+ }
51+
52+ void TearDown () override
53+ {
54+ mAccess .reset ();
55+ mPairer .reset ();
56+ mCommissioner .reset ();
57+ }
58+
4059protected:
41- DeviceCommissioner commissioner;
42- SetUpCodePairer pairer{ &commissioner };
43- PairerAccess access{ &pairer };
60+ PairerAccess & Access () { return *mAccess ; }
61+
62+ private:
63+ std::unique_ptr<DeviceCommissioner> mCommissioner ;
64+ std::unique_ptr<SetUpCodePairer> mPairer ;
65+ std::unique_ptr<PairerAccess> mAccess ;
4466};
4567
4668// When the discovery timeout fires while a PASE attempt is in progress,
4769// DNS-SD should be stopped (it runs indefinitely) but other transports
4870// (BLE, Wi-Fi PAF, NFC) should be left alone since they self-terminate.
4971TEST_F (TestSetUpCodePairer, TimeoutDuringPASE_StopsDNSSD_PreservesOtherTransports)
5072{
51- access .SetRemoteId (1 );
52- access .SetWaitingForPASE (true );
53- access .SetWaitingForDiscovery (PairerAccess::kIPTransport , true );
54- access .SetWaitingForDiscovery (PairerAccess::kBLETransport , true );
73+ Access () .SetRemoteId (1 );
74+ Access () .SetWaitingForPASE (true );
75+ Access () .SetWaitingForDiscovery (PairerAccess::kIPTransport , true );
76+ Access () .SetWaitingForDiscovery (PairerAccess::kBLETransport , true );
5577
56- access .FireTimeoutCallback ();
78+ Access () .FireTimeoutCallback ();
5779
5880 // DNS-SD must be stopped to prevent DiscoveryInProgress() from being stuck true.
59- EXPECT_FALSE (access .GetWaitingForDiscovery (PairerAccess::kIPTransport ));
81+ EXPECT_FALSE (Access () .GetWaitingForDiscovery (PairerAccess::kIPTransport ));
6082 // BLE must be preserved — it may still discover a commissionee.
61- EXPECT_TRUE (access .GetWaitingForDiscovery (PairerAccess::kBLETransport ));
83+ EXPECT_TRUE (Access () .GetWaitingForDiscovery (PairerAccess::kBLETransport ));
6284 // PASE state must not be disturbed.
63- EXPECT_TRUE (access .GetWaitingForPASE ());
85+ EXPECT_TRUE (Access () .GetWaitingForPASE ());
6486}
6587
6688// When the discovery timeout fires with no PASE in progress,
6789// all transports should be stopped and failure reported.
6890TEST_F (TestSetUpCodePairer, TimeoutNoPASE_StopsAllTransports)
6991{
70- access .SetRemoteId (1 );
71- access .SetWaitingForPASE (false );
72- access .SetWaitingForDiscovery (PairerAccess::kIPTransport , true );
73- access .SetWaitingForDiscovery (PairerAccess::kBLETransport , true );
92+ Access () .SetRemoteId (1 );
93+ Access () .SetWaitingForPASE (false );
94+ Access () .SetWaitingForDiscovery (PairerAccess::kIPTransport , true );
95+ Access () .SetWaitingForDiscovery (PairerAccess::kBLETransport , true );
7496
75- access .FireTimeoutCallback ();
97+ Access () .FireTimeoutCallback ();
7698
77- EXPECT_FALSE (access .GetWaitingForDiscovery (PairerAccess::kIPTransport ));
78- EXPECT_FALSE (access .GetWaitingForDiscovery (PairerAccess::kBLETransport ));
99+ EXPECT_FALSE (Access () .GetWaitingForDiscovery (PairerAccess::kIPTransport ));
100+ EXPECT_FALSE (Access () .GetWaitingForDiscovery (PairerAccess::kBLETransport ));
79101 // StopPairingIfTransportsExhausted should have reported failure and cleared mRemoteId.
80- EXPECT_EQ (access .GetRemoteId (), kUndefinedNodeId );
102+ EXPECT_EQ (Access () .GetRemoteId (), kUndefinedNodeId );
81103}
82104
83105// When a PASE attempt fails (OnPairingComplete with error) and DNS-SD
@@ -87,57 +109,57 @@ TEST_F(TestSetUpCodePairer, TimeoutNoPASE_StopsAllTransports)
87109// StopPairingIfTransportsExhausted.
88110TEST_F (TestSetUpCodePairer, PASEFailure_DNSSDStaysAlive)
89111{
90- access .SetRemoteId (1 );
91- access .SetWaitingForDiscovery (PairerAccess::kIPTransport , true );
92- access .SetWaitingForDiscovery (PairerAccess::kBLETransport , true );
112+ Access () .SetRemoteId (1 );
113+ Access () .SetWaitingForDiscovery (PairerAccess::kIPTransport , true );
114+ Access () .SetWaitingForDiscovery (PairerAccess::kBLETransport , true );
93115
94116 // Simulate a UDP PASE attempt: set mCurrentPASEParameters and enter
95117 // the PASE-waiting state (as ConnectToDiscoveredDevice would).
96118 SetUpCodePairerParameters udpParams;
97119 udpParams.SetPeerAddress (Transport::PeerAddress::UDP (Inet::IPAddress::Any, 5540 ));
98- access .SetCurrentPASEParameters (udpParams);
99- access .ExpectPASEEstablishment ();
120+ Access () .SetCurrentPASEParameters (udpParams);
121+ Access () .ExpectPASEEstablishment ();
100122
101123 // PASE fails.
102- access .CallOnPairingComplete (CHIP_ERROR_TIMEOUT);
124+ Access () .CallOnPairingComplete (CHIP_ERROR_TIMEOUT);
103125
104126 // DNS-SD must still be running — the 30s timeout is its natural upper bound.
105- EXPECT_TRUE (access .GetWaitingForDiscovery (PairerAccess::kIPTransport ));
127+ EXPECT_TRUE (Access () .GetWaitingForDiscovery (PairerAccess::kIPTransport ));
106128 // BLE must still be running.
107- EXPECT_TRUE (access .GetWaitingForDiscovery (PairerAccess::kBLETransport ));
129+ EXPECT_TRUE (Access () .GetWaitingForDiscovery (PairerAccess::kBLETransport ));
108130 // The pairer should still be active (not yet reported failure).
109- EXPECT_NE (access .GetRemoteId (), kUndefinedNodeId );
131+ EXPECT_NE (Access () .GetRemoteId (), kUndefinedNodeId );
110132 // The error should be saved for later.
111- EXPECT_EQ (access .GetLastPASEError (), CHIP_ERROR_TIMEOUT);
133+ EXPECT_EQ (Access () .GetLastPASEError (), CHIP_ERROR_TIMEOUT);
112134 // mCurrentPASEParameters should have been cleared.
113- EXPECT_FALSE (access .HasCurrentPASEParameters ());
135+ EXPECT_FALSE (Access () .HasCurrentPASEParameters ());
114136}
115137
116138// When a PASE attempt fails synchronously in ConnectToDiscoveredDevice,
117139// mCurrentPASEParameters must be cleared so a later non-UDP PASE
118140// completion cannot incorrectly use stale parameters for ReconfirmRecord.
119141TEST_F (TestSetUpCodePairer, SyncPASEFailure_ClearsCurrentPASEParameters)
120142{
121- access .SetRemoteId (1 );
122- access .SetWaitingForDiscovery (PairerAccess::kIPTransport , true );
143+ Access () .SetRemoteId (1 );
144+ Access () .SetWaitingForDiscovery (PairerAccess::kIPTransport , true );
123145
124146 // Set up stale UDP parameters (as if ConnectToDiscoveredDevice had set
125147 // them before calling PairDevice, which then failed synchronously).
126148 SetUpCodePairerParameters udpParams;
127149 udpParams.SetPeerAddress (Transport::PeerAddress::UDP (Inet::IPAddress::Any, 5540 ));
128- access .SetCurrentPASEParameters (udpParams);
150+ Access () .SetCurrentPASEParameters (udpParams);
129151
130152 // Now simulate a subsequent non-UDP PASE completion (e.g., BLE PASE
131153 // succeeds or fails). ExpectPASEEstablishment + OnPairingComplete
132154 // with success exercises ResetDiscoveryState which clears everything.
133155 // Instead, verify directly that after a PASE failure the stale
134156 // parameters don't persist: simulate a PASE completion with error.
135- access .ExpectPASEEstablishment ();
136- access .CallOnPairingComplete (CHIP_ERROR_CONNECTION_ABORTED);
157+ Access () .ExpectPASEEstablishment ();
158+ Access () .CallOnPairingComplete (CHIP_ERROR_CONNECTION_ABORTED);
137159
138160 // mCurrentPASEParameters must be cleared — stale UDP params must not
139161 // survive to confuse a later ReconfirmRecord check.
140- EXPECT_FALSE (access .HasCurrentPASEParameters ());
162+ EXPECT_FALSE (Access () .HasCurrentPASEParameters ());
141163}
142164
143165} // namespace
0 commit comments