From 931ee28eb9c5b5e3c2b0d3008f5f65d810dc9b0c Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Thu, 20 Jun 2024 15:10:56 -0400 Subject: Unit tests (#6) * Initial commit of unit tests [WIP] * Address shortcomings and bugs flushed out by unit tests * Enable unit testing in CI workflow * Enable verbose ctests * Handle lack of __FILE_NAME__ define * Only podman support `run --arch` argument * Skip docker build testing if CI system cannot pull an image * Remove errant call to puts() * Identify local repo user * Fix missing xmllint * NULL terminate arrays * Fix filename assignment in is_url mode * Break loop when expected lines are exhausted * strcmp_array expects NULL terminated array. Iterating by size in this case passes NULL to strcmp leading to an invalid read * Remove debug printf statements * Disable a few warnings for tests * Workaround for ctest junit xml truncation * Update checkout@v4 * Prevent false-positive result * Return zero on error * Fix strlist_remove function * Value argument can be constant * Fix test to match changes to startswith and endswith * Add test_ini.c * Fix redaction code to accept NULL pointers in array * And let the caller specify the length of the array of strings to redact. * Redactions now occur directly on authentication strings rather than their command line arguments * Fix BUILD_TESTING_DEBUG * Adds missing -D argument --- src/utils.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 7cc3e6e..86622ad 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,9 +6,15 @@ const ssize_t dirstack_max = sizeof(dirstack) / sizeof(dirstack[0]); ssize_t dirstack_len = 0; int pushd(const char *path) { + if (access(path, F_OK)) { + // the requested path doesn't exist. + return -1; + } if (dirstack_len + 1 > dirstack_max) { + // the stack is full return -1; } + dirstack[dirstack_len] = realpath(".", NULL); dirstack_len++; return chdir(path); @@ -151,7 +157,7 @@ char *path_dirname(char *path) { if (!pos) { return "."; } - *path = '\0'; + *pos = '\0'; return path; } @@ -701,30 +707,26 @@ char *collapse_whitespace(char **s) { * @param maxlen maximum length of dest string * @return 0 on success, -1 on error */ -int redact_sensitive(const char **to_redact, char *src, char *dest, size_t maxlen) { - char **parts = split(src, " ", 0); - if (!parts) { - fprintf(stderr, "Unable to split source string\n"); +int redact_sensitive(const char **to_redact, size_t to_redact_size, char *src, char *dest, size_t maxlen) { + const char *redacted = "***REDACTED***"; + + char *tmp = calloc(strlen(redacted) + strlen(src) + 1, sizeof(*tmp)); + if (!tmp) { return -1; } + strcpy(tmp, src); - for (size_t i = 0; to_redact[i] != NULL; i++) { - for (size_t p = 0; parts[p] != NULL; p++) { - if (strstr(parts[p], to_redact[i])) { - replace_text(parts[p], to_redact[i], "***REDACTED***", REPLACE_TRUNCATE_AFTER_MATCH); - } + for (size_t i = 0; i < to_redact_size; i++) { + if (to_redact[i] && strstr(tmp, to_redact[i])) { + replace_text(tmp, to_redact[i], redacted, 0); + break; } } - char *dest_tmp = join(parts, " "); - if (!dest_tmp) { - fprintf(stderr, "Unable to join message array\n"); - return -1; - } - strncpy(dest, dest_tmp, maxlen); + memset(dest, 0, maxlen); + strncpy(dest, tmp, maxlen - 1); + guard_free(tmp); - GENERIC_ARRAY_FREE(parts); - guard_free(dest_tmp); return 0; } -- cgit