From d2426a5f8c6fc85f3546bbb31a31b6b35bf3c4fe Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Fri, 29 Apr 2022 16:47:52 +0200 Subject: [PATCH] [Testing] Configure the minimal commissioning timeout to be 1 second --- examples/all-clusters-app/linux/main.cpp | 11 +++++++++++ src/app/server/CommissioningWindowManager.cpp | 7 +++++-- src/app/server/CommissioningWindowManager.h | 19 +++++++++++-------- src/app/tests/TestCommissionManager.cpp | 2 +- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/examples/all-clusters-app/linux/main.cpp b/examples/all-clusters-app/linux/main.cpp index c44c7061d7685b..202727d98437da 100644 --- a/examples/all-clusters-app/linux/main.cpp +++ b/examples/all-clusters-app/linux/main.cpp @@ -20,12 +20,23 @@ #include "AppOptions.h" #include "binding-handler.h" +#if defined(CONFIG_IM_BUILD_FOR_UNIT_TEST) || defined(CHIP_CONFIG_TEST) +#include +#include +#endif // defined(CONFIG_IM_BUILD_FOR_UNIT_TEST) || defined(CHIP_CONFIG_TEST) + int main(int argc, char * argv[]) { VerifyOrDie(ChipLinuxAppInit(argc, argv, AppOptions::GetOptions()) == 0); VerifyOrDie(InitBindingHandlers() == CHIP_NO_ERROR); LinuxDeviceOptions::GetInstance().dacProvider = AppOptions::GetDACProvider(); + +#if defined(CONFIG_IM_BUILD_FOR_UNIT_TEST) || defined(CHIP_CONFIG_TEST) + chip::CommissioningWindowManager & commissionMgr = chip::Server::GetInstance().GetCommissioningWindowManager(); + commissionMgr.OverrideMinCommissioningTimeout(chip::System::Clock::Seconds16(1)); +#endif // defined(CONFIG_IM_BUILD_FOR_UNIT_TEST) || defined(CHIP_CONFIG_TEST) + ChipLinuxAppMainLoop(); return 0; } diff --git a/src/app/server/CommissioningWindowManager.cpp b/src/app/server/CommissioningWindowManager.cpp index f9e1c639ef598e..43ca3aef355bb2 100644 --- a/src/app/server/CommissioningWindowManager.cpp +++ b/src/app/server/CommissioningWindowManager.cpp @@ -47,6 +47,10 @@ void OnPlatformEventWrapper(const chip::DeviceLayer::ChipDeviceEvent * event, in namespace chip { +#if defined(CONFIG_IM_BUILD_FOR_UNIT_TEST) || defined(CHIP_CONFIG_TESTS) +Optional CommissioningWindowManager::mMinCommissioningTimeoutOverride; +#endif // defined(CONFIG_IM_BUILD_FOR_UNIT_TEST) || defined(CHIP_CONFIG_TESTS) + void CommissioningWindowManager::OnPlatformEvent(const DeviceLayer::ChipDeviceEvent * event) { if (event->Type == DeviceLayer::DeviceEventType::kCommissioningComplete) @@ -182,8 +186,7 @@ void CommissioningWindowManager::OnSessionEstablished(const SessionHandle & sess CHIP_ERROR CommissioningWindowManager::OpenCommissioningWindow(Seconds16 commissioningTimeout) { - VerifyOrReturnError(commissioningTimeout <= MaxCommissioningTimeout() && - commissioningTimeout >= mMinCommissioningTimeoutOverride.ValueOr(MinCommissioningTimeout()), + VerifyOrReturnError(commissioningTimeout <= MaxCommissioningTimeout() && commissioningTimeout >= MinCommissioningTimeout(), CHIP_ERROR_INVALID_ARGUMENT); DeviceLayer::FailSafeContext & failSafeContext = DeviceLayer::DeviceControlServer::DeviceControlSvr().GetFailSafeContext(); VerifyOrReturnError(!failSafeContext.IsFailSafeArmed(), CHIP_ERROR_INCORRECT_STATE); diff --git a/src/app/server/CommissioningWindowManager.h b/src/app/server/CommissioningWindowManager.h index 21f41ce2d9435e..76609db3a6a844 100644 --- a/src/app/server/CommissioningWindowManager.h +++ b/src/app/server/CommissioningWindowManager.h @@ -56,11 +56,22 @@ class CommissioningWindowManager : public SessionEstablishmentDelegate, public a return System::Clock::Seconds16(15 * 60); } +#if defined(CONFIG_IM_BUILD_FOR_UNIT_TEST) || defined(CHIP_CONFIG_TEST) + // For tests only, allow overriding the spec-defined minimum value of the + // commissioning window timeout. + static Optional mMinCommissioningTimeoutOverride; + static System::Clock::Seconds16 MinCommissioningTimeout() + { + // Specification section 5.4.2.3. Announcement Duration says 3 minutes. + return mMinCommissioningTimeoutOverride.ValueOr(System::Clock::Seconds16(3 * 60)); + } +#else static constexpr System::Clock::Seconds16 MinCommissioningTimeout() { // Specification section 5.4.2.3. Announcement Duration says 3 minutes. return System::Clock::Seconds16(3 * 60); } +#endif // defined(CONFIG_IM_BUILD_FOR_UNIT_TEST) || defined(CHIP_CONFIG_TEST) void SetAppDelegate(AppDelegate * delegate) { mAppDelegate = delegate; } @@ -92,10 +103,6 @@ class CommissioningWindowManager : public SessionEstablishmentDelegate, public a void OnPlatformEvent(const DeviceLayer::ChipDeviceEvent * event); - // For tests only, allow overriding the spec-defined minimum value of the - // commissioning window timeout. - void OverrideMinCommissioningTimeout(System::Clock::Seconds16 timeout) { mMinCommissioningTimeoutOverride.SetValue(timeout); } - private: void SetBLE(bool ble) { mIsBLE = ble; } @@ -163,10 +170,6 @@ class CommissioningWindowManager : public SessionEstablishmentDelegate, public a uint32_t mECMIterations = 0; uint32_t mECMSaltLength = 0; uint8_t mECMSalt[kSpake2p_Max_PBKDF_Salt_Length]; - - // For tests only, so that we can test the commissioning window timeout - // without having to wait 3 minutes. - Optional mMinCommissioningTimeoutOverride; }; } // namespace chip diff --git a/src/app/tests/TestCommissionManager.cpp b/src/app/tests/TestCommissionManager.cpp index cda96445bc9516..c64036aa646c35 100644 --- a/src/app/tests/TestCommissionManager.cpp +++ b/src/app/tests/TestCommissionManager.cpp @@ -123,7 +123,7 @@ void CheckCommissioningWindowManagerWindowTimeoutTask(intptr_t context) constexpr auto kTimeoutSeconds = chip::System::Clock::Seconds16(1); constexpr uint16_t kTimeoutMs = 1000; constexpr unsigned kSleepPadding = 100; - commissionMgr.OverrideMinCommissioningTimeout(kTimeoutSeconds); + commissionMgr.mMinCommissioningTimeoutOverride.SetValue(kTimeoutSeconds); CHIP_ERROR err = commissionMgr.OpenBasicCommissioningWindow(kTimeoutSeconds, CommissioningWindowAdvertisement::kDnssdOnly); NL_TEST_ASSERT(suite, err == CHIP_NO_ERROR); NL_TEST_ASSERT(suite,