diff options
author | Petr Štetiar <ynezz@true.cz> | 2021-03-19 17:04:29 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-04-23 00:42:30 +0200 |
commit | df73b25de218833588a9770f6beb9e4a2908bcb7 (patch) | |
tree | dc28a1b956ad7699a13914f896f37295242a4c1e | |
parent | 41d33d0b2b09efb7b3cddefa2793cf2133a7b5dc (diff) |
tests: add more tests
* add cram based tests
* test under either valgrind or LLVM sanitizers
* add libFuzzer template
Signed-off-by: Petr Štetiar <ynezz@true.cz>
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | CMakeLists.txt | 19 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 6 | ||||
-rw-r--r-- | tests/cram/CMakeLists.txt | 27 | ||||
-rw-r--r-- | tests/cram/test_basic.t | 58 | ||||
-rw-r--r-- | tests/custom/CMakeLists.txt | 11 | ||||
-rw-r--r-- | tests/fuzz/CMakeLists.txt | 16 | ||||
-rw-r--r-- | tests/fuzz/corpus/.keep | 0 | ||||
-rw-r--r-- | tests/fuzz/test-fuzz.c | 9 |
9 files changed, 144 insertions, 3 deletions
@@ -8,3 +8,4 @@ parser.out contrib/lemon utpl lib/*.so +tests/cram/*.t.err diff --git a/CMakeLists.txt b/CMakeLists.txt index c9eb31a..88ee376 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,8 @@ IF(JSONC_FOUND) INCLUDE_DIRECTORIES(${JSONC_INCLUDE_DIRS}) ENDIF() -ADD_EXECUTABLE(ucode main.c lexer.c lib.c vm.c chunk.c value.c object.c compiler.c source.c) +SET(UCODE_SOURCES main.c lexer.c lib.c vm.c chunk.c value.c object.c compiler.c source.c) +ADD_EXECUTABLE(ucode ${UCODE_SOURCES}) TARGET_LINK_LIBRARIES(ucode ${json}) CHECK_FUNCTION_EXISTS(dlopen DLOPEN_FUNCTION_EXISTS) @@ -89,12 +90,26 @@ ENDIF() IF(UCI_SUPPORT) FIND_LIBRARY(uci NAMES uci) + FIND_LIBRARY(ubox NAMES ubox) FIND_PATH(uci_include_dir uci.h) INCLUDE_DIRECTORIES(${uci_include_dir}) SET(LIBRARIES ${LIBRARIES} uci_lib) ADD_LIBRARY(uci_lib MODULE lib/uci.c) SET_TARGET_PROPERTIES(uci_lib PROPERTIES OUTPUT_NAME uci PREFIX "") - TARGET_LINK_LIBRARIES(uci_lib ${uci}) + TARGET_LINK_LIBRARIES(uci_lib ${uci} ${ubox}) +ENDIF() + +IF(UNIT_TESTING) + ENABLE_TESTING() + ADD_DEFINITIONS(-DUNIT_TESTING) + ADD_SUBDIRECTORY(tests) + + IF(CMAKE_C_COMPILER_ID STREQUAL "Clang") + ADD_EXECUTABLE(ucode-san ${UCODE_SOURCES}) + TARGET_LINK_LIBRARIES(ucode-san ${json}) + TARGET_COMPILE_OPTIONS(ucode-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all) + TARGET_LINK_OPTIONS(ucode-san PRIVATE -fsanitize=undefined,address,leak) + ENDIF() ENDIF() INSTALL(TARGETS ucode RUNTIME DESTINATION bin) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..aed0100 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,6 @@ +ADD_SUBDIRECTORY(cram) +ADD_SUBDIRECTORY(custom) + +IF(CMAKE_C_COMPILER_ID STREQUAL "Clang") + ADD_SUBDIRECTORY(fuzz) +ENDIF() diff --git a/tests/cram/CMakeLists.txt b/tests/cram/CMakeLists.txt new file mode 100644 index 0000000..a93add5 --- /dev/null +++ b/tests/cram/CMakeLists.txt @@ -0,0 +1,27 @@ +FIND_PACKAGE(PythonInterp 3 REQUIRED) +FILE(GLOB test_cases "test_*.t") + +SET(PYTHON_VENV_DIR "${CMAKE_CURRENT_BINARY_DIR}/.venv") +SET(PYTHON_VENV_PIP "${PYTHON_VENV_DIR}/bin/pip") +SET(PYTHON_VENV_CRAM "${PYTHON_VENV_DIR}/bin/cram") + +ADD_CUSTOM_COMMAND( + OUTPUT ${PYTHON_VENV_CRAM} + COMMAND ${PYTHON_EXECUTABLE} -m venv ${PYTHON_VENV_DIR} + COMMAND ${PYTHON_VENV_PIP} install cram +) +ADD_CUSTOM_TARGET(prepare-cram-venv ALL DEPENDS ${PYTHON_VENV_CRAM}) + +ADD_TEST( + NAME cram + COMMAND ${PYTHON_VENV_CRAM} ${test_cases} ${test_cases_san} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +SET_PROPERTY(TEST cram APPEND PROPERTY ENVIRONMENT "BUILD_BIN_DIR=$<TARGET_FILE_DIR:ucode>") + +IF(CMAKE_C_COMPILER_ID STREQUAL "Clang") + SET_PROPERTY(TEST cram APPEND PROPERTY ENVIRONMENT "UCODE_BIN=$<TARGET_FILE:ucode-san>") +ELSE() + SET_PROPERTY(TEST cram APPEND PROPERTY ENVIRONMENT "UCODE_BIN=valgrind --quiet --leak-check=full $<TARGET_FILE:ucode>") +ENDIF() diff --git a/tests/cram/test_basic.t b/tests/cram/test_basic.t new file mode 100644 index 0000000..5061d37 --- /dev/null +++ b/tests/cram/test_basic.t @@ -0,0 +1,58 @@ +setup common environment: + + $ [ -n "$BUILD_BIN_DIR" ] && export PATH="$BUILD_BIN_DIR:$PATH" + $ alias ucode="$UCODE_BIN" + + $ for m in $BUILD_BIN_DIR/*.so; do + > ln -s "$m" "$(pwd)/$(basename $m)"; \ + > done + +check that ucode provides exepected help: + + $ ucode | sed 's/ucode-san/ucode/' + == Usage == + + # ucode [-d] [-l] [-r] [-S] [-e '[prefix=]{"var": ...}'] [-E [prefix=]env.json] {-i <file> | -s "ucode script..."} + -h, --help\tPrint this help (esc) + -i file\tSpecify an ucode script to parse (esc) + -s "ucode script..."\tSpecify an ucode fragment to parse (esc) + -d Instead of executing the script, dump the resulting AST as dot + -l Do not strip leading block whitespace + -r Do not trim trailing block newlines + -S Enable strict mode + -e Set global variables from given JSON object + -E Set global variables from given JSON file + -m Preload given module + +check that ucode prints greetings: + + $ ucode -s "{% print('hello world') %}" + hello world (no-eol) + +check that ucode provides proper error messages: + + $ ucode -m foo + One of -i or -s is required + [1] + + $ ucode -m foo -s '' + Runtime error: No module named 'foo' could be found + At offset 0 + + [1] + + $ touch moo; ucode -m foo -i moo + Runtime error: No module named 'foo' could be found + At offset 0 + + [1] + +check that ucode can load fs module: + + $ ucode -m fs + One of -i or -s is required + [1] + + $ ucode -m fs -s '' + + $ touch moo; ucode -m fs -i moo diff --git a/tests/custom/CMakeLists.txt b/tests/custom/CMakeLists.txt index 9672fce..d56cb99 100644 --- a/tests/custom/CMakeLists.txt +++ b/tests/custom/CMakeLists.txt @@ -3,5 +3,14 @@ ADD_TEST( COMMAND run_tests.sh WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) +SET_PROPERTY(TEST custom APPEND PROPERTY ENVIRONMENT "UCODE_BIN=valgrind --quiet --leak-check=full $<TARGET_FILE:ucode>") -SET_PROPERTY(TEST custom APPEND PROPERTY ENVIRONMENT "UCODE_BIN=$<TARGET_FILE:ucode>") +IF(CMAKE_C_COMPILER_ID STREQUAL "Clang") + ADD_TEST( + NAME custom-san + COMMAND run_tests.sh + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + ) + + SET_PROPERTY(TEST custom-san APPEND PROPERTY ENVIRONMENT "UCODE_BIN=$<TARGET_FILE:ucode-san>") +ENDIF() diff --git a/tests/fuzz/CMakeLists.txt b/tests/fuzz/CMakeLists.txt new file mode 100644 index 0000000..384e7e0 --- /dev/null +++ b/tests/fuzz/CMakeLists.txt @@ -0,0 +1,16 @@ +MACRO(ADD_FUZZER_TEST name) + ADD_EXECUTABLE(${name} ${name}.c) + TARGET_COMPILE_OPTIONS(${name} PRIVATE -g -O1 -fno-omit-frame-pointer -fsanitize=fuzzer,address,leak,undefined) + TARGET_INCLUDE_DIRECTORIES(${name} PRIVATE ${PROJECT_SOURCE_DIR}) + TARGET_LINK_OPTIONS(${name} PRIVATE -stdlib=libc++ -fsanitize=fuzzer,address,leak,undefined) + ADD_TEST( + NAME ${name} + COMMAND ${name} -max_len=256 -timeout=10 -max_total_time=300 ${CMAKE_CURRENT_SOURCE_DIR}/corpus + ) +ENDMACRO(ADD_FUZZER_TEST) + +FILE(GLOB test_cases "test-*.c") +FOREACH(test_case ${test_cases}) + GET_FILENAME_COMPONENT(test_case ${test_case} NAME_WE) + ADD_FUZZER_TEST(${test_case}) +ENDFOREACH(test_case) diff --git a/tests/fuzz/corpus/.keep b/tests/fuzz/corpus/.keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/fuzz/corpus/.keep diff --git a/tests/fuzz/test-fuzz.c b/tests/fuzz/test-fuzz.c new file mode 100644 index 0000000..40649e2 --- /dev/null +++ b/tests/fuzz/test-fuzz.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <stdint.h> +#include <stddef.h> +#include <limits.h> + +int LLVMFuzzerTestOneInput(const uint8_t *input, size_t size) +{ + return 0; +} |