1 /* Copyright (c) 2007 The NetBSD Foundation, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
26 #include "atf-c/detail/env.h"
33 #include "atf-c/detail/test_helpers.h"
34 #include "atf-c/detail/text.h"
36 /* ---------------------------------------------------------------------
37 * Test cases for the free functions.
38 * --------------------------------------------------------------------- */
43 atf_tc_set_md_var(tc, "descr", "Tests the atf_env_has function");
47 ATF_REQUIRE(atf_env_has("PATH"));
48 ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_"));
54 atf_tc_set_md_var(tc, "descr", "Tests the atf_env_get function");
60 ATF_REQUIRE(atf_env_has("PATH"));
62 val = atf_env_get("PATH");
63 ATF_REQUIRE(strlen(val) > 0);
64 ATF_REQUIRE(strchr(val, ':') != NULL);
67 ATF_TC(get_with_default);
68 ATF_TC_HEAD(get_with_default, tc)
70 atf_tc_set_md_var(tc, "descr", "Tests the atf_env_get_with_default "
73 ATF_TC_BODY(get_with_default, tc)
77 ATF_REQUIRE(atf_env_has("PATH"));
79 val = atf_env_get_with_default("PATH", "unknown");
80 ATF_REQUIRE(strcmp(val, "unknown") != 0);
82 val = atf_env_get_with_default("_UNKNOWN_VARIABLE_", "foo bar");
83 ATF_REQUIRE(strcmp(val, "foo bar") == 0);
89 atf_tc_set_md_var(tc, "descr", "Tests the atf_env_set function");
95 ATF_REQUIRE(atf_env_has("PATH"));
96 RE(atf_text_format(&oldval, "%s", atf_env_get("PATH")));
97 RE(atf_env_set("PATH", "foo-bar"));
98 ATF_REQUIRE(strcmp(atf_env_get("PATH"), oldval) != 0);
99 ATF_REQUIRE(strcmp(atf_env_get("PATH"), "foo-bar") == 0);
102 ATF_REQUIRE(!atf_env_has("_UNDEFINED_VARIABLE_"));
103 RE(atf_env_set("_UNDEFINED_VARIABLE_", "foo2-bar2"));
104 ATF_REQUIRE(strcmp(atf_env_get("_UNDEFINED_VARIABLE_"),
109 ATF_TC_HEAD(unset, tc)
111 atf_tc_set_md_var(tc, "descr", "Tests the atf_env_unset function");
113 ATF_TC_BODY(unset, tc)
115 ATF_REQUIRE(atf_env_has("PATH"));
116 RE(atf_env_unset("PATH"));
117 ATF_REQUIRE(!atf_env_has("PATH"));
120 /* ---------------------------------------------------------------------
122 * --------------------------------------------------------------------- */
126 ATF_TP_ADD_TC(tp, has);
127 ATF_TP_ADD_TC(tp, get);
128 ATF_TP_ADD_TC(tp, get_with_default);
129 ATF_TP_ADD_TC(tp, set);
130 ATF_TP_ADD_TC(tp, unset);
132 return atf_no_error();