aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_relocation.c5
-rw-r--r--tests/test_str.c12
-rw-r--r--tests/test_template.c29
-rw-r--r--tests/test_utils.c12
4 files changed, 37 insertions, 21 deletions
diff --git a/tests/test_relocation.c b/tests/test_relocation.c
index a6c33f2..69142dc 100644
--- a/tests/test_relocation.c
+++ b/tests/test_relocation.c
@@ -14,9 +14,12 @@ void test_replace_text() {
const char *target = targets[i];
const char *expected = targets[i + 1];
char input[BUFSIZ] = {0};
- strcpy(input, test_string);
+ strncpy(input, test_string, sizeof(input) - 1);
+ printf("input: %s\n", input);
+ printf("target: %s\n", target);
STASIS_ASSERT(replace_text(input, target, "^^^", 0) == 0, "string replacement failed");
+ printf("result: %s\n\n", input);
STASIS_ASSERT(strcmp(input, expected) == 0, "unexpected replacement");
}
diff --git a/tests/test_str.c b/tests/test_str.c
index a98a34d..aac5d71 100644
--- a/tests/test_str.c
+++ b/tests/test_str.c
@@ -37,7 +37,7 @@ void test_tolower_s() {
for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
char input[100] = {0};
- strcpy(input, tc[i].data);
+ strncpy(input, tc[i].data, sizeof(input) - 1);
tolower_s(input);
STASIS_ASSERT(strcmp(input, tc[i].expected) == 0, "unexpected result");
}
@@ -317,9 +317,8 @@ void test_lstrip() {
STASIS_ASSERT(lstrip(NULL) == NULL, "incorrect return type");
for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
char *buf = calloc(255, sizeof(*buf));
- char *result;
- strcpy(buf, tc[i].data);
- result = lstrip(buf);
+ strncpy(buf, tc[i].data, 254);
+ char *result = lstrip(buf);
STASIS_ASSERT(strcmp(result ? result : "", tc[i].expected) == 0, "incorrect strip-from-left");
guard_free(buf);
}
@@ -342,9 +341,8 @@ void test_strip() {
STASIS_ASSERT(strip(NULL) == NULL, "incorrect return type");
for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
char *buf = calloc(255, sizeof(*buf));
- char *result;
- strcpy(buf, tc[i].data);
- result = strip(buf);
+ strncpy(buf, tc[i].data, 254);
+ char *result = strip(buf);
STASIS_ASSERT(strcmp(result ? result : "", tc[i].expected) == 0, "incorrect strip-from-right");
guard_free(buf);
}
diff --git a/tests/test_template.c b/tests/test_template.c
index aaba03b..e8f0c1d 100644
--- a/tests/test_template.c
+++ b/tests/test_template.c
@@ -61,6 +61,19 @@ void test_tpl_workflow() {
STASIS_ASSERT(strcmp(result, "Hello environment!") == 0, "environment variable content mismatch");
guard_free(result);
unsetenv("HELLO");
+
+ const char *message_file = "message.txt";
+ char message_fmt[] = "They wanted a {{ hello_message }} "
+ "So we gave them a {{ hello_message }}";
+ const char *message_expected = "They wanted a Hello world! "
+ "So we gave them a Hello world!";
+ const int state = tpl_render_to_file(message_fmt, message_file);
+ STASIS_ASSERT_FATAL(state == 0, "failed to write rendered string to file");
+ char *message_contents = stasis_testing_read_ascii(message_file);
+ STASIS_ASSERT(strcmp(message_contents, message_expected) == 0, "message in file does not match original message");
+ guard_free(message_contents);
+ remove(message_file);
+
guard_free(data);
}
@@ -72,6 +85,8 @@ void test_tpl_register() {
STASIS_ASSERT(tpl_pool_used == (used_before_register + 1), "tpl_register did not increment allocation counter");
STASIS_ASSERT(tpl_pool[used_before_register] != NULL, "register did not allocate a tpl_item record in the pool");
+ const char *message = tpl_getval("hello_message");
+ STASIS_ASSERT(strcmp(message, "Hello world!") == 0, "stored message corrupt");
free(data);
}
@@ -96,25 +111,25 @@ void test_tpl_register_func() {
char *result = NULL;
result = tpl_render("{{ func:add(0,3) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "add: Answer was not 3");
guard_free(result);
result = tpl_render("{{ func:add(1,2) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "add: Answer was not 3");
guard_free(result);
result = tpl_render("{{ func:sub(6,3) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "sub: was not 3");
guard_free(result);
result = tpl_render("{{ func:sub(4,1) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "sub: Answer was not 3");
guard_free(result);
result = tpl_render("{{ func:mul(1, 3) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "mul: Answer was not 3");
guard_free(result);
result = tpl_render("{{ func:div(6,2) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "div: Answer was not 3");
guard_free(result);
result = tpl_render("{{ func:div(3,1) }}");
- STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "Answer was not 3");
+ STASIS_ASSERT(result != NULL && strcmp(result, "3") == 0, "div: Answer was not 3");
guard_free(result);
}
diff --git a/tests/test_utils.c b/tests/test_utils.c
index 5766f6c..79bca43 100644
--- a/tests/test_utils.c
+++ b/tests/test_utils.c
@@ -65,7 +65,7 @@ void test_fix_tox_conf() {
if (fp) {
fprintf(fp, "%s", data);
fclose(fp);
- STASIS_ASSERT(fix_tox_conf(filename, &result) == 0, "fix_tox_conf failed");
+ STASIS_ASSERT(fix_tox_conf(filename, &result, PATH_MAX) == 0, "fix_tox_conf failed");
} else {
STASIS_ASSERT(false, "writing mock tox.ini failed");
}
@@ -308,7 +308,7 @@ void test_path_dirname() {
const char *input = data[i];
const char *expected = data[i + 1];
char tmp[PATH_MAX] = {0};
- strcpy(tmp, input);
+ strncpy(tmp, input, sizeof(tmp) - 1);
char *result = path_dirname(tmp);
STASIS_ASSERT(strcmp(expected, result) == 0, NULL);
@@ -329,8 +329,7 @@ void test_path_basename() {
}
void test_expandpath() {
- char *home;
-
+ char *home = NULL;
const char *homes[] = {
"HOME",
"USERPROFILE",
@@ -341,10 +340,11 @@ void test_expandpath() {
break;
}
}
+ STASIS_ASSERT_FATAL(home != NULL, "cannot expand without knowing the user's home directory path");
char path[PATH_MAX] = {0};
- strcat(path, "~");
- strcat(path, DIR_SEP);
+ strncat(path, "~", sizeof(path) - strlen(path) - 1);
+ strncat(path, DIR_SEP, sizeof(path) - strlen(path) - 1);
char *expanded = expandpath(path);
STASIS_ASSERT(startswith(expanded, home) > 0, expanded);