Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test code for WifiSignalStrengthMonitor.cpp file #25

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion .github/workflows/gnome_unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ jobs:
unit-tests:
name: Build and run unit tests
runs-on: ubuntu-latest
strategy:
matrix:
compiler: [ gcc, clang ]
coverage: [ with-coverage, without-coverage ]
exclude:
- compiler: clang
coverage: with-coverage
- compiler: clang
coverage: without-coverage
- compiler: gcc
coverage: without-coverage

steps:
- name: Configure cache
Expand All @@ -32,7 +43,7 @@ jobs:
- name: Install packages
run: |
sudo apt update
sudo apt-get install -y pkg-config libglib2.0-dev libnm-dev libcurl4-openssl-dev ninja-build
sudo apt-get install -y pkg-config libglib2.0-dev libnm-dev libcurl4-openssl-dev lcov ninja-build

- name: Configure Python
uses: actions/setup-python@v4
Expand Down Expand Up @@ -90,6 +101,14 @@ jobs:
uses: actions/checkout@v3
with:
path: networkmanager

- name: Set clang toolchain
if: ${{ matrix.compiler == 'clang' }}
run: echo "TOOLCHAIN_FILE=${{github.workspace}}/networkmanager/Tests/clang.cmake" >> $GITHUB_ENV

- name: Set gcc/with-coverage toolchain
if: ${{ matrix.compiler == 'gcc' && matrix.coverage == 'with-coverage' && !env.ACT }}
run: echo "TOOLCHAIN_FILE=${{github.workspace}}/networkmanager/Tests/gcc-with-coverage.cmake" >> $GITHUB_ENV

- name: Build networkmanager with Gnome Proxy
run: >
Expand All @@ -99,7 +118,35 @@ jobs:
-DCMAKE_TOOLCHAIN_FILE="${{ env.TOOLCHAIN_FILE }}"
-DCMAKE_INSTALL_PREFIX="${{github.workspace}}/install/usr"
-DCMAKE_MODULE_PATH="${{github.workspace}}/install/tools/cmake"
-DCMAKE_CXX_FLAGS="
-fprofile-arcs
-ftest-coverage"
-DENABLE_GNOME_NETWORKMANAGER=ON
-DENABLE_UNIT_TESTING=ON
&&
cmake --build build/GnomeNetworkmanager --target install -j8


- name: Generate coverage
if: ${{ matrix.coverage == 'with-coverage' && !env.ACT }}
run: >
cp ${{github.workspace}}/networkmanager/Tests/WiFi/.lcovrc_l1 ~/.lcovrc
&&
lcov -c
-o coverage.info
-d build/GnomeNetworkmanager
&&
lcov
-r coverage.info
'/usr/include/*'
'*/build/GnomeNetworkmanager/_deps/*'
'*/install/usr/include/*'
'*/Tests/headers/*'
'*/Tests/mocks/*'
'*/Tests/WiFi/*'
-o filtered_coverage.info
&&
genhtml
-o coverage
-t "wifisignalstrengthmonitor coverage"
filtered_coverage.info
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ set(PROJECT_VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})
include(CmakeHelperFunctions)

option(USE_RDK_LOGGER "Enable RDK Logger for logging" OFF )
option(ENABLE_UNIT_TESTING "Enable unit tests" ON)


string(TOLOWER ${NAMESPACE} STORAGE_DIRECTORY)
get_directory_property(SEVICES_DEFINES COMPILE_DEFINITIONS)
Expand Down Expand Up @@ -167,3 +169,7 @@ write_config()
write_config(PLUGINS LegacyPlugin_NetworkAPIs CLASSNAME Network LOCATOR lib${PLUGIN_LEGACY_DEPRECATED_NETWORK}.so)
write_config(PLUGINS LegacyPlugin_WiFiManagerAPIs CLASSNAME WiFiManager LOCATOR lib${PLUGIN_LEGACY_DEPRECATED_WIFI}.so)

if(ENABLE_UNIT_TESTING)
include(Tests/WiFi/wifiTest.cmake)
endif(ENABLE_UNIT_TESTING)

181 changes: 181 additions & 0 deletions Tests/WiFi/.lcovrc_l1
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#
# /etc/lcovrc - system-wide defaults for LCOV
#
# To change settings for a single user, place a customized copy of this file
# at location ~/.lcovrc
#

# Specify an external style sheet file (same as --css-file option of genhtml)
#genhtml_css_file = gcov.css

# Specify coverage rate limits (in %) for classifying file entries
# HI: hi_limit <= rate <= 100 graph color: green
# MED: med_limit <= rate < hi_limit graph color: orange
# LO: 0 <= rate < med_limit graph color: red
genhtml_hi_limit = 75
genhtml_med_limit = 50

# Width of line coverage field in source code view
genhtml_line_field_width = 12

# Width of branch coverage field in source code view
genhtml_branch_field_width = 16

# Width of overview image (used by --frames option of genhtml)
genhtml_overview_width = 80

# Resolution of overview navigation: this number specifies the maximum
# difference in lines between the position a user selected from the overview
# and the position the source code window is scrolled to (used by --frames
# option of genhtml)
genhtml_nav_resolution = 4

# Clicking a line in the overview image should show the source code view at
# a position a bit further up so that the requested line is not the first
# line in the window. This number specifies that offset in lines (used by
# --frames option of genhtml)
genhtml_nav_offset = 10

# Do not remove unused test descriptions if non-zero (same as
# --keep-descriptions option of genhtml)
genhtml_keep_descriptions = 0

# Do not remove prefix from directory names if non-zero (same as --no-prefix
# option of genhtml)
genhtml_no_prefix = 0

# Do not create source code view if non-zero (same as --no-source option of
# genhtml)
genhtml_no_source = 0

# Replace tabs with number of spaces in source view (same as --num-spaces
# option of genhtml)
genhtml_num_spaces = 8

# Highlight lines with converted-only data if non-zero (same as --highlight
# option of genhtml)
genhtml_highlight = 0

# Include color legend in HTML output if non-zero (same as --legend option of
# genhtml)
genhtml_legend = 0

# Use FILE as HTML prolog for generated pages (same as --html-prolog option of
# genhtml)
#genhtml_html_prolog = FILE

# Use FILE as HTML epilog for generated pages (same as --html-epilog option of
# genhtml)
#genhtml_html_epilog = FILE

# Use custom filename extension for pages (same as --html-extension option of
# genhtml)
#genhtml_html_extension = html

# Compress all generated html files with gzip.
#genhtml_html_gzip = 1

# Include sorted overview pages (can be disabled by the --no-sort option of
# genhtml)
genhtml_sort = 1

# Include function coverage data display (can be disabled by the
# --no-func-coverage option of genhtml)
#genhtml_function_coverage = 1

# Include branch coverage data display (can be disabled by the
# --no-branch-coverage option of genhtml)
#genhtml_branch_coverage = 1

# Specify the character set of all generated HTML pages
genhtml_charset=UTF-8

# Allow HTML markup in test case description text if non-zero
genhtml_desc_html=0

# Specify the precision for coverage rates
#genhtml_precision=1

# Show missed counts instead of hit counts
#genhtml_missed=1

# Demangle C++ symbols
#genhtml_demangle_cpp=1

# Name of the tool used for demangling C++ function names
#genhtml_demangle_cpp_tool = c++filt

# Specify extra parameters to be passed to the demangling tool
#genhtml_demangle_cpp_params = ""

# Location of the gcov tool (same as --gcov-info option of geninfo)
#geninfo_gcov_tool = gcov

# Adjust test names to include operating system information if non-zero
#geninfo_adjust_testname = 0

# Calculate checksum for each source code line if non-zero (same as --checksum
# option of geninfo if non-zero, same as --no-checksum if zero)
#geninfo_checksum = 1

# Specify whether to capture coverage data for external source files (can
# be overridden by the --external and --no-external options of geninfo/lcov)
#geninfo_external = 1

# Enable libtool compatibility mode if non-zero (same as --compat-libtool option
# of geninfo if non-zero, same as --no-compat-libtool if zero)
#geninfo_compat_libtool = 0

# Use gcov's --all-blocks option if non-zero
#geninfo_gcov_all_blocks = 1

# Specify compatiblity modes (same as --compat option of geninfo).
#geninfo_compat = libtool=on, hammer=auto, split_crc=auto

# Adjust path to source files by removing or changing path components that
# match the specified pattern (Perl regular expression format)
#geninfo_adjust_src_path = /tmp/build => /usr/src

# Specify if geninfo should try to automatically determine the base-directory
# when collecting coverage data.
geninfo_auto_base = 1

# Use gcov intermediate format? Valid values are 0, 1, auto
geninfo_intermediate = auto

# Specify if exception branches should be excluded from branch coverage.
geninfo_no_exception_branch = 0

# Directory containing gcov kernel files
# lcov_gcov_dir = /proc/gcov

# Location of the insmod tool
lcov_insmod_tool = /sbin/insmod

# Location of the modprobe tool
lcov_modprobe_tool = /sbin/modprobe

# Location of the rmmod tool
lcov_rmmod_tool = /sbin/rmmod

# Location for temporary directories
lcov_tmp_dir = /tmp

# Show full paths during list operation if non-zero (same as --list-full-path
# option of lcov)
lcov_list_full_path = 0

# Specify the maximum width for list output. This value is ignored when
# lcov_list_full_path is non-zero.
lcov_list_width = 80

# Specify the maximum percentage of file names which may be truncated when
# choosing a directory prefix in list output. This value is ignored when
# lcov_list_full_path is non-zero.
lcov_list_truncate_max = 20

# Specify if function coverage data should be collected and processed.
lcov_function_coverage = 1

# Specify if branch coverage data should be collected and processed.
lcov_branch_coverage = 0
58 changes: 58 additions & 0 deletions Tests/WiFi/test_WiFiSignalStrengthMonitor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "WiFiSignalStrengthMonitor.h"
#include "NetworkManagerImplementation.h"
#include "NetworkManagerLogger.h"
#include "INetworkManager.h"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <iostream>
#include <thread>
#include <chrono>
#include <list>
#include <string>

using namespace std;
using namespace WPEFramework;
using namespace WPEFramework::Plugin;
namespace WPEFramework
{
namespace Plugin
{
NetworkManagerImplementation* _instance;
void NetworkManagerImplementation::ReportWiFiSignalStrengthChange(const string ssid, const string strength, const WiFiSignalQuality quality)
{
return;
}
void NetworkManagerImplementation::ReportInternetStatusChange(const InternetStatus prevState, const InternetStatus currState)
{
return;
}
}
}

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";
Exchange::INetworkManager::WiFiSignalQuality quality;
std::string strengthOut= "-55";
monitor.getSignalData(ssid, quality, strengthOut);
}

TEST_F(WiFiSignalStrengthMonitorTest, StartWiFiSignalStrengthMonitor) {
monitor.startWiFiSignalStrengthMonitor(1);

}

int main(int argc, char **argv) {
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}

42 changes: 42 additions & 0 deletions Tests/WiFi/wifiTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
cmake_minimum_required(VERSION 3.10)
set(WIFI_TEST "wifiTest")

find_package(PkgConfig REQUIRED)
pkg_check_modules(GLIB REQUIRED glib-2.0)
pkg_check_modules(GIO REQUIRED gio-2.0)
pkg_check_modules(LIBNM REQUIRED libnm)

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)

FetchContent_MakeAvailable(googletest)
add_executable(${WIFI_TEST}
Tests/WiFi/test_WiFiSignalStrengthMonitor.cpp
WiFiSignalStrengthMonitor.cpp
NetworkManagerLogger.cpp
NetworkManagerConnectivity.cpp
)
set_target_properties(${WIFI_TEST} PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
)
target_compile_options(${WIFI_TEST} PRIVATE -Wall -include ${CMAKE_SOURCE_DIR}/INetworkManager.h)

target_include_directories(${WIFI_TEST} PRIVATE
${GLIB_INCLUDE_DIRS}
${LIBNM_INCLUDE_DIRS}
${GIO_INCLUDE_DIRS}
${PROJECT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
Tests
${gtest_SOURCE_DIR}/include
${gtest_SOURCE_DIR}/../googlemock/include
)

target_link_libraries(${WIFI_TEST} PRIVATE gmock_main ${NAMESPACE}Core::${NAMESPACE}Core ${GLIB_LIBRARIES} ${GIO_LIBRARIES} ${LIBNM_LIBRARIES} ${CURL_LIBRARIES})
target_include_directories(${WIFI_TEST} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
install(TARGETS ${WIFI_TEST} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)

12 changes: 12 additions & 0 deletions Tests/clang.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=unused-command-line-argument")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=missing-braces")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=dangling-gsl")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=unused-const-variable")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=inconsistent-missing-override")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=unused-parameter")

# clang Valgrind: debuginfo reader: ensure_valid failed
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gdwarf-4")
1 change: 1 addition & 0 deletions Tests/gcc-with-coverage.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
Loading
Loading