Skip to content

Commit 72c92ce

Browse files
committed
tests: Added tests for include libraries
1 parent 5cf0dac commit 72c92ce

File tree

6 files changed

+111
-10
lines changed

6 files changed

+111
-10
lines changed

.gitmodules

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
[submodule "rules"]
22
path = yara/official_rules
33
url = https://github.com/Yara-Rules/rules
4-
branch = master
5-
[submodule "tests/lib/pl"]
6-
path = tests/lib/pl
7-
url = https://github.com/WerWolv/PatternLanguage
8-
branch = master
4+
branch = master

tests/CMakeLists.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@ project(tests)
44

55
set(CMAKE_CXX_STANDARD 20)
66

7-
add_subdirectory(lib/pl)
7+
include(FetchContent)
8+
9+
FetchContent_Declare(
10+
pattern_language
11+
GIT_REPOSITORY https://github.com/WerWolv/PatternLanguage
12+
GIT_TAG master
13+
)
14+
15+
FetchContent_MakeAvailable(pattern_language)
816

917
enable_testing()
1018

11-
add_subdirectory(patterns)
19+
add_subdirectory(patterns)
20+
add_subdirectory(includes)
21+
22+
add_custom_target(test_all DEPENDS patterns_tests includes_test)

tests/includes/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
project(includes_test)
4+
5+
set(TOP_LEVEL "${CMAKE_CURRENT_SOURCE_DIR}/../..")
6+
7+
file(GLOB INCLUDES
8+
"${TOP_LEVEL}/includes/*/*.pat"
9+
)
10+
11+
set(PATTERN_INCLUDES "${TOP_LEVEL}/includes")
12+
13+
add_executable(includes_test
14+
source/main.cpp
15+
)
16+
17+
target_include_directories(includes_test PRIVATE include)
18+
target_link_libraries(includes_test libpl)
19+
20+
set_target_properties(includes_test PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
21+
22+
foreach (include IN LISTS INCLUDES)
23+
file(RELATIVE_PATH INCLUDE_NAME ${PATTERN_INCLUDES} ${include})
24+
25+
set(TEST_NAME "Includes/${INCLUDE_NAME}")
26+
27+
add_test(NAME ${TEST_NAME} COMMAND includes_test "${INCLUDE_NAME}" "${include}" "${PATTERN_INCLUDES}" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
28+
set_tests_properties(${TEST_NAME} PROPERTIES SKIP_RETURN_CODE 77)
29+
endforeach ()

tests/includes/source/main.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <pl.hpp>
2+
#include <helpers/file.hpp>
3+
4+
#include <fmt/format.h>
5+
#include <cstdlib>
6+
7+
#define EXIT_SKIP 77
8+
9+
int main(int argc, char **argv) {
10+
// Any number of arguments other than 4 are invalid
11+
if (argc != 4)
12+
return EXIT_FAILURE;
13+
14+
// Extract command line arguments
15+
const std::string includeName = argv[1];
16+
const std::fs::path includeFilePath = argv[2];
17+
const std::fs::path includePath = argv[3];
18+
19+
fmt::print("Running test {} on test file {}\n", includeName, includeFilePath.filename().string());
20+
21+
// Open pattern file
22+
pl::fs::File patternFile(includeFilePath, pl::fs::File::Mode::Read);
23+
if (!patternFile.isValid())
24+
return EXIT_FAILURE;
25+
26+
// Setup Pattern Language Runtime
27+
pl::PatternLanguage runtime;
28+
{
29+
constexpr auto DummyPragmaHandler = [](const auto&, const auto&){ pl::LogConsole::abortEvaluation("Include files should never use this pragma!"); return true; };
30+
31+
runtime.setDataSource([&](pl::u64 address, pl::u8 *data, size_t size) {
32+
pl::LogConsole::abortEvaluation("Include files should never read from memory directly!");
33+
}, 0x00, 0x100000);
34+
runtime.setDangerousFunctionCallHandler([]{ return true; });
35+
runtime.setIncludePaths({ includePath });
36+
37+
runtime.addPragma("endian", DummyPragmaHandler);
38+
runtime.addPragma("MIME", DummyPragmaHandler);
39+
runtime.addPragma("base_address", DummyPragmaHandler);
40+
runtime.addPragma("eval_depth", DummyPragmaHandler);
41+
runtime.addPragma("array_limit", DummyPragmaHandler);
42+
runtime.addPragma("pattern_limit", DummyPragmaHandler);
43+
}
44+
45+
// Execute pattern
46+
if (!runtime.executeString(patternFile.readString())) {
47+
fmt::print("Error during execution!\n");
48+
49+
if (const auto &hardError = runtime.getError(); hardError.has_value())
50+
fmt::print("Hard error: {}\n\n", hardError.value().what());
51+
52+
for (const auto &[level, message] : runtime.getConsoleLog()) {
53+
switch (level) {
54+
case pl::LogConsole::Level::Debug: fmt::print(" [DEBUG] "); break;
55+
case pl::LogConsole::Level::Info: fmt::print(" [INFO] "); break;
56+
case pl::LogConsole::Level::Warning: fmt::print(" [WARN] "); break;
57+
case pl::LogConsole::Level::Error: fmt::print(" [ERROR] "); break;
58+
}
59+
60+
fmt::print("{}\n", message);
61+
}
62+
63+
return EXIT_FAILURE;
64+
}
65+
66+
return EXIT_SUCCESS;
67+
}

tests/lib/pl

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/patterns/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ file(GLOB PATTERNS
88
"${TOP_LEVEL}/patterns/*.hexpat"
99
)
1010

11-
set(PATTERN_INCLUDES "${TOP_LEVEL}/includes/std")
11+
set(PATTERN_INCLUDES "${TOP_LEVEL}/includes")
1212

1313
add_executable(patterns_tests
1414
source/main.cpp
@@ -24,7 +24,6 @@ foreach (pattern IN LISTS PATTERNS)
2424

2525
file(GLOB TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/test_data/${PATTERN_NAME}.*")
2626

27-
message(STATUS ${CMAKE_CURRENT_SOURCE_DIR}/test_data/${PATTERN_NAME}.*)
2827
if (TEST_FILES)
2928
list(GET TEST_FILES 0 TEST_FILE)
3029
else ()

0 commit comments

Comments
 (0)