]> git.saurik.com Git - apple/libc.git/blob - tests/getenv.c
Libc-1158.50.2.tar.gz
[apple/libc.git] / tests / getenv.c
1 #include <stdlib.h>
2
3 #include <darwintest.h>
4
5 T_DECL(setenv_getenv, "getenv returns value set by setenv")
6 {
7 char *name = "foo";
8 char *value = "bar";
9 int setenv_rc = setenv(name, value, 0);
10 T_EXPECT_EQ(0, setenv_rc, "setenv must succeed with 0 return code");
11 char *getenv_result = getenv(name);
12 T_EXPECT_EQ_STR(value, getenv_result, "getenv must return setenv argument");
13 }
14
15 T_DECL(setenv_overwrite, "getenv returns the latest setenv argument")
16 {
17 char *name = "foo";
18 char *first_value = "bar";
19 char *second_value = "baz";
20 int setenv_rc = 0;
21 setenv_rc = setenv(name, first_value, 0);
22 T_EXPECT_EQ(0, setenv_rc, "setenv must succeed with 0 return code");
23 setenv_rc = setenv(name, second_value, 1);
24 T_EXPECT_EQ(0, setenv_rc, "setenv must succeed with 0 return code");
25 char *getenv_result = getenv(name);
26 T_EXPECT_EQ_STR(second_value, getenv_result, "getenv must return the latest setenv argument");
27 }
28
29 T_DECL(setenv_dont_overwrite, "setenv respects overwrite")
30 {
31 char *name = "foo";
32 char *first_value = "bar";
33 char *second_value = "baz";
34 int setenv_rc = 0;
35 setenv_rc = setenv(name, first_value, 0);
36 T_EXPECT_EQ(0, setenv_rc, "setenv must succeed with 0 return code");
37 setenv_rc = setenv(name, second_value, 0);
38 T_EXPECT_EQ(0, setenv_rc, "setenv must succeed with 0 return code");
39 char *getenv_result = getenv(name);
40 T_EXPECT_EQ_STR(first_value, getenv_result, "the second setenv must not overwrite the first one");
41 }
42
43 /* There are tests for leading '=' in values because BSDs used to strip them off: rdar://problem/19342460 */
44
45 T_DECL(setenv_accepts_leading_eq_sign, "setenv accepts values starting with '='")
46 {
47 char *name = "foo";
48 char *value = "=bar";
49 int setenv_rc = setenv(name, value, 0);
50 T_EXPECT_EQ(0, setenv_rc, "setenv must succeed with 0 return code");
51 char *getenv_result = getenv(name);
52 T_EXPECT_EQ_STR(value, getenv_result, "getenv must return setenv argument");
53 }
54
55 T_DECL(setenv_accepts_leading_eq_sign_overwrite, "setenv accepts values starting with '=' when overwriting an existing value")
56 {
57 char *name = "foo";
58 char *first_value = "bar";
59 char *second_value = "=baz";
60 int setenv_rc = 0;
61 setenv_rc = setenv(name, first_value, 0);
62 T_EXPECT_EQ(0, setenv_rc, "setenv must succeed with 0 return code");
63 setenv_rc = setenv(name, second_value, 1);
64 T_EXPECT_EQ(0, setenv_rc, "setenv must succeed with 0 return code");
65 char *getenv_result = getenv(name);
66 T_EXPECT_EQ_STR(second_value, getenv_result, "getenv must return the latest setenv argument");
67 }