diff --git a/CMakeLists.txt b/CMakeLists.txt index 760205ab..8e724613 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ option(USE_BUNDLED_DEPS "Bundle hard to find dependencies into the Falco binary" option(BUILD_WARNINGS_AS_ERRORS "Enable building with -Wextra -Werror flags" OFF) option(MINIMAL_BUILD "Build a minimal version of Falco, containing only the engine and basic input/output (EXPERIMENTAL)" OFF) option(MUSL_OPTIMIZED_BUILD "Enable if you want a musl optimized build" OFF) +option(BUILD_FALCO_UNIT_TESTS "Build falco unit tests" OFF) # gVisor is currently only supported on Linux x86_64 if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT MINIMAL_BUILD) @@ -223,3 +224,7 @@ include(falcoctl) # Packages configuration include(CPackConfig) + +if(BUILD_FALCO_UNIT_TESTS) + add_subdirectory(unit_tests) +endif() diff --git a/unit_tests/CMakeLists.txt b/unit_tests/CMakeLists.txt new file mode 100644 index 00000000..77d1675c --- /dev/null +++ b/unit_tests/CMakeLists.txt @@ -0,0 +1,65 @@ +# +# Copyright (C) 2023 The Falco Authors. +# +# 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. +# + +message(STATUS "Falco unit tests build enabled") + +include(FetchContent) + +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-1.12.1 +) + +FetchContent_MakeAvailable(googletest) + +file(GLOB_RECURSE ENGINE_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/engine/*.cpp) +file(GLOB_RECURSE FALCO_TESTS ${CMAKE_CURRENT_SOURCE_DIR}/falco/*.cpp) + +set(FALCO_UNIT_TESTS_SOURCES + "${ENGINE_TESTS}" + "${FALCO_TESTS}" +) + +set(FALCO_UNIT_TESTS_INCLUDES + PRIVATE + ${CMAKE_SOURCE_DIR}/userspace + ${CMAKE_BINARY_DIR}/userspace/falco # we need it to include indirectly `config_falco.h` file + ${CMAKE_SOURCE_DIR}/userspace/engine # we need it to include indirectly `falco_common.h` file +) + +set(FALCO_UNIT_TESTS_DEPENDENCIES + gtest + gtest_main + falco_engine + sinsp +) + +set(FALCO_UNIT_TESTS_LIBRARIES + gtest + gtest_main + falco_engine + sinsp +) + +message(STATUS "FALCO_UNIT_TESTS_SOURCES: ${FALCO_UNIT_TESTS_SOURCES}") +message(STATUS "FALCO_UNIT_TESTS_INCLUDES: ${FALCO_UNIT_TESTS_INCLUDES}") +message(STATUS "FALCO_UNIT_TESTS_DEPENDENCIES: ${FALCO_UNIT_TESTS_DEPENDENCIES}") +message(STATUS "FALCO_UNIT_TESTS_LIBRARIES: ${FALCO_UNIT_TESTS_LIBRARIES}") + +add_executable(falco_unit_tests ${FALCO_UNIT_TESTS_SOURCES}) +target_include_directories(falco_unit_tests ${FALCO_UNIT_TESTS_INCLUDES}) +target_link_libraries(falco_unit_tests ${FALCO_UNIT_TESTS_DEPENDENCIES}) +add_dependencies(falco_unit_tests ${FALCO_UNIT_TESTS_LIBRARIES}) + +# add_test(multiply_gtests falco_unit_tests) diff --git a/unit_tests/README.md b/unit_tests/README.md new file mode 100644 index 00000000..0e6284e6 --- /dev/null +++ b/unit_tests/README.md @@ -0,0 +1,12 @@ +# Falco unit tests + +## Intro + +Under `unit_tests/engine` and `unit_tests/falco` directories, we have different test suites that could be a single file or an entire directory according to the number and the complexity of tests. + +## Build and Run + +```bash +cmake -DMINIMAL_BUILD=On -DBUILD_BPF=Off -DBUILD_DRIVER=Off -DBUILD_FALCO_UNIT_TESTS=On .. +make falco_unit_tests +``` diff --git a/unit_tests/engine/test_falco_utils.cpp b/unit_tests/engine/test_falco_utils.cpp new file mode 100644 index 00000000..4afc3818 --- /dev/null +++ b/unit_tests/engine/test_falco_utils.cpp @@ -0,0 +1,38 @@ +/* +Copyright (C) 2023 The Falco Authors. + +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 + +TEST(FalcoUtils, is_unix_scheme) +{ + /* Wrong prefix */ + ASSERT_EQ(falco::utils::network::is_unix_scheme("something:///run/falco/falco.sock"), false); + + /* Similar prefix, but wrong */ + ASSERT_EQ(falco::utils::network::is_unix_scheme("unix///falco.sock"), false); + + /* Right prefix, passed as an `rvalue` */ + ASSERT_EQ(falco::utils::network::is_unix_scheme("unix:///falco.sock"), true); + + /* Right prefix, passed as a `std::string` */ + std::string url_string("unix:///falco.sock"); + ASSERT_EQ(falco::utils::network::is_unix_scheme(url_string), true); + + /* Right prefix, passed as a `char[]` */ + char url_char[] = "unix:///falco.sock"; + ASSERT_EQ(falco::utils::network::is_unix_scheme(url_char), true); +}