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