Skip to content

Commit

Permalink
Add simple test using ad-hoc defined "mytest" module
Browse files Browse the repository at this point in the history
  • Loading branch information
hillu committed Aug 21, 2022
1 parent 2a14b32 commit 0ca40a2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
92 changes: 92 additions & 0 deletions tests/test-module.c
Original file line number Diff line number Diff line change
@@ -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 <yara.h>

#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;
}

0 comments on commit 0ca40a2

Please sign in to comment.