1 /* Copyright libuv contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22 #include "uv.h"
23 #include "task.h"
24 #include <string.h>
25
26 #define BUF_SIZE 10
27
TEST_IMPL(env_vars)28 TEST_IMPL(env_vars) {
29 const char* name = "UV_TEST_FOO";
30 const char* name2 = "UV_TEST_FOO2";
31 char buf[BUF_SIZE];
32 size_t size;
33 int i, r, envcount, found, found_win_special;
34 uv_env_item_t* envitems;
35
36 #if defined(_WIN32) && defined(__ASAN__)
37 /* See investigation in https://github.com/libuv/libuv/issues/4338 */
38 RETURN_SKIP("Test does not currently work on Windows under ASAN");
39 #endif
40
41 /* Reject invalid inputs when setting an environment variable */
42 r = uv_os_setenv(NULL, "foo");
43 ASSERT_EQ(r, UV_EINVAL);
44 r = uv_os_setenv(name, NULL);
45 ASSERT_EQ(r, UV_EINVAL);
46 r = uv_os_setenv(NULL, NULL);
47 ASSERT_EQ(r, UV_EINVAL);
48
49 /* Reject invalid inputs when retrieving an environment variable */
50 size = BUF_SIZE;
51 r = uv_os_getenv(NULL, buf, &size);
52 ASSERT_EQ(r, UV_EINVAL);
53 r = uv_os_getenv(name, NULL, &size);
54 ASSERT_EQ(r, UV_EINVAL);
55 r = uv_os_getenv(name, buf, NULL);
56 ASSERT_EQ(r, UV_EINVAL);
57 size = 0;
58 r = uv_os_getenv(name, buf, &size);
59 ASSERT_EQ(r, UV_EINVAL);
60
61 /* Reject invalid inputs when deleting an environment variable */
62 r = uv_os_unsetenv(NULL);
63 ASSERT_EQ(r, UV_EINVAL);
64
65 /* Successfully set an environment variable */
66 r = uv_os_setenv(name, "123456789");
67 ASSERT_OK(r);
68
69 /* Successfully read an environment variable */
70 size = BUF_SIZE;
71 buf[0] = '\0';
72 r = uv_os_getenv(name, buf, &size);
73 ASSERT_OK(r);
74 ASSERT_OK(strcmp(buf, "123456789"));
75 ASSERT_EQ(size, BUF_SIZE - 1);
76
77 /* Return UV_ENOBUFS if the buffer cannot hold the environment variable */
78 size = BUF_SIZE - 1;
79 buf[0] = '\0';
80 r = uv_os_getenv(name, buf, &size);
81 ASSERT_EQ(r, UV_ENOBUFS);
82 ASSERT_EQ(size, BUF_SIZE);
83
84 /* Successfully delete an environment variable */
85 r = uv_os_unsetenv(name);
86 ASSERT_OK(r);
87
88 /* Return UV_ENOENT retrieving an environment variable that does not exist */
89 r = uv_os_getenv(name, buf, &size);
90 ASSERT_EQ(r, UV_ENOENT);
91
92 /* Successfully delete an environment variable that does not exist */
93 r = uv_os_unsetenv(name);
94 ASSERT_OK(r);
95
96 /* Setting an environment variable to the empty string does not delete it. */
97 r = uv_os_setenv(name, "");
98 ASSERT_OK(r);
99 size = BUF_SIZE;
100 r = uv_os_getenv(name, buf, &size);
101 ASSERT_OK(r);
102 ASSERT_OK(size);
103 ASSERT_OK(strlen(buf));
104
105 /* Check getting all env variables. */
106 r = uv_os_setenv(name, "123456789");
107 ASSERT_OK(r);
108 r = uv_os_setenv(name2, "");
109 ASSERT_OK(r);
110 #ifdef _WIN32
111 /* Create a special environment variable on Windows in case there are no
112 naturally occurring ones. */
113 r = uv_os_setenv("=Z:", "\\");
114 ASSERT_OK(r);
115 #endif
116
117 r = uv_os_environ(&envitems, &envcount);
118 ASSERT_OK(r);
119 ASSERT_GT(envcount, 0);
120
121 found = 0;
122 found_win_special = 0;
123
124 for (i = 0; i < envcount; i++) {
125 /* printf("Env: %s = %s\n", envitems[i].name, envitems[i].value); */
126 if (strcmp(envitems[i].name, name) == 0) {
127 found++;
128 ASSERT_OK(strcmp(envitems[i].value, "123456789"));
129 } else if (strcmp(envitems[i].name, name2) == 0) {
130 found++;
131 ASSERT_OK(strlen(envitems[i].value));
132 } else if (envitems[i].name[0] == '=') {
133 found_win_special++;
134 }
135 }
136
137 ASSERT_EQ(2, found);
138 #ifdef _WIN32
139 ASSERT_GT(found_win_special, 0);
140 #else
141 /* There's no rule saying a key can't start with '='. */
142 (void) &found_win_special;
143 #endif
144
145 uv_os_free_environ(envitems, envcount);
146
147 r = uv_os_unsetenv(name);
148 ASSERT_OK(r);
149
150 r = uv_os_unsetenv(name2);
151 ASSERT_OK(r);
152
153 for (i = 1; i <= 4; i++) {
154 size_t n;
155 char* p;
156
157 n = i * 32768;
158 size = n + 1;
159
160 p = malloc(size);
161 ASSERT_NOT_NULL(p);
162
163 memset(p, 'x', n);
164 p[n] = '\0';
165
166 ASSERT_OK(uv_os_setenv(name, p));
167 ASSERT_OK(uv_os_getenv(name, p, &size));
168 ASSERT_EQ(n, size);
169
170 for (n = 0; n < size; n++)
171 ASSERT_EQ('x', p[n]);
172
173 ASSERT_OK(uv_os_unsetenv(name));
174 free(p);
175 }
176
177 return 0;
178 }
179