From de752cdbc33847653e827abfcabc3055e985f12e Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 3 Aug 2022 16:18:18 +0200 Subject: [PATCH] tests: Added tests for magic files --- .github/workflows/tests.yml | 3 ++- tests/CMakeLists.txt | 3 ++- tests/magic/CMakeLists.txt | 31 +++++++++++++++++++++++++++++++ tests/magic/source/main.cpp | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/magic/CMakeLists.txt create mode 100644 tests/magic/source/main.cpp diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ba49971..1a84e91 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -33,7 +33,8 @@ jobs: cmake \ make \ python3 \ - python3-pip + python3-pip \ + libmagic-dev sudo pip install jsonschema diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bc328e1..2c6af8d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,5 +18,6 @@ enable_testing() add_subdirectory(patterns) add_subdirectory(includes) +add_subdirectory(magic) -add_custom_target(test_all DEPENDS patterns_tests includes_test) \ No newline at end of file +add_custom_target(test_all DEPENDS patterns_tests includes_test magic_test) \ No newline at end of file diff --git a/tests/magic/CMakeLists.txt b/tests/magic/CMakeLists.txt new file mode 100644 index 0000000..73acfcd --- /dev/null +++ b/tests/magic/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 3.16) + +project(magic_test) + +set(TOP_LEVEL "${CMAKE_CURRENT_SOURCE_DIR}/../..") + +set(MAGIC_FOLDER "${TOP_LEVEL}/magic") +file(GLOB MAGIC_FILES + "${MAGIC_FOLDER}/*" +) + +add_executable(magic_test + source/main.cpp +) + +find_package(PkgConfig REQUIRED) +pkg_search_module(MAGIC libmagic>=5.39) + +target_include_directories(magic_test PRIVATE include) +target_link_libraries(magic_test libpl magic) + +set_target_properties(magic_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) + +foreach (magic_file IN LISTS MAGIC_FILES) + file(RELATIVE_PATH MAGIC_NAME ${MAGIC_FOLDER} ${magic_file}) + + set(TEST_NAME "Magic/${MAGIC_NAME}") + + add_test(NAME ${TEST_NAME} COMMAND magic_test "${MAGIC_NAME}" "${magic_file}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) + set_tests_properties(${TEST_NAME} PROPERTIES SKIP_RETURN_CODE 77) +endforeach () \ No newline at end of file diff --git a/tests/magic/source/main.cpp b/tests/magic/source/main.cpp new file mode 100644 index 0000000..d110253 --- /dev/null +++ b/tests/magic/source/main.cpp @@ -0,0 +1,34 @@ +#include +#include + +#include +#include + +#include + +#define EXIT_SKIP 77 + +int main(int argc, char **argv) { + // Any number of arguments other than 3 are invalid + if (argc != 3) + return EXIT_FAILURE; + + // Extract command line arguments + const std::string magicName = argv[1]; + const std::fs::path magicFilePath = argv[2]; + + fmt::print("Running test {} on test file {}\n", magicName, magicFilePath.filename().string()); + + magic_t ctx = magic_open(MAGIC_NONE); + PL_ON_SCOPE_EXIT { magic_close(ctx); }; + + if (ctx == nullptr) + return EXIT_FAILURE; + + if (magic_compile(ctx, magicFilePath.string().c_str()) != 0) { + fmt::print("Error during compilation: {}\n", magic_error(ctx)); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} \ No newline at end of file