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 --- tests/test_relocation.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/test_relocation.c (limited to 'tests/test_relocation.c') diff --git a/tests/test_relocation.c b/tests/test_relocation.c new file mode 100644 index 0000000..0796074 --- /dev/null +++ b/tests/test_relocation.c @@ -0,0 +1,62 @@ +#include "testing.h" + +static const char *test_string = "The quick brown fox jumps over the lazy dog."; +static const char *targets[] = { + "The", "^^^ quick brown fox jumps over the lazy dog.", + "quick", "The ^^^ brown fox jumps over the lazy dog.", + "brown fox jumps over the", "The quick ^^^ lazy dog.", + "lazy", "The quick brown fox jumps over the ^^^ dog.", + "dog", "The quick brown fox jumps over the lazy ^^^.", +}; + +void test_replace_text() { + for (size_t i = 0; i < sizeof(targets) / sizeof(*targets); i += 2) { + const char *target = targets[i]; + const char *expected = targets[i + 1]; + char input[BUFSIZ] = {0}; + strcpy(input, test_string); + + OMC_ASSERT(replace_text(input, target, "^^^", 0) == 0, "string replacement failed"); + OMC_ASSERT(strcmp(input, expected) == 0, "unexpected replacement"); + } + +} + +void test_file_replace_text() { + for (size_t i = 0; i < sizeof(targets) / sizeof(*targets); i += 2) { + const char *filename = "test_file_replace_text.txt"; + const char *target = targets[i]; + const char *expected = targets[i + 1]; + FILE *fp; + + fp = fopen(filename, "w"); + if (fp) { + fprintf(fp, "%s", test_string); + fclose(fp); + OMC_ASSERT(file_replace_text(filename, target, "^^^", 0) == 0, "string replacement failed"); + } else { + OMC_ASSERT(false, "failed to open file for writing"); + return; + } + + char input[BUFSIZ] = {0}; + fp = fopen(filename, "r"); + if (fp) { + fread(input, sizeof(*input), sizeof(input), fp); + OMC_ASSERT(strcmp(input, expected) == 0, "unexpected replacement"); + } else { + OMC_ASSERT(false, "failed to open file for reading"); + return; + } + } +} + +int main(int argc, char *argv[]) { + OMC_TEST_BEGIN_MAIN(); + OMC_TEST_FUNC *tests[] = { + test_replace_text, + test_file_replace_text, + }; + OMC_TEST_RUN(tests); + OMC_TEST_END_MAIN(); +} \ No newline at end of file -- cgit