1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_getenv.c,v 1.3 2015/02/27 08:55:35 martin Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc * by Christos Zoulas
911be35a1SLionel Sambuc *
1011be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc * are met:
1311be35a1SLionel Sambuc *
1411be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1511be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1611be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1711be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in
1811be35a1SLionel Sambuc * the documentation and/or other materials provided with the
1911be35a1SLionel Sambuc * distribution.
2011be35a1SLionel Sambuc *
2111be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2211be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2311be35a1SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2411be35a1SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2511be35a1SLionel Sambuc * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2611be35a1SLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2711be35a1SLionel Sambuc * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2811be35a1SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2911be35a1SLionel Sambuc * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3011be35a1SLionel Sambuc * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3111be35a1SLionel Sambuc * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3211be35a1SLionel Sambuc * SUCH DAMAGE.
3311be35a1SLionel Sambuc */
3411be35a1SLionel Sambuc
3511be35a1SLionel Sambuc #include <sys/cdefs.h>
36*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_getenv.c,v 1.3 2015/02/27 08:55:35 martin Exp $");
3711be35a1SLionel Sambuc
3811be35a1SLionel Sambuc #include <atf-c.h>
3911be35a1SLionel Sambuc #include <errno.h>
4011be35a1SLionel Sambuc #include <stdio.h>
4111be35a1SLionel Sambuc #include <stdlib.h>
4211be35a1SLionel Sambuc #include <string.h>
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc extern char **environ;
4511be35a1SLionel Sambuc
4611be35a1SLionel Sambuc ATF_TC(clearenv_basic);
ATF_TC_HEAD(clearenv_basic,tc)4711be35a1SLionel Sambuc ATF_TC_HEAD(clearenv_basic, tc)
4811be35a1SLionel Sambuc {
4911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
5011be35a1SLionel Sambuc "Test user clearing environment directly");
5111be35a1SLionel Sambuc }
5211be35a1SLionel Sambuc
ATF_TC_BODY(clearenv_basic,tc)5311be35a1SLionel Sambuc ATF_TC_BODY(clearenv_basic, tc)
5411be35a1SLionel Sambuc {
5511be35a1SLionel Sambuc char name[1024], value[1024];
5611be35a1SLionel Sambuc
5711be35a1SLionel Sambuc for (size_t i = 0; i < 1024; i++) {
5811be35a1SLionel Sambuc snprintf(name, sizeof(name), "crap%zu", i);
5911be35a1SLionel Sambuc snprintf(value, sizeof(value), "%zu", i);
6011be35a1SLionel Sambuc ATF_CHECK(setenv(name, value, 1) != -1);
6111be35a1SLionel Sambuc }
6211be35a1SLionel Sambuc
6311be35a1SLionel Sambuc *environ = NULL;
6411be35a1SLionel Sambuc
6511be35a1SLionel Sambuc for (size_t i = 0; i < 1; i++) {
6611be35a1SLionel Sambuc snprintf(name, sizeof(name), "crap%zu", i);
6711be35a1SLionel Sambuc snprintf(value, sizeof(value), "%zu", i);
6811be35a1SLionel Sambuc ATF_CHECK(setenv(name, value, 1) != -1);
6911be35a1SLionel Sambuc }
7011be35a1SLionel Sambuc
7111be35a1SLionel Sambuc ATF_CHECK_STREQ(getenv("crap0"), "0");
7211be35a1SLionel Sambuc ATF_CHECK(getenv("crap1") == NULL);
7311be35a1SLionel Sambuc ATF_CHECK(getenv("crap2") == NULL);
7411be35a1SLionel Sambuc }
7511be35a1SLionel Sambuc
7611be35a1SLionel Sambuc ATF_TC(getenv_basic);
ATF_TC_HEAD(getenv_basic,tc)7711be35a1SLionel Sambuc ATF_TC_HEAD(getenv_basic, tc)
7811be35a1SLionel Sambuc {
7911be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
8011be35a1SLionel Sambuc "Test setenv(3), getenv(3)");
8111be35a1SLionel Sambuc }
8211be35a1SLionel Sambuc
ATF_TC_BODY(getenv_basic,tc)8311be35a1SLionel Sambuc ATF_TC_BODY(getenv_basic, tc)
8411be35a1SLionel Sambuc {
8511be35a1SLionel Sambuc ATF_CHECK(setenv("EVIL", "very=bad", 1) != -1);
8611be35a1SLionel Sambuc ATF_CHECK_STREQ(getenv("EVIL"), "very=bad");
8711be35a1SLionel Sambuc ATF_CHECK(getenv("EVIL=very") == NULL);
8811be35a1SLionel Sambuc ATF_CHECK(unsetenv("EVIL") != -1);
8911be35a1SLionel Sambuc }
9011be35a1SLionel Sambuc
9111be35a1SLionel Sambuc ATF_TC(putenv_basic);
ATF_TC_HEAD(putenv_basic,tc)9211be35a1SLionel Sambuc ATF_TC_HEAD(putenv_basic, tc)
9311be35a1SLionel Sambuc {
9411be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
9511be35a1SLionel Sambuc "Test putenv(3), getenv(3), unsetenv(3)");
9611be35a1SLionel Sambuc }
9711be35a1SLionel Sambuc
9811be35a1SLionel Sambuc
ATF_TC_BODY(putenv_basic,tc)9911be35a1SLionel Sambuc ATF_TC_BODY(putenv_basic, tc)
10011be35a1SLionel Sambuc {
10111be35a1SLionel Sambuc char string[1024];
10211be35a1SLionel Sambuc
10311be35a1SLionel Sambuc snprintf(string, sizeof(string), "crap=true");
10411be35a1SLionel Sambuc ATF_CHECK(putenv(string) != -1);
10511be35a1SLionel Sambuc ATF_CHECK_STREQ(getenv("crap"), "true");
10611be35a1SLionel Sambuc string[1] = 'l';
10711be35a1SLionel Sambuc ATF_CHECK_STREQ(getenv("clap"), "true");
10811be35a1SLionel Sambuc ATF_CHECK(getenv("crap") == NULL);
10911be35a1SLionel Sambuc string[1] = 'r';
11011be35a1SLionel Sambuc ATF_CHECK(unsetenv("crap") != -1);
11111be35a1SLionel Sambuc ATF_CHECK(getenv("crap") == NULL);
11211be35a1SLionel Sambuc
11311be35a1SLionel Sambuc ATF_CHECK_ERRNO(EINVAL, putenv(NULL) == -1);
11411be35a1SLionel Sambuc ATF_CHECK_ERRNO(EINVAL, putenv(__UNCONST("val")) == -1);
11511be35a1SLionel Sambuc ATF_CHECK_ERRNO(EINVAL, putenv(__UNCONST("=val")) == -1);
11611be35a1SLionel Sambuc }
11711be35a1SLionel Sambuc
11811be35a1SLionel Sambuc ATF_TC(setenv_basic);
ATF_TC_HEAD(setenv_basic,tc)11911be35a1SLionel Sambuc ATF_TC_HEAD(setenv_basic, tc)
12011be35a1SLionel Sambuc {
12111be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
12211be35a1SLionel Sambuc "Test setenv(3), getenv(3), unsetenv(3)");
123*0a6a1f1dSLionel Sambuc atf_tc_set_md_var(tc, "timeout", "600");
12411be35a1SLionel Sambuc }
12511be35a1SLionel Sambuc
ATF_TC_BODY(setenv_basic,tc)12611be35a1SLionel Sambuc ATF_TC_BODY(setenv_basic, tc)
12711be35a1SLionel Sambuc {
12811be35a1SLionel Sambuc const size_t numvars = 8192;
12911be35a1SLionel Sambuc size_t i, offset;
13011be35a1SLionel Sambuc char name[1024];
13111be35a1SLionel Sambuc char value[1024];
13211be35a1SLionel Sambuc
13311be35a1SLionel Sambuc offset = lrand48();
13411be35a1SLionel Sambuc for (i = 0; i < numvars; i++) {
13511be35a1SLionel Sambuc (void)snprintf(name, sizeof(name), "var%zu",
13611be35a1SLionel Sambuc (i * 7 + offset) % numvars);
13711be35a1SLionel Sambuc (void)snprintf(value, sizeof(value), "value%ld", lrand48());
13811be35a1SLionel Sambuc ATF_CHECK(setenv(name, value, 1) != -1);
13911be35a1SLionel Sambuc ATF_CHECK(setenv(name, "foo", 0) != -1);
14011be35a1SLionel Sambuc ATF_CHECK_STREQ(getenv(name), value);
14111be35a1SLionel Sambuc }
14211be35a1SLionel Sambuc
14311be35a1SLionel Sambuc offset = lrand48();
14411be35a1SLionel Sambuc for (i = 0; i < numvars; i++) {
14511be35a1SLionel Sambuc (void)snprintf(name, sizeof(name), "var%zu",
14611be35a1SLionel Sambuc (i * 11 + offset) % numvars);
14711be35a1SLionel Sambuc ATF_CHECK(unsetenv(name) != -1);
14811be35a1SLionel Sambuc ATF_CHECK(getenv(name) == NULL);
14911be35a1SLionel Sambuc ATF_CHECK(unsetenv(name) != -1);
15011be35a1SLionel Sambuc }
15111be35a1SLionel Sambuc
15211be35a1SLionel Sambuc ATF_CHECK_ERRNO(EINVAL, setenv(NULL, "val", 1) == -1);
15311be35a1SLionel Sambuc ATF_CHECK_ERRNO(EINVAL, setenv("", "val", 1) == -1);
15411be35a1SLionel Sambuc ATF_CHECK_ERRNO(EINVAL, setenv("v=r", "val", 1) == -1);
15511be35a1SLionel Sambuc ATF_CHECK_ERRNO(EINVAL, setenv("var", NULL, 1) == -1);
15611be35a1SLionel Sambuc
15711be35a1SLionel Sambuc ATF_CHECK(setenv("var", "=val", 1) == 0);
15811be35a1SLionel Sambuc ATF_CHECK_STREQ(getenv("var"), "=val");
15911be35a1SLionel Sambuc }
16011be35a1SLionel Sambuc
16111be35a1SLionel Sambuc ATF_TC(setenv_mixed);
ATF_TC_HEAD(setenv_mixed,tc)16211be35a1SLionel Sambuc ATF_TC_HEAD(setenv_mixed, tc)
16311be35a1SLionel Sambuc {
16411be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
16511be35a1SLionel Sambuc "Test mixing setenv(3), unsetenv(3) and putenv(3)");
16611be35a1SLionel Sambuc }
16711be35a1SLionel Sambuc
ATF_TC_BODY(setenv_mixed,tc)16811be35a1SLionel Sambuc ATF_TC_BODY(setenv_mixed, tc)
16911be35a1SLionel Sambuc {
17011be35a1SLionel Sambuc char string[32];
17111be35a1SLionel Sambuc
17211be35a1SLionel Sambuc (void)strcpy(string, "mixedcrap=putenv");
17311be35a1SLionel Sambuc
17411be35a1SLionel Sambuc ATF_CHECK(setenv("mixedcrap", "setenv", 1) != -1);
17511be35a1SLionel Sambuc ATF_CHECK_STREQ(getenv("mixedcrap"), "setenv");
17611be35a1SLionel Sambuc ATF_CHECK(putenv(string) != -1);
17711be35a1SLionel Sambuc ATF_CHECK_STREQ(getenv("mixedcrap"), "putenv");
17811be35a1SLionel Sambuc ATF_CHECK(unsetenv("mixedcrap") != -1);
17911be35a1SLionel Sambuc ATF_CHECK(getenv("mixedcrap") == NULL);
18011be35a1SLionel Sambuc
18111be35a1SLionel Sambuc ATF_CHECK(putenv(string) != -1);
18211be35a1SLionel Sambuc ATF_CHECK_STREQ(getenv("mixedcrap"), "putenv");
18311be35a1SLionel Sambuc ATF_CHECK(setenv("mixedcrap", "setenv", 1) != -1);
18411be35a1SLionel Sambuc ATF_CHECK_STREQ(getenv("mixedcrap"), "setenv");
18511be35a1SLionel Sambuc ATF_CHECK(unsetenv("mixedcrap") != -1);
18611be35a1SLionel Sambuc ATF_CHECK(getenv("mixedcrap") == NULL);
18711be35a1SLionel Sambuc }
18811be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)18911be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
19011be35a1SLionel Sambuc {
19111be35a1SLionel Sambuc
19211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, clearenv_basic);
19311be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, getenv_basic);
19411be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, putenv_basic);
19511be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, setenv_basic);
19611be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, setenv_mixed);
19711be35a1SLionel Sambuc
19811be35a1SLionel Sambuc return atf_no_error();
19911be35a1SLionel Sambuc }
200