From 37195eb065e9b481ea24b3f2cec33642f3fabbf9 Mon Sep 17 00:00:00 2001 From: parvathika <131966254+parvathika@users.noreply.github.com> Date: Tue, 3 Dec 2024 20:14:16 +0530 Subject: [PATCH] Implement unit test file for connectivity and stun (#45) --- .../test_NetworkManagerConnectivity.cpp | 132 ++++++++++++++++++ .../test_NetworkManagerStunClient.cpp | 69 +++++++++ Tests/unit_test/tests.cmake | 3 + 3 files changed, 204 insertions(+) create mode 100644 Tests/unit_test/test_NetworkManagerConnectivity.cpp create mode 100644 Tests/unit_test/test_NetworkManagerStunClient.cpp diff --git a/Tests/unit_test/test_NetworkManagerConnectivity.cpp b/Tests/unit_test/test_NetworkManagerConnectivity.cpp new file mode 100644 index 00000000..f175a929 --- /dev/null +++ b/Tests/unit_test/test_NetworkManagerConnectivity.cpp @@ -0,0 +1,132 @@ +#include "NetworkManagerImplementation.h" +#include "NetworkManagerConnectivity.h" +#include "WiFiSignalStrengthMonitor.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace WPEFramework; + +class ConnectivityMonitorTest : public ::testing::Test { +protected: + + WPEFramework::Plugin::ConnectivityMonitor cm; + + void SetUp() override { + + } + + void TearDown() override { + + } +}; + + TEST_F(ConnectivityMonitorTest, StartContinuousMonitor_Success) { + int timeout = 30; + bool result = cm.startContinuousConnectivityMonitor(timeout); + EXPECT_TRUE(result); +} +TEST_F(ConnectivityMonitorTest, StartContinuousMonitor_FailureNegativeTimeout) { + int timeout = -1; + bool result = cm.startContinuousConnectivityMonitor(timeout); + EXPECT_TRUE(result); +} +TEST_F(ConnectivityMonitorTest, StartMonitorWithTimeoutLessThanMinimum) { + int timeout = 3; + bool result = cm.startContinuousConnectivityMonitor(timeout); + EXPECT_TRUE(result); +} +TEST_F(ConnectivityMonitorTest, MonitorFailsToStart) { + int timeout = 0; + bool result = cm.startContinuousConnectivityMonitor(timeout); + EXPECT_TRUE(result); +} + +TEST_F(ConnectivityMonitorTest, StopContinuousMonitor_WhenStarted) { + int timeout = 30; + cm.startContinuousConnectivityMonitor(timeout); + bool result = cm.stopContinuousConnectivityMonitor(); + EXPECT_TRUE(result); +} +TEST_F(ConnectivityMonitorTest, StopContinuousMonitor_WhenNotStarted) { + bool result = cm.stopContinuousConnectivityMonitor(); + EXPECT_TRUE(result); +} +TEST_F(ConnectivityMonitorTest, StopContinuousMonitor_AfterMultipleStartsAndStops) { + int timeout = 30; + cm.startContinuousConnectivityMonitor(timeout); + bool result = cm.stopContinuousConnectivityMonitor(); + EXPECT_TRUE(result); + + cm.startContinuousConnectivityMonitor(timeout); + result = cm.stopContinuousConnectivityMonitor(); + EXPECT_TRUE(result); + + cm.startContinuousConnectivityMonitor(timeout); + result = cm.stopContinuousConnectivityMonitor(); + 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); +} + + 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_NotifyIfAlreadyMonitoring) { + bool interfaceStatus = true; + cm.startConnectivityMonitor(interfaceStatus); + bool result = cm.startConnectivityMonitor(interfaceStatus); + 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 new file mode 100644 index 00000000..b235eb2d --- /dev/null +++ b/Tests/unit_test/test_NetworkManagerStunClient.cpp @@ -0,0 +1,69 @@ +#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" + +using namespace std; +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 { + + } +}; + +TEST_F(ClientTest, BindSuccess) { + stun::bind_result result; + bool success = cl.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); + 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); + EXPECT_FALSE(success); + EXPECT_FALSE(result.is_valid()); +} + diff --git a/Tests/unit_test/tests.cmake b/Tests/unit_test/tests.cmake index 177c0294..82d2cd92 100644 --- a/Tests/unit_test/tests.cmake +++ b/Tests/unit_test/tests.cmake @@ -15,9 +15,12 @@ FetchContent_Declare( FetchContent_MakeAvailable(googletest) add_executable(${UNIT_TEST} Tests/unit_test/test_WiFiSignalStrengthMonitor.cpp + Tests/unit_test/test_NetworkManagerConnectivity.cpp + Tests/unit_test/test_NetworkManagerStunClient.cpp WiFiSignalStrengthMonitor.cpp NetworkManagerLogger.cpp NetworkManagerConnectivity.cpp + NetworkManagerStunClient.cpp ) set_target_properties(${UNIT_TEST} PROPERTIES CXX_STANDARD 11