xref: /freebsd/contrib/netbsd-tests/lib/libc/stdlib/t_getenv.c (revision 8f0ea33f2bbf3a6aa80235f0a02fa5f2780c2b17)
1 /*	$NetBSD: t_getenv.c,v 1.2 2011/07/15 13:54:31 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: t_getenv.c,v 1.2 2011/07/15 13:54:31 jruoho Exp $");
37 
38 #include <atf-c.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #ifdef __FreeBSD__
44 #include <signal.h>
45 #endif
46 
47 extern char	**environ;
48 
49 ATF_TC(clearenv_basic);
50 ATF_TC_HEAD(clearenv_basic, tc)
51 {
52 	atf_tc_set_md_var(tc, "descr",
53 	    "Test user clearing environment directly");
54 }
55 
56 ATF_TC_BODY(clearenv_basic, tc)
57 {
58 	char name[1024], value[1024];
59 
60 	for (size_t i = 0; i < 1024; i++) {
61 		snprintf(name, sizeof(name), "crap%zu", i);
62 		snprintf(value, sizeof(value), "%zu", i);
63 		ATF_CHECK(setenv(name, value, 1) != -1);
64 	}
65 
66 	*environ = NULL;
67 
68 	for (size_t i = 0; i < 1; i++) {
69 		snprintf(name, sizeof(name), "crap%zu", i);
70 		snprintf(value, sizeof(value), "%zu", i);
71 		ATF_CHECK(setenv(name, value, 1) != -1);
72 	}
73 
74 	ATF_CHECK_STREQ(getenv("crap0"), "0");
75 	ATF_CHECK(getenv("crap1") == NULL);
76 	ATF_CHECK(getenv("crap2") == NULL);
77 }
78 
79 ATF_TC(getenv_basic);
80 ATF_TC_HEAD(getenv_basic, tc)
81 {
82 	atf_tc_set_md_var(tc, "descr",
83 	    "Test setenv(3), getenv(3)");
84 }
85 
86 ATF_TC_BODY(getenv_basic, tc)
87 {
88 	ATF_CHECK(setenv("EVIL", "very=bad", 1) != -1);
89 	ATF_CHECK_STREQ(getenv("EVIL"), "very=bad");
90 	ATF_CHECK(getenv("EVIL=very") == NULL);
91 	ATF_CHECK(unsetenv("EVIL") != -1);
92 }
93 
94 ATF_TC(putenv_basic);
95 ATF_TC_HEAD(putenv_basic, tc)
96 {
97 	atf_tc_set_md_var(tc, "descr",
98 	    "Test putenv(3), getenv(3), unsetenv(3)");
99 }
100 
101 
102 ATF_TC_BODY(putenv_basic, tc)
103 {
104 	char string[1024];
105 
106 	snprintf(string, sizeof(string), "crap=true");
107 	ATF_CHECK(putenv(string) != -1);
108 	ATF_CHECK_STREQ(getenv("crap"), "true");
109 	string[1] = 'l';
110 	ATF_CHECK_STREQ(getenv("clap"), "true");
111 	ATF_CHECK(getenv("crap") == NULL);
112 	string[1] = 'r';
113 	ATF_CHECK(unsetenv("crap") != -1);
114 	ATF_CHECK(getenv("crap") == NULL);
115 
116 	ATF_CHECK_ERRNO(EINVAL, putenv(NULL) == -1);
117 	ATF_CHECK_ERRNO(EINVAL, putenv(__UNCONST("val")) == -1);
118 	ATF_CHECK_ERRNO(EINVAL, putenv(__UNCONST("=val")) == -1);
119 }
120 
121 ATF_TC(setenv_basic);
122 ATF_TC_HEAD(setenv_basic, tc)
123 {
124 	atf_tc_set_md_var(tc, "descr",
125 	    "Test setenv(3), getenv(3), unsetenv(3)");
126 	atf_tc_set_md_var(tc, "timeout", "300");
127 }
128 
129 ATF_TC_BODY(setenv_basic, tc)
130 {
131 	const size_t numvars = 8192;
132 	size_t i, offset;
133 	char name[1024];
134 	char value[1024];
135 
136 	offset = lrand48();
137 	for (i = 0; i < numvars; i++) {
138 		(void)snprintf(name, sizeof(name), "var%zu",
139 		    (i * 7 + offset) % numvars);
140 		(void)snprintf(value, sizeof(value), "value%ld", lrand48());
141 		ATF_CHECK(setenv(name, value, 1) != -1);
142 		ATF_CHECK(setenv(name, "foo", 0) != -1);
143 		ATF_CHECK_STREQ(getenv(name), value);
144 	}
145 
146 	offset = lrand48();
147 	for (i = 0; i < numvars; i++) {
148 		(void)snprintf(name, sizeof(name), "var%zu",
149 		    (i * 11 + offset) % numvars);
150 		ATF_CHECK(unsetenv(name) != -1);
151 		ATF_CHECK(getenv(name) == NULL);
152 		ATF_CHECK(unsetenv(name) != -1);
153 	}
154 
155 	ATF_CHECK_ERRNO(EINVAL, setenv(NULL, "val", 1) == -1);
156 	ATF_CHECK_ERRNO(EINVAL, setenv("", "val", 1) == -1);
157 	ATF_CHECK_ERRNO(EINVAL, setenv("v=r", "val", 1) == -1);
158 #ifdef __FreeBSD__
159 	/*
160 	   Both FreeBSD and OS/X does not validate the second
161 	   argument to setenv(3)
162 	 */
163 	atf_tc_expect_signal(SIGSEGV, "FreeBSD does not validate the second "
164 	    "argument to setenv(3); see bin/189805");
165 #endif
166 
167 	ATF_CHECK_ERRNO(EINVAL, setenv("var", NULL, 1) == -1);
168 
169 	ATF_CHECK(setenv("var", "=val", 1) == 0);
170 	ATF_CHECK_STREQ(getenv("var"), "=val");
171 }
172 
173 ATF_TC(setenv_mixed);
174 ATF_TC_HEAD(setenv_mixed, tc)
175 {
176 	atf_tc_set_md_var(tc, "descr",
177 	    "Test mixing setenv(3), unsetenv(3) and putenv(3)");
178 }
179 
180 ATF_TC_BODY(setenv_mixed, tc)
181 {
182 	char string[32];
183 
184 	(void)strcpy(string, "mixedcrap=putenv");
185 
186 	ATF_CHECK(setenv("mixedcrap", "setenv", 1) != -1);
187 	ATF_CHECK_STREQ(getenv("mixedcrap"), "setenv");
188 	ATF_CHECK(putenv(string) != -1);
189 	ATF_CHECK_STREQ(getenv("mixedcrap"), "putenv");
190 	ATF_CHECK(unsetenv("mixedcrap") != -1);
191 	ATF_CHECK(getenv("mixedcrap") == NULL);
192 
193 	ATF_CHECK(putenv(string) != -1);
194 	ATF_CHECK_STREQ(getenv("mixedcrap"), "putenv");
195 	ATF_CHECK(setenv("mixedcrap", "setenv", 1) != -1);
196 	ATF_CHECK_STREQ(getenv("mixedcrap"), "setenv");
197 	ATF_CHECK(unsetenv("mixedcrap") != -1);
198 	ATF_CHECK(getenv("mixedcrap") == NULL);
199 }
200 
201 ATF_TP_ADD_TCS(tp)
202 {
203 
204 	ATF_TP_ADD_TC(tp, clearenv_basic);
205 	ATF_TP_ADD_TC(tp, getenv_basic);
206 	ATF_TP_ADD_TC(tp, putenv_basic);
207 	ATF_TP_ADD_TC(tp, setenv_basic);
208 	ATF_TP_ADD_TC(tp, setenv_mixed);
209 
210 	return atf_no_error();
211 }
212