From 9120eda5beb2725271cea0e16591f4d5727178d7 Mon Sep 17 00:00:00 2001 From: Daniel Skinstad Drabitzius Date: Fri, 23 Aug 2024 15:16:51 +0200 Subject: [PATCH 1/2] test: add cmocka to perform unit-testing Signed-off-by: Daniel Skinstad Drabitzius --- .gitlab-ci.yml | 24 ++++++++++++++++++++++++ CMakeLists.txt | 7 +++++++ tests/cmake/FetchCMocka.cmake | 22 ++++++++++++++++++++++ tests/tests/CMakeLists.txt | 15 +++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 tests/cmake/FetchCMocka.cmake create mode 100644 tests/tests/CMakeLists.txt diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b77cc5df..95423a20 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -48,3 +48,27 @@ test:smoke-build: -DCONFIG_MENDER_PROVIDES_DEPENDS=ON - cmake --build build --parallel $(nproc --all) - ./build/mender-mcu-client.elf --help + +test:unit: + stage: test + image: debian:12-slim + needs: [] + before_script: + - apt-get update && apt-get install -y git cmake libcurl4-openssl-dev libcjson-dev libmbedtls-dev + script: + - cmake -B build tests + -DCONFIG_MENDER_PLATFORM_FLASH_TYPE="posix" + -DCONFIG_MENDER_PLATFORM_LOG_TYPE="posix" + -DCONFIG_MENDER_PLATFORM_NET_TYPE="generic/curl" + -DCONFIG_MENDER_PLATFORM_SCHEDULER_TYPE="posix" + -DCONFIG_MENDER_PLATFORM_STORAGE_TYPE="posix" + -DCONFIG_MENDER_PLATFORM_TLS_TYPE="generic/mbedtls" + -DCONFIG_MENDER_CLIENT_ADD_ON_INVENTORY=ON + -DCONFIG_MENDER_CLIENT_ADD_ON_CONFIGURE=OFF + -DCONFIG_MENDER_CLIENT_CONFIGURE_STORAGE=OFF + -DCONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT=OFF + -DCONFIG_MENDER_FULL_PARSE_ARTIFACT=ON + -DCONFIG_MENDER_PROVIDES_DEPENDS=ON + -DBUILD_TESTS=ON + - cmake --build build --parallel $(nproc --all) + - ctest --test-dir build/tests/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 13819160..bfd82587 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -237,6 +237,13 @@ if (CONFIG_MENDER_PLATFORM_TLS_TYPE STREQUAL "mbedtls") endif() endif() +# cmocka +if (BUILD_TESTS) + include(cmake/FetchCMocka.cmake) + enable_testing() + add_subdirectory(tests) +endif(BUILD_TESTS) + # Define version file(STRINGS "${CMAKE_CURRENT_LIST_DIR}/VERSION" MENDER_CLIENT_VERSION) add_definitions("-DMENDER_CLIENT_VERSION=\"${MENDER_CLIENT_VERSION}\"") diff --git a/tests/cmake/FetchCMocka.cmake b/tests/cmake/FetchCMocka.cmake new file mode 100644 index 00000000..05ddf7cf --- /dev/null +++ b/tests/cmake/FetchCMocka.cmake @@ -0,0 +1,22 @@ +#https://github.com/OlivierLDff/cmocka-cmake-example +include(FetchContent) + +# Declare our target. We want the lastest stable version, not the master. +# Also specify GIT_SHALLOW to avoid cloning branch we don't care about +FetchContent_Declare( + cmocka + GIT_REPOSITORY https://git.cryptomilk.org/projects/cmocka.git + GIT_TAG cmocka-1.1.5 + GIT_SHALLOW 1 +) + +# We want to link to cmocka-static, so we need to set this option before calling the FetchContent_MakeAvailable +# We also don't care about example and tests +set(WITH_STATIC_LIB ON CACHE BOOL "CMocka: Build with a static library" FORCE) +set(WITH_CMOCKERY_SUPPORT OFF CACHE BOOL "CMocka: Install a cmockery header" FORCE) +set(WITH_EXAMPLES OFF CACHE BOOL "CMocka: Build examples" FORCE) +set(UNIT_TESTING OFF CACHE BOOL "CMocka: Build with unit testing" FORCE) +set(PICKY_DEVELOPER OFF CACHE BOOL "CMocka: Build with picky developer flags" FORCE) + +# Download cmocka, and execute its cmakelists.txt +FetchContent_MakeAvailable(cmocka) diff --git a/tests/tests/CMakeLists.txt b/tests/tests/CMakeLists.txt new file mode 100644 index 00000000..4ae39123 --- /dev/null +++ b/tests/tests/CMakeLists.txt @@ -0,0 +1,15 @@ +include_directories(${CMAKE_CURRENT_LIST_DIR}/../../include) +set(MCU_LIBRARIES "mender-mcu-client;pthread;cjson;mbedtls;mbedx509;mbedcrypto") + +# Add tests by adding the target name to this list +# Example: +# list(APPEND TEST_TARGETS mender_test_target) + +foreach(TEST_TARGET IN LISTS TEST_TARGETS) + add_cmocka_test( + ${TEST_TARGET} + SOURCES ${TEST_TARGET}.c + LINK_LIBRARIES ${CMOCKA_LIBRARIES} ${MCU_LIBRARIES}) + + target_link_libraries(${TEST_TARGET} PRIVATE cmocka-static) +endforeach() From 816be69972f2a98f8de6bf717b92413e05318017 Mon Sep 17 00:00:00 2001 From: Daniel Skinstad Drabitzius Date: Fri, 23 Aug 2024 15:17:36 +0200 Subject: [PATCH 2/2] test: unit tests for wildcard-comparison Signed-off-by: Daniel Skinstad Drabitzius --- tests/tests/CMakeLists.txt | 1 + tests/tests/mender_test_client.c | 80 ++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 tests/tests/mender_test_client.c diff --git a/tests/tests/CMakeLists.txt b/tests/tests/CMakeLists.txt index 4ae39123..08fae63b 100644 --- a/tests/tests/CMakeLists.txt +++ b/tests/tests/CMakeLists.txt @@ -4,6 +4,7 @@ set(MCU_LIBRARIES "mender-mcu-client;pthread;cjson;mbedtls;mbedx509;mbedcrypto") # Add tests by adding the target name to this list # Example: # list(APPEND TEST_TARGETS mender_test_target) +list(APPEND TEST_TARGETS mender_test_client) foreach(TEST_TARGET IN LISTS TEST_TARGETS) add_cmocka_test( diff --git a/tests/tests/mender_test_client.c b/tests/tests/mender_test_client.c new file mode 100644 index 00000000..ec9e513e --- /dev/null +++ b/tests/tests/mender_test_client.c @@ -0,0 +1,80 @@ +/** + * @file mender-test-client.c + * @brief Test + * + * Copyright Northern.tech AS + * + * 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. + */ + +#include +#include +#include +#include + +#include "mender-utils.h" +#include "mender-client.h" + +typedef struct { + char *string; + char *wildcard_string; + bool expected; +} TestWildCard; + +static void +test_wildcard_comparison(void **state) { + + TestWildCard test_wildcard[] = { { "wow", "wow", true }, + { "abc_123_def_456_ghi", "abc_123_def_456_g", false }, + { "abc_123_def_456_ghi", "abc*def*ghi", true }, + { "abc_123_def_456_ghi", "abc*123*def*ghi", true }, + { "abc_123_def_456_ghi", "*def*456*", true }, + { "abc_123_def_456_ghi", "abc*789*ghi", false }, + { "hello_world", "hello*world", true }, + { "hello_world", "hello*worlds", false }, + { "a_b_c", "a*_*b*", true }, + { "a_b_c", "a*c*b", false }, + { "abc", "*a*bc*", true }, + { "abcabcabc", "a*c*a*c*", true }, + { "test_key_1", "test_*", true }, + { "test_key_1", "test_", false }, + { "best_test_key_1", "test_*", false }, + { "test_key_1", "", false }, + { "", "", true }, + { "abc", "*", true }, + { "", "abc", false }, + { "", "abc*", false }, + { "", "*", true }, + { "abc", "xyz", false }, + { "abc", "abc*", true }, + { "abc", "*abc", true }, + { "abc", "a*xyz", false }, + { "test_string", "test_*test_*", false }, + { "test_string", "test_*string*", true }, + { "test_test_key", "***", true } }; + + for (size_t i = 0; i < sizeof(test_wildcard) / sizeof(test_wildcard[0]); i++) { + bool result; + mender_utils_compare_wildcard(test_wildcard[i].string, test_wildcard[i].wildcard_string, &result); + assert_int_equal(result, test_wildcard[i].expected); + } +} + +int +main(void) { + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_wildcard_comparison), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +}