aboutsummaryrefslogtreecommitdiff
path: root/tests/test_ini.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@users.noreply.github.com>2024-06-20 15:10:56 -0400
committerGitHub <noreply@github.com>2024-06-20 15:10:56 -0400
commit931ee28eb9c5b5e3c2b0d3008f5f65d810dc9b0c (patch)
tree5dbcccffd509fa71a99c351ed4628ed0841e1e46 /tests/test_ini.c
parent11aa1d44d95da221073e512fbec3bbccc0f1a46b (diff)
downloadstasis-931ee28eb9c5b5e3c2b0d3008f5f65d810dc9b0c.tar.gz
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
Diffstat (limited to 'tests/test_ini.c')
-rw-r--r--tests/test_ini.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/tests/test_ini.c b/tests/test_ini.c
new file mode 100644
index 0000000..f9f8234
--- /dev/null
+++ b/tests/test_ini.c
@@ -0,0 +1,108 @@
+#include "testing.h"
+#include "ini.h"
+
+void test_ini_open_empty() {
+ struct INIFILE *ini;
+ ini = ini_open("/dev/null");
+ OMC_ASSERT(ini->section_count == 1, "should have at least one section pre-allocated");
+ OMC_ASSERT(ini->section != NULL, "default section not present");
+ ini_free(&ini);
+ OMC_ASSERT(ini == NULL, "ini_free did not set pointer argument to NULL");
+}
+
+void test_ini_open() {
+ const char *filename = "ini_open.ini";
+ const char *data = "[default]\na=1\nb=2\nc=3\n[section name]test=true\n";
+ struct INIFILE *ini;
+ omc_testing_write_ascii(filename, data);
+ ini = ini_open(filename);
+ OMC_ASSERT(ini->section_count == 2, "should have two sections");
+ OMC_ASSERT(ini->section != NULL, "sections are not allocated");
+ ini_free(&ini);
+ OMC_ASSERT(ini == NULL, "ini_free did not set pointer argument to NULL");
+ remove(filename);
+}
+
+void test_ini_section_search() {
+ const char *filename = "ini_open.ini";
+ const char *data = "[default]\na=1\nb=2\nc=3\n[section name here]\ntest=true\n";
+ struct INIFILE *ini;
+ struct INISection *section;
+
+ omc_testing_write_ascii(filename, data);
+
+ ini = ini_open(filename);
+ section = ini_section_search(&ini, INI_SEARCH_BEGINS, "section");
+ OMC_ASSERT(section->data_count == 1, "should have one data variable");
+ OMC_ASSERT(strcmp(section->data[0]->key, "test") == 0, "should have one data variable named 'test'");
+ OMC_ASSERT(strcmp(section->data[0]->value, "true") == 0, "'test' should be equal to 'true'");
+ OMC_ASSERT(strcmp(section->key, "section name here") == 0, "defined section not found");
+ section = ini_section_search(&ini, INI_SEARCH_SUBSTR, "name");
+ OMC_ASSERT(strcmp(section->data[0]->key, "test") == 0, "should have one data variable named 'test'");
+ OMC_ASSERT(strcmp(section->data[0]->value, "true") == 0, "'test' should be equal to 'true'");
+ OMC_ASSERT(strcmp(section->key, "section name here") == 0, "defined section not found");
+ section = ini_section_search(&ini, INI_SEARCH_EXACT, "section name here");
+ OMC_ASSERT(strcmp(section->data[0]->key, "test") == 0, "should have one data variable named 'test'");
+ OMC_ASSERT(strcmp(section->data[0]->value, "true") == 0, "'test' should be equal to 'true'");
+ OMC_ASSERT(strcmp(section->key, "section name here") == 0, "defined section not found");
+
+ section = ini_section_search(&ini, INI_SEARCH_BEGINS, "bogus");
+ OMC_ASSERT(section == NULL, "bogus section search returned non-NULL");
+ section = ini_section_search(&ini, INI_SEARCH_BEGINS, NULL);
+ OMC_ASSERT(section == NULL, "NULL argument returned non-NULL");
+ ini_free(&ini);
+ remove(filename);
+}
+
+void test_ini_has_key() {
+ const char *filename = "ini_open.ini";
+ const char *data = "[default]\na=1\nb=2\nc=3\n[section name here]\ntest=true\n";
+ struct INIFILE *ini;
+
+ omc_testing_write_ascii(filename, data);
+
+ ini = ini_open(filename);
+ OMC_ASSERT(ini_has_key(ini, "default", "a") == true, "'default:a' key should exist");
+ OMC_ASSERT(ini_has_key(NULL, "default", NULL) == false, "NULL ini object should return false");
+ OMC_ASSERT(ini_has_key(ini, "default", NULL) == false, "NULL key should return false");
+ OMC_ASSERT(ini_has_key(ini, NULL, "a") == false, "NULL section should return false");
+ OMC_ASSERT(ini_has_key(ini, "default", "noname") == false, "'default:noname' key should not exist");
+ OMC_ASSERT(ini_has_key(ini, "doesnotexist", "name") == false, "'doesnotexist:name' key should not exist");
+ ini_free(&ini);
+ remove(filename);
+}
+
+void test_ini_setval_getval() {
+ const char *filename = "ini_open.ini";
+ const char *data = "[default]\na=1\nb=2\nc=3\n[section name here]\ntest=true\n";
+ struct INIFILE *ini;
+
+ omc_testing_write_ascii(filename, data);
+
+ ini = ini_open(filename);
+ union INIVal val;
+ OMC_ASSERT(ini_setval(&ini, INI_SETVAL_REPLACE, "default", "a", "changed") == 0, "failed to set value");
+ OMC_ASSERT(ini_getval(ini, "default", "a", INIVAL_TYPE_STR, &val) == 0, "failed to get value");
+ OMC_ASSERT(strcmp(val.as_char_p, "a") != 0, "unexpected value loaded from modified variable");
+ OMC_ASSERT(strcmp(val.as_char_p, "changed") == 0, "unexpected value loaded from modified variable");
+
+ OMC_ASSERT(ini_setval(&ini, INI_SETVAL_APPEND, "default", "a", " twice") == 0, "failed to set value");
+ OMC_ASSERT(ini_getval(ini, "default", "a", INIVAL_TYPE_STR, &val) == 0, "failed to get value");
+ OMC_ASSERT(strcmp(val.as_char_p, "changed") != 0, "unexpected value loaded from modified variable");
+ OMC_ASSERT(strcmp(val.as_char_p, "changed twice") == 0, "unexpected value loaded from modified variable");
+ ini_free(&ini);
+ remove(filename);
+}
+
+int main(int argc, char *argv[]) {
+ OMC_TEST_BEGIN_MAIN();
+ OMC_TEST_FUNC *tests[] = {
+ test_ini_open_empty,
+ test_ini_open,
+ test_ini_section_search,
+ test_ini_has_key,
+ test_ini_setval_getval,
+ };
+ OMC_TEST_RUN(tests);
+ OMC_TEST_END_MAIN();
+} \ No newline at end of file