diff --git a/.github/workflows/ekf_functional_change_indicator.yml b/.github/workflows/ekf_functional_change_indicator.yml index 3900e06443..d84572386e 100644 --- a/.github/workflows/ekf_functional_change_indicator.yml +++ b/.github/workflows/ekf_functional_change_indicator.yml @@ -23,7 +23,7 @@ jobs: - name: main test uses: addnab/docker-run-action@v3 with: - image: px4io/px4-dev-base-focal:2021-09-08 + image: px4io/px4-dev:v1.16.0-rc1-258-g0369abd556 options: -v ${{ github.workspace }}:/workspace run: | cd /workspace diff --git a/.github/workflows/ekf_update_change_indicator.yml b/.github/workflows/ekf_update_change_indicator.yml index 40ce14ec54..991dfd397b 100644 --- a/.github/workflows/ekf_update_change_indicator.yml +++ b/.github/workflows/ekf_update_change_indicator.yml @@ -19,7 +19,7 @@ jobs: - name: main test uses: addnab/docker-run-action@v3 with: - image: px4io/px4-dev-base-focal:2021-09-08 + image: px4io/px4-dev:v1.16.0-rc1-258-g0369abd556 options: -v ${{ github.workspace }}:/workspace run: | cd /workspace diff --git a/.gitmodules b/.gitmodules index 1ebafcf5a4..081c2ca994 100644 --- a/.gitmodules +++ b/.gitmodules @@ -89,3 +89,6 @@ [submodule "src/drivers/uavcan/libdronecan/libuavcan/dsdl_compiler/pydronecan"] path = src/drivers/uavcan/libdronecan/libuavcan/dsdl_compiler/pydronecan url = https://github.com/dronecan/pydronecan +[submodule "test/fuzztest"] + path = test/fuzztest + url = https://github.com/google/fuzztest.git diff --git a/CMakeLists.txt b/CMakeLists.txt index c15937c0fb..d827f7a8c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -334,6 +334,33 @@ if(NOT PX4_CHIP) message(FATAL_ERROR "px4_os_determine_build_chip() needs to set PX4_CHIP") endif() +#============================================================================= +# Testing - Automatic unit and integration testing with CTest +# (Needs to be before setting the common compile flags) +# + +# optionally enable cmake testing (supported only on posix) +option(CMAKE_TESTING "Configure test targets" OFF) +if(${PX4_CONFIG} STREQUAL "px4_sitl_test") + set(CMAKE_TESTING ON) +endif() +if(CMAKE_TESTING) + include(CTest) # sets BUILD_TESTING variable +endif() + +# enable test filtering to run only specific tests with the ctest -R regex functionality +set(TESTFILTER "" CACHE STRING "Filter string for ctest to selectively only run specific tests (ctest -R)") + +include(px4_add_gtest) +if(BUILD_TESTING) + # Setting FUZZTEST_FUZZING_MODE=on enables ASAN, and is only supported with Clang + if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "AppleClang")) + set(FUZZTEST_FUZZING_MODE ON) + endif() + add_subdirectory(test) + fuzztest_setup_fuzzing_flags() +endif() + #============================================================================= # build flags # @@ -386,46 +413,6 @@ if (NOT EXTERNAL_MODULES_LOCATION STREQUAL "") endforeach() endif() -#============================================================================= -# Testing - Automatic unit and integration testing with CTest -# - -# optionally enable cmake testing (supported only on posix) -option(CMAKE_TESTING "Configure test targets" OFF) -if(${PX4_CONFIG} STREQUAL "px4_sitl_test") - set(CMAKE_TESTING ON) -endif() -if(CMAKE_TESTING) - include(CTest) # sets BUILD_TESTING variable -endif() - -# enable test filtering to run only specific tests with the ctest -R regex functionality -set(TESTFILTER "" CACHE STRING "Filter string for ctest to selectively only run specific tests (ctest -R)") - -# if testing is enabled download and configure gtest -list(APPEND CMAKE_MODULE_PATH ${PX4_SOURCE_DIR}/cmake/gtest/) -include(px4_add_gtest) -if(BUILD_TESTING) - include(gtest) - - # Ensure there's no -R without any filter expression since that trips newer ctest versions - if(TESTFILTER) - set(TESTFILTERARG "-R") - else() - set(TESTFILTERARG "") - endif() - - add_custom_target(test_results - COMMAND GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND} --output-on-failure -T Test ${TESTFILTERARG} ${TESTFILTER} - DEPENDS - px4 - examples__dyn_hello - USES_TERMINAL - COMMENT "Running tests" - WORKING_DIRECTORY ${PX4_BINARY_DIR}) - set_target_properties(test_results PROPERTIES EXCLUDE_FROM_ALL TRUE) -endif() - #============================================================================= # subdirectories # diff --git a/Tools/astyle/files_to_check_code_style.sh b/Tools/astyle/files_to_check_code_style.sh index 266760d7af..7c480f1be2 100755 --- a/Tools/astyle/files_to_check_code_style.sh +++ b/Tools/astyle/files_to_check_code_style.sh @@ -24,6 +24,7 @@ exec find boards msg src platforms test \ -path src/modules/ekf2/EKF/yaw_estimator/derivation/generated -prune -o \ -path src/modules/gyro_fft/CMSIS_5 -prune -o \ -path src/modules/mavlink/mavlink -prune -o \ + -path test/fuzztest -prune -o \ -path test/mavsdk_tests/catch2 -prune -o \ -path src/lib/crypto/monocypher -prune -o \ -path src/lib/crypto/libtomcrypt -prune -o \ diff --git a/cmake/gtest/CMakeLists.txt.in b/cmake/gtest/CMakeLists.txt.in deleted file mode 100644 index aeebfe68db..0000000000 --- a/cmake/gtest/CMakeLists.txt.in +++ /dev/null @@ -1,18 +0,0 @@ -cmake_minimum_required(VERSION 3.5) - -project(googletest-download NONE) - -include(ExternalProject) -ExternalProject_Add(googletest - URL https://github.com/google/googletest/archive/b796f7d44681514f58a683a3a71ff17c94edb0c1.zip # 1.13 - SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src" - BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - # Wrap download, configure and build steps in a script to log output - LOG_DOWNLOAD ON - LOG_CONFIGURE ON - LOG_BUILD ON -) diff --git a/cmake/gtest/gtest.cmake b/cmake/gtest/gtest.cmake deleted file mode 100644 index 7aa70e04d9..0000000000 --- a/cmake/gtest/gtest.cmake +++ /dev/null @@ -1,52 +0,0 @@ -############################################################################ -# -# Copyright (c) 2019 PX4 Development Team. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in -# the documentation and/or other materials provided with the -# distribution. -# 3. Neither the name PX4 nor the names of its contributors may be -# used to endorse or promote products derived from this software -# without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS -# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, -# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS -# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED -# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN -# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. -# -############################################################################ - -# Download and unpack googletest at configure time -configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in googletest-download/CMakeLists.txt) -execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . RESULT_VARIABLE result1 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download) -execute_process(COMMAND ${CMAKE_COMMAND} --build . RESULT_VARIABLE result2 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download) -if(result1 OR result2) - message(FATAL_ERROR "Preparing googletest failed: ${result1} ${result2}") -endif() - -# Add googletest, defines gtest and gtest_main targets -add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src ${CMAKE_CURRENT_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL) - -# Remove visibility.h from the compile flags for gtest because of poisoned exit() -get_target_property(GTEST_COMPILE_FLAGS gtest COMPILE_OPTIONS) -list(REMOVE_ITEM GTEST_COMPILE_FLAGS "-include") -list(REMOVE_ITEM GTEST_COMPILE_FLAGS "visibility.h") -# Remove float warnings added by PX4 which trigger in gtest-printers.h -list(REMOVE_ITEM GTEST_COMPILE_FLAGS "-Wdouble-promotion") -list(REMOVE_ITEM GTEST_COMPILE_FLAGS "-Wfloat-equal") -set_target_properties(gtest PROPERTIES COMPILE_OPTIONS "${GTEST_COMPILE_FLAGS}") diff --git a/cmake/px4_add_common_flags.cmake b/cmake/px4_add_common_flags.cmake index 3daedfa044..ff3842053b 100644 --- a/cmake/px4_add_common_flags.cmake +++ b/cmake/px4_add_common_flags.cmake @@ -105,6 +105,7 @@ function(px4_add_common_flags) -Wno-unknown-warning-option -Wno-unused-const-variable -Wno-varargs + -Wno-vla-cxx-extension # FIXME: do not use variable length arrays ) elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") diff --git a/cmake/gtest/px4_add_gtest.cmake b/cmake/px4_add_gtest.cmake similarity index 91% rename from cmake/gtest/px4_add_gtest.cmake rename to cmake/px4_add_gtest.cmake index c93b15734a..f4cee17e99 100644 --- a/cmake/gtest/px4_add_gtest.cmake +++ b/cmake/px4_add_gtest.cmake @@ -57,7 +57,7 @@ function(px4_add_unit_gtest) add_executable(${TESTNAME} EXCLUDE_FROM_ALL ${SRC} ${EXTRA_SRCS}) # link the libary to test and gtest - target_link_libraries(${TESTNAME} ${LINKLIBS} gtest_main) + target_link_libraries(${TESTNAME} PRIVATE ${LINKLIBS}) if(COMPILE_FLAGS) target_compile_options(${TESTNAME} PRIVATE ${COMPILE_FLAGS}) @@ -66,6 +66,7 @@ function(px4_add_unit_gtest) if(INCLUDES) target_include_directories(${TESTNAME} PRIVATE ${INCLUDES}) endif() + link_fuzztest(${TESTNAME}) # add the test to the ctest plan add_test(NAME ${TESTNAME} @@ -97,7 +98,7 @@ function(px4_add_functional_gtest) add_executable(${TESTNAME} EXCLUDE_FROM_ALL ${SRC} ${EXTRA_SRCS}) # link the libary to test and gtest - target_link_libraries(${TESTNAME} ${LINKLIBS} gtest_functional_main + target_link_libraries(${TESTNAME} PRIVATE ${LINKLIBS} gtest_functional_main px4_layer px4_platform uORB @@ -111,7 +112,9 @@ function(px4_add_functional_gtest) perf tinybson uorb_msgs - test_stubs) #put test_stubs last + fuzztest::fuzztest # Do not use link_fuzztest() here because that + # also links to fuzztest_gtest_main + test_stubs) # put test_stubs last if(COMPILE_FLAGS) target_compile_options(${TESTNAME} PRIVATE ${COMPILE_FLAGS}) diff --git a/platforms/posix/src/px4/common/gtest_runner/CMakeLists.txt b/platforms/posix/src/px4/common/gtest_runner/CMakeLists.txt index 0000dda9f8..735769d32f 100644 --- a/platforms/posix/src/px4/common/gtest_runner/CMakeLists.txt +++ b/platforms/posix/src/px4/common/gtest_runner/CMakeLists.txt @@ -36,4 +36,4 @@ set(SRCS ) px4_add_library(gtest_functional_main ${SRCS}) -target_link_libraries(gtest_functional_main PUBLIC gtest) +target_link_libraries(gtest_functional_main PUBLIC gtest fuzztest::init_fuzztest) diff --git a/platforms/posix/src/px4/common/gtest_runner/gtest_functional_main.cpp b/platforms/posix/src/px4/common/gtest_runner/gtest_functional_main.cpp index ef0719b5be..bb32ca4212 100644 --- a/platforms/posix/src/px4/common/gtest_runner/gtest_functional_main.cpp +++ b/platforms/posix/src/px4/common/gtest_runner/gtest_functional_main.cpp @@ -32,6 +32,7 @@ ****************************************************************************/ #include +#include #include @@ -41,6 +42,9 @@ int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); + fuzztest::ParseAbslFlags(argc, argv); + fuzztest::InitFuzzTest(&argc, &argv); + uORB::Manager::initialize(); param_init(); return RUN_ALL_TESTS(); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000000..d3815d44d7 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,23 @@ +px4_add_git_submodule(TARGET git_fuzztest PATH "fuzztest") + +message(STATUS "Adding fuzztest") +# This will also add GTest +add_subdirectory(fuzztest EXCLUDE_FROM_ALL) + +# Ensure there's no -R without any filter expression since that trips newer ctest versions +if(TESTFILTER) + set(TESTFILTERARG "-R") +else() + set(TESTFILTERARG "") +endif() + +add_custom_target(test_results + # antlr4_tests_NOT_BUILT gets added by fuzztest + COMMAND GTEST_COLOR=1 ${CMAKE_CTEST_COMMAND} --output-on-failure -T Test ${TESTFILTERARG} ${TESTFILTER} --exclude-regex "antlr4_tests_NOT_BUILT" + DEPENDS + px4 + examples__dyn_hello + USES_TERMINAL + COMMENT "Running tests" + WORKING_DIRECTORY ${PX4_BINARY_DIR}) +set_target_properties(test_results PROPERTIES EXCLUDE_FROM_ALL TRUE) diff --git a/test/fuzztest b/test/fuzztest new file mode 160000 index 0000000000..1e47f9d743 --- /dev/null +++ b/test/fuzztest @@ -0,0 +1 @@ +Subproject commit 1e47f9d7437de5c3ee4cb0ac860d5ec875478059