Skip to content

Commit

Permalink
Tests: Set top_srcdir variable instead of changing directory (#1466)
Browse files Browse the repository at this point in the history
  • Loading branch information
hillu authored Mar 22, 2021
1 parent 6322d70 commit 71980e0
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 23 deletions.
6 changes: 3 additions & 3 deletions tests/test-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void test_file_descriptor()
exit(1);
}
#else
int fd = open("tests/data/baz.yar", O_RDONLY);
int fd = open(prefix_top_srcdir("tests/data/baz.yar"), O_RDONLY);
if (fd < 0)
{
perror("open");
Expand Down Expand Up @@ -830,7 +830,7 @@ void test_runtime_warnings() {

yr_compiler_destroy(compiler);

if (yr_rules_scan_file(rules, "tests/data/x.txt", 0, count, &counters, 0) != ERROR_SUCCESS) {
if (yr_rules_scan_file(rules, prefix_top_srcdir("tests/data/x.txt"), 0, count, &counters, 0) != ERROR_SUCCESS) {
yr_rules_destroy(rules);
perror("yr_rules_scan_file");
exit(EXIT_FAILURE);
Expand All @@ -853,7 +853,7 @@ int main(int argc, char** argv)
YR_DEBUG_INITIALIZE();
YR_DEBUG_FPRINTF(1, stderr, "+ %s() { // in %s\n", __FUNCTION__, argv[0]);

chdir_if_env_top_srcdir();
init_top_srcdir();

test_disabled_rules();
test_file_descriptor();
Expand Down
2 changes: 1 addition & 1 deletion tests/test-async.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ int main(int argc, char** argv)
YR_DEBUG_INITIALIZE();
YR_DEBUG_FPRINTF(1, stderr, "+ %s() { // in %s\n", __FUNCTION__, argv[0]);

chdir_if_env_top_srcdir();
init_top_srcdir();

yr_initialize();

Expand Down
2 changes: 1 addition & 1 deletion tests/test-dotnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main(int argc, char** argv)
YR_DEBUG_INITIALIZE();
YR_DEBUG_FPRINTF(1, stderr, "+ %s() { // in %s\n", __FUNCTION__, argv[0]);

chdir_if_env_top_srcdir();
init_top_srcdir();

yr_initialize();

Expand Down
2 changes: 1 addition & 1 deletion tests/test-macho.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main(int argc, char** argv)
YR_DEBUG_INITIALIZE();
YR_DEBUG_FPRINTF(1, stderr, "+ %s() { // in %s\n", __FUNCTION__, argv[0]);

chdir_if_env_top_srcdir();
init_top_srcdir();

yr_initialize();

Expand Down
1 change: 1 addition & 0 deletions tests/test-pb.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ int main(int argc, char** argv)
YR_DEBUG_INITIALIZE();
YR_DEBUG_FPRINTF(1, stderr, "+ %s() { // in %s\n", __FUNCTION__, __FILE__);

init_top_srcdir();
yr_initialize();

assert_true_rule_module_data_file(
Expand Down
2 changes: 1 addition & 1 deletion tests/test-pe.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main(int argc, char** argv)
YR_DEBUG_INITIALIZE();
YR_DEBUG_FPRINTF(1, stderr, "+ %s() { // in %s\n", __FUNCTION__, argv[0]);

chdir_if_env_top_srcdir();
init_top_srcdir();

yr_initialize();

Expand Down
19 changes: 14 additions & 5 deletions tests/test-rules.c
Original file line number Diff line number Diff line change
Expand Up @@ -2737,11 +2737,20 @@ void test_include_files()
{
YR_DEBUG_FPRINTF(1, stderr, "+ %s() {\n", __FUNCTION__);

assert_true_rule(
"include \"tests/data/baz.yar\" rule t { condition: baz }", NULL);
char rule[4096];
snprintf(
rule,
sizeof(rule),
"include \"%s/tests/data/baz.yar\" rule t { condition: baz }",
top_srcdir);
assert_true_rule(rule, NULL);

assert_true_rule(
"include \"tests/data/foo.yar\" rule t { condition: foo }", NULL);
snprintf(
rule,
sizeof(rule),
"include \"%s/tests/data/foo.yar\" rule t { condition: foo }",
top_srcdir);
assert_true_rule(rule, NULL);

YR_DEBUG_FPRINTF(1, stderr, "} // %s()\n", __FUNCTION__);
}
Expand Down Expand Up @@ -3129,7 +3138,7 @@ int main(int argc, char** argv)
YR_DEBUG_INITIALIZE();
YR_DEBUG_FPRINTF(1, stderr, "+ %s() { \n", __FUNCTION__);

chdir_if_env_top_srcdir();
init_top_srcdir();
yr_initialize();

assert_true_expr(strlen(TEXT_1024_BYTES) == 1024);
Expand Down
20 changes: 13 additions & 7 deletions tests/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,20 @@ void init_test_iterator(
}
}

void chdir_if_env_top_srcdir(void)
char* top_srcdir;

void init_top_srcdir(void)
{
char* top_srcdir = getenv("TOP_SRCDIR");
if (top_srcdir)
{
int result = chdir(top_srcdir);
assert_true_expr(0 == result);
}
top_srcdir = getenv("TOP_SRCDIR");
if (!top_srcdir)
top_srcdir = ".";
}

char* prefix_top_srcdir(char* dir)
{
static char buf[4096];
snprintf(buf, sizeof(buf), "%s/%s", top_srcdir, dir);
return buf;
}

//
Expand Down
11 changes: 7 additions & 4 deletions tests/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ struct YR_TEST_ITERATOR_CTX
int blocks_returned_successfully;
};

extern char* top_srcdir;

extern void chdir_if_env_top_srcdir(void);
extern void init_top_srcdir(void);

extern char* prefix_top_srcdir(char* dir);

struct COUNTERS
{
Expand Down Expand Up @@ -209,7 +212,7 @@ void assert_hex_atoms(
do { \
char* buf; \
size_t sz; \
if ((sz = read_file(filename, &buf)) == -1) { \
if ((sz = read_file(prefix_top_srcdir(filename), &buf)) == -1) { \
fprintf(stderr, "%s:%d: cannot read file '%s'\n", \
__FILE__, __LINE__, filename); \
exit(EXIT_FAILURE); \
Expand Down Expand Up @@ -252,7 +255,7 @@ void assert_hex_atoms(
do { \
char* buf; \
size_t sz; \
if ((sz = read_file(filename, &buf)) == -1) { \
if ((sz = read_file(prefix_top_srcdir(filename), &buf)) == -1) { \
fprintf(stderr, "%s:%d: cannot read file '%s'\n", \
__FILE__, __LINE__, filename); \
exit(EXIT_FAILURE); \
Expand All @@ -270,7 +273,7 @@ void assert_hex_atoms(
do { \
char* buf; \
size_t sz; \
if ((sz = read_file(filename, &buf)) == -1) { \
if ((sz = read_file(prefix_top_srcdir(filename), &buf)) == -1) { \
fprintf(stderr, "%s:%d: cannot read file '%s'\n", \
__FILE__, __LINE__, filename); \
exit(EXIT_FAILURE); \
Expand Down

0 comments on commit 71980e0

Please sign in to comment.