confyaml: convert to FAIL/PASS API

pull/5646/head
Shivani Bhardwaj 5 years ago committed by Victor Julien
parent fb311e3d9d
commit 7eef4e83e5

@ -566,39 +566,28 @@ default-log-dir: /tmp\n\
ConfNode *node; ConfNode *node;
node = ConfGetNode("rule-files"); node = ConfGetNode("rule-files");
if (node == NULL) FAIL_IF_NULL(node);
return 0; FAIL_IF_NOT(ConfNodeIsSequence(node));
if (!ConfNodeIsSequence(node)) FAIL_IF(TAILQ_EMPTY(&node->head));
return 0;
if (TAILQ_EMPTY(&node->head))
return 0;
int i = 0; int i = 0;
ConfNode *filename; ConfNode *filename;
TAILQ_FOREACH(filename, &node->head, next) { TAILQ_FOREACH(filename, &node->head, next) {
if (i == 0) { if (i == 0) {
if (strcmp(filename->val, "netbios.rules") != 0) FAIL_IF(strcmp(filename->val, "netbios.rules") != 0);
return 0; FAIL_IF(ConfNodeIsSequence(filename));
if (ConfNodeIsSequence(filename)) FAIL_IF(filename->is_seq != 0);
return 0;
if (filename->is_seq != 0)
return 0;
} }
else if (i == 1) { else if (i == 1) {
if (strcmp(filename->val, "x11.rules") != 0) FAIL_IF(strcmp(filename->val, "x11.rules") != 0);
return 0; FAIL_IF(ConfNodeIsSequence(filename));
if (ConfNodeIsSequence(filename))
return 0;
}
else {
return 0;
} }
FAIL_IF(i > 1);
i++; i++;
} }
ConfDeInit(); ConfDeInit();
ConfRestoreContextBackup(); ConfRestoreContextBackup();
PASS;
return 1;
} }
static int static int
@ -623,57 +612,45 @@ logging:\n\
ConfNode *outputs; ConfNode *outputs;
outputs = ConfGetNode("logging.output"); outputs = ConfGetNode("logging.output");
if (outputs == NULL) FAIL_IF_NULL(outputs);
return 0;
ConfNode *output; ConfNode *output;
ConfNode *output_param; ConfNode *output_param;
output = TAILQ_FIRST(&outputs->head); output = TAILQ_FIRST(&outputs->head);
if (output == NULL) FAIL_IF_NULL(output);
return 0; FAIL_IF(strcmp(output->name, "0") != 0);
if (strcmp(output->name, "0") != 0)
return 0;
output_param = TAILQ_FIRST(&output->head); output_param = TAILQ_FIRST(&output->head);
if (output_param == NULL) FAIL_IF_NULL(output_param);
return 0; FAIL_IF(strcmp(output_param->name, "interface") != 0);
if (strcmp(output_param->name, "interface") != 0) FAIL_IF(strcmp(output_param->val, "console") != 0);
return 0;
if (strcmp(output_param->val, "console") != 0)
return 0;
output_param = TAILQ_NEXT(output_param, next); output_param = TAILQ_NEXT(output_param, next);
if (strcmp(output_param->name, "log-level") != 0) FAIL_IF(strcmp(output_param->name, "log-level") != 0);
return 0; FAIL_IF(strcmp(output_param->val, "error") != 0);
if (strcmp(output_param->val, "error") != 0)
return 0;
output = TAILQ_NEXT(output, next); output = TAILQ_NEXT(output, next);
if (output == NULL) FAIL_IF_NULL(output);
return 0; FAIL_IF(strcmp(output->name, "1") != 0);
if (strcmp(output->name, "1") != 0)
return 0;
output_param = TAILQ_FIRST(&output->head); output_param = TAILQ_FIRST(&output->head);
if (output_param == NULL) FAIL_IF_NULL(output_param);
return 0; FAIL_IF(strcmp(output_param->name, "interface") != 0);
if (strcmp(output_param->name, "interface") != 0) FAIL_IF(strcmp(output_param->val, "syslog") != 0);
return 0;
if (strcmp(output_param->val, "syslog") != 0)
return 0;
output_param = TAILQ_NEXT(output_param, next); output_param = TAILQ_NEXT(output_param, next);
if (strcmp(output_param->name, "facility") != 0) FAIL_IF(strcmp(output_param->name, "facility") != 0);
return 0; FAIL_IF(strcmp(output_param->val, "local4") != 0);
if (strcmp(output_param->val, "local4") != 0)
return 0;
output_param = TAILQ_NEXT(output_param, next); output_param = TAILQ_NEXT(output_param, next);
if (strcmp(output_param->name, "log-level") != 0) FAIL_IF(strcmp(output_param->name, "log-level") != 0);
return 0; FAIL_IF(strcmp(output_param->val, "info") != 0);
if (strcmp(output_param->val, "info") != 0)
return 0;
ConfDeInit(); ConfDeInit();
ConfRestoreContextBackup(); ConfRestoreContextBackup();
return 1; PASS;
} }
/** /**
@ -685,13 +662,12 @@ ConfYamlNonYamlFileTest(void)
ConfCreateContextBackup(); ConfCreateContextBackup();
ConfInit(); ConfInit();
if (ConfYamlLoadFile("/etc/passwd") != -1) FAIL_IF(ConfYamlLoadFile("/etc/passwd") != -1);
return 0;
ConfDeInit(); ConfDeInit();
ConfRestoreContextBackup(); ConfRestoreContextBackup();
return 1; PASS;
} }
static int static int
@ -712,13 +688,12 @@ logging:\n\
ConfCreateContextBackup(); ConfCreateContextBackup();
ConfInit(); ConfInit();
if (ConfYamlLoadString(input, strlen(input)) != -1) FAIL_IF(ConfYamlLoadString(input, strlen(input)) != -1);
return 0;
ConfDeInit(); ConfDeInit();
ConfRestoreContextBackup(); ConfRestoreContextBackup();
return 1; PASS;
} }
static int static int
@ -748,42 +723,34 @@ libhtp:\n\
ConfCreateContextBackup(); ConfCreateContextBackup();
ConfInit(); ConfInit();
if (ConfYamlLoadString(input, strlen(input)) != 0) FAIL_IF(ConfYamlLoadString(input, strlen(input)) != 0);
return 0;
ConfNode *outputs; ConfNode *outputs;
outputs = ConfGetNode("libhtp.server-config"); outputs = ConfGetNode("libhtp.server-config");
if (outputs == NULL) FAIL_IF_NULL(outputs);
return 0;
ConfNode *node; ConfNode *node;
node = TAILQ_FIRST(&outputs->head); node = TAILQ_FIRST(&outputs->head);
if (node == NULL) FAIL_IF_NULL(node);
return 0; FAIL_IF(strcmp(node->name, "0") != 0);
if (strcmp(node->name, "0") != 0)
return 0;
node = TAILQ_FIRST(&node->head); node = TAILQ_FIRST(&node->head);
if (node == NULL) FAIL_IF_NULL(node);
return 0; FAIL_IF(strcmp(node->name, "apache-php") != 0);
if (strcmp(node->name, "apache-php") != 0)
return 0;
node = ConfNodeLookupChild(node, "address"); node = ConfNodeLookupChild(node, "address");
if (node == NULL) FAIL_IF_NULL(node);
return 0;
node = TAILQ_FIRST(&node->head); node = TAILQ_FIRST(&node->head);
if (node == NULL) FAIL_IF_NULL(node);
return 0; FAIL_IF(strcmp(node->name, "0") != 0);
if (strcmp(node->name, "0") != 0) FAIL_IF(strcmp(node->val, "192.168.1.0/24") != 0);
return 0;
if (strcmp(node->val, "192.168.1.0/24") != 0)
return 0;
ConfDeInit(); ConfDeInit();
ConfRestoreContextBackup(); ConfRestoreContextBackup();
return 1; PASS;
} }
/** /**
@ -792,7 +759,6 @@ libhtp:\n\
static int static int
ConfYamlFileIncludeTest(void) ConfYamlFileIncludeTest(void)
{ {
int ret = 0;
FILE *config_file; FILE *config_file;
const char config_filename[] = "ConfYamlFileIncludeTest-config.yaml"; const char config_filename[] = "ConfYamlFileIncludeTest-config.yaml";
@ -816,21 +782,12 @@ ConfYamlFileIncludeTest(void)
ConfInit(); ConfInit();
/* Write out the test files. */ /* Write out the test files. */
if ((config_file = fopen(config_filename, "w")) == NULL) { FAIL_IF_NULL((config_file = fopen(config_filename, "w")));
goto cleanup; FAIL_IF(fwrite(config_file_contents, strlen(config_file_contents), 1, config_file) != 1);
}
if (fwrite(config_file_contents, strlen(config_file_contents), 1,
config_file) != 1) {
goto cleanup;
}
fclose(config_file); fclose(config_file);
if ((config_file = fopen(include_filename, "w")) == NULL) {
goto cleanup; FAIL_IF_NULL((config_file = fopen(include_filename, "w")));
} FAIL_IF(fwrite(include_file_contents, strlen(include_file_contents), 1, config_file) != 1);
if (fwrite(include_file_contents, strlen(include_file_contents), 1,
config_file) != 1) {
goto cleanup;
}
fclose(config_file); fclose(config_file);
/* Reset conf_dirname. */ /* Reset conf_dirname. */
@ -839,45 +796,35 @@ ConfYamlFileIncludeTest(void)
conf_dirname = NULL; conf_dirname = NULL;
} }
if (ConfYamlLoadFile("ConfYamlFileIncludeTest-config.yaml") != 0) FAIL_IF(ConfYamlLoadFile("ConfYamlFileIncludeTest-config.yaml") != 0);
goto cleanup;
/* Check values that should have been loaded into the root of the /* Check values that should have been loaded into the root of the
* configuration. */ * configuration. */
ConfNode *node; ConfNode *node;
node = ConfGetNode("host-mode"); node = ConfGetNode("host-mode");
if (node == NULL) FAIL_IF_NULL(node);
goto cleanup; FAIL_IF(strcmp(node->val, "auto") != 0);
if (strcmp(node->val, "auto") != 0)
goto cleanup;
node = ConfGetNode("unix-command.enabled"); node = ConfGetNode("unix-command.enabled");
if (node == NULL) FAIL_IF_NULL(node);
goto cleanup; FAIL_IF(strcmp(node->val, "no") != 0);
if (strcmp(node->val, "no") != 0)
goto cleanup;
/* Check for values that were included under a mapping. */ /* Check for values that were included under a mapping. */
node = ConfGetNode("mapping.host-mode"); node = ConfGetNode("mapping.host-mode");
if (node == NULL) FAIL_IF_NULL(node);
goto cleanup; FAIL_IF(strcmp(node->val, "auto") != 0);
if (strcmp(node->val, "auto") != 0)
goto cleanup;
node = ConfGetNode("mapping.unix-command.enabled"); node = ConfGetNode("mapping.unix-command.enabled");
if (node == NULL) FAIL_IF_NULL(node);
goto cleanup; FAIL_IF(strcmp(node->val, "no") != 0);
if (strcmp(node->val, "no") != 0)
goto cleanup;
ConfDeInit(); ConfDeInit();
ConfRestoreContextBackup(); ConfRestoreContextBackup();
ret = 1;
cleanup:
unlink(config_filename); unlink(config_filename);
unlink(include_filename); unlink(include_filename);
return ret; PASS;
} }
/** /**
@ -905,25 +852,19 @@ ConfYamlOverrideTest(void)
ConfCreateContextBackup(); ConfCreateContextBackup();
ConfInit(); ConfInit();
if (ConfYamlLoadString(config, strlen(config)) != 0) FAIL_IF(ConfYamlLoadString(config, strlen(config)) != 0);
return 0; FAIL_IF_NOT(ConfGet("some-log-dir", &value));
if (!ConfGet("some-log-dir", &value)) FAIL_IF(strcmp(value, "/tmp") != 0);
return 0;
if (strcmp(value, "/tmp") != 0)
return 0;
/* Test that parent.child0 does not exist, but child1 does. */ /* Test that parent.child0 does not exist, but child1 does. */
if (ConfGetNode("parent.child0") != NULL) FAIL_IF_NOT_NULL(ConfGetNode("parent.child0"));
return 0; FAIL_IF_NOT(ConfGet("parent.child1.key", &value));
if (!ConfGet("parent.child1.key", &value)) FAIL_IF(strcmp(value, "value") != 0);
return 0;
if (strcmp(value, "value") != 0)
return 0;
ConfDeInit(); ConfDeInit();
ConfRestoreContextBackup(); ConfRestoreContextBackup();
return 1; PASS;
} }
/** /**
@ -942,24 +883,18 @@ ConfYamlOverrideFinalTest(void)
"default-log-dir: /var/log\n"; "default-log-dir: /var/log\n";
/* Set the log directory as if it was set on the command line. */ /* Set the log directory as if it was set on the command line. */
if (!ConfSetFinal("default-log-dir", "/tmp")) FAIL_IF_NOT(ConfSetFinal("default-log-dir", "/tmp"));
return 0; FAIL_IF(ConfYamlLoadString(config, strlen(config)) != 0);
if (ConfYamlLoadString(config, strlen(config)) != 0)
return 0;
const char *default_log_dir; const char *default_log_dir;
if (!ConfGet("default-log-dir", &default_log_dir)) FAIL_IF_NOT(ConfGet("default-log-dir", &default_log_dir));
return 0; FAIL_IF(strcmp(default_log_dir, "/tmp") != 0);
if (strcmp(default_log_dir, "/tmp") != 0) {
fprintf(stderr, "final value was reassigned\n");
return 0;
}
ConfDeInit(); ConfDeInit();
ConfRestoreContextBackup(); ConfRestoreContextBackup();
return 1; PASS;
} }
#endif /* UNITTESTS */ #endif /* UNITTESTS */

Loading…
Cancel
Save