From 5a0304ddc9c81e26e870a749a198d341cf074082 Mon Sep 17 00:00:00 2001 From: Sukhmani Minhas <50919130+sukhmanm@users.noreply.github.com> Date: Thu, 25 Feb 2021 16:37:13 -0500 Subject: [PATCH 1/2] Add test and tools directory --- lexicon.txt | 0 sigv4FilePaths.cmake | 14 +++ test/CMakeLists.txt | 93 +++++++++++++++++ test/unit-test/CMakeLists.txt | 53 ++++++++++ test/unit-test/cmock_build.cmake | 58 +++++++++++ tools/cmock/coverage.cmake | 70 +++++++++++++ tools/cmock/create_test.cmake | 168 +++++++++++++++++++++++++++++++ tools/cmock/project.yml | 26 +++++ 8 files changed, 482 insertions(+) create mode 100644 lexicon.txt create mode 100644 sigv4FilePaths.cmake create mode 100644 test/CMakeLists.txt create mode 100644 test/unit-test/CMakeLists.txt create mode 100644 test/unit-test/cmock_build.cmake create mode 100644 tools/cmock/coverage.cmake create mode 100644 tools/cmock/create_test.cmake create mode 100644 tools/cmock/project.yml diff --git a/lexicon.txt b/lexicon.txt new file mode 100644 index 00000000..e69de29b diff --git a/sigv4FilePaths.cmake b/sigv4FilePaths.cmake new file mode 100644 index 00000000..dabde315 --- /dev/null +++ b/sigv4FilePaths.cmake @@ -0,0 +1,14 @@ +# This file sets source files and include directories to variables so that they +# can be reused from different repositories in their CMake based build system by +# including this file. +# +# Files specific to the repository such as test runner and platform tests are +# not added to the variables. + +# SigV4 library source files. +set( SIGV4_SOURCES + "${CMAKE_CURRENT_LIST_DIR}/source/sigv4.c" ) + +# SigV4 library public include directories. +set( SIGV4_INCLUDE_PUBLIC_DIRS + "${CMAKE_CURRENT_LIST_DIR}/source/include" ) \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 00000000..bca8a43f --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,93 @@ +# Project information. +cmake_minimum_required( VERSION 3.13.0 ) +project( "SigV4 unit test" + VERSION 1.0.0 + LANGUAGES C ) + +# Allow the project to be organized into folders. +set_property( GLOBAL PROPERTY USE_FOLDERS ON ) + +# Use C90. +set( CMAKE_C_STANDARD 90 ) +set( CMAKE_C_STANDARD_REQUIRED ON ) + +# Do not allow in-source build. +if( ${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR} ) + message( FATAL_ERROR "In-source build is not allowed. Please build in a separate directory, such as ${PROJECT_SOURCE_DIR}/build." ) +endif() + +# Set global path variables. +get_filename_component( __MODULE_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE ) +set( MODULE_ROOT_DIR ${__MODULE_ROOT_DIR} CACHE INTERNAL "SigV4 repository root." ) + +# Configure options to always show in CMake GUI. +option( BUILD_CLONE_SUBMODULES + "Set this to ON to automatically clone any required Git submodules. When OFF, submodules must be manually cloned." + ON ) + +# Set output directories. +set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin ) +set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) +set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) + +# ====================== Coverity Analysis Configuration ====================== + +# Include filepaths for source and include. +include( ${MODULE_ROOT_DIR}/sigv4FilePaths.cmake ) + +# Target for Coverity analysis that builds the library. +add_library( coverity_analysis + ${SIGV4_SOURCES} ) + +# Build SigV4 library target without custom config dependencies. +target_compile_definitions( coverity_analysis PUBLIC SIGV4_DO_NOT_USE_CUSTOM_CONFIG=1 ) + +# SigV4 public include path. +target_include_directories( coverity_analysis PUBLIC ${SIGV4_INCLUDE_PUBLIC_DIRS} ) + +# ============================ Test Configuration ============================ + +# Define a CMock resource path. +set( CMOCK_DIR ${MODULE_ROOT_DIR}/test/unit-test/CMock CACHE INTERNAL "CMock library source directory." ) + +# Include CMock build configuration. +include( unit-test/cmock_build.cmake ) + +# Check if the CMock source directory exists, and if not present, clone the submodule +# if BUILD_CLONE_SUBMODULES configuration is enabled. +if( NOT EXISTS ${CMOCK_DIR}/src ) + # Attempt to clone CMock. + if( ${BUILD_CLONE_SUBMODULES} ) + clone_cmock() + else() + message( FATAL_ERROR "The required submodule CMock does not exist. Either clone it manually, or set BUILD_CLONE_SUBMODULES to 1 to automatically clone it during build." ) + endif() +endif() + +# Add unit test and coverage configuration. + +# Use CTest utility for managing test runs. This has to be added BEFORE +# defining test targets with add_test() +enable_testing() + +# Disable to include SigV4 custom configuration header. +add_compile_definitions(SIGV4_DO_NOT_USE_CUSTOM_CONFIG) + +# Add build targets for CMock and Unit, required for unit testing. +add_cmock_targets() + +# Add function to enable CMock based tests and coverage. +include( ${MODULE_ROOT_DIR}/tools/cmock/create_test.cmake ) + +# Include build configuration for unit tests. +add_subdirectory( unit-test ) + +# ==================== Coverage Analysis configuration ======================== + +# Add a target for running coverage on tests. +add_custom_target( coverage + COMMAND ${CMAKE_COMMAND} -DCMOCK_DIR=${CMOCK_DIR} + -P ${MODULE_ROOT_DIR}/tools/cmock/coverage.cmake + DEPENDS cmock unity sigv4_utest + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} +) diff --git a/test/unit-test/CMakeLists.txt b/test/unit-test/CMakeLists.txt new file mode 100644 index 00000000..93c183df --- /dev/null +++ b/test/unit-test/CMakeLists.txt @@ -0,0 +1,53 @@ +# Include filepaths for source and include. +include( ${MODULE_ROOT_DIR}/sigv4FilePaths.cmake ) + +# ==================== Define your project name (edit) ======================== +set(project_name "sigv4") + +# ===================== Create your mock here (edit) ======================== +# ================= Create the library under test here (edit) ================== + +# list the files you would like to test here +list(APPEND real_source_files + ${SIGV4_SOURCES} + ) +# list the directories the module under test includes +list(APPEND real_include_directories + . + ${SIGV4_INCLUDE_PUBLIC_DIRS} + ) + +# ===================== Create UnitTest Code here (edit) ===================== + +# list the directories your test needs to include +list(APPEND test_include_directories + . + ${SIGV4_INCLUDE_PUBLIC_DIRS} + ) + +# ============================= (end edit) =================================== + +set(real_name "${project_name}_real") + +create_real_library(${real_name} + "${real_source_files}" + "${real_include_directories}" + "${mock_name}" + ) + +list(APPEND utest_link_list + lib${real_name}.a + ) + +list(APPEND utest_dep_list + ${real_name} + ) + +set(utest_name "${project_name}_utest") +set(utest_source "${project_name}_utest.c") +create_test(${utest_name} + ${utest_source} + "${utest_link_list}" + "${utest_dep_list}" + "${test_include_directories}" + ) diff --git a/test/unit-test/cmock_build.cmake b/test/unit-test/cmock_build.cmake new file mode 100644 index 00000000..ef7da713 --- /dev/null +++ b/test/unit-test/cmock_build.cmake @@ -0,0 +1,58 @@ +# Macro utility to clone the CMock submodule. +macro( clone_cmock ) + find_package( Git REQUIRED ) + message( "Cloning submodule CMock." ) + execute_process( COMMAND rm -rf ${CMOCK_DIR} + COMMAND ${GIT_EXECUTABLE} submodule update --checkout --init --recursive ${CMOCK_DIR} + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + RESULT_VARIABLE CMOCK_CLONE_RESULT ) + + if( NOT ${CMOCK_CLONE_RESULT} STREQUAL "0" ) + message( FATAL_ERROR "Failed to clone CMock submodule." ) + endif() +endmacro() + +# Macro utility to add library targets for Unity and CMock to build configuration. +macro( add_cmock_targets ) + # Build Configuration for CMock and Unity libraries. + list( APPEND CMOCK_INCLUDE_DIRS + "${CMOCK_DIR}/vendor/unity/src/" + "${CMOCK_DIR}/vendor/unity/extras/fixture/src" + "${CMOCK_DIR}/vendor/unity/extras/memory/src" + "${CMOCK_DIR}/src" + ) + + add_library(cmock STATIC + "${CMOCK_DIR}/src/cmock.c" + ) + + set_target_properties(cmock PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib + POSITION_INDEPENDENT_CODE ON + COMPILE_FLAGS "-Og" + ) + + target_include_directories(cmock PUBLIC + ${CMOCK_DIR}/src + ${CMOCK_DIR}/vendor/unity/src/ + ${CMOCK_DIR}/examples + ${CMOCK_INCLUDE_DIRS} + ) + + add_library(unity STATIC + "${CMOCK_DIR}/vendor/unity/src/unity.c" + "${CMOCK_DIR}/vendor/unity/extras/fixture/src/unity_fixture.c" + "${CMOCK_DIR}/vendor/unity/extras/memory/src/unity_memory.c" + ) + + set_target_properties(unity PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib + POSITION_INDEPENDENT_CODE ON + ) + + target_include_directories(unity PUBLIC + ${CMOCK_INCLUDE_DIRS} + ) + + target_link_libraries(cmock unity) +endmacro() diff --git a/tools/cmock/coverage.cmake b/tools/cmock/coverage.cmake new file mode 100644 index 00000000..e035ea4c --- /dev/null +++ b/tools/cmock/coverage.cmake @@ -0,0 +1,70 @@ +# Taken from amazon-freertos repository +cmake_minimum_required(VERSION 3.13) +set(BINARY_DIR ${CMAKE_BINARY_DIR}) +# reset coverage counters +execute_process( + COMMAND lcov --directory ${CMAKE_BINARY_DIR} + --base-directory ${CMAKE_BINARY_DIR} + --zerocounters + + COMMAND mkdir -p ${CMAKE_BINARY_DIR}/coverage + ) +# make the initial/baseline capture a zeroed out files +execute_process( COMMAND lcov --directory ${CMAKE_BINARY_DIR} + --base-directory ${CMAKE_BINARY_DIR} + --initial + --capture + --rc lcov_branch_coverage=1 + --rc genhtml_branch_coverage=1 + --output-file=${CMAKE_BINARY_DIR}/base_coverage.info + ) +file(GLOB files "${CMAKE_BINARY_DIR}/bin/tests/*") + +set(REPORT_FILE ${CMAKE_BINARY_DIR}/utest_report.txt) +file(WRITE ${REPORT_FILE} "") +# execute all files in bin directory, gathering the output to show it in CI +foreach(testname ${files}) + get_filename_component(test + ${testname} + NAME_WLE + ) + message("Running ${testname}") + execute_process(COMMAND ${testname} OUTPUT_FILE ${CMAKE_BINARY_DIR}/${test}_out.txt) + + file(READ ${CMAKE_BINARY_DIR}/${test}_out.txt CONTENTS) + file(APPEND ${REPORT_FILE} "${CONTENTS}") +endforeach() + +# generate Junit style xml output +execute_process(COMMAND ruby + ${CMOCK_DIR}/vendor/unity/auto/parse_output.rb + -xml ${REPORT_FILE} + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + ) + +# capture data after running the tests +execute_process( + COMMAND lcov --capture + --rc lcov_branch_coverage=1 + --rc genhtml_branch_coverage=1 + --base-directory ${CMAKE_BINARY_DIR} + --directory ${CMAKE_BINARY_DIR} + --output-file ${CMAKE_BINARY_DIR}/second_coverage.info + ) + +# combile baseline results (zeros) with the one after running the tests +execute_process( + COMMAND lcov --base-directory ${CMAKE_BINARY_DIR} + --directory ${CMAKE_BINARY_DIR} + --add-tracefile ${CMAKE_BINARY_DIR}/base_coverage.info + --add-tracefile ${CMAKE_BINARY_DIR}/second_coverage.info + --output-file ${CMAKE_BINARY_DIR}/coverage.info + --no-external + --rc lcov_branch_coverage=1 + ) +execute_process( + COMMAND genhtml --rc lcov_branch_coverage=1 + --branch-coverage + --output-directory ${CMAKE_BINARY_DIR}/coverage + ${CMAKE_BINARY_DIR}/coverage.info + ) diff --git a/tools/cmock/create_test.cmake b/tools/cmock/create_test.cmake new file mode 100644 index 00000000..24b7e56f --- /dev/null +++ b/tools/cmock/create_test.cmake @@ -0,0 +1,168 @@ +# Taken from amazon-freertos repository + +#function to create the test executable +function(create_test test_name + test_src + link_list + dep_list + include_list) + set(mocks_dir "${CMAKE_CURRENT_BINARY_DIR}/mocks") + include (CTest) + get_filename_component(test_src_absolute ${test_src} ABSOLUTE) + add_custom_command(OUTPUT ${test_name}_runner.c + COMMAND ruby + ${CMOCK_DIR}/vendor/unity/auto/generate_test_runner.rb + ${MODULE_ROOT_DIR}/tools/cmock/project.yml + ${test_src_absolute} + ${test_name}_runner.c + DEPENDS ${test_src} + ) + add_executable(${test_name} ${test_src} ${test_name}_runner.c) + set_target_properties(${test_name} PROPERTIES + COMPILE_FLAG "-O0 -ggdb" + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/tests" + INSTALL_RPATH_USE_LINK_PATH TRUE + LINK_FLAGS " \ + -Wl,-rpath,${CMAKE_BINARY_DIR}/lib \ + -Wl,-rpath,${CMAKE_CURRENT_BINARY_DIR}/lib" + ) + target_include_directories(${test_name} PUBLIC + ${mocks_dir} + ${include_list} + ) + + target_link_directories(${test_name} PUBLIC + ${CMAKE_CURRENT_BINARY_DIR} + ) + + # link all libraries sent through parameters + foreach(link IN LISTS link_list) + target_link_libraries(${test_name} ${link}) + endforeach() + + # add dependency to all the dep_list parameter + foreach(dependency IN LISTS dep_list) + add_dependencies(${test_name} ${dependency}) + target_link_libraries(${test_name} ${dependency}) + endforeach() + target_link_libraries(${test_name} -lgcov unity) + target_link_directories(${test_name} PUBLIC + ${CMAKE_CURRENT_BINARY_DIR}/lib + ) + add_test(NAME ${test_name} + COMMAND ${CMAKE_BINARY_DIR}/bin/tests/${test_name} + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + ) +endfunction() + +# Run the C preprocessor on target files. +# Takes a CMAKE list of arguments to pass to the C compiler +function(preprocess_mock_list mock_name file_list compiler_args) + set_property(GLOBAL PROPERTY ${mock_name}_processed TRUE) + foreach (target_file IN LISTS file_list) + # Has to be TARGET ALL so the file is pre-processed before CMOCK + # is executed on the file. + add_custom_command(OUTPUT ${target_file}.backup + COMMAND scp ${target_file} ${target_file}.backup + VERBATIM COMMAND ${CMAKE_C_COMPILER} -E ${compiler_args} ${target_file} > ${target_file}.out + ) + add_custom_target(pre_${mock_name} + COMMAND mv ${target_file}.out ${target_file} + DEPENDS ${target_file}.backup + ) + endforeach() + + # Clean up temporary files that were created. + # First we test to see if the backup file still exists. If it does we revert + # the change made to the original file. + foreach (target_file IN LISTS file_list) + add_custom_command(TARGET ${mock_name} + POST_BUILD + COMMAND test ! -e ${target_file}.backup || mv ${target_file}.backup ${target_file} + ) + endforeach() +endfunction() + +# Generates a mock library based on a module's header file +# places the generated source file in the build directory +# @param mock_name: name of the target name +# @param mock_list list of header files to mock +# @param cmock_config configuration file of the cmock framework +# @param mock_include_list include list for the target +# @param mock_define_list special definitions to control compilation +function(create_mock_list mock_name + mock_list + cmock_config + mock_include_list + mock_define_list) + set(mocks_dir "${CMAKE_CURRENT_BINARY_DIR}/mocks") + add_library(${mock_name} SHARED) + foreach (mock_file IN LISTS mock_list) + get_filename_component(mock_file_abs + ${mock_file} + ABSOLUTE + ) + get_filename_component(mock_file_name + ${mock_file} + NAME_WLE + ) + get_filename_component(mock_file_dir + ${mock_file} + DIRECTORY + ) + add_custom_command ( + OUTPUT ${mocks_dir}/mock_${mock_file_name}.c + COMMAND ruby + ${CMOCK_DIR}/lib/cmock.rb + -o${cmock_config} ${mock_file_abs} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + ) + target_sources(${mock_name} PUBLIC + ${mocks_dir}/mock_${mock_file_name}.c + ) + + target_include_directories(${mock_name} PUBLIC + ${mock_file_dir} + ) + endforeach() + target_include_directories(${mock_name} PUBLIC + ${mocks_dir} + ${mock_include_list} + ) + set_target_properties(${mock_name} PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib + POSITION_INDEPENDENT_CODE ON + ) + target_compile_definitions(${mock_name} PUBLIC + ${mock_define_list} + ) + target_link_libraries(${mock_name} cmock unity) +endfunction() + + +function(create_real_library target + src_file + real_include_list + mock_name) + add_library(${target} STATIC + ${src_file} + ) + target_include_directories(${target} PUBLIC + ${real_include_list} + ) + set_target_properties(${target} PROPERTIES + COMPILE_FLAGS "-Wextra -Wpedantic \ + -fprofile-arcs -ftest-coverage -fprofile-generate \ + -Wno-unused-but-set-variable" + LINK_FLAGS "-fprofile-arcs -ftest-coverage \ + -fprofile-generate " + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib + ) + if(NOT(mock_name STREQUAL "")) + add_dependencies(${target} ${mock_name}) + target_link_libraries(${target} + -l${mock_name} + -lgcov + ) + endif() +endfunction() diff --git a/tools/cmock/project.yml b/tools/cmock/project.yml new file mode 100644 index 00000000..8a90416b --- /dev/null +++ b/tools/cmock/project.yml @@ -0,0 +1,26 @@ +# Taken from amazon-freertos repository +:cmock: + :mock_prefix: mock_ + :when_no_prototypes: :warn + :enforce_strict_ordering: TRUE + :plugins: + - :ignore + - :ignore_arg + - :expect_any_args + - :array + - :callback + - :return_thru_ptr + :callback_include_count: true # include a count arg when calling the callback + :callback_after_arg_check: false # check arguments before calling the callback + :treat_as: + uint8: HEX8 + uint16: HEX16 + uint32: UINT32 + int8: INT8 + bool: UINT8 + :includes: # This will add these includes to each mock. + - + - + :treat_externs: :exclude # Now the extern-ed functions will be mocked. + :weak: __attribute__((weak)) + :treat_externs: :include From 60f1ace454535f18d8f54895f6470016c1996712 Mon Sep 17 00:00:00 2001 From: Sukhmani Minhas <50919130+sukhmanm@users.noreply.github.com> Date: Thu, 25 Feb 2021 17:00:27 -0500 Subject: [PATCH 2/2] CMock submodule --- .gitmodules | 3 +++ sigv4FilePaths.cmake | 2 +- test/unit-test/CMock | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 160000 test/unit-test/CMock diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..629e4fae --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "test/unit-test/CMock"] + path = test/unit-test/CMock + url = https://github.com/ThrowTheSwitch/CMock.git diff --git a/sigv4FilePaths.cmake b/sigv4FilePaths.cmake index dabde315..7863562e 100644 --- a/sigv4FilePaths.cmake +++ b/sigv4FilePaths.cmake @@ -11,4 +11,4 @@ set( SIGV4_SOURCES # SigV4 library public include directories. set( SIGV4_INCLUDE_PUBLIC_DIRS - "${CMAKE_CURRENT_LIST_DIR}/source/include" ) \ No newline at end of file + "${CMAKE_CURRENT_LIST_DIR}/source/include" ) diff --git a/test/unit-test/CMock b/test/unit-test/CMock new file mode 160000 index 00000000..3b443e55 --- /dev/null +++ b/test/unit-test/CMock @@ -0,0 +1 @@ +Subproject commit 3b443e551d538e93e86ec7bb56a0d7fde3715a3c