1*0a6a1f1dSLionel Sambuc //
2*0a6a1f1dSLionel Sambuc // Automated Testing Framework (atf)
3*0a6a1f1dSLionel Sambuc //
4*0a6a1f1dSLionel Sambuc // Copyright (c) 2007 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc // All rights reserved.
6*0a6a1f1dSLionel Sambuc //
7*0a6a1f1dSLionel Sambuc // Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc // modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc // are met:
10*0a6a1f1dSLionel Sambuc // 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc // notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc // 2. Redistributions in binary form must reproduce the above copyright
13*0a6a1f1dSLionel Sambuc // notice, this list of conditions and the following disclaimer in the
14*0a6a1f1dSLionel Sambuc // documentation and/or other materials provided with the distribution.
15*0a6a1f1dSLionel Sambuc //
16*0a6a1f1dSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*0a6a1f1dSLionel Sambuc // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*0a6a1f1dSLionel Sambuc // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*0a6a1f1dSLionel Sambuc // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*0a6a1f1dSLionel Sambuc // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*0a6a1f1dSLionel Sambuc // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*0a6a1f1dSLionel Sambuc // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*0a6a1f1dSLionel Sambuc // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*0a6a1f1dSLionel Sambuc // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*0a6a1f1dSLionel Sambuc // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*0a6a1f1dSLionel Sambuc // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*0a6a1f1dSLionel Sambuc // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*0a6a1f1dSLionel Sambuc //
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambuc #include <cstring>
31*0a6a1f1dSLionel Sambuc #include <iostream>
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc #include <atf-c++.hpp>
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc #include "config.hpp"
36*0a6a1f1dSLionel Sambuc #include "env.hpp"
37*0a6a1f1dSLionel Sambuc #include "exceptions.hpp"
38*0a6a1f1dSLionel Sambuc
39*0a6a1f1dSLionel Sambuc static const char *test_value = "env-value";
40*0a6a1f1dSLionel Sambuc
41*0a6a1f1dSLionel Sambuc static struct varnames {
42*0a6a1f1dSLionel Sambuc const char *lc;
43*0a6a1f1dSLionel Sambuc const char *uc;
44*0a6a1f1dSLionel Sambuc bool can_be_empty;
45*0a6a1f1dSLionel Sambuc } all_vars[] = {
46*0a6a1f1dSLionel Sambuc { "atf_arch", "ATF_ARCH", false },
47*0a6a1f1dSLionel Sambuc { "atf_build_cc", "ATF_BUILD_CC", false },
48*0a6a1f1dSLionel Sambuc { "atf_build_cflags", "ATF_BUILD_CFLAGS", true },
49*0a6a1f1dSLionel Sambuc { "atf_build_cpp", "ATF_BUILD_CPP", false },
50*0a6a1f1dSLionel Sambuc { "atf_build_cppflags", "ATF_BUILD_CPPFLAGS", true },
51*0a6a1f1dSLionel Sambuc { "atf_build_cxx", "ATF_BUILD_CXX", false },
52*0a6a1f1dSLionel Sambuc { "atf_build_cxxflags", "ATF_BUILD_CXXFLAGS", true },
53*0a6a1f1dSLionel Sambuc { "atf_confdir", "ATF_CONFDIR", false },
54*0a6a1f1dSLionel Sambuc { "atf_includedir", "ATF_INCLUDEDIR", false },
55*0a6a1f1dSLionel Sambuc { "atf_libdir", "ATF_LIBDIR", false },
56*0a6a1f1dSLionel Sambuc { "atf_libexecdir", "ATF_LIBEXECDIR", false },
57*0a6a1f1dSLionel Sambuc { "atf_machine", "ATF_MACHINE", false },
58*0a6a1f1dSLionel Sambuc { "atf_pkgdatadir", "ATF_PKGDATADIR", false },
59*0a6a1f1dSLionel Sambuc { "atf_shell", "ATF_SHELL", false },
60*0a6a1f1dSLionel Sambuc { "atf_workdir", "ATF_WORKDIR", false },
61*0a6a1f1dSLionel Sambuc { NULL, NULL, false }
62*0a6a1f1dSLionel Sambuc };
63*0a6a1f1dSLionel Sambuc
64*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
65*0a6a1f1dSLionel Sambuc // Auxiliary functions.
66*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
67*0a6a1f1dSLionel Sambuc
68*0a6a1f1dSLionel Sambuc namespace tools {
69*0a6a1f1dSLionel Sambuc namespace config {
70*0a6a1f1dSLionel Sambuc void __reinit(void);
71*0a6a1f1dSLionel Sambuc }
72*0a6a1f1dSLionel Sambuc }
73*0a6a1f1dSLionel Sambuc
74*0a6a1f1dSLionel Sambuc static
75*0a6a1f1dSLionel Sambuc void
set_env_var(const char * name,const char * val)76*0a6a1f1dSLionel Sambuc set_env_var(const char* name, const char* val)
77*0a6a1f1dSLionel Sambuc {
78*0a6a1f1dSLionel Sambuc try {
79*0a6a1f1dSLionel Sambuc tools::env::set(name, val);
80*0a6a1f1dSLionel Sambuc } catch (const tools::system_error&) {
81*0a6a1f1dSLionel Sambuc ATF_FAIL(std::string("set_env_var(") + name + ", " + val +
82*0a6a1f1dSLionel Sambuc ") failed");
83*0a6a1f1dSLionel Sambuc }
84*0a6a1f1dSLionel Sambuc }
85*0a6a1f1dSLionel Sambuc
86*0a6a1f1dSLionel Sambuc static
87*0a6a1f1dSLionel Sambuc void
unset_env_var(const char * name)88*0a6a1f1dSLionel Sambuc unset_env_var(const char* name)
89*0a6a1f1dSLionel Sambuc {
90*0a6a1f1dSLionel Sambuc try {
91*0a6a1f1dSLionel Sambuc tools::env::unset(name);
92*0a6a1f1dSLionel Sambuc } catch (const tools::system_error&) {
93*0a6a1f1dSLionel Sambuc ATF_FAIL(std::string("unset_env_var(") + name + ") failed");
94*0a6a1f1dSLionel Sambuc }
95*0a6a1f1dSLionel Sambuc }
96*0a6a1f1dSLionel Sambuc
97*0a6a1f1dSLionel Sambuc static
98*0a6a1f1dSLionel Sambuc size_t
all_vars_count(void)99*0a6a1f1dSLionel Sambuc all_vars_count(void)
100*0a6a1f1dSLionel Sambuc {
101*0a6a1f1dSLionel Sambuc size_t count = 0;
102*0a6a1f1dSLionel Sambuc for (const struct varnames* v = all_vars; v->lc != NULL; v++)
103*0a6a1f1dSLionel Sambuc count++;
104*0a6a1f1dSLionel Sambuc return count;
105*0a6a1f1dSLionel Sambuc }
106*0a6a1f1dSLionel Sambuc
107*0a6a1f1dSLionel Sambuc static
108*0a6a1f1dSLionel Sambuc void
unset_all(void)109*0a6a1f1dSLionel Sambuc unset_all(void)
110*0a6a1f1dSLionel Sambuc {
111*0a6a1f1dSLionel Sambuc for (const struct varnames* v = all_vars; v->lc != NULL; v++)
112*0a6a1f1dSLionel Sambuc unset_env_var(v->uc);
113*0a6a1f1dSLionel Sambuc }
114*0a6a1f1dSLionel Sambuc
115*0a6a1f1dSLionel Sambuc static
116*0a6a1f1dSLionel Sambuc void
compare_one(const char * var,const char * expvalue)117*0a6a1f1dSLionel Sambuc compare_one(const char* var, const char* expvalue)
118*0a6a1f1dSLionel Sambuc {
119*0a6a1f1dSLionel Sambuc std::cout << "Checking that " << var << " is set to " << expvalue << "\n";
120*0a6a1f1dSLionel Sambuc
121*0a6a1f1dSLionel Sambuc for (const struct varnames* v = all_vars; v->lc != NULL; v++) {
122*0a6a1f1dSLionel Sambuc if (std::strcmp(v->lc, var) == 0)
123*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ(tools::config::get(v->lc), test_value);
124*0a6a1f1dSLionel Sambuc else
125*0a6a1f1dSLionel Sambuc ATF_REQUIRE(tools::config::get(v->lc) != test_value);
126*0a6a1f1dSLionel Sambuc }
127*0a6a1f1dSLionel Sambuc }
128*0a6a1f1dSLionel Sambuc
129*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
130*0a6a1f1dSLionel Sambuc // Test cases for the free functions.
131*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
132*0a6a1f1dSLionel Sambuc
133*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(get);
ATF_TEST_CASE_HEAD(get)134*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(get)
135*0a6a1f1dSLionel Sambuc {
136*0a6a1f1dSLionel Sambuc set_md_var("descr", "Tests the config::get function");
137*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(get)138*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(get)
139*0a6a1f1dSLionel Sambuc {
140*0a6a1f1dSLionel Sambuc // Unset all known environment variables and make sure the built-in
141*0a6a1f1dSLionel Sambuc // values do not match the bogus value we will use for testing.
142*0a6a1f1dSLionel Sambuc unset_all();
143*0a6a1f1dSLionel Sambuc tools::config::__reinit();
144*0a6a1f1dSLionel Sambuc for (const struct varnames* v = all_vars; v->lc != NULL; v++)
145*0a6a1f1dSLionel Sambuc ATF_REQUIRE(tools::config::get(v->lc) != test_value);
146*0a6a1f1dSLionel Sambuc
147*0a6a1f1dSLionel Sambuc // Test the behavior of empty values.
148*0a6a1f1dSLionel Sambuc for (const struct varnames* v = all_vars; v->lc != NULL; v++) {
149*0a6a1f1dSLionel Sambuc unset_all();
150*0a6a1f1dSLionel Sambuc if (!tools::config::get(v->lc).empty()) {
151*0a6a1f1dSLionel Sambuc set_env_var(v->uc, "");
152*0a6a1f1dSLionel Sambuc tools::config::__reinit();
153*0a6a1f1dSLionel Sambuc if (v->can_be_empty)
154*0a6a1f1dSLionel Sambuc ATF_REQUIRE(tools::config::get(v->lc).empty());
155*0a6a1f1dSLionel Sambuc else
156*0a6a1f1dSLionel Sambuc ATF_REQUIRE(!tools::config::get(v->lc).empty());
157*0a6a1f1dSLionel Sambuc }
158*0a6a1f1dSLionel Sambuc }
159*0a6a1f1dSLionel Sambuc
160*0a6a1f1dSLionel Sambuc // Check if the ATF_ARCH variable is recognized.
161*0a6a1f1dSLionel Sambuc for (const struct varnames* v = all_vars; v->lc != NULL; v++) {
162*0a6a1f1dSLionel Sambuc unset_all();
163*0a6a1f1dSLionel Sambuc set_env_var(v->uc, test_value);
164*0a6a1f1dSLionel Sambuc tools::config::__reinit();
165*0a6a1f1dSLionel Sambuc compare_one(v->lc, test_value);
166*0a6a1f1dSLionel Sambuc }
167*0a6a1f1dSLionel Sambuc }
168*0a6a1f1dSLionel Sambuc
169*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(get_all);
ATF_TEST_CASE_HEAD(get_all)170*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(get_all)
171*0a6a1f1dSLionel Sambuc {
172*0a6a1f1dSLionel Sambuc set_md_var("descr", "Tests the config::get_all function");
173*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(get_all)174*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(get_all)
175*0a6a1f1dSLionel Sambuc {
176*0a6a1f1dSLionel Sambuc tools::config::__reinit();
177*0a6a1f1dSLionel Sambuc
178*0a6a1f1dSLionel Sambuc // Check that the valid variables, and only those, are returned.
179*0a6a1f1dSLionel Sambuc std::map< std::string, std::string > vars = tools::config::get_all();
180*0a6a1f1dSLionel Sambuc ATF_REQUIRE_EQ(vars.size(), all_vars_count());
181*0a6a1f1dSLionel Sambuc for (const struct varnames* v = all_vars; v->lc != NULL; v++)
182*0a6a1f1dSLionel Sambuc ATF_REQUIRE(vars.find(v->lc) != vars.end());
183*0a6a1f1dSLionel Sambuc }
184*0a6a1f1dSLionel Sambuc
185*0a6a1f1dSLionel Sambuc ATF_TEST_CASE(has);
ATF_TEST_CASE_HEAD(has)186*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_HEAD(has)
187*0a6a1f1dSLionel Sambuc {
188*0a6a1f1dSLionel Sambuc set_md_var("descr", "Tests the config::has function");
189*0a6a1f1dSLionel Sambuc }
ATF_TEST_CASE_BODY(has)190*0a6a1f1dSLionel Sambuc ATF_TEST_CASE_BODY(has)
191*0a6a1f1dSLionel Sambuc {
192*0a6a1f1dSLionel Sambuc tools::config::__reinit();
193*0a6a1f1dSLionel Sambuc
194*0a6a1f1dSLionel Sambuc // Check for all the variables that must exist.
195*0a6a1f1dSLionel Sambuc for (const struct varnames* v = all_vars; v->lc != NULL; v++)
196*0a6a1f1dSLionel Sambuc ATF_REQUIRE(tools::config::has(v->lc));
197*0a6a1f1dSLionel Sambuc
198*0a6a1f1dSLionel Sambuc // Same as above, but using uppercase (which is incorrect).
199*0a6a1f1dSLionel Sambuc for (const struct varnames* v = all_vars; v->lc != NULL; v++)
200*0a6a1f1dSLionel Sambuc ATF_REQUIRE(!tools::config::has(v->uc));
201*0a6a1f1dSLionel Sambuc
202*0a6a1f1dSLionel Sambuc // Check for some other variables that cannot exist.
203*0a6a1f1dSLionel Sambuc ATF_REQUIRE(!tools::config::has("foo"));
204*0a6a1f1dSLionel Sambuc ATF_REQUIRE(!tools::config::has("BAR"));
205*0a6a1f1dSLionel Sambuc ATF_REQUIRE(!tools::config::has("atf_foo"));
206*0a6a1f1dSLionel Sambuc ATF_REQUIRE(!tools::config::has("ATF_BAR"));
207*0a6a1f1dSLionel Sambuc ATF_REQUIRE(!tools::config::has("atf_shel"));
208*0a6a1f1dSLionel Sambuc ATF_REQUIRE(!tools::config::has("atf_shells"));
209*0a6a1f1dSLionel Sambuc }
210*0a6a1f1dSLionel Sambuc
211*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
212*0a6a1f1dSLionel Sambuc // Main.
213*0a6a1f1dSLionel Sambuc // ------------------------------------------------------------------------
214*0a6a1f1dSLionel Sambuc
ATF_INIT_TEST_CASES(tcs)215*0a6a1f1dSLionel Sambuc ATF_INIT_TEST_CASES(tcs)
216*0a6a1f1dSLionel Sambuc {
217*0a6a1f1dSLionel Sambuc // Add the test cases for the free functions.
218*0a6a1f1dSLionel Sambuc ATF_ADD_TEST_CASE(tcs, has);
219*0a6a1f1dSLionel Sambuc ATF_ADD_TEST_CASE(tcs, get);
220*0a6a1f1dSLionel Sambuc ATF_ADD_TEST_CASE(tcs, get_all);
221*0a6a1f1dSLionel Sambuc }
222