test-env-vars.c
Go to the documentation of this file.
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 
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 */
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 */
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  return 0;
146 }
uv_os_setenv
UV_EXTERN int uv_os_setenv(const char *name, const char *value)
Definition: unix/core.c:1332
task.h
string.h
uv_os_getenv
UV_EXTERN int uv_os_getenv(const char *name, char *buffer, size_t *size)
Definition: unix/core.c:1306
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
ASSERT
#define ASSERT(expr)
Definition: task.h:102
setup.name
name
Definition: setup.py:542
BUF_SIZE
#define BUF_SIZE
Definition: test-env-vars.c:26
gen_stats_data.found
bool found
Definition: gen_stats_data.py:61
uv_env_item_s
Definition: uv.h:1218
uv_os_environ
UV_EXTERN int uv_os_environ(uv_env_item_t **envitems, int *count)
Definition: unix/core.c:1250
value
const char * value
Definition: hpack_parser_table.cc:165
uv.h
uv_os_free_environ
UV_EXTERN void uv_os_free_environ(uv_env_item_t *envitems, int count)
Definition: uv-common.c:805
uv_os_unsetenv
UV_EXTERN int uv_os_unsetenv(const char *name)
Definition: unix/core.c:1343
TEST_IMPL
TEST_IMPL(env_vars)
Definition: test-env-vars.c:28
fix_build_deps.r
r
Definition: fix_build_deps.py:491
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:26