Skip to content

Commit

Permalink
Squashed 'connectivity/libraries/nanostack-libservice/' changes from …
Browse files Browse the repository at this point in the history
…c343e4e2cc..ee958668e3

ee958668e3 Reorganize CMakeFiles based on target (ARMmbed#105)
2584ce4151 Merge pull request ARMmbed#104 from PelionIoT/cmake_add_interface_lib
0bdb64c4bf Update unit tests readme to not use "make check"
245f487472 Update the make command in run_unit_tests script
c297519298 Allow tests to be built by "make all" and remove "check" custom target
289cc6f657 Add test_all option
b66d49364e Added interface library
8e890fbbc6 Fix ns_nvm_helper unit tests (ARMmbed#103)
93fc3fe6b5 Merge pull request ARMmbed#102 from PelionIoT/add_test_readme
848585c8e1 Added readme to test folder
a7dc930933 Merge pull request ARMmbed#101 from PelionIoT/cmake_refactor
032bfecce8 Added UT CI support for GTest
9e8bda84b8 Removed test from make all. Now tests can be build with make check.
17d4bde2c8 Add CPP flags for unittest coverage
bec5b33544 Licence added
454140f974 Fix ut coverage
ca9e1e49f1 CMake improvements
9585b3d67a Fix compilation warnings from dynmem_test.cpp
4ef3fa12ba Remove CXX 11 requirement
a6d275db50 Add root directory to public include directories
2036a7431a Added CMake support + UT -> GTest

git-subtree-dir: connectivity/libraries/nanostack-libservice
git-subtree-split: ee958668e3e4b373ec4e2ee8425b7631fec2d80a
  • Loading branch information
artokin committed Dec 2, 2021
1 parent 16ad9f6 commit c0cf88a
Show file tree
Hide file tree
Showing 38 changed files with 750 additions and 1,429 deletions.
27 changes: 27 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#################################################################################
## Copyright 2021 Pelion.
##
## SPDX-License-Identifier: Apache-2.0
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
#################################################################################

if(DEFINED MBED_TOOLCHAIN)
# Mbed OS target build
include(nanostack-libservice-mbed.cmake)
else()
cmake_minimum_required (VERSION 3.11)
project(nanostack-libservice)
include(nanostack-libservice.cmake)
endif()

54 changes: 54 additions & 0 deletions nanostack-libservice-mbed.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#################################################################################
## Copyright 2020-2021 Pelion.
##
## SPDX-License-Identifier: Apache-2.0
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
#################################################################################

target_include_directories(mbed-nanostack-libservice
INTERFACE
.
./mbed-client-libservice
./mbed-client-libservice/platform
)

target_sources(mbed-nanostack-libservice
INTERFACE
source/IPv6_fcf_lib/ip_fsc.c
source/libList/ns_list.c
source/libip4string/ip4tos.c
source/libip4string/stoip4.c
source/libip6string/stoip6.c
source/nsdynmemLIB/nsdynmemLIB.c
source/nsdynmemtracker/nsdynmem_tracker_lib.c
source/nvmHelper/ns_nvm_helper.c
)

# The definition, source files and include directories below
# are needed by mbed-trace which is part of the mbed-core CMake target
target_compile_definitions(mbed-core
INTERFACE
MBED_CONF_NANOSTACK_LIBSERVICE_PRESENT=1
)
target_include_directories(mbed-core
INTERFACE
.
./mbed-client-libservice
)
target_sources(mbed-core
INTERFACE
source/libBits/common_functions.c
source/libip6string/ip6tos.c
)

151 changes: 151 additions & 0 deletions nanostack-libservice.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#################################################################################
## Copyright 2020-2021 Pelion.
##
## SPDX-License-Identifier: Apache-2.0
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
#################################################################################

cmake_minimum_required (VERSION 3.11)

include(FindUnixCommands)

project(nanostack-libservice)

option(enable_coverage_data "Enable Coverage data" OFF)

include(repositories.cmake)

set(LS_SOURCES
source/IPv6_fcf_lib/ip_fsc.c
source/libBits/common_functions.c
source/libip6string/ip6tos.c
source/libip6string/stoip6.c
source/libList/ns_list.c
source/nsdynmemLIB/nsdynmemLIB.c
source/nsdynmemtracker/nsdynmem_tracker_lib.c
source/nvmHelper/ns_nvm_helper.c)

add_library(nanostack-libservice
${LS_SOURCES})

add_library(nanostack-libserviceInterface INTERFACE)
target_include_directories(nanostack-libserviceInterface INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/mbed-client-libservice)

# some other projects include libservice stuff with path
target_include_directories(nanostack-libservice PUBLIC ${nanostack-libservice_SOURCE_DIR})
target_include_directories(nanostack-libservice PUBLIC ${nanostack-libservice_SOURCE_DIR}/mbed-client-libservice)
target_include_directories(nanostack-libservice PUBLIC ${nanostack-libservice_SOURCE_DIR}/mbed-client-libservice/platform)

if (test_all OR ${CMAKE_PROJECT_NAME} STREQUAL "nanostack-libservice")
# Tests after this line
enable_testing()

if (enable_coverage_data)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif ()

add_executable(ip6tos_test
source/libip6string/ip6tos.c
source/libBits/common_functions.c
test/ip6tos/ip6tos_test.cpp
)

add_test(ip6tos_test ip6tos_test)

target_include_directories(ip6tos_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/mbed-client-libservice)
target_include_directories(ip6tos_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/mbed-client-libservice/platform)

target_link_libraries(
ip6tos_test
gtest_main
)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/test/stubs)

add_executable(stoip6_test
source/libip6string/stoip6.c
source/libBits/common_functions.c
test/stoip6/stoip6_test.cpp
)

add_test(stoip6_test stoip6_test)

target_include_directories(stoip6_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/mbed-client-libservice)
target_include_directories(stoip6_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/mbed-client-libservice/platform)

target_link_libraries(
stoip6_test
gtest_main
)

add_executable(dynmem_test
source/nsdynmemLIB/nsdynmemLIB.c
test/nsdynmem/dynmem_test.cpp
test/nsdynmem/error_callback.c
test/nsdynmem/error_callback.h
test/stubs/platform_critical.c
test/stubs/ns_list_stub.c
)

add_test(dynmem_test dynmem_test)

target_include_directories(dynmem_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/mbed-client-libservice)
target_include_directories(dynmem_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/mbed-client-libservice/platform)

target_link_libraries(
dynmem_test
gtest_main
)

add_executable(nsnvmhelper_test
source/nvmHelper/ns_nvm_helper.c
test/nsnvmhelper/nsnvmhelper_test.cpp
test/nsnvmhelper/test_ns_nvm_helper.c
test/nsnvmhelper/test_ns_nvm_helper.h
test/stubs/platform_nvm_stub.c
test/stubs/nsdynmemLIB_stub.c
test/stubs/ns_list_stub.c
)

target_include_directories(nsnvmhelper_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/mbed-client-libservice)
target_include_directories(nsnvmhelper_test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/mbed-client-libservice/platform)

target_link_libraries(
nsnvmhelper_test
gtest_main
)

# GTest framework requires C++ version 11
set_target_properties(dynmem_test ip6tos_test stoip6_test nsnvmhelper_test
PROPERTIES
CXX_STANDARD 11
)

include(GoogleTest)
gtest_discover_tests(ip6tos_test EXTRA_ARGS --gtest_output=xml: XML_OUTPUT_DIR ip6tos)
gtest_discover_tests(stoip6_test EXTRA_ARGS --gtest_output=xml: XML_OUTPUT_DIR stoip6)
gtest_discover_tests(dynmem_test EXTRA_ARGS --gtest_output=xml: XML_OUTPUT_DIR nsdynmem)
gtest_discover_tests(nsnvmhelper_test EXTRA_ARGS --gtest_output=xml: XML_OUTPUT_DIR nvmhelper)

if (enable_coverage_data AND ${CMAKE_PROJECT_NAME} STREQUAL "nanostack-libservice")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/html")

add_test(NAME ls_cov WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${BASH} -c "gcovr -r . -e ${CMAKE_CURRENT_SOURCE_DIR}/build -e '.*test.*' --html --html-details -o build/html/coverity_index.html"
)
endif ()
endif()

28 changes: 28 additions & 0 deletions repositories.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#################################################################################
## Copyright 2020-2021 Pelion.
##
## SPDX-License-Identifier: Apache-2.0
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
#################################################################################

#Googletest for testing
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.10.x.zip
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
30 changes: 30 additions & 0 deletions run_unit_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash
#
#################################################################################
## Copyright (c) 2021 Pelion. All rights reserved.
##
## SPDX-License-Identifier: Apache-2.0
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
#################################################################################
#
#
# Run unit tests and produce *.xml output to build folder.
#

TEST_DIR="build"

mkdir -p ${TEST_DIR}
cd ${TEST_DIR}
cmake .. -DCMAKE_BUILD_TYPE=Debug -Denable_coverage_data=ON -Dtest_all=ON
make all test
48 changes: 48 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Unit tests

You can run unit tests with script `run_unit_tests.sh`.

The unit tests are implemented with GoogleTest (gtest) C++ test framework.

## Prerequisites

If you need to generate code coverage report, install `gcovr` tool:

```
pip install gcovr
or...
sudo apt-get install gcovr
```

## Building and running

Prepare unit test build with `cmake ..` and then run the test with `make all test` as follows:

```
# clone repository if needed
git clone nanostack-libservice
cd nanostack-libservice
mkdir build
cd build
cmake ..
make all test
```

### Code coverage

Code coverage is optional feature and it is disabled by default.
To enable code coverage use the following command to build the tests:
```
cmake .. -Denable_coverage_data=ON
```

### Debugging

You can add mode debug information to the build by adding a `-DCMAKE_BUILD_TYPE=Debug` to the build command, like:
```
cmake .. -Denable_coverage_data=ON -DCMAKE_BUILD_TYPE=Debug
```
And then debug the generated executable with a debugger like `Visual code`, `gdb`, `Eclipse`.
Loading

0 comments on commit c0cf88a

Please sign in to comment.