From a346c22c68c7bc39c7bba7eec0cce5b7dea9c174 Mon Sep 17 00:00:00 2001 From: Hilko Bengen Date: Sun, 21 Aug 2022 22:53:24 +0200 Subject: [PATCH] Add simple test using ad-hoc defined "mytest" module --- Makefile.am | 5 ++- tests/test-module.c | 92 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/test-module.c diff --git a/Makefile.am b/Makefile.am index ba66dd3421..5f19b07d67 100644 --- a/Makefile.am +++ b/Makefile.am @@ -92,6 +92,8 @@ test_re_split_SOURCES = tests/test-re-split.c tests/util.c test_re_split_LDADD = libyara/.libs/libyara.a test_async_SOURCES = tests/test-async.c tests/util.c test_async_LDADD = libyara/.libs/libyara.a +test_module_SOURCES = tests/test-module.c tests/util.c +test_module_LDADD = libyara/.libs/libyara.a TESTS = $(check_PROGRAMS) TESTS_ENVIRONMENT = TOP_SRCDIR=$(top_srcdir) TOP_BUILDDIR=$(top_builddir) @@ -109,7 +111,8 @@ check_PROGRAMS = \ test-math \ test-stack \ test-re-split \ - test-async + test-async \ + test-module EXTRA_PROGRAMS = tests/mapper CLEANFILES = tests/mapper$(EXEEXT) diff --git a/tests/test-module.c b/tests/test-module.c new file mode 100644 index 0000000000..f6199be4c6 --- /dev/null +++ b/tests/test-module.c @@ -0,0 +1,92 @@ +/* +Copyright (c) 2022. The YARA Authors. 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 of the copyright holder 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 HOLDER 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. +*/ + +#include + +#include "util.h" + +#define MODULE_NAME mytest + +begin_declarations +declare_integer("fortytwo"); +end_declarations + +int module_initialize(YR_MODULE* module) +{ + return ERROR_SUCCESS; +} + +int module_finalize(YR_MODULE* module) +{ + return ERROR_SUCCESS; +} + +int module_load( + YR_SCAN_CONTEXT* context, + YR_OBJECT* module_object, + void* module_data, + size_t module_data_size) +{ + yr_set_integer(42, module_object, "fortytwo"); + + return ERROR_SUCCESS; +} + +int module_unload(YR_OBJECT* module_object) +{ + return ERROR_SUCCESS; +} + +yr_module_define(); + +#undef MODULE_NAME + +int main(int argc, char** argv) +{ + int result = 0; + + YR_DEBUG_INITIALIZE(); + YR_DEBUG_FPRINTF(1, stderr, "+ %s() { // in %s\n", __FUNCTION__, __FILE__); + + init_top_srcdir(); + yr_initialize(); + + yr_modules_add(&mytest__module); + + assert_true_rule( + "import \"mytest\" rule test { condition: mytest.fortytwo == 42 }", + NULL); + + yr_finalize(); + + YR_DEBUG_FPRINTF( + 1, stderr, "} = %d // %s() in %s\n", result, __FUNCTION__, __FILE__); + + return result; +}