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 /* Reject invalid inputs when setting an environment variable */
37 r = uv_os_setenv(NULL, "foo");
38 ASSERT(r == UV_EINVAL);
39 r = uv_os_setenv(name, NULL);
40 ASSERT(r == UV_EINVAL);
41 r = uv_os_setenv(NULL, NULL);
42 ASSERT(r == UV_EINVAL);
43
44 /* Reject invalid inputs when retrieving an environment variable */
45 size = BUF_SIZE;
46 r = uv_os_getenv(NULL, buf, &size);
47 ASSERT(r == UV_EINVAL);
48 r = uv_os_getenv(name, NULL, &size);
49 ASSERT(r == UV_EINVAL);
50 r = uv_os_getenv(name, buf, NULL);
51 ASSERT(r == UV_EINVAL);
52 size = 0;
53 r = uv_os_getenv(name, buf, &size);
54 ASSERT(r == UV_EINVAL);
55
56 /* Reject invalid inputs when deleting an environment variable */
57 r = uv_os_unsetenv(NULL);
58 ASSERT(r == UV_EINVAL);
59
60 /* Successfully set an environment variable */
61 r = uv_os_setenv(name, "123456789");
62 ASSERT(r == 0);
63
64 /* Successfully read an environment variable */
65 size = BUF_SIZE;
66 buf[0] = '\0';
67 r = uv_os_getenv(name, buf, &size);
68 ASSERT(r == 0);
69 ASSERT(strcmp(buf, "123456789") == 0);
70 ASSERT(size == BUF_SIZE - 1);
71
72 /* Return UV_ENOBUFS if the buffer cannot hold the environment variable */
73 size = BUF_SIZE - 1;
74 buf[0] = '\0';
75 r = uv_os_getenv(name, buf, &size);
76 ASSERT(r == UV_ENOBUFS);
77 ASSERT(size == BUF_SIZE);
78
79 /* Successfully delete an environment variable */
80 r = uv_os_unsetenv(name);
81 ASSERT(r == 0);
82
83 /* Return UV_ENOENT retrieving an environment variable that does not exist */
84 r = uv_os_getenv(name, buf, &size);
85 ASSERT(r == UV_ENOENT);
86
87 /* Successfully delete an environment variable that does not exist */
88 r = uv_os_unsetenv(name);
89 ASSERT(r == 0);
90
91 /* Setting an environment variable to the empty string does not delete it. */
92 r = uv_os_setenv(name, "");
93 ASSERT(r == 0);
94 size = BUF_SIZE;
95 r = uv_os_getenv(name, buf, &size);
96 ASSERT(r == 0);
97 ASSERT(size == 0);
98 ASSERT(strlen(buf) == 0);
99
100 /* Check getting all env variables. */
101 r = uv_os_setenv(name, "123456789");
102 ASSERT(r == 0);
103 r = uv_os_setenv(name2, "");
104 ASSERT(r == 0);
105 #ifdef _WIN32
106 /* Create a special environment variable on Windows in case there are no
107 naturally occurring ones. */
108 r = uv_os_setenv("=Z:", "\\");
109 ASSERT(r == 0);
110 #endif
111
112 r = uv_os_environ(&envitems, &envcount);
113 ASSERT(r == 0);
114 ASSERT(envcount > 0);
115
116 found = 0;
117 found_win_special = 0;
118
119 for (i = 0; i < envcount; i++) {
120 /* printf("Env: %s = %s\n", envitems[i].name, envitems[i].value); */
121 if (strcmp(envitems[i].name, name) == 0) {
122 found++;
123 ASSERT(strcmp(envitems[i].value, "123456789") == 0);
124 } else if (strcmp(envitems[i].name, name2) == 0) {
125 found++;
126 ASSERT(strlen(envitems[i].value) == 0);
127 } else if (envitems[i].name[0] == '=') {
128 found_win_special++;
129 }
130 }
131
132 ASSERT(found == 2);
133 #ifdef _WIN32
134 ASSERT(found_win_special > 0);
135 #endif
136
137 uv_os_free_environ(envitems, envcount);
138
139 r = uv_os_unsetenv(name);
140 ASSERT(r == 0);
141
142 r = uv_os_unsetenv(name2);
143 ASSERT(r == 0);
144
145 for (i = 1; i <= 4; i++) {
146 size_t n;
147 char* p;
148
149 n = i * 32768;
150 size = n + 1;
151
152 p = malloc(size);
153 ASSERT_NOT_NULL(p);
154
155 memset(p, 'x', n);
156 p[n] = '\0';
157
158 ASSERT_EQ(0, uv_os_setenv(name, p));
159 ASSERT_EQ(0, uv_os_getenv(name, p, &size));
160 ASSERT_EQ(n, size);
161
162 for (n = 0; n < size; n++)
163 ASSERT_EQ('x', p[n]);
164
165 ASSERT_EQ(0, uv_os_unsetenv(name));
166 free(p);
167 }
168
169 return 0;
170 }
171