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
28TEST_IMPL(env_vars) {
29 const char* name = "UV_TEST_FOO";
30 char buf[BUF_SIZE];
31 size_t size;
32 int r;
33
34 /* Reject invalid inputs when setting an environment variable */
35 r = uv_os_setenv(NULL, "foo");
36 ASSERT(r == UV_EINVAL);
37 r = uv_os_setenv(name, NULL);
38 ASSERT(r == UV_EINVAL);
39 r = uv_os_setenv(NULL, NULL);
40 ASSERT(r == UV_EINVAL);
41
42 /* Reject invalid inputs when retrieving an environment variable */
43 size = BUF_SIZE;
44 r = uv_os_getenv(NULL, buf, &size);
45 ASSERT(r == UV_EINVAL);
46 r = uv_os_getenv(name, NULL, &size);
47 ASSERT(r == UV_EINVAL);
48 r = uv_os_getenv(name, buf, NULL);
49 ASSERT(r == UV_EINVAL);
50 size = 0;
51 r = uv_os_getenv(name, buf, &size);
52 ASSERT(r == UV_EINVAL);
53
54 /* Reject invalid inputs when deleting an environment variable */
55 r = uv_os_unsetenv(NULL);
56 ASSERT(r == UV_EINVAL);
57
58 /* Successfully set an environment variable */
59 r = uv_os_setenv(name, "123456789");
60 ASSERT(r == 0);
61
62 /* Successfully read an environment variable */
63 size = BUF_SIZE;
64 buf[0] = '\0';
65 r = uv_os_getenv(name, buf, &size);
66 ASSERT(r == 0);
67 ASSERT(strcmp(buf, "123456789") == 0);
68 ASSERT(size == BUF_SIZE - 1);
69
70 /* Return UV_ENOBUFS if the buffer cannot hold the environment variable */
71 size = BUF_SIZE - 1;
72 buf[0] = '\0';
73 r = uv_os_getenv(name, buf, &size);
74 ASSERT(r == UV_ENOBUFS);
75 ASSERT(size == BUF_SIZE);
76
77 /* Successfully delete an environment variable */
78 r = uv_os_unsetenv(name);
79 ASSERT(r == 0);
80
81 /* Return UV_ENOENT retrieving an environment variable that does not exist */
82 r = uv_os_getenv(name, buf, &size);
83 ASSERT(r == UV_ENOENT);
84
85 /* Successfully delete an environment variable that does not exist */
86 r = uv_os_unsetenv(name);
87 ASSERT(r == 0);
88
89 return 0;
90}
91