File: test_env_config.cpp

package info (click to toggle)
scitokens-cpp 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,172 kB
  • sloc: cpp: 11,717; ansic: 596; sh: 161; python: 132; makefile: 22
file content (162 lines) | stat: -rw-r--r-- 6,073 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
 * Test program to verify environment variable configuration loading.
 * This must be run as a separate process with environment variables set
 * before the library is loaded to properly test the constructor function.
 */

#include "scitokens.h"
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdexcept>

int main() {
    int failures = 0;
    char *err_msg = nullptr;

    // Test 1: Check if SCITOKEN_CONFIG_KEYCACHE_UPDATE_INTERVAL_S was loaded
    const char *env_update =
        std::getenv("SCITOKEN_CONFIG_KEYCACHE_UPDATE_INTERVAL_S");
    if (env_update) {
        try {
            int expected = std::stoi(env_update);
            int actual =
                scitoken_config_get_int("keycache.update_interval_s", &err_msg);
            if (actual != expected) {
                std::cerr << "FAIL: keycache.update_interval_s expected "
                          << expected << " but got " << actual << std::endl;
                if (err_msg) {
                    std::cerr << "Error: " << err_msg << std::endl;
                    free(err_msg);
                    err_msg = nullptr;
                }
                failures++;
            } else {
                std::cout << "PASS: keycache.update_interval_s = " << actual
                          << std::endl;
            }
        } catch (const std::exception &e) {
            std::cerr << "FAIL: Could not parse env var value: " << e.what()
                      << std::endl;
            failures++;
        }
    }

    // Test 2: Check if SCITOKEN_CONFIG_KEYCACHE_EXPIRATION_INTERVAL_S was
    // loaded
    const char *env_expiry =
        std::getenv("SCITOKEN_CONFIG_KEYCACHE_EXPIRATION_INTERVAL_S");
    if (env_expiry) {
        try {
            int expected = std::stoi(env_expiry);
            int actual = scitoken_config_get_int(
                "keycache.expiration_interval_s", &err_msg);
            if (actual != expected) {
                std::cerr << "FAIL: keycache.expiration_interval_s expected "
                          << expected << " but got " << actual << std::endl;
                if (err_msg) {
                    std::cerr << "Error: " << err_msg << std::endl;
                    free(err_msg);
                    err_msg = nullptr;
                }
                failures++;
            } else {
                std::cout << "PASS: keycache.expiration_interval_s = " << actual
                          << std::endl;
            }
        } catch (const std::exception &e) {
            std::cerr << "FAIL: Could not parse env var value: " << e.what()
                      << std::endl;
            failures++;
        }
    }

    // Test 3: Check if SCITOKEN_CONFIG_KEYCACHE_CACHE_HOME was loaded
    const char *env_cache = std::getenv("SCITOKEN_CONFIG_KEYCACHE_CACHE_HOME");
    if (env_cache) {
        char *actual = nullptr;
        int rv =
            scitoken_config_get_str("keycache.cache_home", &actual, &err_msg);
        if (rv != 0 || !actual) {
            std::cerr << "FAIL: Could not retrieve keycache.cache_home"
                      << std::endl;
            if (err_msg) {
                std::cerr << "Error: " << err_msg << std::endl;
                free(err_msg);
                err_msg = nullptr;
            }
            failures++;
        } else if (strcmp(actual, env_cache) != 0) {
            std::cerr << "FAIL: keycache.cache_home expected '" << env_cache
                      << "' but got '" << actual << "'" << std::endl;
            failures++;
        } else {
            std::cout << "PASS: keycache.cache_home = " << actual << std::endl;
        }
        if (actual)
            free(actual);
    }

    // Test 4: Check if SCITOKEN_CONFIG_TLS_CA_FILE was loaded
    const char *env_ca = std::getenv("SCITOKEN_CONFIG_TLS_CA_FILE");
    if (env_ca) {
        char *actual = nullptr;
        int rv = scitoken_config_get_str("tls.ca_file", &actual, &err_msg);
        if (rv != 0 || !actual) {
            std::cerr << "FAIL: Could not retrieve tls.ca_file" << std::endl;
            if (err_msg) {
                std::cerr << "Error: " << err_msg << std::endl;
                free(err_msg);
                err_msg = nullptr;
            }
            failures++;
        } else if (strcmp(actual, env_ca) != 0) {
            std::cerr << "FAIL: tls.ca_file expected '" << env_ca
                      << "' but got '" << actual << "'" << std::endl;
            failures++;
        } else {
            std::cout << "PASS: tls.ca_file = " << actual << std::endl;
        }
        if (actual)
            free(actual);
    }

    // Test 5: Test case insensitivity (lowercase env var)
    const char *env_lower =
        std::getenv("scitoken_config_keycache_update_interval_s");
    if (env_lower) {
        try {
            int expected = std::stoi(env_lower);
            int actual =
                scitoken_config_get_int("keycache.update_interval_s", &err_msg);
            if (actual != expected) {
                std::cerr << "FAIL: lowercase env var - "
                             "keycache.update_interval_s expected "
                          << expected << " but got " << actual << std::endl;
                if (err_msg) {
                    std::cerr << "Error: " << err_msg << std::endl;
                    free(err_msg);
                    err_msg = nullptr;
                }
                failures++;
            } else {
                std::cout
                    << "PASS: lowercase env var - keycache.update_interval_s = "
                    << actual << std::endl;
            }
        } catch (const std::exception &e) {
            std::cerr << "FAIL: Could not parse env var value: " << e.what()
                      << std::endl;
            failures++;
        }
    }

    if (failures == 0) {
        std::cout << "\nAll environment variable configuration tests passed!"
                  << std::endl;
        return 0;
    } else {
        std::cerr << "\n" << failures << " test(s) failed!" << std::endl;
        return 1;
    }
}