xref: /freebsd/contrib/netbsd-tests/lib/libc/stdlib/t_getenv.c (revision c22165b4f1f5d38b681921797a44b3ba8c13b7e0)
1*640235e2SEnji Cooper /*	$NetBSD: t_getenv.c,v 1.3 2015/02/27 08:55:35 martin Exp $ */
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2010 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper  * by Christos Zoulas
957718be8SEnji Cooper  *
1057718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper  * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper  * are met:
1357718be8SEnji Cooper  *
1457718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1557718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1657718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1757718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in
1857718be8SEnji Cooper  *    the documentation and/or other materials provided with the
1957718be8SEnji Cooper  *    distribution.
2057718be8SEnji Cooper  *
2157718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2257718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2357718be8SEnji Cooper  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2457718be8SEnji Cooper  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
2557718be8SEnji Cooper  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2657718be8SEnji Cooper  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2757718be8SEnji Cooper  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2857718be8SEnji Cooper  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2957718be8SEnji Cooper  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3057718be8SEnji Cooper  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3157718be8SEnji Cooper  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3257718be8SEnji Cooper  * SUCH DAMAGE.
3357718be8SEnji Cooper  */
3457718be8SEnji Cooper 
3557718be8SEnji Cooper #include <sys/cdefs.h>
36*640235e2SEnji Cooper __RCSID("$NetBSD: t_getenv.c,v 1.3 2015/02/27 08:55:35 martin Exp $");
3757718be8SEnji Cooper 
3857718be8SEnji Cooper #include <atf-c.h>
3957718be8SEnji Cooper #include <errno.h>
4057718be8SEnji Cooper #include <stdio.h>
4157718be8SEnji Cooper #include <stdlib.h>
4257718be8SEnji Cooper #include <string.h>
43ff0ba872SEnji Cooper #ifdef __FreeBSD__
44ac2a1d3dSEnji Cooper #include <signal.h>
45ac2a1d3dSEnji Cooper #endif
4657718be8SEnji Cooper 
4757718be8SEnji Cooper extern char	**environ;
4857718be8SEnji Cooper 
4957718be8SEnji Cooper ATF_TC(clearenv_basic);
ATF_TC_HEAD(clearenv_basic,tc)5057718be8SEnji Cooper ATF_TC_HEAD(clearenv_basic, tc)
5157718be8SEnji Cooper {
5257718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
5357718be8SEnji Cooper 	    "Test user clearing environment directly");
5457718be8SEnji Cooper }
5557718be8SEnji Cooper 
ATF_TC_BODY(clearenv_basic,tc)5657718be8SEnji Cooper ATF_TC_BODY(clearenv_basic, tc)
5757718be8SEnji Cooper {
5857718be8SEnji Cooper 	char name[1024], value[1024];
5957718be8SEnji Cooper 
6057718be8SEnji Cooper 	for (size_t i = 0; i < 1024; i++) {
6157718be8SEnji Cooper 		snprintf(name, sizeof(name), "crap%zu", i);
6257718be8SEnji Cooper 		snprintf(value, sizeof(value), "%zu", i);
6357718be8SEnji Cooper 		ATF_CHECK(setenv(name, value, 1) != -1);
6457718be8SEnji Cooper 	}
6557718be8SEnji Cooper 
6657718be8SEnji Cooper 	*environ = NULL;
6757718be8SEnji Cooper 
6857718be8SEnji Cooper 	for (size_t i = 0; i < 1; i++) {
6957718be8SEnji Cooper 		snprintf(name, sizeof(name), "crap%zu", i);
7057718be8SEnji Cooper 		snprintf(value, sizeof(value), "%zu", i);
7157718be8SEnji Cooper 		ATF_CHECK(setenv(name, value, 1) != -1);
7257718be8SEnji Cooper 	}
7357718be8SEnji Cooper 
7457718be8SEnji Cooper 	ATF_CHECK_STREQ(getenv("crap0"), "0");
7557718be8SEnji Cooper 	ATF_CHECK(getenv("crap1") == NULL);
7657718be8SEnji Cooper 	ATF_CHECK(getenv("crap2") == NULL);
7757718be8SEnji Cooper }
7857718be8SEnji Cooper 
7957718be8SEnji Cooper ATF_TC(getenv_basic);
ATF_TC_HEAD(getenv_basic,tc)8057718be8SEnji Cooper ATF_TC_HEAD(getenv_basic, tc)
8157718be8SEnji Cooper {
8257718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
8357718be8SEnji Cooper 	    "Test setenv(3), getenv(3)");
8457718be8SEnji Cooper }
8557718be8SEnji Cooper 
ATF_TC_BODY(getenv_basic,tc)8657718be8SEnji Cooper ATF_TC_BODY(getenv_basic, tc)
8757718be8SEnji Cooper {
8857718be8SEnji Cooper 	ATF_CHECK(setenv("EVIL", "very=bad", 1) != -1);
8957718be8SEnji Cooper 	ATF_CHECK_STREQ(getenv("EVIL"), "very=bad");
9057718be8SEnji Cooper 	ATF_CHECK(getenv("EVIL=very") == NULL);
9157718be8SEnji Cooper 	ATF_CHECK(unsetenv("EVIL") != -1);
9257718be8SEnji Cooper }
9357718be8SEnji Cooper 
9457718be8SEnji Cooper ATF_TC(putenv_basic);
ATF_TC_HEAD(putenv_basic,tc)9557718be8SEnji Cooper ATF_TC_HEAD(putenv_basic, tc)
9657718be8SEnji Cooper {
9757718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
9857718be8SEnji Cooper 	    "Test putenv(3), getenv(3), unsetenv(3)");
9957718be8SEnji Cooper }
10057718be8SEnji Cooper 
10157718be8SEnji Cooper 
ATF_TC_BODY(putenv_basic,tc)10257718be8SEnji Cooper ATF_TC_BODY(putenv_basic, tc)
10357718be8SEnji Cooper {
10457718be8SEnji Cooper 	char string[1024];
10557718be8SEnji Cooper 
10657718be8SEnji Cooper 	snprintf(string, sizeof(string), "crap=true");
10757718be8SEnji Cooper 	ATF_CHECK(putenv(string) != -1);
10857718be8SEnji Cooper 	ATF_CHECK_STREQ(getenv("crap"), "true");
10957718be8SEnji Cooper 	string[1] = 'l';
11057718be8SEnji Cooper 	ATF_CHECK_STREQ(getenv("clap"), "true");
11157718be8SEnji Cooper 	ATF_CHECK(getenv("crap") == NULL);
11257718be8SEnji Cooper 	string[1] = 'r';
11357718be8SEnji Cooper 	ATF_CHECK(unsetenv("crap") != -1);
11457718be8SEnji Cooper 	ATF_CHECK(getenv("crap") == NULL);
11557718be8SEnji Cooper 
11657718be8SEnji Cooper 	ATF_CHECK_ERRNO(EINVAL, putenv(NULL) == -1);
11757718be8SEnji Cooper 	ATF_CHECK_ERRNO(EINVAL, putenv(__UNCONST("val")) == -1);
11857718be8SEnji Cooper 	ATF_CHECK_ERRNO(EINVAL, putenv(__UNCONST("=val")) == -1);
11957718be8SEnji Cooper }
12057718be8SEnji Cooper 
12157718be8SEnji Cooper ATF_TC(setenv_basic);
ATF_TC_HEAD(setenv_basic,tc)12257718be8SEnji Cooper ATF_TC_HEAD(setenv_basic, tc)
12357718be8SEnji Cooper {
12457718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
12557718be8SEnji Cooper 	    "Test setenv(3), getenv(3), unsetenv(3)");
126*640235e2SEnji Cooper 	atf_tc_set_md_var(tc, "timeout", "600");
12757718be8SEnji Cooper }
12857718be8SEnji Cooper 
ATF_TC_BODY(setenv_basic,tc)12957718be8SEnji Cooper ATF_TC_BODY(setenv_basic, tc)
13057718be8SEnji Cooper {
13157718be8SEnji Cooper 	const size_t numvars = 8192;
13257718be8SEnji Cooper 	size_t i, offset;
13357718be8SEnji Cooper 	char name[1024];
13457718be8SEnji Cooper 	char value[1024];
13557718be8SEnji Cooper 
13657718be8SEnji Cooper 	offset = lrand48();
13757718be8SEnji Cooper 	for (i = 0; i < numvars; i++) {
13857718be8SEnji Cooper 		(void)snprintf(name, sizeof(name), "var%zu",
13957718be8SEnji Cooper 		    (i * 7 + offset) % numvars);
14057718be8SEnji Cooper 		(void)snprintf(value, sizeof(value), "value%ld", lrand48());
14157718be8SEnji Cooper 		ATF_CHECK(setenv(name, value, 1) != -1);
14257718be8SEnji Cooper 		ATF_CHECK(setenv(name, "foo", 0) != -1);
14357718be8SEnji Cooper 		ATF_CHECK_STREQ(getenv(name), value);
14457718be8SEnji Cooper 	}
14557718be8SEnji Cooper 
14657718be8SEnji Cooper 	offset = lrand48();
14757718be8SEnji Cooper 	for (i = 0; i < numvars; i++) {
14857718be8SEnji Cooper 		(void)snprintf(name, sizeof(name), "var%zu",
14957718be8SEnji Cooper 		    (i * 11 + offset) % numvars);
15057718be8SEnji Cooper 		ATF_CHECK(unsetenv(name) != -1);
15157718be8SEnji Cooper 		ATF_CHECK(getenv(name) == NULL);
15257718be8SEnji Cooper 		ATF_CHECK(unsetenv(name) != -1);
15357718be8SEnji Cooper 	}
15457718be8SEnji Cooper 
15557718be8SEnji Cooper 	ATF_CHECK_ERRNO(EINVAL, setenv(NULL, "val", 1) == -1);
15657718be8SEnji Cooper 	ATF_CHECK_ERRNO(EINVAL, setenv("", "val", 1) == -1);
15757718be8SEnji Cooper 	ATF_CHECK_ERRNO(EINVAL, setenv("v=r", "val", 1) == -1);
158ff0ba872SEnji Cooper #ifdef __FreeBSD__
159ac2a1d3dSEnji Cooper 	/*
160ac2a1d3dSEnji Cooper 	   Both FreeBSD and OS/X does not validate the second
161ac2a1d3dSEnji Cooper 	   argument to setenv(3)
162ac2a1d3dSEnji Cooper 	 */
163ac2a1d3dSEnji Cooper 	atf_tc_expect_signal(SIGSEGV, "FreeBSD does not validate the second "
164ac2a1d3dSEnji Cooper 	    "argument to setenv(3); see bin/189805");
165ac2a1d3dSEnji Cooper #endif
166ac2a1d3dSEnji Cooper 
16757718be8SEnji Cooper 	ATF_CHECK_ERRNO(EINVAL, setenv("var", NULL, 1) == -1);
16857718be8SEnji Cooper 
16957718be8SEnji Cooper 	ATF_CHECK(setenv("var", "=val", 1) == 0);
17057718be8SEnji Cooper 	ATF_CHECK_STREQ(getenv("var"), "=val");
17157718be8SEnji Cooper }
17257718be8SEnji Cooper 
17357718be8SEnji Cooper ATF_TC(setenv_mixed);
ATF_TC_HEAD(setenv_mixed,tc)17457718be8SEnji Cooper ATF_TC_HEAD(setenv_mixed, tc)
17557718be8SEnji Cooper {
17657718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
17757718be8SEnji Cooper 	    "Test mixing setenv(3), unsetenv(3) and putenv(3)");
17857718be8SEnji Cooper }
17957718be8SEnji Cooper 
ATF_TC_BODY(setenv_mixed,tc)18057718be8SEnji Cooper ATF_TC_BODY(setenv_mixed, tc)
18157718be8SEnji Cooper {
18257718be8SEnji Cooper 	char string[32];
18357718be8SEnji Cooper 
18457718be8SEnji Cooper 	(void)strcpy(string, "mixedcrap=putenv");
18557718be8SEnji Cooper 
18657718be8SEnji Cooper 	ATF_CHECK(setenv("mixedcrap", "setenv", 1) != -1);
18757718be8SEnji Cooper 	ATF_CHECK_STREQ(getenv("mixedcrap"), "setenv");
18857718be8SEnji Cooper 	ATF_CHECK(putenv(string) != -1);
18957718be8SEnji Cooper 	ATF_CHECK_STREQ(getenv("mixedcrap"), "putenv");
19057718be8SEnji Cooper 	ATF_CHECK(unsetenv("mixedcrap") != -1);
19157718be8SEnji Cooper 	ATF_CHECK(getenv("mixedcrap") == NULL);
19257718be8SEnji Cooper 
19357718be8SEnji Cooper 	ATF_CHECK(putenv(string) != -1);
19457718be8SEnji Cooper 	ATF_CHECK_STREQ(getenv("mixedcrap"), "putenv");
19557718be8SEnji Cooper 	ATF_CHECK(setenv("mixedcrap", "setenv", 1) != -1);
19657718be8SEnji Cooper 	ATF_CHECK_STREQ(getenv("mixedcrap"), "setenv");
19757718be8SEnji Cooper 	ATF_CHECK(unsetenv("mixedcrap") != -1);
19857718be8SEnji Cooper 	ATF_CHECK(getenv("mixedcrap") == NULL);
19957718be8SEnji Cooper }
20057718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)20157718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
20257718be8SEnji Cooper {
20357718be8SEnji Cooper 
20457718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, clearenv_basic);
20557718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, getenv_basic);
20657718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, putenv_basic);
20757718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, setenv_basic);
20857718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, setenv_mixed);
20957718be8SEnji Cooper 
21057718be8SEnji Cooper 	return atf_no_error();
21157718be8SEnji Cooper }
212