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