aboutsummaryrefslogtreecommitdiff
path: root/tests/test_envctl.c
blob: eab01867d9f83d8ee64fcb869bc7491a9c7e5219 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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();
}