diff --git a/.github/workflows/gnome_unit_test.yml b/.github/workflows/gnome_unit_test.yml index 107e6e45..4dcb52e8 100644 --- a/.github/workflows/gnome_unit_test.yml +++ b/.github/workflows/gnome_unit_test.yml @@ -110,7 +110,7 @@ jobs: run: > PATH=${{github.workspace}}/install/usr/bin:${PATH} LD_LIBRARY_PATH=${{github.workspace}}/install/usr/lib:${{github.workspace}}/install/usr/lib/wpeframework/plugins:${LD_LIBRARY_PATH} - tests + unit_tests - name: Generate coverage run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b5ba9d9..592e9915 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -170,6 +170,6 @@ write_config(PLUGINS LegacyPlugin_NetworkAPIs CLASSNAME Network LOCATOR lib${PLU write_config(PLUGINS LegacyPlugin_WiFiManagerAPIs CLASSNAME WiFiManager LOCATOR lib${PLUGIN_LEGACY_DEPRECATED_WIFI}.so) if(ENABLE_UNIT_TESTING) - include(Tests/unit_test/tests.cmake) + include(Tests/unit_test/unit_tests.cmake) endif(ENABLE_UNIT_TESTING) diff --git a/NetworkManagerConnectivity.cpp b/NetworkManagerConnectivity.cpp index f76c8b4b..1a4667e1 100644 --- a/NetworkManagerConnectivity.cpp +++ b/NetworkManagerConnectivity.cpp @@ -158,7 +158,7 @@ namespace WPEFramework curl_easy_setopt(curl_easy_handle, CURLOPT_TIMEOUT_MS, deadline - current_time()); if ((ipversion == CURL_IPRESOLVE_V4) || (ipversion == CURL_IPRESOLVE_V6)) { - NMLOG_DEBUG("curlopt ipversion = %s reqtyp = %s", ipversion == CURL_IPRESOLVE_V4?"ipv4 only":"ipv6 only", headReq? "HEAD":"GET"); + NMLOG_DEBUG("curlopt ipversion = %s reqtyp = %s", ipversion == CURL_IPRESOLVE_V4?"IPv4":"IPv6", headReq? "HEAD":"GET"); curl_easy_setopt(curl_easy_handle, CURLOPT_IPRESOLVE, ipversion); } else @@ -324,10 +324,9 @@ namespace WPEFramework doContinuousMonitor = false; doConnectivityMonitor = false; - gInternetState = nsm_internetState::UNKNOWN; - gIpv4InternetState = nsm_internetState::UNKNOWN; - gIpv6InternetState = nsm_internetState::UNKNOWN; - ginterfaceStatus = false; + m_InternetState = nsm_internetState::UNKNOWN; + m_Ipv4InternetState = nsm_internetState::UNKNOWN; + m_Ipv6InternetState = nsm_internetState::UNKNOWN; } ConnectivityMonitor::~ConnectivityMonitor() @@ -390,15 +389,15 @@ namespace WPEFramework // If monitor connectivity is running take the cache value if ( doContinuousMonitor && (nsm_ipversion::NSM_IPRESOLVE_V4 == ipversion || nsm_ipversion::NSM_IPRESOLVE_WHATEVER == ipversion) - && gIpv4InternetState != nsm_internetState::UNKNOWN ) { - NMLOG_WARNING("Reading Ipv4 internet state cached value %s", getInternetStateString(gIpv4InternetState)); - internetState = gIpv4InternetState; + && m_Ipv4InternetState != nsm_internetState::UNKNOWN ) { + NMLOG_WARNING("Reading Ipv4 internet state cached value %s", getInternetStateString(m_Ipv4InternetState)); + internetState = m_Ipv4InternetState; ipversion = NSM_IPRESOLVE_V4; } else if ( doContinuousMonitor && (nsm_ipversion::NSM_IPRESOLVE_V6 == ipversion || nsm_ipversion::NSM_IPRESOLVE_WHATEVER == ipversion) - && gIpv6InternetState != nsm_internetState::UNKNOWN ) { - NMLOG_WARNING("Reading Ipv6 internet state cached value %s", getInternetStateString(gIpv6InternetState)); - internetState = gIpv6InternetState; + && m_Ipv6InternetState != nsm_internetState::UNKNOWN ) { + NMLOG_WARNING("Reading Ipv6 internet state cached value %s", getInternetStateString(m_Ipv6InternetState)); + internetState = m_Ipv6InternetState; ipversion = NSM_IPRESOLVE_V6; } else @@ -425,6 +424,8 @@ namespace WPEFramework bool ConnectivityMonitor::startContinuousConnectivityMonitor(int timeoutInSeconds) { + if(_instance != nullptr ) + NMLOG_INFO("interface status eth - %s wlan - %s ", _instance->m_ethConnected? "up":"down", _instance->m_wlanConnected? "up":"down"); continuousMonitorTimeout.store(timeoutInSeconds >= NMCONNECTIVITY_MONITOR_MIN_INTERVAL ? timeoutInSeconds : NMCONNECTIVITY_MONITOR_DEFAULT_INTERVAL); if (doContinuousMonitor) { @@ -472,12 +473,11 @@ namespace WPEFramework * --> when IP address accuired * --> when etherenet/wifi disconnected */ - bool ConnectivityMonitor::startConnectivityMonitor(bool interfaceStatus) + bool ConnectivityMonitor::startConnectivityMonitor() { - ginterfaceStatus = interfaceStatus; /* this will give interface status connected/ disconnected */ - gInternetState = UNKNOWN; - gIpv4InternetState = UNKNOWN; - gIpv6InternetState = UNKNOWN; + m_InternetState = nsm_internetState::UNKNOWN; + m_Ipv4InternetState = nsm_internetState::UNKNOWN; + m_Ipv6InternetState = nsm_internetState::UNKNOWN; if (doConnectivityMonitor) { cvConnectivityMonitor.notify_one(); @@ -497,21 +497,26 @@ namespace WPEFramework return false; } - NMLOG_ERROR("connectivity monitor started %d", NMCONNECTIVITY_MONITOR_MIN_INTERVAL); + if(_instance != nullptr) { + NMLOG_INFO("connectivity monitor started %d sec - eth %s - wlan %s", NMCONNECTIVITY_MONITOR_MIN_INTERVAL, + _instance->m_ethConnected? "up":"down", _instance->m_wlanConnected? "up":"down"); + } return true; } void ConnectivityMonitor::notifyInternetStatusChangedEvent(nsm_internetState newInternetState) { + static Exchange::INetworkManager::InternetStatus oldState = Exchange::INetworkManager::InternetStatus::INTERNET_UNKNOWN; if(_instance != nullptr) { - Exchange::INetworkManager::InternetStatus oldState = static_cast(gInternetState.load()); + NMLOG_INFO("notify internet state %s", getInternetStateString(newInternetState)); Exchange::INetworkManager::InternetStatus newState = static_cast(newInternetState); _instance->ReportInternetStatusChange(oldState , newState); - gInternetState = newInternetState; + m_InternetState = newInternetState; + oldState = newState; // 'm_InternetState' not exactly previous state, it may change to unknow when interface changed } else - NMLOG_WARNING("NetworkManagerImplementation Instance NULL notifyInternetStatusChange failed."); + NMLOG_FATAL("NetworkManagerImplementation Instance NULL notifyInternetStatusChange failed."); } void ConnectivityMonitor::continuousMonitorFunction() @@ -524,20 +529,31 @@ namespace WPEFramework do { + if(_instance != nullptr && (!_instance->m_ethConnected && !_instance->m_wlanConnected)) // no wifi no ethernet connected + { + NMLOG_DEBUG("no interface connected; no ccm check"); + m_Ipv4InternetState = NO_INTERNET; + m_Ipv6InternetState = NO_INTERNET; + std::unique_lock lock(connMutex); + cvContinuousMonitor.wait_for(lock, std::chrono::seconds(continuousMonitorTimeout.load())); + ipResolveTyp = NSM_IPRESOLVE_WHATEVER; + continue; + } + if(doConnectivityMonitor) { NMLOG_DEBUG("connectivity monitor running so skiping ccm check"); - gIpv4InternetState = UNKNOWN; - gIpv6InternetState = UNKNOWN; + m_Ipv4InternetState = nsm_internetState::UNKNOWN; + m_Ipv6InternetState = nsm_internetState::UNKNOWN; std::unique_lock lock(connMutex); cvContinuousMonitor.wait_for(lock, std::chrono::seconds(continuousMonitorTimeout.load())); - ipResolveTyp = NSM_IPRESOLVE_WHATEVER; /* some interface change happense*/ + ipResolveTyp = NSM_IPRESOLVE_WHATEVER; /* some interface change happense */ continue; } else if (ipResolveTyp == NSM_IPRESOLVE_WHATEVER) { - nsm_internetState ipV4InternetState = UNKNOWN; - nsm_internetState ipV6InternetState = UNKNOWN; + nsm_internetState ipV4InternetState = nsm_internetState::UNKNOWN; + nsm_internetState ipV6InternetState = nsm_internetState::UNKNOWN; auto curlCheckThrdIpv4 = [&]() { TestConnectivity testInternet(getConnectivityMonitorEndpoints(), NMCONNECTIVITY_CURL_REQUEST_TIMEOUT_MS, NMCONNECTIVITY_CURL_GET_REQUEST, NSM_IPRESOLVE_V4); @@ -573,17 +589,17 @@ namespace WPEFramework NMCONNECTIVITY_CURL_HEAD_REQUEST, ipResolveTyp); currentInternetState = testInternet.getInternetState(); if(ipResolveTyp == NSM_IPRESOLVE_V4) - gIpv4InternetState = currentInternetState; + m_Ipv4InternetState = currentInternetState; else if(ipResolveTyp == NSM_IPRESOLVE_V6) - gIpv6InternetState = currentInternetState; + m_Ipv6InternetState = currentInternetState; } if (currentInternetState == NO_INTERNET) { - if(gInternetState == FULLY_CONNECTED && notifyPreRetry < NMCONNECTIVITY_NO_INTERNET_RETRY_COUNT) + if(m_InternetState == FULLY_CONNECTED && notifyPreRetry < NMCONNECTIVITY_NO_INTERNET_RETRY_COUNT) { /* it will prevent posting notification */ - currentInternetState = gInternetState; + currentInternetState = m_InternetState; TempInterval = 5; NMLOG_INFO("No internet retrying connection check %d ...", notifyPreRetry); notifyPreRetry++; @@ -601,9 +617,8 @@ namespace WPEFramework TempInterval = continuousMonitorTimeout.load(); } - if(gInternetState != currentInternetState) + if(m_InternetState != currentInternetState) { - NMLOG_INFO("Internet state changed to %s", getInternetStateString(currentInternetState)); /* Notify Internet state change */ notifyInternetStatusChangedEvent(currentInternetState); } @@ -619,8 +634,8 @@ namespace WPEFramework } while(doContinuousMonitor); - gIpv4InternetState = UNKNOWN; - gIpv6InternetState = UNKNOWN; + m_Ipv4InternetState = nsm_internetState::UNKNOWN; + m_Ipv6InternetState = nsm_internetState::UNKNOWN; NMLOG_DEBUG("continous connectivity monitor exit"); } @@ -663,8 +678,8 @@ namespace WPEFramework NMLOG_INFO("Connectivity check retrying %d ...", notifyPreRetry); } - if(gInternetState != UNKNOWN) - currentInternetState = gInternetState; + if(m_InternetState != nsm_internetState::UNKNOWN) + currentInternetState = m_InternetState; TempInterval = 5; } else if(tempInternetState != currentInternetState) // last state have change @@ -681,8 +696,9 @@ namespace WPEFramework doConnectivityMonitor = false; // self exit notifyNow = true; // post current state when retry complete } - else if(ginterfaceStatus) // interface is active and still no internet, continue check every 30 sec + else if(_instance != nullptr && (_instance->m_ethConnected | _instance->m_wlanConnected)) { + /* interface is connected and still no internet, continue check every 30 sec */ TempInterval = NMCONNECTIVITY_CONN_MONITOR_RETRY_INTERVAL; /* notify if retry completed and state stil no internet state */ if(notifyPreRetry == NMCONNECTIVITY_CONN_MONITOR_RETRY_COUNT) @@ -699,10 +715,9 @@ namespace WPEFramework } } - if(gInternetState != currentInternetState || notifyNow) + if(m_InternetState != currentInternetState || notifyNow) { notifyNow = false; - NMLOG_INFO("notify internet state %s", getInternetStateString(currentInternetState)); notifyInternetStatusChangedEvent(currentInternetState); } @@ -719,7 +734,7 @@ namespace WPEFramework } while(doConnectivityMonitor); if(!doContinuousMonitor) - gInternetState = nsm_internetState::UNKNOWN; // no continous monitor running reset to unknow + m_InternetState = nsm_internetState::UNKNOWN; // no continous monitor running reset to unknow NMLOG_DEBUG("initial connectivity monitor exit"); } diff --git a/NetworkManagerConnectivity.h b/NetworkManagerConnectivity.h index 26f256fa..3a5b215a 100644 --- a/NetworkManagerConnectivity.h +++ b/NetworkManagerConnectivity.h @@ -114,7 +114,7 @@ namespace WPEFramework ~ConnectivityMonitor(); bool startContinuousConnectivityMonitor(int timeoutInSeconds); bool stopContinuousConnectivityMonitor(); - bool startConnectivityMonitor(bool interfaceStatus); + bool startConnectivityMonitor(); void setConnectivityMonitorEndpoints(const std::vector &endpoints); std::vector getConnectivityMonitorEndpoints(); bool isConnectedToInternet(nsm_ipversion ipversion); @@ -140,10 +140,9 @@ namespace WPEFramework EndpointCache endpointCache; std::mutex endpointMutex; - std::atomic ginterfaceStatus; - std::atomic gInternetState; - std::atomic gIpv4InternetState; - std::atomic gIpv6InternetState; + std::atomic m_InternetState; + std::atomic m_Ipv4InternetState; + std::atomic m_Ipv6InternetState; }; } // namespace Plugin } // namespace WPEFramework diff --git a/NetworkManagerGnomeProxy.cpp b/NetworkManagerGnomeProxy.cpp index b6145ee4..9a00cc82 100644 --- a/NetworkManagerGnomeProxy.cpp +++ b/NetworkManagerGnomeProxy.cpp @@ -84,23 +84,25 @@ namespace WPEFramework { NMDeviceState deviceState = NM_DEVICE_STATE_UNKNOWN; Exchange::INetworkManager::InterfaceDetails interface; + interface.mac = nm_device_get_hw_address(device); + deviceState = nm_device_get_state(device); + interface.enabled = (deviceState >= NM_DEVICE_STATE_UNAVAILABLE)? true : false; + if(deviceState > NM_DEVICE_STATE_DISCONNECTED && deviceState < NM_DEVICE_STATE_DEACTIVATING) + interface.connected = true; + else + interface.connected = false; + if(ifaceStr == wifiname) { interface.type = INTERFACE_TYPE_WIFI; interface.name = wifiname; + m_wlanConnected = interface.connected; } if(ifaceStr == ethname) { interface.type = INTERFACE_TYPE_ETHERNET; interface.name = ethname; + m_ethConnected = interface.connected; } - interface.mac = nm_device_get_hw_address(device); - deviceState = nm_device_get_state(device); - interface.enabled = (deviceState >= NM_DEVICE_STATE_UNAVAILABLE) ? true : false; - if(deviceState > NM_DEVICE_STATE_DISCONNECTED && deviceState < NM_DEVICE_STATE_DEACTIVATING){ - interface.connected = true; - m_defaultInterface = interface.name; - } - else - interface.connected = false; + interfaceList.push_back(interface); rc = Core::ERROR_NONE; } diff --git a/NetworkManagerImplementation.cpp b/NetworkManagerImplementation.cpp index 7da44a9e..66b0d0fa 100644 --- a/NetworkManagerImplementation.cpp +++ b/NetworkManagerImplementation.cpp @@ -50,6 +50,8 @@ namespace WPEFramework m_stunCacheTimeout = 0; m_defaultInterface = ""; m_publicIP = ""; + m_ethConnected = false; + m_wlanConnected = false; } NetworkManagerImplementation::~NetworkManagerImplementation() @@ -520,13 +522,22 @@ namespace WPEFramework void NetworkManagerImplementation::ReportInterfaceStateChange(const Exchange::INetworkManager::InterfaceState state, const string interface) { LOG_ENTRY_FUNCTION(); - if(Exchange::INetworkManager::INTERFACE_LINK_DOWN == state) { + if(Exchange::INetworkManager::INTERFACE_LINK_DOWN == state || Exchange::INetworkManager::INTERFACE_REMOVED == state) { // Start the connectivity monitor with 'false' to indicate the interface is down. // The monitor will automatically exit after the retry attempts are completed, posting a 'noInternet' event. - connectivityMonitor.startConnectivityMonitor(false); + if(interface == "eth0") + m_ethConnected = false; + else + m_wlanConnected = false; + connectivityMonitor.startConnectivityMonitor(); } + /* Only the Ethernet connection status is changing here. The WiFi status is updated in the WiFi state callback. */ + if(Exchange::INetworkManager::INTERFACE_LINK_UP == state && interface == "eth0") + m_ethConnected = true; + _notificationLock.Lock(); + NMLOG_INFO("Posting onInterfaceChange %s - %u", interface.c_str(), (unsigned)state); for (const auto callback : _notificationCallbacks) { callback->onInterfaceStateChange(state, interface); } @@ -535,8 +546,8 @@ namespace WPEFramework void NetworkManagerImplementation::ReportActiveInterfaceChange(const string prevActiveInterface, const string currentActiveinterface) { - NMLOG_INFO("Posting onActiveInterfaceChange %s", currentActiveinterface.c_str()); _notificationLock.Lock(); + NMLOG_INFO("Posting onActiveInterfaceChange %s", currentActiveinterface.c_str()); for (const auto callback : _notificationCallbacks) { callback->onActiveInterfaceChange(prevActiveInterface, currentActiveinterface); } @@ -549,11 +560,11 @@ namespace WPEFramework if (Exchange::INetworkManager::IP_ACQUIRED == status) { // Start the connectivity monitor with 'true' to indicate the interface is up. // The monitor will conntinoue even after no internet retry completed, Exit when fully connectd. - connectivityMonitor.startConnectivityMonitor(true); + connectivityMonitor.startConnectivityMonitor(); } - NMLOG_INFO("Posting onIPAddressChange %s", ipaddress.c_str()); _notificationLock.Lock(); + NMLOG_INFO("Posting onIPAddressChange %s - %s", ipaddress.c_str(), interface.c_str()); for (const auto callback : _notificationCallbacks) { callback->onIPAddressChange(interface, ipversion, ipaddress, status); } @@ -562,8 +573,8 @@ namespace WPEFramework void NetworkManagerImplementation::ReportInternetStatusChange(const Exchange::INetworkManager::InternetStatus prevState, const Exchange::INetworkManager::InternetStatus currState) { - NMLOG_INFO("Posting onInternetStatusChange with current state as %u", (unsigned)currState); _notificationLock.Lock(); + NMLOG_INFO("Posting onInternetStatusChange with current state as %u", (unsigned)currState); for (const auto callback : _notificationCallbacks) { callback->onInternetStatusChange(prevState, currState); } @@ -584,10 +595,15 @@ namespace WPEFramework { /* start signal strength monitor when wifi connected */ if(INetworkManager::WiFiState::WIFI_STATE_CONNECTED == state) + { + m_wlanConnected = true; m_wifiSignalMonitor.startWiFiSignalStrengthMonitor(DEFAULT_WIFI_SIGNAL_TEST_INTERVAL_SEC); + } + else + m_wlanConnected = false; /* Any other state is considered as WiFi not connected. */ - NMLOG_INFO("Posting onWiFiStateChange"); _notificationLock.Lock(); + NMLOG_INFO("Posting onWiFiStateChange (%d)", state); for (const auto callback : _notificationCallbacks) { callback->onWiFiStateChange(state); } @@ -596,11 +612,10 @@ namespace WPEFramework void NetworkManagerImplementation::ReportWiFiSignalStrengthChange(const string ssid, const string strength, const Exchange::INetworkManager::WiFiSignalQuality quality) { - NMLOG_INFO("Posting onWiFiSignalStrengthChange"); _notificationLock.Lock(); + NMLOG_INFO("Posting onWiFiSignalStrengthChange %s", strength.c_str()); for (const auto callback : _notificationCallbacks) { callback->onWiFiSignalStrengthChange(ssid, strength, quality); - } _notificationLock.Unlock(); } diff --git a/NetworkManagerImplementation.h b/NetworkManagerImplementation.h index d6ae934c..a9cdcaf5 100644 --- a/NetworkManagerImplementation.h +++ b/NetworkManagerImplementation.h @@ -25,6 +25,7 @@ #include #include #include +#include using namespace std; @@ -222,9 +223,14 @@ namespace WPEFramework void ReportWiFiStateChange(const Exchange::INetworkManager::WiFiState state); void ReportWiFiSignalStrengthChange(const string ssid, const string strength, const Exchange::INetworkManager::WiFiSignalQuality quality); + public: + std::atomic m_ethConnected; + std::atomic m_wlanConnected; + private: void platform_init(); void retryIarmEventRegistration(); + void getInitialConnectionState(); void threadEventRegistration(); void executeExternally(NetworkEvents event, const string commandToExecute, string& response); diff --git a/NetworkManagerRDKProxy.cpp b/NetworkManagerRDKProxy.cpp index 3ad434b0..dfdea98a 100644 --- a/NetworkManagerRDKProxy.cpp +++ b/NetworkManagerRDKProxy.cpp @@ -457,8 +457,7 @@ namespace WPEFramework { IARM_BUS_NetSrvMgr_Iface_EventInterfaceIPAddress_t *e = (IARM_BUS_NetSrvMgr_Iface_EventInterfaceIPAddress_t*) data; interface = e->interface; - NMLOG_INFO ("IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_IPADDRESS: %s - %s - %s", interface.c_str(), e->ip_address, e->acquired?"Acquired":"Lost" -); + NMLOG_INFO ("IARM_BUS_NETWORK_MANAGER_EVENT_INTERFACE_IPADDRESS: %s - %s - %s", interface.c_str(), e->ip_address, e->acquired?"Acquired":"Lost"); if(interface == "eth0" || interface == "wlan0") { string ipversion("IPv4"); @@ -577,10 +576,57 @@ namespace WPEFramework IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_WIFI_MGR_EVENT_onAvailableSSIDs, NetworkManagerInternalEventHandler); NMLOG_INFO("threadEventRegistration successfully subscribed to IARM event for NetworkManager Plugin"); + /* + * Read current network state and post the event. + * Useful if NetworkManager plugin or WPEFramework is restarted + * or netsrvmgr misses to post iarm events during bootup. + */ + getInitialConnectionState(); } } + void NetworkManagerImplementation::getInitialConnectionState() + { + // check the connection state and post event + Exchange::INetworkManager::IInterfaceDetailsIterator* _interfaces{}; + uint32_t rc = GetAvailableInterfaces(_interfaces); + + if (Core::ERROR_NONE == rc) + { + if (_interfaces != nullptr) + { + Exchange::INetworkManager::InterfaceDetails iface{}; + while (_interfaces->Next(iface) == true) + { + Core::JSON::EnumType type{iface.type}; + if(iface.enabled) + { + NMLOG_INFO("'%s' interface is enabled", iface.name.c_str()); + // ReportInterfaceStateChange(Exchange::INetworkManager::INTERFACE_ADDED, iface.name); + if(iface.connected) + { + NMLOG_INFO("'%s' interface is connected", iface.name.c_str()); + ReportActiveInterfaceChange(iface.name, iface.name); + std::string ipversion = {}; + Exchange::INetworkManager::IPAddress addr; + rc = GetIPSettings(iface.name, ipversion, addr); + if (Core::ERROR_NONE == rc) + { + if(!addr.ipaddress.empty()) { + NMLOG_INFO("'%s' interface have ip '%s'", iface.name.c_str(), addr.ipaddress.c_str()); + ReportIPAddressChange(iface.name, addr.ipversion, addr.ipaddress, Exchange::INetworkManager::IP_ACQUIRED); + } + } + } + } + } + + _interfaces->Release(); + } + } + } + void NetworkManagerImplementation::platform_init() { LOG_ENTRY_FUNCTION(); @@ -625,7 +671,14 @@ namespace WPEFramework IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_WIFI_MGR_EVENT_onWIFIStateChanged, NetworkManagerInternalEventHandler); IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_WIFI_MGR_EVENT_onError, NetworkManagerInternalEventHandler); IARM_Bus_RegisterEventHandler(IARM_BUS_NM_SRV_MGR_NAME, IARM_BUS_WIFI_MGR_EVENT_onAvailableSSIDs, NetworkManagerInternalEventHandler); + /* + * Read current network state and post the event. + * Useful if NetworkManager plugin or WPEFramework is restarted + * or netsrvmgr misses to post iarm events during bootup. + */ + getInitialConnectionState(); } + } uint32_t NetworkManagerImplementation::GetAvailableInterfaces (Exchange::INetworkManager::IInterfaceDetailsIterator*& interfacesItr/* @out */) @@ -644,15 +697,19 @@ namespace WPEFramework { InterfaceDetails tmp; /* Update the interface as per RDK NetSrvMgr */ - if ("eth0" == interfaceName) - tmp.type = Exchange::INetworkManager::INTERFACE_TYPE_ETHERNET; - else if ("wlan0" == interfaceName) - tmp.type = Exchange::INetworkManager::INTERFACE_TYPE_WIFI; - tmp.name = interfaceName; tmp.mac = string(list.interfaces[i].mac); tmp.enabled = ((list.interfaces[i].flags & IFF_UP) != 0); tmp.connected = ((list.interfaces[i].flags & IFF_RUNNING) != 0); + if ("eth0" == interfaceName) { + tmp.type = Exchange::INetworkManager::INTERFACE_TYPE_ETHERNET; + m_ethConnected = tmp.connected; + } + else if ("wlan0" == interfaceName) { + tmp.type = Exchange::INetworkManager::INTERFACE_TYPE_WIFI; + m_wlanConnected = tmp.connected; + } + interfaceList.push_back(tmp); } } diff --git a/Tests/unit_test/test_NetworkManagerConnectivity.cpp b/Tests/unit_test/test_NetworkManagerConnectivity.cpp index f175a929..74f4309f 100644 --- a/Tests/unit_test/test_NetworkManagerConnectivity.cpp +++ b/Tests/unit_test/test_NetworkManagerConnectivity.cpp @@ -1,6 +1,6 @@ #include "NetworkManagerImplementation.h" #include "NetworkManagerConnectivity.h" -#include "WiFiSignalStrengthMonitor.h" + #include #include #include @@ -20,36 +20,30 @@ using namespace std; using namespace WPEFramework; - + class ConnectivityMonitorTest : public ::testing::Test { protected: - - WPEFramework::Plugin::ConnectivityMonitor cm; - - void SetUp() override { - - } - - void TearDown() override { - - } + WPEFramework::Plugin::ConnectivityMonitor cm; }; - - TEST_F(ConnectivityMonitorTest, StartContinuousMonitor_Success) { - int timeout = 30; + +TEST_F(ConnectivityMonitorTest, StartContinuousMonitor_Success) { + int timeout = 30; bool result = cm.startContinuousConnectivityMonitor(timeout); - EXPECT_TRUE(result); + EXPECT_TRUE(result); } + TEST_F(ConnectivityMonitorTest, StartContinuousMonitor_FailureNegativeTimeout) { - int timeout = -1; + int timeout = -1; bool result = cm.startContinuousConnectivityMonitor(timeout); - EXPECT_TRUE(result); -} + EXPECT_TRUE(result); +} + TEST_F(ConnectivityMonitorTest, StartMonitorWithTimeoutLessThanMinimum) { - int timeout = 3; + int timeout = 3; bool result = cm.startContinuousConnectivityMonitor(timeout); EXPECT_TRUE(result); } + TEST_F(ConnectivityMonitorTest, MonitorFailsToStart) { int timeout = 0; bool result = cm.startContinuousConnectivityMonitor(timeout); @@ -60,17 +54,19 @@ TEST_F(ConnectivityMonitorTest, StopContinuousMonitor_WhenStarted) { int timeout = 30; cm.startContinuousConnectivityMonitor(timeout); bool result = cm.stopContinuousConnectivityMonitor(); - EXPECT_TRUE(result); + EXPECT_TRUE(result); } + TEST_F(ConnectivityMonitorTest, StopContinuousMonitor_WhenNotStarted) { bool result = cm.stopContinuousConnectivityMonitor(); - EXPECT_TRUE(result); + EXPECT_TRUE(result); } + TEST_F(ConnectivityMonitorTest, StopContinuousMonitor_AfterMultipleStartsAndStops) { int timeout = 30; cm.startContinuousConnectivityMonitor(timeout); bool result = cm.stopContinuousConnectivityMonitor(); - EXPECT_TRUE(result); + EXPECT_TRUE(result); cm.startContinuousConnectivityMonitor(timeout); result = cm.stopContinuousConnectivityMonitor(); @@ -78,55 +74,51 @@ TEST_F(ConnectivityMonitorTest, StopContinuousMonitor_AfterMultipleStartsAndStop cm.startContinuousConnectivityMonitor(timeout); result = cm.stopContinuousConnectivityMonitor(); - EXPECT_TRUE(result); + EXPECT_TRUE(result); } + TEST_F(ConnectivityMonitorTest, StopContinuousMonitor_LongRunningMonitor) { int timeout = 1000; cm.startContinuousConnectivityMonitor(timeout); std::this_thread::sleep_for(std::chrono::seconds(2)); bool result = cm.stopContinuousConnectivityMonitor(); - EXPECT_TRUE(result); + EXPECT_TRUE(result); } - TEST_F(ConnectivityMonitorTest, StartMonitor_WithInterfaceStatusTrue) { - bool interfaceStatus = true; - bool result = cm.startConnectivityMonitor(interfaceStatus); - EXPECT_TRUE(result); -} -TEST_F(ConnectivityMonitorTest, StartMonitor_WithInterfaceStatusFalse) { - bool interfaceStatus = false; - bool result = cm.startConnectivityMonitor(interfaceStatus); - EXPECT_TRUE(result); +TEST_F(ConnectivityMonitorTest, StartMonitor_WithInterfaceStatus) { + bool result = cm.startConnectivityMonitor(); + EXPECT_TRUE(result); } + TEST_F(ConnectivityMonitorTest, StartMonitor_NotifyIfAlreadyMonitoring) { - bool interfaceStatus = true; - cm.startConnectivityMonitor(interfaceStatus); - bool result = cm.startConnectivityMonitor(interfaceStatus); - EXPECT_TRUE(result); + bool result = false; + result = cm.startConnectivityMonitor(); + EXPECT_TRUE(result); + result = cm.startConnectivityMonitor(); + EXPECT_TRUE(result); } TEST_F(ConnectivityMonitorTest, SetEndpoints_Valid) { std::vector endpoints = {"https://github.com/rdkcentral", "https://github.com/rdkcentral/rdkservices"}; cm.setConnectivityMonitorEndpoints(endpoints); EXPECT_EQ(cm.getConnectivityMonitorEndpoints(), endpoints); -} +} + TEST_F(ConnectivityMonitorTest, SetEndpoints_EmptyList) { std::vector endpoints; cm.setConnectivityMonitorEndpoints(endpoints); EXPECT_TRUE(cm.getConnectivityMonitorEndpoints().empty()); } + TEST_F(ConnectivityMonitorTest, SetEndpoints_InvalidShortEndpoints) { std::vector endpoints = {"ab", "htt", "xyz"}; cm.setConnectivityMonitorEndpoints(endpoints); EXPECT_TRUE(cm.getConnectivityMonitorEndpoints().empty()); } + TEST_F(ConnectivityMonitorTest, SetEndpoints_DuplicateEndpoints) { std::vector endpoints = {"https://github.com", "https://github.com"}; cm.setConnectivityMonitorEndpoints(endpoints); EXPECT_EQ(cm.getConnectivityMonitorEndpoints().size(), 2); } - - - - diff --git a/Tests/unit_test/test_NetworkManagerStunClient.cpp b/Tests/unit_test/test_NetworkManagerStunClient.cpp index b235eb2d..5276344a 100644 --- a/Tests/unit_test/test_NetworkManagerStunClient.cpp +++ b/Tests/unit_test/test_NetworkManagerStunClient.cpp @@ -1,23 +1,3 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include #include #include "NetworkManagerStunClient.h" @@ -28,42 +8,30 @@ using namespace stun; class AddressTest : public ::testing::Test { protected: stun::attributes::address ad; - void SetUp() override { - - } - void TearDown() override { - - } }; class ClientTest : public ::testing::Test { protected: - stun::client cl; - void SetUp() override { - - } - void TearDown() override { - - } + stun::client _client; }; TEST_F(ClientTest, BindSuccess) { stun::bind_result result; - bool success = cl.bind("https://github.com/", 3478, "eth0", stun::protocol::af_inet, 5000, 10000, result); + bool success = _client.bind("https://github.com/", 3478, "eth0", stun::protocol::af_inet, 5000, 10000, result); EXPECT_FALSE(success); EXPECT_FALSE(result.is_valid()); } TEST_F(ClientTest, BindFailure) { stun::bind_result result; - bool success = cl.bind("http//tata.com", 3478, "eth0", stun::protocol::af_inet, 5000, 10000, result); + bool success = _client.bind("http//tata.com", 3478, "eth0", stun::protocol::af_inet, 5000, 10000, result); EXPECT_FALSE(success); EXPECT_FALSE(result.is_valid()); } + TEST_F(ClientTest, BindWithInvalidInterface) { stun::bind_result result; - bool success = cl.bind("https://github.com/", 3478, "invalid_interface", stun::protocol::af_inet, 5000, 10000, result); + bool success = _client.bind("https://github.com/", 3478, "invalid_interface", stun::protocol::af_inet, 5000, 10000, result); EXPECT_FALSE(success); EXPECT_FALSE(result.is_valid()); } - diff --git a/Tests/unit_test/test_WiFiSignalStrengthMonitor.cpp b/Tests/unit_test/test_WiFiSignalStrengthMonitor.cpp index a2e1d3f9..5618a6cb 100644 --- a/Tests/unit_test/test_WiFiSignalStrengthMonitor.cpp +++ b/Tests/unit_test/test_WiFiSignalStrengthMonitor.cpp @@ -17,11 +17,12 @@ namespace WPEFramework { namespace Plugin { - NetworkManagerImplementation* _instance; + NetworkManagerImplementation* _instance = nullptr; void NetworkManagerImplementation::ReportWiFiSignalStrengthChange(const string ssid, const string strength, const WiFiSignalQuality quality) - { + { return; } + void NetworkManagerImplementation::ReportInternetStatusChange(const InternetStatus prevState, const InternetStatus currState) { return; @@ -32,12 +33,7 @@ namespace WPEFramework class WiFiSignalStrengthMonitorTest : public ::testing::Test { protected: WPEFramework::Plugin::WiFiSignalStrengthMonitor monitor; - void SetUp() override { - } - void TearDown() override { - } }; -#define BUFFER_SIZE 1024 TEST_F(WiFiSignalStrengthMonitorTest, GetSignalData_Connected) { std::string ssid = "TestSSID"; @@ -47,12 +43,10 @@ TEST_F(WiFiSignalStrengthMonitorTest, GetSignalData_Connected) { } TEST_F(WiFiSignalStrengthMonitorTest, StartWiFiSignalStrengthMonitor) { - monitor.startWiFiSignalStrengthMonitor(1); - + monitor.startWiFiSignalStrengthMonitor(1); } int main(int argc, char **argv) { ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); } - diff --git a/Tests/unit_test/tests.cmake b/Tests/unit_test/unit_tests.cmake similarity index 96% rename from Tests/unit_test/tests.cmake rename to Tests/unit_test/unit_tests.cmake index 82d2cd92..9c56c242 100644 --- a/Tests/unit_test/tests.cmake +++ b/Tests/unit_test/unit_tests.cmake @@ -1,5 +1,4 @@ -cmake_minimum_required(VERSION 3.10) -set(UNIT_TEST "tests") +set(UNIT_TEST "unit_tests") find_package(PkgConfig REQUIRED) pkg_check_modules(GLIB REQUIRED glib-2.0) @@ -22,10 +21,12 @@ add_executable(${UNIT_TEST} NetworkManagerConnectivity.cpp NetworkManagerStunClient.cpp ) + set_target_properties(${UNIT_TEST} PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES ) + target_compile_options(${UNIT_TEST} PRIVATE -Wall -include ${CMAKE_SOURCE_DIR}/INetworkManager.h) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage") @@ -43,4 +44,3 @@ target_include_directories(${UNIT_TEST} PRIVATE target_link_libraries(${UNIT_TEST} PRIVATE gmock_main ${NAMESPACE}Core::${NAMESPACE}Core ${GLIB_LIBRARIES} ${GIO_LIBRARIES} ${LIBNM_LIBRARIES} ${CURL_LIBRARIES}) target_include_directories(${UNIT_TEST} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) install(TARGETS ${UNIT_TEST} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) -