|
| 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 | +#include <gtest/gtest.h> |
| 20 | + |
| 21 | +#include <controller/CHIPDeviceController.h> |
| 22 | +#include <controller/SetUpCodePairer.h> |
| 23 | +#include <controller/tests/SetUpCodePairerTestAccess.h> |
| 24 | +#include <lib/core/CHIPError.h> |
| 25 | + |
| 26 | +using namespace chip; |
| 27 | +using namespace chip::Controller; |
| 28 | +using PairerAccess = chip::Testing::SetUpCodePairerTestAccess; |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +class TestSetUpCodePairer : public ::testing::Test |
| 33 | +{ |
| 34 | +public: |
| 35 | + static void SetUpTestSuite() { ASSERT_EQ(Platform::MemoryInit(), CHIP_NO_ERROR); } |
| 36 | + static void TearDownTestSuite() { Platform::MemoryShutdown(); } |
| 37 | + |
| 38 | +protected: |
| 39 | + DeviceCommissioner commissioner; |
| 40 | + SetUpCodePairer pairer{ &commissioner }; |
| 41 | + PairerAccess access{ &pairer }; |
| 42 | +}; |
| 43 | + |
| 44 | +// When the discovery timeout fires while a PASE attempt is in progress, |
| 45 | +// DNS-SD should be stopped (it runs indefinitely) but other transports |
| 46 | +// (BLE, Wi-Fi PAF, NFC) should be left alone since they self-terminate. |
| 47 | +TEST_F(TestSetUpCodePairer, TimeoutDuringPASE_StopsDNSSD_PreservesOtherTransports) |
| 48 | +{ |
| 49 | + access.SetRemoteId(1); |
| 50 | + access.SetWaitingForPASE(true); |
| 51 | + access.SetWaitingForDiscovery(PairerAccess::kIPTransport, true); |
| 52 | + access.SetWaitingForDiscovery(PairerAccess::kBLETransport, true); |
| 53 | + |
| 54 | + access.FireTimeoutCallback(); |
| 55 | + |
| 56 | + // DNS-SD must be stopped to prevent DiscoveryInProgress() from being stuck true. |
| 57 | + EXPECT_FALSE(access.GetWaitingForDiscovery(PairerAccess::kIPTransport)); |
| 58 | + // BLE must be preserved — it may still discover a commissionee. |
| 59 | + EXPECT_TRUE(access.GetWaitingForDiscovery(PairerAccess::kBLETransport)); |
| 60 | + // PASE state must not be disturbed. |
| 61 | + EXPECT_TRUE(access.GetWaitingForPASE()); |
| 62 | +} |
| 63 | + |
| 64 | +// When the discovery timeout fires with no PASE in progress, |
| 65 | +// all transports should be stopped and failure reported. |
| 66 | +TEST_F(TestSetUpCodePairer, TimeoutNoPASE_StopsAllTransports) |
| 67 | +{ |
| 68 | + access.SetRemoteId(1); |
| 69 | + access.SetWaitingForPASE(false); |
| 70 | + access.SetWaitingForDiscovery(PairerAccess::kIPTransport, true); |
| 71 | + access.SetWaitingForDiscovery(PairerAccess::kBLETransport, true); |
| 72 | + |
| 73 | + access.FireTimeoutCallback(); |
| 74 | + |
| 75 | + EXPECT_FALSE(access.GetWaitingForDiscovery(PairerAccess::kIPTransport)); |
| 76 | + EXPECT_FALSE(access.GetWaitingForDiscovery(PairerAccess::kBLETransport)); |
| 77 | + // StopPairingIfTransportsExhausted should have reported failure and cleared mRemoteId. |
| 78 | + EXPECT_EQ(access.GetRemoteId(), kUndefinedNodeId); |
| 79 | +} |
| 80 | + |
| 81 | +} // namespace |
0 commit comments