Debian Codegraph @

tests/api/env.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2020 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* Env API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 static void set_up(void) {
32 if (p == NULL) {
33 p = make_sub_pool(NULL);
34 }
35 }
36
37 static void tear_down(void) {
38 if (p) {
39 destroy_pool(p);
40 p = NULL;
41 }
42 }
43
44 START_TEST (env_get_test) {
45 const char *key = "foo";
46 char *res;
47
48 res = pr_env_get(NULL, NULL);
49 ck_assert_msg(res == NULL, "Failed to handle null arguments");
50 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
51
52 res = pr_env_get(p, NULL);
53 ck_assert_msg(res == NULL, "Failed to handle null arguments");
54 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
55
56 res = pr_env_get(NULL, key);
57 ck_assert_msg(res == NULL, "Failed to handle null arguments");
58 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
59
60 #if defined(HAVE_GETENV)
61 pr_env_unset(p, key);
62
63 res = pr_env_get(p, key);
64 ck_assert_msg(res == NULL, "Unexpectedly found value '%s' for key '%s'",
65 res, key);
66
67 /* XXX PATH should always be set in the environment, right? */
68 res = pr_env_get(p, "PATH");
69 ck_assert_msg(res != NULL, "Failed to get value for 'PATH': %s",
70 strerror(errno));
71
72 #else
73 res = pr_env_get(p, key);
74 ck_assert_msg(errno == ENOSYS, "Failed to set errno to ENOSYS");
75 ck_assert_msg(res == NULL);
76 #endif
77 }
78 END_TEST
79
80 START_TEST (env_set_test) {
81 const char *key = "PR_TEST_FOO", *value = "bar";
82 char *v;
83 int res;
84
85 res = pr_env_set(NULL, NULL, NULL);
86 ck_assert_msg(res == -1, "Failed to handle null arguments");
87 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
88
89 res = pr_env_set(p, NULL, NULL);
90 ck_assert_msg(res == -1, "Failed to handle null arguments");
91 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
92
93 res = pr_env_set(NULL, key, NULL);
94 ck_assert_msg(res == -1, "Failed to handle null arguments");
95 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
96
97 res = pr_env_set(NULL, NULL, value);
98 ck_assert_msg(res == -1, "Failed to handle null arguments");
99 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
100
101 res = pr_env_set(p, key, NULL);
102 ck_assert_msg(res == -1, "Failed to handle null arguments");
103 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
104
105 res = pr_env_set(p, NULL, value);
106 ck_assert_msg(res == -1, "Failed to handle null arguments");
107 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
108
109 res = pr_env_set(NULL, key, value);
110 ck_assert_msg(res == -1, "Failed to handle null arguments");
111 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
112
113 res = pr_env_set(p, key, value);
114 ck_assert_msg(res == 0, "Failed to handle set '%s': %s", key, strerror(errno));
115
116 v = pr_env_get(p, key);
117 ck_assert_msg(strcmp(v, value) == 0, "Expected '%s', got '%s'", value, v);
118 }
119 END_TEST
120
121 START_TEST (env_unset_test) {
122 const char *key = "PR_TEST_FOO", *value = "bar";
123 char *v;
124 int res;
125
126 res = pr_env_unset(NULL, NULL);
127 ck_assert_msg(res == -1, "Failed to handle null arguments");
128 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
129
130 res = pr_env_unset(p, NULL);
131 ck_assert_msg(res == -1, "Failed to handle null arguments");
132 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
133
134 res = pr_env_unset(NULL, key);
135 ck_assert_msg(res == -1, "Failed to handle null arguments");
136 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
137
138 res = pr_env_set(p, key, value);
139 ck_assert_msg(res == 0, "Failed to set '%s': %s", key, strerror(errno));
140
141 v = pr_env_get(p, key);
142 ck_assert_msg(strcmp(v, value) == 0, "Expected '%s', got '%s'", value, v);
143
144 #if defined(HAVE_UNSETENV)
145 res = pr_env_unset(p, key);
146 ck_assert_msg(res == 0, "Failed to unset '%s': %s", key, strerror(errno));
147
148 v = pr_env_get(p, key);
149 ck_assert_msg(v == NULL, "Expected null, got '%s'", v);
150 #else
151 res = pr_env_unset(p, key);
152 ck_assert_msg(errno == ENOSYS, "Failed to set errno to ENOSYS");
153 ck_assert_msg(res == -1);
154 #endif
155 }
156 END_TEST
157
158 Suite *tests_get_env_suite(void) {
159 Suite *suite;
160 TCase *testcase;
161
162 suite = suite_create("env");
163
164 testcase = tcase_create("base");
165
166 tcase_add_checked_fixture(testcase, set_up, tear_down);
167
168 tcase_add_test(testcase, env_get_test);
169 tcase_add_test(testcase, env_set_test);
170 tcase_add_test(testcase, env_unset_test);
171
172 suite_add_tcase(suite, testcase);
173
174 return suite;
175 }