blob: 5e866b4b25ea729fc0be06a508ffb2b20c238018 [file] [log] [blame]
/*
*
* Copyright (c) 2020-2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <controller/CHIPCommissionableNodeController.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/UnitTestRegistration.h>
#include <nlunit-test.h>
using namespace chip;
using namespace chip::Dnssd;
using namespace chip::Controller;
namespace chip {
namespace Dnssd {
namespace {
class MockResolver : public Resolver
{
public:
CHIP_ERROR Init(chip::Inet::EndPointManager<chip::Inet::UDPEndPoint> * udpEndPointManager) override { return InitStatus; }
bool IsInitialized() override { return true; }
void Shutdown() override {}
void SetOperationalDelegate(OperationalResolveDelegate * delegate) override {}
CHIP_ERROR ResolveNodeId(const PeerId & peerId) override { return ResolveNodeIdStatus; }
void NodeIdResolutionNoLongerNeeded(const PeerId & peerId) override {}
CHIP_ERROR StartDiscovery(DiscoveryType type, DiscoveryFilter filter, DiscoveryContext &) override
{
if (DiscoveryType::kCommissionerNode == type)
return DiscoverCommissionersStatus;
return CHIP_ERROR_NOT_IMPLEMENTED;
}
CHIP_ERROR StopDiscovery(DiscoveryContext &) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR ReconfirmRecord(const char * hostname, Inet::IPAddress address, Inet::InterfaceId interfaceId) override
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
CHIP_ERROR InitStatus = CHIP_NO_ERROR;
CHIP_ERROR ResolveNodeIdStatus = CHIP_NO_ERROR;
CHIP_ERROR DiscoverCommissionersStatus = CHIP_NO_ERROR;
};
} // namespace
} // namespace Dnssd
} // namespace chip
namespace {
#if INET_CONFIG_ENABLE_IPV4
void TestGetDiscoveredCommissioner_HappyCase(nlTestSuite * inSuite, void * inContext)
{
MockResolver resolver;
CommissionableNodeController controller(&resolver);
chip::Dnssd::DiscoveredNodeData discNodeData;
discNodeData.Set<chip::Dnssd::CommissionNodeData>();
chip::Dnssd::CommissionNodeData & inNodeData = discNodeData.Get<chip::Dnssd::CommissionNodeData>();
Platform::CopyString(inNodeData.hostName, "mockHostName");
Inet::IPAddress::FromString("192.168.1.10", inNodeData.ipAddress[0]);
inNodeData.numIPs++;
inNodeData.port = 5540;
controller.OnNodeDiscovered(discNodeData);
NL_TEST_ASSERT(inSuite, controller.GetDiscoveredCommissioner(0) != nullptr);
NL_TEST_ASSERT(inSuite, strcmp(inNodeData.hostName, controller.GetDiscoveredCommissioner(0)->hostName) == 0);
NL_TEST_ASSERT(inSuite, inNodeData.ipAddress[0] == controller.GetDiscoveredCommissioner(0)->ipAddress[0]);
NL_TEST_ASSERT(inSuite, controller.GetDiscoveredCommissioner(0)->port == 5540);
NL_TEST_ASSERT(inSuite, controller.GetDiscoveredCommissioner(0)->numIPs == 1);
}
void TestGetDiscoveredCommissioner_InvalidNodeDiscovered_ReturnsNullptr(nlTestSuite * inSuite, void * inContext)
{
MockResolver resolver;
CommissionableNodeController controller(&resolver);
chip::Dnssd::DiscoveredNodeData discNodeData;
discNodeData.Set<chip::Dnssd::CommissionNodeData>();
chip::Dnssd::CommissionNodeData & inNodeData = discNodeData.Get<chip::Dnssd::CommissionNodeData>();
Inet::IPAddress::FromString("192.168.1.10", inNodeData.ipAddress[0]);
inNodeData.numIPs++;
inNodeData.port = 5540;
controller.OnNodeDiscovered(discNodeData);
for (int i = 0; i < CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES; i++)
{
NL_TEST_ASSERT(inSuite, controller.GetDiscoveredCommissioner(i) == nullptr);
}
}
void TestGetDiscoveredCommissioner_HappyCase_OneValidOneInvalidNode(nlTestSuite * inSuite, void * inContext)
{
MockResolver resolver;
CommissionableNodeController controller(&resolver);
chip::Dnssd::DiscoveredNodeData invalidDiscNodeData, validDiscNodeData;
invalidDiscNodeData.Set<chip::Dnssd::CommissionNodeData>();
validDiscNodeData.Set<chip::Dnssd::CommissionNodeData>();
chip::Dnssd::CommissionNodeData & invalidNodeData = invalidDiscNodeData.Get<chip::Dnssd::CommissionNodeData>();
chip::Dnssd::CommissionNodeData & validNodeData = validDiscNodeData.Get<chip::Dnssd::CommissionNodeData>();
Inet::IPAddress::FromString("192.168.1.10", invalidNodeData.ipAddress[0]);
invalidNodeData.numIPs++;
invalidNodeData.port = 5540;
Platform::CopyString(validNodeData.hostName, "mockHostName2");
Inet::IPAddress::FromString("192.168.1.11", validNodeData.ipAddress[0]);
validNodeData.numIPs++;
validNodeData.port = 5540;
controller.OnNodeDiscovered(validDiscNodeData);
controller.OnNodeDiscovered(invalidDiscNodeData);
NL_TEST_ASSERT(inSuite, controller.GetDiscoveredCommissioner(0) != nullptr);
NL_TEST_ASSERT(inSuite, strcmp(validNodeData.hostName, controller.GetDiscoveredCommissioner(0)->hostName) == 0);
NL_TEST_ASSERT(inSuite, validNodeData.ipAddress[0] == controller.GetDiscoveredCommissioner(0)->ipAddress[0]);
NL_TEST_ASSERT(inSuite, controller.GetDiscoveredCommissioner(0)->port == 5540);
NL_TEST_ASSERT(inSuite, controller.GetDiscoveredCommissioner(0)->numIPs == 1);
NL_TEST_ASSERT(inSuite, controller.GetDiscoveredCommissioner(1) == nullptr);
}
#endif // INET_CONFIG_ENABLE_IPV4
void TestGetDiscoveredCommissioner_NoNodesDiscovered_ReturnsNullptr(nlTestSuite * inSuite, void * inContext)
{
MockResolver resolver;
CommissionableNodeController controller(&resolver);
for (int i = 0; i < CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES; i++)
{
NL_TEST_ASSERT(inSuite, controller.GetDiscoveredCommissioner(i) == nullptr);
}
}
void TestDiscoverCommissioners_HappyCase(nlTestSuite * inSuite, void * inContext)
{
MockResolver resolver;
CommissionableNodeController controller(&resolver);
NL_TEST_ASSERT(inSuite, controller.DiscoverCommissioners() == CHIP_NO_ERROR);
}
void TestDiscoverCommissioners_HappyCaseWithDiscoveryFilter(nlTestSuite * inSuite, void * inContext)
{
MockResolver resolver;
CommissionableNodeController controller(&resolver);
NL_TEST_ASSERT(inSuite,
controller.DiscoverCommissioners(Dnssd::DiscoveryFilter(Dnssd::DiscoveryFilterType::kDeviceType, 35)) ==
CHIP_NO_ERROR);
}
void TestDiscoverCommissioners_InitError_ReturnsError(nlTestSuite * inSuite, void * inContext)
{
MockResolver resolver;
resolver.InitStatus = CHIP_ERROR_INTERNAL;
CommissionableNodeController controller(&resolver);
NL_TEST_ASSERT(inSuite, controller.DiscoverCommissioners() != CHIP_NO_ERROR);
}
void TestDiscoverCommissioners_DiscoverCommissionersError_ReturnsError(nlTestSuite * inSuite, void * inContext)
{
MockResolver resolver;
resolver.DiscoverCommissionersStatus = CHIP_ERROR_INTERNAL;
CommissionableNodeController controller(&resolver);
NL_TEST_ASSERT(inSuite, controller.DiscoverCommissioners() != CHIP_NO_ERROR);
}
// clang-format off
const nlTest sTests[] =
{
#if INET_CONFIG_ENABLE_IPV4
NL_TEST_DEF("TestGetDiscoveredCommissioner_HappyCase", TestGetDiscoveredCommissioner_HappyCase),
NL_TEST_DEF("TestGetDiscoveredCommissioner_HappyCase_OneValidOneInvalidNode", TestGetDiscoveredCommissioner_HappyCase_OneValidOneInvalidNode),
NL_TEST_DEF("TestGetDiscoveredCommissioner_InvalidNodeDiscovered_ReturnsNullptr", TestGetDiscoveredCommissioner_InvalidNodeDiscovered_ReturnsNullptr),
#endif // INET_CONFIG_ENABLE_IPV4
NL_TEST_DEF("TestGetDiscoveredCommissioner_NoNodesDiscovered_ReturnsNullptr", TestGetDiscoveredCommissioner_NoNodesDiscovered_ReturnsNullptr),
NL_TEST_DEF("TestDiscoverCommissioners_HappyCase", TestDiscoverCommissioners_HappyCase),
NL_TEST_DEF("TestDiscoverCommissioners_HappyCaseWithDiscoveryFilter", TestDiscoverCommissioners_HappyCaseWithDiscoveryFilter),
NL_TEST_DEF("TestDiscoverCommissioners_InitError_ReturnsError", TestDiscoverCommissioners_InitError_ReturnsError),
NL_TEST_DEF("TestDiscoverCommissioners_DiscoverCommissionersError_ReturnsError", TestDiscoverCommissioners_DiscoverCommissionersError_ReturnsError),
NL_TEST_SENTINEL()
};
// clang-format on
} // namespace
int TestCommissionableNodeController_Setup(void * inContext)
{
if (CHIP_NO_ERROR != chip::Platform::MemoryInit())
{
return FAILURE;
}
return SUCCESS;
}
int TestCommissionableNodeController_Teardown(void * inContext)
{
chip::Platform::MemoryShutdown();
return SUCCESS;
}
int TestCommissionableNodeController()
{
nlTestSuite theSuite = { "CommissionableNodeController", &sTests[0], TestCommissionableNodeController_Setup,
TestCommissionableNodeController_Teardown };
nlTestRunner(&theSuite, nullptr);
return nlTestRunnerStats(&theSuite);
}
CHIP_REGISTER_TEST_SUITE(TestCommissionableNodeController)