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