• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EQ(r, UV_EINVAL);
39   r = uv_os_setenv(name, NULL);
40   ASSERT_EQ(r, UV_EINVAL);
41   r = uv_os_setenv(NULL, NULL);
42   ASSERT_EQ(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_EQ(r, UV_EINVAL);
48   r = uv_os_getenv(name, NULL, &size);
49   ASSERT_EQ(r, UV_EINVAL);
50   r = uv_os_getenv(name, buf, NULL);
51   ASSERT_EQ(r, UV_EINVAL);
52   size = 0;
53   r = uv_os_getenv(name, buf, &size);
54   ASSERT_EQ(r, UV_EINVAL);
55 
56   /* Reject invalid inputs when deleting an environment variable */
57   r = uv_os_unsetenv(NULL);
58   ASSERT_EQ(r, UV_EINVAL);
59 
60   /* Successfully set an environment variable */
61   r = uv_os_setenv(name, "123456789");
62   ASSERT_OK(r);
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_OK(r);
69   ASSERT_OK(strcmp(buf, "123456789"));
70   ASSERT_EQ(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_EQ(r, UV_ENOBUFS);
77   ASSERT_EQ(size, BUF_SIZE);
78 
79   /* Successfully delete an environment variable */
80   r = uv_os_unsetenv(name);
81   ASSERT_OK(r);
82 
83   /* Return UV_ENOENT retrieving an environment variable that does not exist */
84   r = uv_os_getenv(name, buf, &size);
85   ASSERT_EQ(r, UV_ENOENT);
86 
87   /* Successfully delete an environment variable that does not exist */
88   r = uv_os_unsetenv(name);
89   ASSERT_OK(r);
90 
91   /* Setting an environment variable to the empty string does not delete it. */
92   r = uv_os_setenv(name, "");
93   ASSERT_OK(r);
94   size = BUF_SIZE;
95   r = uv_os_getenv(name, buf, &size);
96   ASSERT_OK(r);
97   ASSERT_OK(size);
98   ASSERT_OK(strlen(buf));
99 
100   /* Check getting all env variables. */
101   r = uv_os_setenv(name, "123456789");
102   ASSERT_OK(r);
103   r = uv_os_setenv(name2, "");
104   ASSERT_OK(r);
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_OK(r);
110 #endif
111 
112   r = uv_os_environ(&envitems, &envcount);
113   ASSERT_OK(r);
114   ASSERT_GT(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_OK(strcmp(envitems[i].value, "123456789"));
124     } else if (strcmp(envitems[i].name, name2) == 0) {
125       found++;
126       ASSERT_OK(strlen(envitems[i].value));
127     } else if (envitems[i].name[0] == '=') {
128       found_win_special++;
129     }
130   }
131 
132   ASSERT_EQ(2, found);
133 #ifdef _WIN32
134   ASSERT_GT(found_win_special, 0);
135 #else
136   /* There's no rule saying a key can't start with '='. */
137   (void) &found_win_special;
138 #endif
139 
140   uv_os_free_environ(envitems, envcount);
141 
142   r = uv_os_unsetenv(name);
143   ASSERT_OK(r);
144 
145   r = uv_os_unsetenv(name2);
146   ASSERT_OK(r);
147 
148   for (i = 1; i <= 4; i++) {
149     size_t n;
150     char* p;
151 
152     n = i * 32768;
153     size = n + 1;
154 
155     p = malloc(size);
156     ASSERT_NOT_NULL(p);
157 
158     memset(p, 'x', n);
159     p[n] = '\0';
160 
161     ASSERT_OK(uv_os_setenv(name, p));
162     ASSERT_OK(uv_os_getenv(name, p, &size));
163     ASSERT_EQ(n, size);
164 
165     for (n = 0; n < size; n++)
166       ASSERT_EQ('x', p[n]);
167 
168     ASSERT_OK(uv_os_unsetenv(name));
169     free(p);
170   }
171 
172   return 0;
173 }
174