Skip to content

Commit

Permalink
[Testing] Configure the minimal commissioning timeout to be 1 second
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Apr 29, 2022
1 parent 145d7bf commit d2426a5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
11 changes: 11 additions & 0 deletions examples/all-clusters-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@
#include "AppOptions.h"
#include "binding-handler.h"

#if defined(CONFIG_IM_BUILD_FOR_UNIT_TEST) || defined(CHIP_CONFIG_TEST)
#include <app/server/CommissioningWindowManager.h>
#include <app/server/Server.h>
#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;
}
7 changes: 5 additions & 2 deletions src/app/server/CommissioningWindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<System::Clock::Seconds16> 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)
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 11 additions & 8 deletions src/app/server/CommissioningWindowManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<System::Clock::Seconds16> 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; }

Expand Down Expand Up @@ -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; }

Expand Down Expand Up @@ -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<System::Clock::Seconds16> mMinCommissioningTimeoutOverride;
};

} // namespace chip
2 changes: 1 addition & 1 deletion src/app/tests/TestCommissionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d2426a5

Please sign in to comment.