diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index e8e4734e34..2883948f43 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -214,6 +214,8 @@ jobs: -DCMAKE_INSTALL_PREFIX="${{github.workspace}}/install/usr" -DCMAKE_MODULE_PATH="${{github.workspace}}/install/tools/cmake" -DCMAKE_CXX_FLAGS=" + -fprofile-arcs + -ftest-coverage -I ${{github.workspace}}/rdkservices/Tests/headers -I ${{github.workspace}}/rdkservices/Tests/headers/audiocapturemgr -I ${{github.workspace}}/rdkservices/Tests/headers/rdk/ds diff --git a/Network/CHANGELOG.md b/Network/CHANGELOG.md index f396659e7b..c4e15a1167 100644 --- a/Network/CHANGELOG.md +++ b/Network/CHANGELOG.md @@ -16,6 +16,10 @@ All notable changes to this RDK Service will be documented in this file. * For more details, refer to [versioning](https://github.com/rdkcentral/rdkservices#versioning) section under Main README. +## [1.3.2] - 2023-12-21 +### Changed +- Changed eventHandler function to public + ## [1.3.1] - 2023-11-30 ### Fixed - Addressed the IPv4 & IPv6 cache issue where it was not suppose to be cached when DNS or other parameter is not received yet diff --git a/Network/Network.h b/Network/Network.h index 25296f582d..7d441f5c53 100644 --- a/Network/Network.h +++ b/Network/Network.h @@ -199,7 +199,6 @@ namespace WPEFramework { void onInterfaceIPAddressChanged(std::string interface, std::string ipv6Addr, std::string ipv4Addr, bool acquired); void onDefaultInterfaceChanged(std::string oldInterface, std::string newInterface); - static void eventHandler(const char *owner, IARM_EventId_t eventId, void *data, size_t len); void iarmEventHandler(const char *owner, IARM_EventId_t eventId, void *data, size_t len); // Netmask Validation @@ -222,6 +221,7 @@ namespace WPEFramework { public: Network(); virtual ~Network(); + static void eventHandler(const char *owner, IARM_EventId_t eventId, void *data, size_t len); //Build QueryInterface implementation, specifying all possible interfaces to be returned. BEGIN_INTERFACE_MAP(Network) diff --git a/Tests/mocks/Iarm.h b/Tests/mocks/Iarm.h index 0092f6a478..64ec179a50 100644 --- a/Tests/mocks/Iarm.h +++ b/Tests/mocks/Iarm.h @@ -920,6 +920,7 @@ typedef enum _IARM_Bus_NMgr_WiFi_EventId_t { #define IARM_BUS_NETSRVMGR_API_stopConnectivityMonitoring "stopConnectivityMonitoring" #define IARM_BUS_NETSRVMGR_API_isAvailable "isAvailable" #define IARM_BUS_NETSRVMGR_API_getPublicIP "getPublicIP" +#define IARM_BUS_NETSRVMGR_API_configurePNI "configurePNI" // TODO: remove this #define registerMethod(...) for (uint8_t i = 1; GetHandler(i); i++) GetHandler(i)->Register(__VA_ARGS__) diff --git a/Tests/tests/test_Network.cpp b/Tests/tests/test_Network.cpp index 846f63c8ca..5710926d23 100644 --- a/Tests/tests/test_Network.cpp +++ b/Tests/tests/test_Network.cpp @@ -2,6 +2,7 @@ #include #include #include "Network.h" +#include "NetworkConnectivity.h" #include "FactoriesImplementation.h" #include "ServiceMock.h" @@ -104,6 +105,31 @@ class NetworkTest : public NetworkTestBase { } }; +class NetworkInitializedEventTest : public NetworkTest { +protected: + FactoriesImplementation factoriesImplementation; + PluginHost::IDispatcher* dispatcher; + + + NetworkInitializedEventTest() + : NetworkTest() + { + PluginHost::IFactories::Assign(&factoriesImplementation); + + dispatcher = static_cast( + plugin->QueryInterface(PluginHost::IDispatcher::ID)); + dispatcher->Activate(&service); + } + + virtual ~NetworkInitializedEventTest() override + { + dispatcher->Deactivate(); + dispatcher->Release(); + PluginHost::IFactories::Assign(nullptr); + + } +}; + TEST_F(NetworkTest, RegisteredMethods) { EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getStbIp"))); @@ -148,6 +174,63 @@ TEST_F(NetworkTest, getStbIp) EXPECT_EQ(response, string("{\"ip\":\"192.168.1.101\",\"success\":true}")); } +TEST_F(NetworkTest, getStbIp_cache) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getSTBip))); + + auto param = static_cast(arg); + memcpy(¶m->activeIfaceIpaddr, "192.168.1.101", sizeof("192.168.1.101")); + EXPECT_EQ(string(param->activeIfaceIpaddr), string(_T("192.168.1.101"))); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getStbIp"), _T("{}"), response)); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getStbIp"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"ip\":\"192.168.1.101\",\"success\":true}")); + +} + +TEST_F(NetworkTest, getNullStbIp) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getSTBip))); + + auto param = static_cast(arg); + EXPECT_EQ(string(param->activeIfaceIpaddr), string(_T(""))); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getStbIp"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"ip\":\"\",\"success\":true}")); +} + +TEST_F(NetworkTest, getFailedStbIp) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getSTBip))); + + auto param = static_cast(arg); + EXPECT_EQ(string(param->activeIfaceIpaddr), string(_T(""))); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("getStbIp"), _T("{}"), response)); + +} + TEST_F(NetworkTest, getInterfaces) { EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) @@ -173,6 +256,20 @@ TEST_F(NetworkTest, getInterfaces) EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); } +TEST_F(NetworkTest, getInterfacesFailed) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getInterfaceList))); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("getInterfaces"), _T("{}"), response)); +} + TEST_F(NetworkTest, isInterfaceEnabled) { EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) @@ -196,6 +293,27 @@ TEST_F(NetworkTest, isInterfaceEnabled) EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); } +TEST_F(NetworkTest, isInterfaceEnabled_failed) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_isInterfaceEnabled))); + auto param = static_cast(arg); + + memcpy(¶m->setInterface, "ETHERNET", sizeof("ETHERNET")); + param->isInterfaceEnabled = true; + + EXPECT_EQ(string(param->setInterface), string(_T("ETHERNET"))); + EXPECT_EQ(param->isInterfaceEnabled, true); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("isInterfaceEnabled"), _T("{\"interface\": \"ETHERNET\"}"), response)); +} + TEST_F(NetworkTest, getDefaultInterface) { EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) @@ -215,6 +333,70 @@ TEST_F(NetworkTest, getDefaultInterface) EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); } +TEST_F(NetworkTest, getDefaultInterface_cache) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getDefaultInterface))); + auto param = static_cast(arg); + memcpy(¶m->interface, "eth0", sizeof("eth0")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getDefaultInterface"), _T("{}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"interface\":\"ETHERNET\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getDefaultInterface"), _T("{}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"interface\":\"ETHERNET\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); +} + +TEST_F(NetworkTest, isInterfaceEnabled_WrongIface) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_isInterfaceEnabled))); + auto param = static_cast(arg); + + memcpy(¶m->setInterface, "ETHERNET", sizeof("ETHERNET")); + param->isInterfaceEnabled = true; + + EXPECT_EQ(string(param->setInterface), string(_T("ETHERNET"))); + EXPECT_EQ(param->isInterfaceEnabled, true); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("isInterfaceEnabled"), _T("{\"interface\": \"TEST\"}"), response)); +} + +TEST_F(NetworkTest, isInterfaceEnabled_Fail) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_isInterfaceEnabled))); + auto param = static_cast(arg); + + memcpy(¶m->setInterface, "ETHERNET", sizeof("ETHERNET")); + param->isInterfaceEnabled = true; + + EXPECT_EQ(string(param->setInterface), string(_T("ETHERNET"))); + EXPECT_EQ(param->isInterfaceEnabled, true); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("isInterfaceEnabled"), _T("{\"interface\": \"TEST\"}"), response)); +} + TEST_F(NetworkTest, getIPSettings) { EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) @@ -257,44 +439,93 @@ TEST_F(NetworkTest, getIPSettings) EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); } -TEST_F(NetworkTest, getPublicIP) +TEST_F(NetworkTest, getIP6Settings) { EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) .Times(::testing::AnyNumber()) .WillRepeatedly( [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getPublicIP))); - auto param = static_cast(arg); - memcpy(¶m->public_ip, "69.136.49.95", sizeof("69.136.49.95")); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getIPSettings))); - EXPECT_EQ(string(param->public_ip), string(_T("69.136.49.95"))); + auto param = static_cast(arg); + memcpy(¶m->interface, "ETHERNET", sizeof("ETHERNET")); + memcpy(¶m->ipversion, "IPV6", sizeof("IPV6")); + param->autoconfig = true; + memcpy(¶m->ipaddress, "192.168.1.101", sizeof("192.168.1.101")); + memcpy(¶m->netmask, "255.255.255.0", sizeof("255.255.255.0")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->primarydns, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->secondarydns, "192.168.1.2", sizeof("192.168.1.2")); + + EXPECT_EQ(string(param->interface), string(_T("ETHERNET"))); + EXPECT_EQ(string(param->ipversion), string(_T("IPV6"))); + EXPECT_EQ(string(param->ipaddress), string(_T("192.168.1.101"))); + EXPECT_EQ(string(param->netmask), string(_T("255.255.255.0"))); + EXPECT_EQ(string(param->gateway), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->primarydns), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->secondarydns), string(_T("192.168.1.2"))); + EXPECT_EQ(param->autoconfig, true); return IARM_RESULT_SUCCESS; }); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getPublicIP"), _T("{\"iface\": \"WIFI\", \"ipv6\": false}"), response)); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"public_ip\":\"69.136.49.95\""))); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getIPSettings"), _T("{\"interface\": \"ETHERNET\",\"ipversion\": \"IPV6\"}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"interface\":\"ETHERNET\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ipversion\":\"IPV6\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"autoconfig\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ipaddr\":\"192.168.1.101\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"netmask\":\"255.255.255.0\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"gateway\":\"192.168.1.1\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"primarydns\":\"192.168.1.1\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"secondarydns\":\"192.168.1.2\""))); EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); } -TEST_F(NetworkTest, setInterfaceEnabled) +TEST_F(NetworkTest, getIPSettings_wifi) { EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) .Times(::testing::AnyNumber()) .WillRepeatedly( [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setInterfaceEnabled))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getIPSettings))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "WIFI", sizeof("WIFI")); + memcpy(¶m->ipversion, "IPV4", sizeof("IPV4")); + param->autoconfig = true; + memcpy(¶m->ipaddress, "192.168.1.101", sizeof("192.168.1.101")); + memcpy(¶m->netmask, "255.255.255.0", sizeof("255.255.255.0")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->primarydns, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->secondarydns, "192.168.1.2", sizeof("192.168.1.2")); + + EXPECT_EQ(string(param->interface), string(_T("WIFI"))); + EXPECT_EQ(string(param->ipversion), string(_T("IPV4"))); + EXPECT_EQ(string(param->ipaddress), string(_T("192.168.1.101"))); + EXPECT_EQ(string(param->netmask), string(_T("255.255.255.0"))); + EXPECT_EQ(string(param->gateway), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->primarydns), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->secondarydns), string(_T("192.168.1.2"))); + EXPECT_EQ(param->autoconfig, true); return IARM_RESULT_SUCCESS; }); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setInterfaceEnabled"), _T("{\"interface\": \"WIFI\", \"enabled\": true, \"persist\": true}"), response)); - EXPECT_EQ(response, string("{\"success\":true}")); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getIPSettings"), _T("{\"interface\": \"WIFI\",\"ipversion\": \"IPV4\"}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"interface\":\"WIFI\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ipversion\":\"IPV4\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"autoconfig\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ipaddr\":\"192.168.1.101\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"netmask\":\"255.255.255.0\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"gateway\":\"192.168.1.1\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"primarydns\":\"192.168.1.1\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"secondarydns\":\"192.168.1.2\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); + } -TEST_F(NetworkTest, getSTBIPFamily) +TEST_F(NetworkTest, getIP6Settings_wifi) { - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) .Times(::testing::AnyNumber()) .WillRepeatedly( @@ -303,17 +534,17 @@ TEST_F(NetworkTest, getSTBIPFamily) EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getIPSettings))); auto param = static_cast(arg); - memcpy(¶m->interface, "ETHERNET", sizeof("ETHERNET")); - memcpy(¶m->ipversion, "IPV4", sizeof("IPV4")); + memcpy(¶m->interface, "WIFI", sizeof("WIFI")); + memcpy(¶m->ipversion, "IPV6", sizeof("IPV6")); param->autoconfig = true; memcpy(¶m->ipaddress, "192.168.1.101", sizeof("192.168.1.101")); memcpy(¶m->netmask, "255.255.255.0", sizeof("255.255.255.0")); - memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); memcpy(¶m->primarydns, "192.168.1.1", sizeof("192.168.1.1")); memcpy(¶m->secondarydns, "192.168.1.2", sizeof("192.168.1.2")); - EXPECT_EQ(string(param->interface), string(_T("ETHERNET"))); - EXPECT_EQ(string(param->ipversion), string(_T("IPV4"))); + EXPECT_EQ(string(param->interface), string(_T("WIFI"))); + EXPECT_EQ(string(param->ipversion), string(_T("IPV6"))); EXPECT_EQ(string(param->ipaddress), string(_T("192.168.1.101"))); EXPECT_EQ(string(param->netmask), string(_T("255.255.255.0"))); EXPECT_EQ(string(param->gateway), string(_T("192.168.1.1"))); @@ -323,46 +554,318 @@ TEST_F(NetworkTest, getSTBIPFamily) return IARM_RESULT_SUCCESS; }); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getIPSettings"), _T("{\"interface\": \"ETHERNET\",\"ipversion\": \"IPV4\"}"), response)); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getSTBIPFamily"), _T("{\"family\": \"AF_INET\"}"), response)); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ip\":\"192.168.1.101\""))); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getIPSettings"), _T("{\"interface\": \"WIFI\",\"ipversion\": \"IPV6\"}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"interface\":\"WIFI\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ipversion\":\"IPV6\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"autoconfig\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ipaddr\":\"192.168.1.101\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"netmask\":\"255.255.255.0\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"gateway\":\"192.168.1.1\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"primarydns\":\"192.168.1.1\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"secondarydns\":\"192.168.1.2\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); + } -TEST_F(NetworkTest, setDefaultInterface) +TEST_F(NetworkTest, getIPSettings_Failed) { EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) .Times(::testing::AnyNumber()) .WillRepeatedly( [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setDefaultInterface))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getIPSettings))); - return IARM_RESULT_SUCCESS; + auto param = static_cast(arg); + memcpy(¶m->interface, "ETHERNET", sizeof("ETHERNET")); + memcpy(¶m->ipversion, "IPV4", sizeof("IPV4")); + param->autoconfig = true; + memcpy(¶m->ipaddress, "192.168.1.101", sizeof("192.168.1.101")); + memcpy(¶m->netmask, "255.255.255.0", sizeof("255.255.255.0")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->primarydns, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->secondarydns, "192.168.1.2", sizeof("192.168.1.2")); + + EXPECT_EQ(string(param->interface), string(_T("ETHERNET"))); + EXPECT_EQ(string(param->ipversion), string(_T("IPV4"))); + EXPECT_EQ(string(param->ipaddress), string(_T("192.168.1.101"))); + EXPECT_EQ(string(param->netmask), string(_T("255.255.255.0"))); + EXPECT_EQ(string(param->gateway), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->primarydns), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->secondarydns), string(_T("192.168.1.2"))); + EXPECT_EQ(param->autoconfig, true); + + return IARM_RESULT_IPCCORE_FAIL; }); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setDefaultInterface"), _T("{\"interface\": \"WIFI\", \"persist\": true}"), response)); - EXPECT_EQ(response, string("{\"success\":true}")); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("getIPSettings"), _T("{\"interface\": \"ETHERNET\",\"ipversion\": \"IPV4\"}"), response)); } -TEST_F(NetworkTest, setIPSettings) +TEST_F(NetworkTest, getIPSettings_WrongIface) { EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) .Times(::testing::AnyNumber()) .WillRepeatedly( [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setIPSettings))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getIPSettings))); auto param = static_cast(arg); - param->isSupported = true; - return IARM_RESULT_SUCCESS; - }); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setIPSettings"), _T("{\"interface\": \"WIFI\", \"ipversion\": \"IPV4\",\"autoconfig\": true,\"ipaddr\": \"192.168.1.101\",\"netmask\": \"255.255.255.0\",\"gateway\": \"192.168.1.1\",\"primarydns\": \"192.168.1.1\",\"secondarydns\": \"192.168.1.2\"}"), response)); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"supported\":true"))); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); -} - -//TEST_F(NetworkTest, trace) + memcpy(¶m->interface, "ETHERNET", sizeof("ETHERNET")); + memcpy(¶m->ipversion, "IPV4", sizeof("IPV4")); + param->autoconfig = true; + memcpy(¶m->ipaddress, "192.168.1.101", sizeof("192.168.1.101")); + memcpy(¶m->netmask, "255.255.255.0", sizeof("255.255.255.0")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->primarydns, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->secondarydns, "192.168.1.2", sizeof("192.168.1.2")); + + EXPECT_EQ(string(param->interface), string(_T("ETHERNET"))); + EXPECT_EQ(string(param->ipversion), string(_T("IPV4"))); + EXPECT_EQ(string(param->ipaddress), string(_T("192.168.1.101"))); + EXPECT_EQ(string(param->netmask), string(_T("255.255.255.0"))); + EXPECT_EQ(string(param->gateway), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->primarydns), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->secondarydns), string(_T("192.168.1.2"))); + EXPECT_EQ(param->autoconfig, true); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("getIPSettings"), _T("{\"interface\": \"TEST\",\"ipversion\": \"IPV4\"}"), response)); +} + +TEST_F(NetworkTest, getPublicIP) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getPublicIP))); + auto param = static_cast(arg); + memcpy(¶m->public_ip, "69.136.49.95", sizeof("69.136.49.95")); + + EXPECT_EQ(string(param->public_ip), string(_T("69.136.49.95"))); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getPublicIP"), _T("{\"iface\": \"WIFI\", \"ipv6\": false}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"public_ip\":\"69.136.49.95\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); +} + +TEST_F(NetworkTest, setInterfaceEnabled) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setInterfaceEnabled))); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setInterfaceEnabled"), _T("{\"interface\": \"WIFI\", \"enabled\": true, \"persist\": true}"), response)); + EXPECT_EQ(response, string("{\"success\":true}")); +} + +TEST_F(NetworkTest, setInterfaceEnabled_Failed) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setInterfaceEnabled))); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("setInterfaceEnabled"), _T("{\"interface\": \"WIFI\", \"enabled\": true, \"persist\": true}"), response)); +} + +TEST_F(NetworkTest, setInterfaceEnabled_WrongIface) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setInterfaceEnabled))); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("setInterfaceEnabled"), _T("{\"interface\": \"TEST\", \"enabled\": true, \"persist\": true}"), response)); +} + +TEST_F(NetworkTest, getSTBIPFamily) +{ + + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getIPSettings))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "ETHERNET", sizeof("ETHERNET")); + memcpy(¶m->ipversion, "IPV4", sizeof("IPV4")); + param->autoconfig = true; + memcpy(¶m->ipaddress, "192.168.1.101", sizeof("192.168.1.101")); + memcpy(¶m->netmask, "255.255.255.0", sizeof("255.255.255.0")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->primarydns, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->secondarydns, "192.168.1.2", sizeof("192.168.1.2")); + + EXPECT_EQ(string(param->interface), string(_T("ETHERNET"))); + EXPECT_EQ(string(param->ipversion), string(_T("IPV4"))); + EXPECT_EQ(string(param->ipaddress), string(_T("192.168.1.101"))); + EXPECT_EQ(string(param->netmask), string(_T("255.255.255.0"))); + EXPECT_EQ(string(param->gateway), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->primarydns), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->secondarydns), string(_T("192.168.1.2"))); + EXPECT_EQ(param->autoconfig, true); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getIPSettings"), _T("{\"interface\": \"ETHERNET\",\"ipversion\": \"IPV4\"}"), response)); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getSTBIPFamily"), _T("{\"family\": \"AF_INET\"}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ip\":\"192.168.1.101\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); +} + +TEST_F(NetworkTest, getSTBIPFamily_Error) +{ + + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getIPSettings))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "ETHERNET", sizeof("ETHERNET")); + memcpy(¶m->ipversion, "IPV4", sizeof("IPV4")); + param->autoconfig = true; + memcpy(¶m->ipaddress, "192.168.1.101", sizeof("192.168.1.101")); + memcpy(¶m->netmask, "255.255.255.0", sizeof("255.255.255.0")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->primarydns, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->secondarydns, "192.168.1.2", sizeof("192.168.1.2")); + + EXPECT_EQ(string(param->interface), string(_T("ETHERNET"))); + EXPECT_EQ(string(param->ipversion), string(_T("IPV4"))); + EXPECT_EQ(string(param->ipaddress), string(_T("192.168.1.101"))); + EXPECT_EQ(string(param->netmask), string(_T("255.255.255.0"))); + EXPECT_EQ(string(param->gateway), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->primarydns), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->secondarydns), string(_T("192.168.1.2"))); + EXPECT_EQ(param->autoconfig, true); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getIPSettings"), _T("{\"interface\": \"ETHERNET\",\"ipversion\": \"IPV4\"}"), response)); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("getSTBIPFamily"), _T("{\"test\": \"AF_INET\"}"), response)); +} + +TEST_F(NetworkTest, getSTBIPFamily_Failed) +{ + + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getIPSettings))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "ETHERNET", sizeof("ETHERNET")); + memcpy(¶m->ipversion, "IPV4", sizeof("IPV4")); + param->autoconfig = true; + memcpy(¶m->ipaddress, "192.168.1.101", sizeof("192.168.1.101")); + memcpy(¶m->netmask, "255.255.255.0", sizeof("255.255.255.0")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->primarydns, "192.168.1.1", sizeof("192.168.1.1")); + memcpy(¶m->secondarydns, "192.168.1.2", sizeof("192.168.1.2")); + + EXPECT_EQ(string(param->interface), string(_T("ETHERNET"))); + EXPECT_EQ(string(param->ipversion), string(_T("IPV4"))); + EXPECT_EQ(string(param->ipaddress), string(_T("192.168.1.101"))); + EXPECT_EQ(string(param->netmask), string(_T("255.255.255.0"))); + EXPECT_EQ(string(param->gateway), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->primarydns), string(_T("192.168.1.1"))); + EXPECT_EQ(string(param->secondarydns), string(_T("192.168.1.2"))); + EXPECT_EQ(param->autoconfig, true); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("getIPSettings"), _T("{\"interface\": \"ETHERNET\",\"ipversion\": \"IPV4\"}"), response)); +} + +TEST_F(NetworkTest, setDefaultInterface) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setDefaultInterface))); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setDefaultInterface"), _T("{\"interface\": \"WIFI\", \"persist\": true}"), response)); + EXPECT_EQ(response, string("{\"success\":true}")); +} + +TEST_F(NetworkTest, setDefaultInterface_WrongIface) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setDefaultInterface))); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("setDefaultInterface"), _T("{\"interface\": \"TEST\", \"persist\": true}"), response)); +} + +TEST_F(NetworkTest, setDefaultInterface_failed) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setDefaultInterface))); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("setDefaultInterface"), _T("{\"interface\": \"TEST\", \"persist\": true}"), response)); +} + +TEST_F(NetworkTest, setIPSettings) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setIPSettings))); + + auto param = static_cast(arg); + param->isSupported = true; + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setIPSettings"), _T("{\"interface\": \"WIFI\", \"ipversion\": \"IPV4\",\"autoconfig\": true,\"ipaddr\": \"192.168.1.101\",\"netmask\": \"255.255.255.0\",\"gateway\": \"192.168.1.1\",\"primarydns\": \"192.168.1.1\",\"secondarydns\": \"192.168.1.2\"}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"supported\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); +} + + + +//TEST_F(NetworkTest, trace) //{ // EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) // .Times(::testing::AnyNumber()) @@ -382,6 +885,42 @@ TEST_F(NetworkTest, setIPSettings) // EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); //} // +TEST_F(NetworkTest, trace_fail) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getDefaultInterface))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "eth0", sizeof("eth0")); + memcpy(¶m->gateway, "45.57.221.20", sizeof("45.57.221.20")); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("trace"), _T("{\"endpoint\":\"45.57.221.20\", \"packets\":5}"), response)); +} + +TEST_F(NetworkTest, trace_noiface) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getDefaultInterface))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "eth0", sizeof("eth0")); + memcpy(¶m->gateway, "45.57.221.20", sizeof("45.57.221.20")); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("trace"), _T("{\"test\":\"45.57.221.20\", \"packets\":5}"), response)); +} + //TEST_F(NetworkTest, traceNamedEndpoint) //{ // EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) @@ -402,6 +941,43 @@ TEST_F(NetworkTest, setIPSettings) // EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); //} +TEST_F(NetworkTest, traceNamedEndpoint_noendoint) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getDefaultInterface))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "eth0", sizeof("eth0")); + memcpy(¶m->gateway, "45.57.221.20", sizeof("45.57.221.20")); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getDefaultInterface"), _T("{}"), response)); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("traceNamedEndpoint"), _T("{\"test\": \"CMTS\", \"packets\": 5}"), response)); +} + +TEST_F(NetworkTest, traceNamedEndpoint_fail) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getDefaultInterface))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "eth0", sizeof("eth0")); + memcpy(¶m->gateway, "45.57.221.20", sizeof("45.57.221.20")); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("traceNamedEndpoint"), _T("{\"endpointName\": \"CMTS\", \"packets\": 5}"), response)); +} + TEST_F(NetworkTest, getNamedEndpoints) { EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getNamedEndpoints"), _T("{}"), response)); @@ -431,7 +1007,44 @@ TEST_F(NetworkTest, getNamedEndpoints) // EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"packetsReceived\":5"))); // EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"packetLoss\":\" 0\""))); //} -// + +TEST_F(NetworkTest, pingNamedEndpoint_noarg) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getDefaultInterface))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "eth0", sizeof("eth0")); + memcpy(¶m->gateway, "127.0.0.1", sizeof("127.0.0.1")); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getDefaultInterface"), _T("{}"), response)); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("pingNamedEndpoint"), _T("{\"test\": \"CMTS\", \"packets\": 5, \"guid\": \"...\"}"), response)); +} + +TEST_F(NetworkTest, pingNamedEndpoint_fail) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getDefaultInterface))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "eth0", sizeof("eth0")); + memcpy(¶m->gateway, "127.0.0.1", sizeof("127.0.0.1")); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("pingNamedEndpoint"), _T("{\"endpointName\": \"CMTS\", \"packets\": 5, \"guid\": \"...\"}"), response)); +} + //TEST_F(NetworkTest, ping) //{ // EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) @@ -456,10 +1069,65 @@ TEST_F(NetworkTest, getNamedEndpoints) // EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"packetLoss\":\" 0\""))); //} +TEST_F(NetworkTest, ping_noendpoint) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getDefaultInterface))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "eth0", sizeof("eth0")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getDefaultInterface"), _T("{}"), response)); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("ping"), _T("{\"test\": \"127.0.0.1\", \"packets\": 5, \"guid\": \"...\"}"), response)); +} + +TEST_F(NetworkTest, ping_fail) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getDefaultInterface))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "eth0", sizeof("eth0")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("ping"), _T("{\"endpoint\": \"127.0.0.1\", \"packets\": 5, \"guid\": \"...\"}"), response)); +} + +TEST_F(NetworkTest, ping_fail1) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getDefaultInterface))); + + auto param = static_cast(arg); + memcpy(¶m->interface, "eth0", sizeof("eth0")); + memcpy(¶m->gateway, "192.168.1.1", sizeof("192.168.1.1")); + + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("ping"), _T("{\"test\": \"127.0.0.1\", \"packets\": 5, \"guid\": \"...\"}"), response)); +} + TEST_F(NetworkTest, getQuirks) { EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getQuirks"), _T("{}"), response)); - EXPECT_EQ(response, string("{\"quirks\":[\"RDK-20093\"],\"success\":true}")); + EXPECT_EQ(response, string("{\"quirks\":[\"RDK-20093\"],\"success\":true}")); } /* TEST_F(NetworkTest, getInternetConnectionState) @@ -468,6 +1136,14 @@ TEST_F(NetworkTest, getInternetConnectionState) EXPECT_EQ(response, string("{\"state\":0,\"ipversion\":\"IPV6\",\"success\":true}")); } +TEST_F(NetworkTest, getInternetConnectionState_cache) +{ + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getInternetConnectionState"), _T("{\"ipversion\": \"IPV6\"}"), response)); + EXPECT_EQ(response, string("{\"state\":0,\"ipversion\":\"IPV6\",\"success\":true}")); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getInternetConnectionState"), _T("{\"ipversion\": \"IPV6\"}"), response)); + EXPECT_EQ(response, string("{\"state\":0,\"ipversion\":\"IPV6\",\"success\":true}")); +} + TEST_F(NetworkTest, isConnectedToInternet) { EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isConnectedToInternet"), _T("{\"ipversion\": \"IPV6\"}"), response)); @@ -477,7 +1153,24 @@ TEST_F(NetworkTest, isConnectedToInternet) EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isConnectedToInternet"), _T("{}"), response)); EXPECT_EQ(response, string("{\"connectedToInternet\":false,\"success\":true}")); } + +TEST_F(NetworkTest, isConnectedToInternet_cache) +{ + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isConnectedToInternet"), _T("{\"ipversion\": \"IPV6\"}"), response)); + EXPECT_EQ(response, string("{\"connectedToInternet\":false,\"ipversion\":\"IPV6\",\"success\":true}")); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isConnectedToInternet"), _T("{\"ipversion\": \"IPV6\"}"), response)); + EXPECT_EQ(response, string("{\"connectedToInternet\":false,\"ipversion\":\"IPV6\",\"success\":true}")); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isConnectedToInternet"), _T("{\"ipversion\": \"IPV4\"}"), response)); + EXPECT_EQ(response, string("{\"connectedToInternet\":false,\"ipversion\":\"IPV4\",\"success\":true}")); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isConnectedToInternet"), _T("{\"ipversion\": \"IPV4\"}"), response)); + EXPECT_EQ(response, string("{\"connectedToInternet\":false,\"ipversion\":\"IPV4\",\"success\":true}")); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isConnectedToInternet"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"connectedToInternet\":false,\"success\":true}")); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isConnectedToInternet"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"connectedToInternet\":false,\"success\":true}")); +} */ + TEST_F(NetworkTest, stopConnectivityMonitoring) { EXPECT_NE(Core::ERROR_NONE, handler.Invoke(connection, _T("stopConnectivityMonitoring"), _T("{}"), response)); @@ -502,4 +1195,157 @@ TEST_F(NetworkTest, setConnectivityTestEndpoints) { EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setConnectivityTestEndpoints"), _T("{\"endpoints\": [\"http://clients3.google.com/generate_204\"]}"), response)); EXPECT_EQ(response, string("{\"success\":true}")); -} \ No newline at end of file +} + +TEST_F(NetworkTest, setStunEndPoint) +{ + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setStunEndPoint"), _T("{}"), response)); + //EXPECT_EQ(response, string("{\"quirks\":[\"RDK-20093\"],\"success\":true}")); +} + +TEST_F(NetworkTest, configurePNI) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_configurePNI))); + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("configurePNI"), _T("{}"), response)); +} + +TEST_F(NetworkTest, configurePNI_fail) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_configurePNI))); + return IARM_RESULT_IPCCORE_FAIL; + }); + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("configurePNI"), _T("{}"), response)); +} + +TEST_F(NetworkInitializedEventTest, onInterfaceStatusChanged) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_getInterfaceList))); + auto param = static_cast(arg); + + param->size = 1; + memcpy(¶m->interfaces[0].name, "eth0", sizeof("eth0")); + memcpy(¶m->interfaces[0].mac, "AA:AA:AA:AA:AA:AA", sizeof("AA:AA:AA:AA:AA:AA")); + param->interfaces[0].flags = 69699; + + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getInterfaces"), _T("{}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"interface\":\"ETHERNET\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"macAddress\":\"AA:AA:AA:AA:AA:AA\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"enabled\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"connected\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); + EXPECT_CALL(service, Submit(::testing::_, ::testing::_)) + .Times(1) + .WillOnce(::testing::Invoke( + [&](const uint32_t, const Core::ProxyType& json) { + string text; + EXPECT_TRUE(json->ToString(text)); + EXPECT_EQ(text, string(_T("{\"jsonrpc\":\"2.0\",\"method\":\"org.rdk.Network.onInterfaceStatusChanged\",\"params\":{\"interface\":\"ETHERNET\",\"enabled\":true}}"))); + return Core::ERROR_NONE; + })); + IARM_BUS_NetSrvMgr_Iface_EventInterfaceEnabledStatus_t intData; + intData.status = 1; + strcpy(intData.interface,"eth0"); + handler.Subscribe(0, _T("onInterfaceStatusChanged"), _T("org.rdk.Network"), message); + plugin->eventHandler("NET_SRV_MGR", IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_ENABLED_STATUS, static_cast(&intData), sizeof(intData)); + handler.Unsubscribe(0, _T("onInterfaceStatusChanged"), _T("org.rdk.Network"), message); +} + +TEST_F(NetworkInitializedEventTest, onConnectionStatusChanged) +{ + EXPECT_CALL(service, Submit(::testing::_, ::testing::_)) + .Times(1) + .WillOnce(::testing::Invoke( + [&](const uint32_t, const Core::ProxyType& json) { + string text; + EXPECT_TRUE(json->ToString(text)); + EXPECT_EQ(text, string(_T("{\"jsonrpc\":\"2.0\",\"method\":\"org.rdk.Network.onConnectionStatusChanged\",\"params\":{\"interface\":\"ETHERNET\",\"status\":\"CONNECTED\"}}"))); + return Core::ERROR_NONE; + })); + IARM_BUS_NetSrvMgr_Iface_EventInterfaceConnectionStatus_t intData; + intData.status = 1; + strcpy(intData.interface,"eth0"); + handler.Subscribe(0, _T("onConnectionStatusChanged"), _T("org.rdk.Network"), message); + plugin->eventHandler("NET_SRV_MGR", IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_CONNECTION_STATUS, static_cast(&intData), sizeof(intData)); + handler.Unsubscribe(0, _T("onConnectionStatusChanged"), _T("org.rdk.Network"), message); +} + +TEST_F(NetworkInitializedEventTest, onIPAddressStatusChanged) +{ + EXPECT_CALL(service, Submit(::testing::_, ::testing::_)) + .Times(1) + .WillOnce(::testing::Invoke( + [&](const uint32_t, const Core::ProxyType& json) { + string text; + EXPECT_TRUE(json->ToString(text)); + EXPECT_EQ(text, string(_T("{\"jsonrpc\":\"2.0\",\"method\":\"org.rdk.Network.onIPAddressStatusChanged\",\"params\":{\"interface\":\"ETHERNET\",\"ip4Address\":\"192.168.1.10\",\"status\":\"ACQUIRED\"}}"))); + return Core::ERROR_NONE; + })); + IARM_BUS_NetSrvMgr_Iface_EventInterfaceIPAddress_t intData; + intData.is_ipv6 = 0; + strcpy(intData.interface,"eth0"); + intData.acquired = 1; + strcpy(intData.ip_address,"192.168.1.10"); + handler.Subscribe(0, _T("onIPAddressStatusChanged"), _T("org.rdk.Network"), message); + plugin->eventHandler("NET_SRV_MGR", IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_IPADDRESS, static_cast(&intData), sizeof(intData)); + handler.Unsubscribe(0, _T("onIPAddressStatusChanged"), _T("org.rdk.Network"), message); +} + +TEST_F(NetworkInitializedEventTest, onDefaultInterfaceChanged) +{ + /*EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getInterfaces"), _T("{}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"interface\":\"ETHERNET\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"macAddress\":\"AA:AA:AA:AA:AA:AA\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"enabled\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"connected\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true")));*/ + EXPECT_CALL(service, Submit(::testing::_, ::testing::_)) + .Times(1) + .WillOnce(::testing::Invoke( + [&](const uint32_t, const Core::ProxyType& json) { + string text; + EXPECT_TRUE(json->ToString(text)); + EXPECT_EQ(text, string(_T("{\"jsonrpc\":\"2.0\",\"method\":\"org.rdk.Network.onDefaultInterfaceChanged\",\"params\":{\"oldInterfaceName\":\"\",\"newInterfaceName\":\"ETHERNET\"}}"))); + return Core::ERROR_NONE; + })); + IARM_BUS_NetSrvMgr_Iface_EventDefaultInterface_t intData; + strcpy(intData.newInterface,"eth0"); + handler.Subscribe(0, _T("onDefaultInterfaceChanged"), _T("org.rdk.Network"), message); + plugin->eventHandler("NET_SRV_MGR", IARM_BUS_NETWORK_MANAGER_EVENT_DEFAULT_INTERFACE, static_cast(&intData), sizeof(intData)); + handler.Unsubscribe(0, _T("onDefaultInterfaceChanged"), _T("org.rdk.Network"), message); +} + +TEST_F(NetworkInitializedEventTest, onInternetStatusChange) +{ + EXPECT_CALL(service, Submit(::testing::_, ::testing::_)) + .Times(1) + .WillOnce(::testing::Invoke( + [&](const uint32_t, const Core::ProxyType& json) { + string text; + EXPECT_TRUE(json->ToString(text)); + EXPECT_EQ(text, string(_T("{\"jsonrpc\":\"2.0\",\"method\":\"org.rdk.Network.onInternetStatusChange\",\"params\":{\"state\":0,\"status\":\"NO_INTERNET\"}}"))); + return Core::ERROR_NONE; + })); + nsm_internetState intData = NO_INTERNET; + handler.Subscribe(0, _T("onInternetStatusChange"), _T("org.rdk.Network"), message); + plugin->notifyInternetStatusChange(intData); + handler.Unsubscribe(0, _T("onInternetStatusChange"), _T("org.rdk.Network"), message); +} diff --git a/Tests/tests/test_WifiManager.cpp b/Tests/tests/test_WifiManager.cpp index e047ec3061..f0b3dd9023 100644 --- a/Tests/tests/test_WifiManager.cpp +++ b/Tests/tests/test_WifiManager.cpp @@ -1,312 +1,576 @@ -#include -#include -#include "WifiManager.h" - -#include "ServiceMock.h" -#include "IarmBusMock.h" -#include "WrapsMock.h" - -using namespace WPEFramework; - -using ::testing::NiceMock; - -class WifiManagerTest : public ::testing::Test { -protected: - Core::ProxyType plugin; - Core::JSONRPC::Handler& handler; - Core::JSONRPC::Connection connection; - string response; - Core::JSONRPC::Message message; - WrapsImplMock wrapsImplMock; - NiceMock iarmBusImplMock; - - WifiManagerTest() - : plugin(Core::ProxyType::Create()) - , handler(*(plugin)) - , connection(1, 0) - { - Wraps::getInstance().impl = &wrapsImplMock; - IarmBus::getInstance().impl = &iarmBusImplMock; - } - virtual ~WifiManagerTest() override - { - Wraps::getInstance().impl = nullptr; - IarmBus::getInstance().impl = nullptr; - } -}; - -TEST_F(WifiManagerTest, TestedAPIsShouldExist) -{ - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("startScan"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("stopScan"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("setEnabled"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("connect"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("disconnect"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("cancelWPSPairing"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("saveSSID"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("clearSSID"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("setSignalThresholdChangeEnabled"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getPairedSSID"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getPairedSSIDInfo"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("isPaired"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getCurrentState"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getConnectedSSID"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getQuirks"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("isSignalThresholdChangeEnabled"))); - EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getSupportedSecurityModes"))); -} - - -TEST_F(WifiManagerTest, setEnabled) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setInterfaceEnabled))); - auto param = static_cast(arg); - EXPECT_EQ(param->isInterfaceEnabled, true); - return IARM_RESULT_SUCCESS; - }); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setEnabled"), _T("{\"enable\":true}"), response)); - EXPECT_EQ(response, string("{\"success\":true}")); -} - -TEST_F(WifiManagerTest, getCurrentState) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getCurrentState))); - auto param = static_cast(arg); - param->data.wifiStatus = WIFI_DISCONNECTED; - return IARM_RESULT_SUCCESS; - }); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getCurrentState"), _T("{}"), response)); - EXPECT_EQ(response, string("{\"state\":2,\"success\":true}")); // WIFI_PAIRING enum 3 -} - -TEST_F(WifiManagerTest, getPairedSSID) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getPairedSSIDInfo))); - auto param = static_cast(arg); - memcpy(¶m->data.getPairedSSID, "rdk.test.wifi.network", sizeof("rdk.test.wifi.network")); - return IARM_RESULT_SUCCESS; - }); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getPairedSSID"), _T("{}"), response)); - EXPECT_EQ(response, string("{\"ssid\":\"rdk.test.wifi.network\",\"success\":true}")); -} - -TEST_F(WifiManagerTest, getPairedSSIDInfo) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getPairedSSIDInfo))); - auto param = static_cast(arg); - memcpy(¶m->data.getPairedSSIDInfo.ssid, "rdk.test.wifi.network", sizeof("rdk.test.wifi.network")); - memcpy(¶m->data.getPairedSSIDInfo.bssid, "ff:ff:ff:ff:ff:ff", sizeof("ff:ff:ff:ff:ff:ff")); - return IARM_RESULT_SUCCESS; - }); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getPairedSSIDInfo"), _T("{}"), response)); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ssid\":\"rdk.test.wifi.network\""))); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"bssid\":\"ff:ff:ff:ff:ff:ff\""))); -} - -TEST_F(WifiManagerTest, getConnectedSSID) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getConnectedSSID))); - auto param = static_cast(arg); - memcpy(¶m->data.getConnectedSSID.ssid, "rdk.test.wifi.network", sizeof("rdk.test.wifi.network")); - memcpy(¶m->data.getConnectedSSID.bssid, "ff:ff:ff:ff:ff:ff", sizeof("ff:ff:ff:ff:ff:ff")); - param->data.getConnectedSSID.rate = 130.000; - param->data.getConnectedSSID.noise = -88.000; - param->data.getConnectedSSID.securityMode = NET_WIFI_SECURITY_WPA2_PSK_AES; - param->data.getConnectedSSID.signalStrength = -33.00; - param->data.getConnectedSSID.frequency = 2437.00; - return IARM_RESULT_SUCCESS; - }); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getConnectedSSID"), _T("{}"), response)); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ssid\":\"rdk.test.wifi.network\""))); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"bssid\":\"ff:ff:ff:ff:ff:ff\""))); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"rate\":\"130.000000\""))); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"noise\":\"-88.000000\""))); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"frequency\":\"2.437000\""))); -} - -TEST_F(WifiManagerTest, connect) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_connect))); - auto param = static_cast(arg); - param->status = true; - EXPECT_EQ(string(param->data.connect.ssid), string(_T("123412341234"))); - EXPECT_EQ(string(param->data.connect.passphrase), string(_T("password"))); - EXPECT_EQ(param->data.connect.security_mode, NET_WIFI_SECURITY_WEP_128); //securityMode: 2 (NET_WIFI_SECURITY_WEP_128) - return IARM_RESULT_SUCCESS; - }); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("connect"), _T("{\"ssid\": \"123412341234\",\"passphrase\": \"password\",\"securityMode\": 2}"), response)); - EXPECT_EQ(response, string("{\"success\":true}")); -} - -TEST_F(WifiManagerTest, disconnect) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_disconnectSSID))); - auto param = static_cast(arg); - param->status = true; - return IARM_RESULT_SUCCESS; - }); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("disconnect"), _T("{}"), response)); - EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); -} - -TEST_F(WifiManagerTest, saveSSID) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_saveSSID))); - auto param = static_cast(arg); - param->status = true; - EXPECT_EQ(string(param->data.connect.ssid), string(_T("123412341234"))); - EXPECT_EQ(string(param->data.connect.passphrase), string(_T("password"))); - EXPECT_EQ(param->data.connect.security_mode, NET_WIFI_SECURITY_WEP_128); //securityMode: 2 (NET_WIFI_SECURITY_WEP_128) - return IARM_RESULT_SUCCESS; - }); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("saveSSID"), _T("{\"ssid\": \"123412341234\",\"passphrase\": \"password\",\"securityMode\": 2}"), response)); - EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); -} - -TEST_F(WifiManagerTest, clearSSID) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_clearSSID))); - auto param = static_cast(arg); - param->status = true; - return IARM_RESULT_SUCCESS; - }); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("clearSSID"), _T("{}"), response)); - EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); -} - -TEST_F(WifiManagerTest, isPaired) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getPairedSSIDInfo))); - auto param = static_cast(arg); - memcpy(¶m->data.getPairedSSIDInfo.ssid, "123412341234", sizeof("123412341234")); - return IARM_RESULT_SUCCESS; - }); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isPaired"), _T("{}"), response)); - EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); -} - -TEST_F(WifiManagerTest, startScan) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getAvailableSSIDsAsync))); - return IARM_RESULT_SUCCESS; - }); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("startScan"), _T("{\"incremental\": false,\"ssid\": \"...\",\"frequency\": \"...\"}"), response)); - EXPECT_EQ(response, string("{\"success\":true}")); - - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getAvailableSSIDsAsyncIncr))); - return IARM_RESULT_SUCCESS; - }); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("startScan"), _T("{\"incremental\": true,\"ssid\": \"...\",\"frequency\": \"...\"}"), response)); - EXPECT_EQ(response, string("{\"success\":true}")); -} - -TEST_F(WifiManagerTest, stopScan) -{ - EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) - .Times(::testing::AnyNumber()) - .WillRepeatedly( - [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { - EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); - EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_stopProgressiveWifiScanning))); - return IARM_RESULT_SUCCESS; - }); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("stopScan"), _T("{}"), response)); - EXPECT_EQ(response, string("{\"success\":true}")); -} - -TEST_F(WifiManagerTest, SignalThresholdChangeEnabled) -{ - ServiceMock service; - EXPECT_EQ(string(""), plugin->Initialize(&service)); - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isSignalThresholdChangeEnabled"), _T("{}"), response)); - EXPECT_EQ(response, string("{\"result\":1,\"success\":true}")); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setSignalThresholdChangeEnabled"), _T("{\"enabled\": true, \"interval\":2000}"), response)); - EXPECT_EQ(response, string("{\"success\":true}")); - - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isSignalThresholdChangeEnabled"), _T("{}"), response)); - EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); - plugin->Deinitialize(&service); -} - -TEST_F(WifiManagerTest, getSupportedSecurityModes) -{ - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getSupportedSecurityModes"), _T("{}"), response)); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); -} - -TEST_F(WifiManagerTest, getQuirks) -{ - EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getQuirks"), _T("{}"), response)); - EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); -} +#include +#include +#include "WifiManager.h" + +#include "ServiceMock.h" +#include "IarmBusMock.h" +#include "WrapsMock.h" +#include "FactoriesImplementation.h" +#include "WifiManagerDefines.h" + +using namespace WPEFramework; + +using ::testing::NiceMock; + +class WifiManagerTest : public ::testing::Test { +protected: + Core::ProxyType plugin; + Core::JSONRPC::Handler& handler; + Core::JSONRPC::Connection connection; + string response; + Core::JSONRPC::Message message; + WrapsImplMock wrapsImplMock; + NiceMock iarmBusImplMock; + ServiceMock services; + + WifiManagerTest() + : plugin(Core::ProxyType::Create()) + , handler(*(plugin)) + , connection(1, 0) + { + Wraps::getInstance().impl = &wrapsImplMock; + IarmBus::getInstance().impl = &iarmBusImplMock; + } + virtual ~WifiManagerTest() override + { + Wraps::getInstance().impl = nullptr; + IarmBus::getInstance().impl = nullptr; + } +}; + +class WifiInitializedEventTest : public WifiManagerTest { +protected: + FactoriesImplementation factoriesImplementation; + PluginHost::IDispatcher* dispatcher; + + + WifiInitializedEventTest() + : WifiManagerTest() + { + PluginHost::IFactories::Assign(&factoriesImplementation); + + dispatcher = static_cast( + plugin->QueryInterface(PluginHost::IDispatcher::ID)); + dispatcher->Activate(&services); + } + + virtual ~WifiInitializedEventTest() override + { + dispatcher->Deactivate(); + dispatcher->Release(); + PluginHost::IFactories::Assign(nullptr); + + } +}; + + +TEST_F(WifiManagerTest, TestedAPIsShouldExist) +{ + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("startScan"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("stopScan"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("setEnabled"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("connect"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("disconnect"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("cancelWPSPairing"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("saveSSID"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("clearSSID"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("setSignalThresholdChangeEnabled"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getPairedSSID"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getPairedSSIDInfo"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("isPaired"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getCurrentState"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getConnectedSSID"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getQuirks"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("isSignalThresholdChangeEnabled"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getSupportedSecurityModes"))); +} + + +TEST_F(WifiManagerTest, setEnabled) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_NETSRVMGR_API_setInterfaceEnabled))); + auto param = static_cast(arg); + EXPECT_EQ(param->isInterfaceEnabled, true); + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setEnabled"), _T("{\"enable\":true}"), response)); + EXPECT_EQ(response, string("{\"success\":true}")); +} + +TEST_F(WifiManagerTest, getCurrentState) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getCurrentState))); + auto param = static_cast(arg); + param->data.wifiStatus = WIFI_DISCONNECTED; + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getCurrentState"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"state\":2,\"success\":true}")); // WIFI_PAIRING enum 3 +} + +TEST_F(WifiManagerTest, getPairedSSID) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getPairedSSIDInfo))); + auto param = static_cast(arg); + memcpy(¶m->data.getPairedSSID, "rdk.test.wifi.network", sizeof("rdk.test.wifi.network")); + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getPairedSSID"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"ssid\":\"rdk.test.wifi.network\",\"success\":true}")); +} + +TEST_F(WifiManagerTest, getPairedSSIDi_cache) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getPairedSSIDInfo))); + auto param = static_cast(arg); + memcpy(¶m->data.getPairedSSID, "rdk.test.wifi.network", sizeof("rdk.test.wifi.network")); + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getPairedSSID"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"ssid\":\"rdk.test.wifi.network\",\"success\":true}")); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getPairedSSID"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"ssid\":\"rdk.test.wifi.network\",\"success\":true}")); +} + +TEST_F(WifiManagerTest, getPairedSSID_failed) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getPairedSSIDInfo))); + auto param = static_cast(arg); + memcpy(¶m->data.getPairedSSID, "rdk.test.wifi.network", sizeof("rdk.test.wifi.network")); + return IARM_RESULT_IPCCORE_FAIL; + }); + + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("getPairedSSID"), _T("{}"), response)); +} + +TEST_F(WifiManagerTest, getPairedSSIDInfo) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getPairedSSIDInfo))); + auto param = static_cast(arg); + memcpy(¶m->data.getPairedSSIDInfo.ssid, "rdk.test.wifi.network", sizeof("rdk.test.wifi.network")); + memcpy(¶m->data.getPairedSSIDInfo.bssid, "ff:ff:ff:ff:ff:ff", sizeof("ff:ff:ff:ff:ff:ff")); + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getPairedSSIDInfo"), _T("{}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ssid\":\"rdk.test.wifi.network\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"bssid\":\"ff:ff:ff:ff:ff:ff\""))); +} + +TEST_F(WifiManagerTest, getPairedSSIDInfo_cache) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getPairedSSIDInfo))); + auto param = static_cast(arg); + memcpy(¶m->data.getPairedSSIDInfo.ssid, "rdk.test.wifi.network", sizeof("rdk.test.wifi.network")); + memcpy(¶m->data.getPairedSSIDInfo.bssid, "ff:ff:ff:ff:ff:ff", sizeof("ff:ff:ff:ff:ff:ff")); + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getPairedSSIDInfo"), _T("{}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ssid\":\"rdk.test.wifi.network\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"bssid\":\"ff:ff:ff:ff:ff:ff\""))); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getPairedSSIDInfo"), _T("{}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ssid\":\"rdk.test.wifi.network\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"bssid\":\"ff:ff:ff:ff:ff:ff\""))); +} + +TEST_F(WifiManagerTest, getPairedSSIDInfo_fail) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getPairedSSIDInfo))); + auto param = static_cast(arg); + memcpy(¶m->data.getPairedSSIDInfo.ssid, "rdk.test.wifi.network", sizeof("rdk.test.wifi.network")); + memcpy(¶m->data.getPairedSSIDInfo.bssid, "ff:ff:ff:ff:ff:ff", sizeof("ff:ff:ff:ff:ff:ff")); + return IARM_RESULT_IPCCORE_FAIL; + }); + + EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("getPairedSSIDInfo"), _T("{}"), response)); +} + + +TEST_F(WifiManagerTest, getConnectedSSID) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getConnectedSSID))); + auto param = static_cast(arg); + memcpy(¶m->data.getConnectedSSID.ssid, "rdk.test.wifi.network", sizeof("rdk.test.wifi.network")); + memcpy(¶m->data.getConnectedSSID.bssid, "ff:ff:ff:ff:ff:ff", sizeof("ff:ff:ff:ff:ff:ff")); + param->data.getConnectedSSID.rate = 130.000; + param->data.getConnectedSSID.noise = -88.000; + param->data.getConnectedSSID.securityMode = NET_WIFI_SECURITY_WPA2_PSK_AES; + param->data.getConnectedSSID.signalStrength = -33.00; + param->data.getConnectedSSID.frequency = 2437.00; + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getConnectedSSID"), _T("{}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"ssid\":\"rdk.test.wifi.network\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"bssid\":\"ff:ff:ff:ff:ff:ff\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"rate\":\"130.000000\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"noise\":\"-88.000000\""))); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"frequency\":\"2.437000\""))); +} + +TEST_F(WifiManagerTest, connect) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_connect))); + auto param = static_cast(arg); + param->status = true; + EXPECT_EQ(string(param->data.connect.ssid), string(_T("123412341234"))); + EXPECT_EQ(string(param->data.connect.passphrase), string(_T("password"))); + EXPECT_EQ(param->data.connect.security_mode, NET_WIFI_SECURITY_WEP_128); //securityMode: 2 (NET_WIFI_SECURITY_WEP_128) + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("connect"), _T("{\"ssid\": \"123412341234\",\"passphrase\": \"password\",\"securityMode\": 2}"), response)); + EXPECT_EQ(response, string("{\"success\":true}")); +} + +TEST_F(WifiManagerTest, disconnect) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_disconnectSSID))); + auto param = static_cast(arg); + param->status = true; + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("disconnect"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); +} + +TEST_F(WifiManagerTest, saveSSID) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_saveSSID))); + auto param = static_cast(arg); + param->status = true; + EXPECT_EQ(string(param->data.connect.ssid), string(_T("123412341234"))); + EXPECT_EQ(string(param->data.connect.passphrase), string(_T("password"))); + EXPECT_EQ(param->data.connect.security_mode, NET_WIFI_SECURITY_WEP_128); //securityMode: 2 (NET_WIFI_SECURITY_WEP_128) + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("saveSSID"), _T("{\"ssid\": \"123412341234\",\"passphrase\": \"password\",\"securityMode\": 2}"), response)); + EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); +} + +TEST_F(WifiManagerTest, clearSSID) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_clearSSID))); + auto param = static_cast(arg); + param->status = true; + return IARM_RESULT_SUCCESS; + }); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("clearSSID"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); +} + +TEST_F(WifiManagerTest, isPaired) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getPairedSSIDInfo))); + auto param = static_cast(arg); + memcpy(¶m->data.getPairedSSIDInfo.ssid, "123412341234", sizeof("123412341234")); + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isPaired"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); +} + +TEST_F(WifiManagerTest, isPaired_cache) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getPairedSSIDInfo))); + auto param = static_cast(arg); + memcpy(¶m->data.getPairedSSIDInfo.ssid, "123412341234", sizeof("123412341234")); + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isPaired"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isPaired"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); +} + +TEST_F(WifiManagerTest, startScan) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getAvailableSSIDsAsync))); + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("startScan"), _T("{\"incremental\": false,\"ssid\": \"...\",\"frequency\": \"...\"}"), response)); + EXPECT_EQ(response, string("{\"success\":true}")); + + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_getAvailableSSIDsAsyncIncr))); + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("startScan"), _T("{\"incremental\": true,\"ssid\": \"...\",\"frequency\": \"...\"}"), response)); + EXPECT_EQ(response, string("{\"success\":true}")); +} + +TEST_F(WifiManagerTest, stopScan) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_stopProgressiveWifiScanning))); + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("stopScan"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"success\":true}")); +} + +TEST_F(WifiManagerTest, cancelWPSPairing) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_cancelWPSPairing))); + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("cancelWPSPairing"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"result\":\"\",\"success\":true}")); +} + +TEST_F(WifiManagerTest, initiateWPSPairing) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_initiateWPSPairing))); + return IARM_RESULT_SUCCESS; + }); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("initiateWPSPairing"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"result\":\"\",\"success\":true}")); +} + +TEST_F(WifiManagerTest, initiateWPSPairing2) +{ + EXPECT_CALL(iarmBusImplMock, IARM_Bus_Call) + .Times(::testing::AnyNumber()) + .WillRepeatedly( + [](const char* ownerName, const char* methodName, void* arg, size_t argLen) { + EXPECT_EQ(string(ownerName), string(_T(IARM_BUS_NM_SRV_MGR_NAME))); + EXPECT_EQ(string(methodName), string(_T(IARM_BUS_WIFI_MGR_API_initiateWPSPairing2))); + return IARM_RESULT_SUCCESS; + }); + + + JsonObject jsonObj; + JsonObject res; + jsonObj["method"] = "PIN"; + jsonObj["wps_pin"] = "88888888"; + uint32_t result = plugin->initiateWPSPairing2(jsonObj,res); + EXPECT_EQ(result, 0); +} + + +TEST_F(WifiManagerTest, SignalThresholdChangeEnabled) +{ + ServiceMock service; + EXPECT_EQ(string(""), plugin->Initialize(&service)); + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isSignalThresholdChangeEnabled"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"result\":1,\"success\":true}")); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("setSignalThresholdChangeEnabled"), _T("{\"enabled\": true, \"interval\":2000}"), response)); + EXPECT_EQ(response, string("{\"success\":true}")); + + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("isSignalThresholdChangeEnabled"), _T("{}"), response)); + EXPECT_EQ(response, string("{\"result\":0,\"success\":true}")); + plugin->Deinitialize(&service); +} + +TEST_F(WifiManagerTest, getSupportedSecurityModes) +{ + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getSupportedSecurityModes"), _T("{}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); +} + +TEST_F(WifiManagerTest, getQuirks) +{ + EXPECT_EQ(Core::ERROR_NONE, handler.Invoke(connection, _T("getQuirks"), _T("{}"), response)); + EXPECT_THAT(response, ::testing::ContainsRegex(_T("\"success\":true"))); +} + +TEST_F(WifiInitializedEventTest, onWIFIStateChanged) +{ + EXPECT_CALL(services, Submit(::testing::_, ::testing::_)) + .Times(1) + .WillOnce(::testing::Invoke( + [&](const uint32_t, const Core::ProxyType& json) { + string text; + EXPECT_TRUE(json->ToString(text)); + EXPECT_EQ(text, string(_T("{\"jsonrpc\":\"2.0\",\"method\":\"org.rdk.WifiManager.onWIFIStateChanged\",\"params\":{\"state\":1,\"isLNF\":false}}"))); + return Core::ERROR_NONE; + })); + WPEFramework::Plugin::WifiState intData = WPEFramework::Plugin::WifiState::DISABLED; + handler.Subscribe(0, _T("onWIFIStateChanged"), _T("org.rdk.WifiManager"), message); + plugin->onWIFIStateChanged(intData,false); + handler.Unsubscribe(0, _T("onWIFIStateChanged"), _T("org.rdk.WifiManager"), message); +} + +TEST_F(WifiInitializedEventTest, onSSIDsChanged) +{ + EXPECT_CALL(services, Submit(::testing::_, ::testing::_)) + .Times(1) + .WillOnce(::testing::Invoke( + [&](const uint32_t, const Core::ProxyType& json) { + string text; + EXPECT_TRUE(json->ToString(text)); + EXPECT_EQ(text, string(_T("{\"jsonrpc\":\"2.0\",\"method\":\"org.rdk.WifiManager.onSSIDsChanged\",\"params\":{}}"))); + return Core::ERROR_NONE; + })); + handler.Subscribe(0, _T("onSSIDsChanged"), _T("org.rdk.WifiManager"), message); + plugin->onSSIDsChanged(); + handler.Unsubscribe(0, _T("onSSIDsChanged"), _T("org.rdk.WifiManager"), message); +} +TEST_F(WifiInitializedEventTest, onWifiSignalThresholdChanged) +{ + EXPECT_CALL(services, Submit(::testing::_, ::testing::_)) + .Times(1) + .WillOnce(::testing::Invoke( + [&](const uint32_t, const Core::ProxyType& json) { + string text; + EXPECT_TRUE(json->ToString(text)); + EXPECT_EQ(text, string(_T("{\"jsonrpc\":\"2.0\",\"method\":\"org.rdk.WifiManager.onWifiSignalThresholdChanged\",\"params\":{\"signalStrength\":\"-27.000000\",\"strength\":\"Excellent\"}}"))); + return Core::ERROR_NONE; + })); + handler.Subscribe(0, _T("onWifiSignalThresholdChanged"), _T("org.rdk.WifiManager"), message); + plugin->onWifiSignalThresholdChanged(-27.000000,"Excellent"); + handler.Unsubscribe(0, _T("onWifiSignalThresholdChanged"), _T("org.rdk.WifiManager"), message); +} + +TEST_F(WifiInitializedEventTest, onError) +{ + EXPECT_CALL(services, Submit(::testing::_, ::testing::_)) + .Times(1) + .WillOnce(::testing::Invoke( + [&](const uint32_t, const Core::ProxyType& json) { + string text; + EXPECT_TRUE(json->ToString(text)); + EXPECT_EQ(text, string(_T("{\"jsonrpc\":\"2.0\",\"method\":\"org.rdk.WifiManager.onError\",\"params\":{\"code\":2}}"))); + return Core::ERROR_NONE; + })); + WPEFramework::Plugin::ErrorCode intData = WPEFramework::Plugin::ErrorCode::CONNECTION_FAILED; + handler.Subscribe(0, _T("onError"), _T("org.rdk.WifiManager"), message); + plugin->onError(intData); + handler.Unsubscribe(0, _T("onError"), _T("org.rdk.WifiManager"), message); +} + + +TEST_F(WifiInitializedEventTest, onAvailableSSIDs) +{ + EXPECT_CALL(services, Submit(::testing::_, ::testing::_)) + .Times(1) + .WillOnce(::testing::Invoke( + [&](const uint32_t, const Core::ProxyType& json) { + string text; + EXPECT_TRUE(json->ToString(text)); + EXPECT_EQ(text, string(_T("{\"jsonrpc\":\"2.0\",\"method\":\"org.rdk.WifiManager.onAvailableSSIDs\",\"params\":{\"ssid\":\"123412341234\",\"security\":2,\"signalStrength\":\"-27.000000\",\"frequency\":\"2.442000\"}}"))); + return Core::ERROR_NONE; + })); + JsonObject jsonObj; + jsonObj["ssid"] = "123412341234"; + jsonObj["security"] = 2; + jsonObj["signalStrength"] = "-27.000000"; + jsonObj["frequency"] = "2.442000"; + handler.Subscribe(0, _T("onAvailableSSIDs"), _T("org.rdk.WifiManager"), message); + plugin->onAvailableSSIDs(jsonObj); + handler.Unsubscribe(0, _T("onAvailableSSIDs"), _T("org.rdk.WifiManager"), message); +} +