Skip to content

Commit

Permalink
[tests] add unit test for RcpHost SetCountryCode (#2564)
Browse files Browse the repository at this point in the history
This commit adds a unit test for `RcpHost::SetCountryCode`.

The commit also fixes an issue of current usage of `FakePlatform`. In
each gtest case, we should get a new OT Instance. Otherwise the OT
instance used in last gtest case will have some remaining tasks which
are invalid to process.
  • Loading branch information
Irving-cl authored Oct 30, 2024
1 parent 789ed7f commit 7e03f9a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
17 changes: 13 additions & 4 deletions tests/gtest/fake_posix_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@

#include "fake_platform.hpp"

#include <assert.h>

#include "openthread/openthread-system.h"

otPlatResetReason gPlatResetReason = OT_PLAT_RESET_REASON_POWER_ON;
static ot::FakePlatform sFakePlatform;
otPlatResetReason gPlatResetReason = OT_PLAT_RESET_REASON_POWER_ON;
static ot::FakePlatform *sFakePlatform;

const otRadioSpinelMetrics *otSysGetRadioSpinelMetrics(void)
{
Expand Down Expand Up @@ -60,11 +62,18 @@ otInstance *otSysInit(otPlatformConfig *aPlatformConfig)
{
OT_UNUSED_VARIABLE(aPlatformConfig);

return sFakePlatform.CurrentInstance();
assert(sFakePlatform == nullptr);
sFakePlatform = new ot::FakePlatform();
return sFakePlatform->CurrentInstance();
}

void otSysDeinit(void)
{
if (sFakePlatform != nullptr)
{
delete sFakePlatform;
sFakePlatform = nullptr;
}
}

void otSysMainloopUpdate(otInstance *, otSysMainloopContext *)
Expand All @@ -73,5 +82,5 @@ void otSysMainloopUpdate(otInstance *, otSysMainloopContext *)

void otSysMainloopProcess(otInstance *, const otSysMainloopContext *)
{
sFakePlatform.Run(/* microseconds */ 1000);
sFakePlatform->Run(/* microseconds */ 1000);
}
52 changes: 52 additions & 0 deletions tests/gtest/test_rcp_host_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,55 @@ TEST(RcpHostApi, DeviceRoleChangesCorrectlyAfterSetThreadEnabled)

host.Deinit();
}

TEST(RcpHostApi, SetCountryCodeWorkCorrectly)
{
otError error = OT_ERROR_FAILED;
bool resultReceived = false;
otbr::MainloopContext mainloop;
otbr::Ncp::ThreadHost::AsyncResultReceiver receiver = [&resultReceived, &error](otError aError,
const std::string &aErrorMsg) {
OT_UNUSED_VARIABLE(aErrorMsg);
resultReceived = true;
error = aError;
};
otbr::Ncp::RcpHost host("wpan0", std::vector<const char *>(), /* aBackboneInterfaceName */ "", /* aDryRun */ false,
/* aEnableAutoAttach */ false);

// 1. Call SetCountryCode when host hasn't been initialized.
otbr::MainloopManager::GetInstance().RemoveMainloopProcessor(
&host); // Temporarily remove RcpHost because it's not initialized yet.
host.SetCountryCode("AF", receiver);
MainloopProcessUntil(mainloop, /* aTimeoutSec */ 0, [&resultReceived]() { return resultReceived; });
EXPECT_EQ(error, OT_ERROR_INVALID_STATE);
otbr::MainloopManager::GetInstance().AddMainloopProcessor(&host);

host.Init();
// 2. Call SetCountryCode with invalid arguments
resultReceived = false;
error = OT_ERROR_NONE;
host.SetCountryCode("AFA", receiver);
MainloopProcessUntil(mainloop, /* aTimeoutSec */ 0, [&resultReceived]() { return resultReceived; });
EXPECT_EQ(error, OT_ERROR_INVALID_ARGS);

resultReceived = false;
error = OT_ERROR_NONE;
host.SetCountryCode("A", receiver);
MainloopProcessUntil(mainloop, /* aTimeoutSec */ 0, [&resultReceived]() { return resultReceived; });
EXPECT_EQ(error, OT_ERROR_INVALID_ARGS);

resultReceived = false;
error = OT_ERROR_NONE;
host.SetCountryCode("12", receiver);
MainloopProcessUntil(mainloop, /* aTimeoutSec */ 0, [&resultReceived]() { return resultReceived; });
EXPECT_EQ(error, OT_ERROR_INVALID_ARGS);

// 3. Call SetCountryCode with valid argument
resultReceived = false;
error = OT_ERROR_NONE;
host.SetCountryCode("AF", receiver);
MainloopProcessUntil(mainloop, /* aTimeoutSec */ 0, [&resultReceived]() { return resultReceived; });
EXPECT_EQ(error, OT_ERROR_NOT_IMPLEMENTED); // The current platform weak implmentation returns 'NOT_IMPLEMENTED'.

host.Deinit();
}

0 comments on commit 7e03f9a

Please sign in to comment.