aboutsummaryrefslogtreecommitdiff
path: root/tests/test_envctl.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@users.noreply.github.com>2024-10-22 11:04:17 -0400
committerGitHub <noreply@github.com>2024-10-22 11:04:17 -0400
commit7729d546d2dbda85ca1d86a913e97b51487355ba (patch)
treee9a0e7f9f2069ecd9e718dd66d3e11fa7a80722d /tests/test_envctl.c
parent8edc87d51900ccf7d1d67ad3647a4b8fa2d9b7ae (diff)
parent30f48145d1a1c747c40f94e2a7314d4bdf61cf55 (diff)
downloadstasis-7729d546d2dbda85ca1d86a913e97b51487355ba.tar.gz
Merge pull request #63 from jhunkeler/update-tests
Update tests / Bug fixes
Diffstat (limited to 'tests/test_envctl.c')
-rw-r--r--tests/test_envctl.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/test_envctl.c b/tests/test_envctl.c
new file mode 100644
index 0000000..eab0186
--- /dev/null
+++ b/tests/test_envctl.c
@@ -0,0 +1,72 @@
+#include "testing.h"
+#include "envctl.h"
+
+void test_envctl_init() {
+ struct EnvCtl *envctl;
+ STASIS_ASSERT_FATAL((envctl = envctl_init()) != NULL, "envctl could not be initialized");
+ STASIS_ASSERT(envctl->num_alloc == STASIS_ENVCTL_DEFAULT_ALLOC, "freshly initialized envctl does not have the correct number records");
+ STASIS_ASSERT(envctl->num_used == 0, "freshly initialized envctl should have no allocations in use");
+ STASIS_ASSERT(envctl->item != NULL, "freshly initialized envctl should have an empty items array. this one is NULL.");
+ STASIS_ASSERT(envctl->item[0] == NULL, "freshly initialized envctl should not have any items. this one does.");
+ envctl_free(&envctl);
+ STASIS_ASSERT(envctl == NULL, "envctl should be NULL after envctl_free()");
+}
+
+static int except_passthru(const void *a, const void *b) {
+ const struct EnvCtl_Item *item = a;
+ const char *name = b;
+ if (!envctl_check_required(item->flags) && envctl_check_present(item, name)) {
+ return STASIS_ENVCTL_RET_SUCCESS;
+ }
+ return STASIS_ENVCTL_RET_FAIL;
+}
+
+static int except_required(const void *a, const void *b) {
+ const struct EnvCtl_Item *item = a;
+ const char *name = b;
+ if (envctl_check_required(item->flags) && envctl_check_present(item, name)) {
+ return STASIS_ENVCTL_RET_SUCCESS;
+ }
+ return STASIS_ENVCTL_RET_FAIL;
+}
+
+static int except_redact(const void *a, const void *b) {
+ const struct EnvCtl_Item *item = a;
+ const char *name = b;
+ if (envctl_check_redact(item->flags) && envctl_check_present(item, name)) {
+ return STASIS_ENVCTL_RET_SUCCESS;
+ }
+ return STASIS_ENVCTL_RET_FAIL;
+}
+
+void test_envctl_register() {
+ struct EnvCtl *envctl;
+ envctl = envctl_init();
+ setenv("passthru", "true", 1);
+ setenv("required", "true", 1);
+ setenv("redact", "true", 1);
+ envctl_register(&envctl, STASIS_ENVCTL_PASSTHRU, except_passthru, "passthru");
+ envctl_register(&envctl, STASIS_ENVCTL_REQUIRED, except_required, "required");
+ envctl_register(&envctl, STASIS_ENVCTL_REDACT, except_redact, "redact");
+
+ unsigned flags[] = {
+ STASIS_ENVCTL_PASSTHRU,
+ STASIS_ENVCTL_REQUIRED,
+ STASIS_ENVCTL_REDACT,
+ };
+ for (size_t i = 0; i < envctl->num_used; i++) {
+ struct EnvCtl_Item *item = envctl->item[i];
+ STASIS_ASSERT(item->flags == flags[i], "incorrect flag for item");
+ }
+ envctl_free(&envctl);
+}
+
+int main(int argc, char *argv[]) {
+ STASIS_TEST_BEGIN_MAIN();
+ STASIS_TEST_FUNC *tests[] = {
+ test_envctl_init,
+ test_envctl_register,
+ };
+ STASIS_TEST_RUN(tests);
+ STASIS_TEST_END_MAIN();
+} \ No newline at end of file