xref: /freebsd/lib/libc/tests/stdlib/clearenv_test.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2021 Mariusz Zaborski <oshogbo@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * Test for clearenv(3) routine.
30  */
31 
32 #include <sys/cdefs.h>
33 #include <atf-c.h>
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 
38 #define TEST_VARIABLE		"TEST_VAR"
39 #define TEST_SYSTEM_VARIABLE	"PWD"
40 
41 extern char **environ;
42 
43 void
44 create_multiple_variables(int num)
45 {
46 	char name[64];
47 	char value[64];
48 	int i;
49 
50 	for (i = 0; i < num; i++) {
51 		snprintf(name, sizeof(name), "%s_%d", TEST_VARIABLE, i);
52 		snprintf(value, sizeof(value), "%d", i);
53 		ATF_CHECK(getenv(name) == NULL);
54 		ATF_CHECK(setenv(name, value, 0) != -1);
55 		ATF_CHECK_STREQ(getenv(name), value);
56 	}
57 }
58 
59 void
60 check_if_nulled_variables(int num)
61 {
62 	char name[64];
63 	int i;
64 
65 	for (i = 0; i < num; i++) {
66 		snprintf(name, sizeof(name), "%s_%d", TEST_VARIABLE, i);
67 		ATF_CHECK(getenv(name) == NULL);
68 	}
69 }
70 
71 ATF_TC_WITHOUT_HEAD(clearenv__single_var_test);
72 ATF_TC_BODY(clearenv__single_var_test, tc)
73 {
74 
75 	ATF_CHECK(setenv(TEST_VARIABLE, "true", 0) != -1);
76 	ATF_CHECK_STREQ(getenv(TEST_VARIABLE), "true");
77 	ATF_CHECK(clearenv() == 0);
78 	ATF_CHECK(getenv(TEST_VARIABLE) == NULL);
79 }
80 
81 ATF_TC_WITHOUT_HEAD(clearenv__multiple_vars_test);
82 ATF_TC_BODY(clearenv__multiple_vars_test, tc)
83 {
84 
85 	create_multiple_variables(10);
86 	ATF_CHECK(clearenv() == 0);
87 	check_if_nulled_variables(10);
88 }
89 
90 ATF_TC_WITHOUT_HEAD(clearenv__recreated_vars_test);
91 ATF_TC_BODY(clearenv__recreated_vars_test, tc)
92 {
93 
94 	create_multiple_variables(10);
95 	ATF_CHECK(clearenv() == 0);
96 	check_if_nulled_variables(10);
97 	create_multiple_variables(10);
98 }
99 
100 ATF_TC_WITHOUT_HEAD(clearenv__system_var_test);
101 ATF_TC_BODY(clearenv__system_var_test, tc)
102 {
103 
104 	ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) != NULL);
105 	ATF_CHECK(clearenv() == 0);
106 	ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) == NULL);
107 }
108 
109 ATF_TC_WITHOUT_HEAD(clearenv__recreated_system_var_test);
110 ATF_TC_BODY(clearenv__recreated_system_var_test, tc)
111 {
112 
113 	ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) != NULL);
114 	ATF_CHECK(clearenv() == 0);
115 	ATF_CHECK(getenv(TEST_SYSTEM_VARIABLE) == NULL);
116 	ATF_CHECK(setenv(TEST_SYSTEM_VARIABLE, "test", 0) != -1);
117 	ATF_CHECK_STREQ(getenv(TEST_SYSTEM_VARIABLE), "test");
118 }
119 
120 ATF_TC_WITHOUT_HEAD(clearenv__double_clear_vars);
121 ATF_TC_BODY(clearenv__double_clear_vars, tc)
122 {
123 
124 	create_multiple_variables(10);
125 	ATF_CHECK(clearenv() == 0);
126 	check_if_nulled_variables(10);
127 	ATF_CHECK(clearenv() == 0);
128 	check_if_nulled_variables(10);
129 	create_multiple_variables(10);
130 }
131 
132 ATF_TC_WITHOUT_HEAD(clearenv__environ_null);
133 ATF_TC_BODY(clearenv__environ_null, tc)
134 {
135 
136 	ATF_CHECK(clearenv() == 0);
137 	ATF_CHECK(environ != NULL);
138 }
139 
140 ATF_TC_WITHOUT_HEAD(clearenv__putenv_vars);
141 ATF_TC_BODY(clearenv__putenv_vars, tc)
142 {
143 	char buf[64], ref[64];
144 
145 	snprintf(buf, sizeof(buf), "%s=1", TEST_VARIABLE);
146 	strcpy(ref, buf);
147 
148 	ATF_CHECK(getenv(TEST_VARIABLE) == NULL);
149 	ATF_CHECK(putenv(buf) != -1);
150 	ATF_CHECK(strcmp(getenv(TEST_VARIABLE), "1") == 0);
151 
152 	ATF_CHECK(clearenv() == 0);
153 
154 	ATF_CHECK(getenv(TEST_VARIABLE) == NULL);
155 	ATF_CHECK(strcmp(buf, ref) == 0);
156 }
157 
158 ATF_TP_ADD_TCS(tp)
159 {
160 
161 	ATF_TP_ADD_TC(tp, clearenv__single_var_test);
162 	ATF_TP_ADD_TC(tp, clearenv__multiple_vars_test);
163 	ATF_TP_ADD_TC(tp, clearenv__recreated_vars_test);
164 
165 	ATF_TP_ADD_TC(tp, clearenv__system_var_test);
166 	ATF_TP_ADD_TC(tp, clearenv__recreated_system_var_test);
167 
168 	ATF_TP_ADD_TC(tp, clearenv__double_clear_vars);
169 	ATF_TP_ADD_TC(tp, clearenv__environ_null);
170 
171 	ATF_TP_ADD_TC(tp, clearenv__putenv_vars);
172 
173 	return (atf_no_error());
174 }
175