2 // Automated Testing Framework (atf)
4 // Copyright (c) 2010 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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.
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.
30 #include "atf-c++/config.hpp"
31 #include "atf-c++/detail/text.hpp"
32 #include "atf-c++/macros.hpp"
34 #include "requirements.hpp"
37 namespace impl
= atf::atf_run
;
39 // -------------------------------------------------------------------------
40 // Auxiliary functions.
41 // -------------------------------------------------------------------------
45 const atf::tests::vars_map no_config
;
48 do_check(const std::string
& expected
, const atf::tests::vars_map
& metadata
,
49 const atf::tests::vars_map
& config
= no_config
)
51 const std::string actual
= impl::check_requirements(metadata
, config
);
52 if (!atf::text::match(actual
, expected
))
53 ATF_FAIL("Requirements failure reason \"" + actual
+ "\" does not "
54 "match \"" + expected
+ "\"");
57 } // anonymous namespace
59 // -------------------------------------------------------------------------
60 // Tests for the require.arch metadata property.
61 // -------------------------------------------------------------------------
63 ATF_TEST_CASE(require_arch_one_ok
);
64 ATF_TEST_CASE_HEAD(require_arch_one_ok
) {}
65 ATF_TEST_CASE_BODY(require_arch_one_ok
) {
66 atf::tests::vars_map metadata
;
67 metadata
["require.arch"] = atf::config::get("atf_arch");
68 do_check("", metadata
);
71 ATF_TEST_CASE(require_arch_one_fail
);
72 ATF_TEST_CASE_HEAD(require_arch_one_fail
) {}
73 ATF_TEST_CASE_BODY(require_arch_one_fail
) {
74 atf::tests::vars_map metadata
;
75 metadata
["require.arch"] = "__fake_arch__";
76 do_check("Requires the '__fake_arch__' architecture", metadata
);
79 ATF_TEST_CASE(require_arch_many_ok
);
80 ATF_TEST_CASE_HEAD(require_arch_many_ok
) {}
81 ATF_TEST_CASE_BODY(require_arch_many_ok
) {
82 atf::tests::vars_map metadata
;
83 metadata
["require.arch"] = "__foo__ " + atf::config::get("atf_arch") +
85 do_check("", metadata
);
88 ATF_TEST_CASE(require_arch_many_fail
);
89 ATF_TEST_CASE_HEAD(require_arch_many_fail
) {}
90 ATF_TEST_CASE_BODY(require_arch_many_fail
) {
91 atf::tests::vars_map metadata
;
92 metadata
["require.arch"] = "__foo__ __bar__ __baz__";
93 do_check("Requires one of the '__foo__ __bar__ __baz__' architectures",
97 // -------------------------------------------------------------------------
98 // Tests for the require.config metadata property.
99 // -------------------------------------------------------------------------
101 ATF_TEST_CASE(require_config_one_ok
);
102 ATF_TEST_CASE_HEAD(require_config_one_ok
) {}
103 ATF_TEST_CASE_BODY(require_config_one_ok
) {
104 atf::tests::vars_map metadata
, config
;
105 metadata
["require.config"] = "var1";
106 config
["var1"] = "some-value";
107 do_check("", metadata
, config
);
110 ATF_TEST_CASE(require_config_one_fail
);
111 ATF_TEST_CASE_HEAD(require_config_one_fail
) {}
112 ATF_TEST_CASE_BODY(require_config_one_fail
) {
113 atf::tests::vars_map metadata
, config
;
114 metadata
["require.config"] = "var1";
115 do_check("Required configuration variable 'var1' not defined", metadata
,
119 ATF_TEST_CASE(require_config_many_ok
);
120 ATF_TEST_CASE_HEAD(require_config_many_ok
) {}
121 ATF_TEST_CASE_BODY(require_config_many_ok
) {
122 atf::tests::vars_map metadata
, config
;
123 metadata
["require.config"] = "var1 var2 var3";
124 config
["var1"] = "first-value";
125 config
["var2"] = "second-value";
126 config
["var3"] = "third-value";
127 do_check("", metadata
, config
);
130 ATF_TEST_CASE(require_config_many_fail
);
131 ATF_TEST_CASE_HEAD(require_config_many_fail
) {}
132 ATF_TEST_CASE_BODY(require_config_many_fail
) {
133 atf::tests::vars_map metadata
, config
;
134 metadata
["require.config"] = "var1 var2 var3";
135 config
["var1"] = "first-value";
136 config
["var3"] = "third-value";
137 do_check("Required configuration variable 'var2' not defined", metadata
,
141 // -------------------------------------------------------------------------
142 // Tests for the require.files metadata property.
143 // -------------------------------------------------------------------------
145 ATF_TEST_CASE_WITHOUT_HEAD(require_files_one_ok
);
146 ATF_TEST_CASE_BODY(require_files_one_ok
) {
147 atf::tests::vars_map metadata
;
148 metadata
["require.files"] = "/bin/ls";
149 do_check("", metadata
);
152 ATF_TEST_CASE_WITHOUT_HEAD(require_files_one_missing
);
153 ATF_TEST_CASE_BODY(require_files_one_missing
) {
154 atf::tests::vars_map metadata
;
155 metadata
["require.files"] = "/non-existent/foo";
156 do_check("Required file '/non-existent/foo' not found", metadata
);
159 ATF_TEST_CASE_WITHOUT_HEAD(require_files_one_fail
);
160 ATF_TEST_CASE_BODY(require_files_one_fail
) {
161 atf::tests::vars_map metadata
;
162 metadata
["require.files"] = "/bin/cp this-is-relative";
163 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Relative.*(this-is-relative)",
164 impl::check_requirements(metadata
, no_config
));
167 ATF_TEST_CASE_WITHOUT_HEAD(require_files_many_ok
);
168 ATF_TEST_CASE_BODY(require_files_many_ok
) {
169 atf::tests::vars_map metadata
;
170 metadata
["require.files"] = "/bin/ls /bin/cp";
171 do_check("", metadata
);
174 ATF_TEST_CASE_WITHOUT_HEAD(require_files_many_missing
);
175 ATF_TEST_CASE_BODY(require_files_many_missing
) {
176 atf::tests::vars_map metadata
;
177 metadata
["require.files"] = "/bin/ls /non-existent/bar /bin/cp";
178 do_check("Required file '/non-existent/bar' not found", metadata
);
181 ATF_TEST_CASE_WITHOUT_HEAD(require_files_many_fail
);
182 ATF_TEST_CASE_BODY(require_files_many_fail
) {
183 atf::tests::vars_map metadata
;
184 metadata
["require.files"] = "/bin/cp also-relative";
185 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Relative.*(also-relative)",
186 impl::check_requirements(metadata
, no_config
));
189 // -------------------------------------------------------------------------
190 // Tests for the require.machine metadata property.
191 // -------------------------------------------------------------------------
193 ATF_TEST_CASE(require_machine_one_ok
);
194 ATF_TEST_CASE_HEAD(require_machine_one_ok
) {}
195 ATF_TEST_CASE_BODY(require_machine_one_ok
) {
196 atf::tests::vars_map metadata
;
197 metadata
["require.machine"] = atf::config::get("atf_machine");
198 do_check("", metadata
);
201 ATF_TEST_CASE(require_machine_one_fail
);
202 ATF_TEST_CASE_HEAD(require_machine_one_fail
) {}
203 ATF_TEST_CASE_BODY(require_machine_one_fail
) {
204 atf::tests::vars_map metadata
;
205 metadata
["require.machine"] = "__fake_machine__";
206 do_check("Requires the '__fake_machine__' machine type", metadata
);
209 ATF_TEST_CASE(require_machine_many_ok
);
210 ATF_TEST_CASE_HEAD(require_machine_many_ok
) {}
211 ATF_TEST_CASE_BODY(require_machine_many_ok
) {
212 atf::tests::vars_map metadata
;
213 metadata
["require.machine"] = "__foo__ " + atf::config::get("atf_machine") +
215 do_check("", metadata
);
218 ATF_TEST_CASE(require_machine_many_fail
);
219 ATF_TEST_CASE_HEAD(require_machine_many_fail
) {}
220 ATF_TEST_CASE_BODY(require_machine_many_fail
) {
221 atf::tests::vars_map metadata
;
222 metadata
["require.machine"] = "__foo__ __bar__ __baz__";
223 do_check("Requires one of the '__foo__ __bar__ __baz__' machine types",
227 // -------------------------------------------------------------------------
228 // Tests for the require.memory metadata property.
229 // -------------------------------------------------------------------------
231 ATF_TEST_CASE_WITHOUT_HEAD(require_memory_ok
);
232 ATF_TEST_CASE_BODY(require_memory_ok
) {
233 atf::tests::vars_map metadata
;
234 metadata
["require.memory"] = "1m";
235 do_check("", metadata
);
238 ATF_TEST_CASE_WITHOUT_HEAD(require_memory_not_enough
);
239 ATF_TEST_CASE_BODY(require_memory_not_enough
) {
240 atf::tests::vars_map metadata
;
241 metadata
["require.memory"] = "128t";
242 #if defined(__APPLE__) || defined(__NetBSD__)
243 do_check("Not enough memory; needed 140737488355328, available [0-9]*",
246 skip("Don't know how to check for the amount of physical memory");
250 ATF_TEST_CASE_WITHOUT_HEAD(require_memory_fail
);
251 ATF_TEST_CASE_BODY(require_memory_fail
) {
252 atf::tests::vars_map metadata
;
253 metadata
["require.memory"] = "foo";
254 ATF_REQUIRE_THROW(std::runtime_error
,
255 impl::check_requirements(metadata
, no_config
));
258 // -------------------------------------------------------------------------
259 // Tests for the require.progs metadata property.
260 // -------------------------------------------------------------------------
262 ATF_TEST_CASE(require_progs_one_ok
);
263 ATF_TEST_CASE_HEAD(require_progs_one_ok
) {}
264 ATF_TEST_CASE_BODY(require_progs_one_ok
) {
265 atf::tests::vars_map metadata
;
266 metadata
["require.progs"] = "cp";
267 do_check("", metadata
);
270 ATF_TEST_CASE(require_progs_one_missing
);
271 ATF_TEST_CASE_HEAD(require_progs_one_missing
) {}
272 ATF_TEST_CASE_BODY(require_progs_one_missing
) {
273 atf::tests::vars_map metadata
;
274 metadata
["require.progs"] = "cp __non-existent__";
275 do_check("Required program '__non-existent__' not found in the PATH",
279 ATF_TEST_CASE(require_progs_one_fail
);
280 ATF_TEST_CASE_HEAD(require_progs_one_fail
) {}
281 ATF_TEST_CASE_BODY(require_progs_one_fail
) {
282 atf::tests::vars_map metadata
;
283 metadata
["require.progs"] = "bin/cp";
284 ATF_REQUIRE_THROW(std::runtime_error
,
285 impl::check_requirements(metadata
, no_config
));
288 ATF_TEST_CASE(require_progs_many_ok
);
289 ATF_TEST_CASE_HEAD(require_progs_many_ok
) {}
290 ATF_TEST_CASE_BODY(require_progs_many_ok
) {
291 atf::tests::vars_map metadata
;
292 metadata
["require.progs"] = "cp ls mv";
293 do_check("", metadata
);
296 ATF_TEST_CASE(require_progs_many_missing
);
297 ATF_TEST_CASE_HEAD(require_progs_many_missing
) {}
298 ATF_TEST_CASE_BODY(require_progs_many_missing
) {
299 atf::tests::vars_map metadata
;
300 metadata
["require.progs"] = "mv ls __foo__ cp";
301 do_check("Required program '__foo__' not found in the PATH", metadata
);
304 ATF_TEST_CASE(require_progs_many_fail
);
305 ATF_TEST_CASE_HEAD(require_progs_many_fail
) {}
306 ATF_TEST_CASE_BODY(require_progs_many_fail
) {
307 atf::tests::vars_map metadata
;
308 metadata
["require.progs"] = "ls cp ../bin/cp";
309 ATF_REQUIRE_THROW(std::runtime_error
,
310 impl::check_requirements(metadata
, no_config
));
313 // -------------------------------------------------------------------------
314 // Tests for the require.user metadata property.
315 // -------------------------------------------------------------------------
317 ATF_TEST_CASE(require_user_root
);
318 ATF_TEST_CASE_HEAD(require_user_root
) {}
319 ATF_TEST_CASE_BODY(require_user_root
) {
320 atf::tests::vars_map metadata
;
321 metadata
["require.user"] = "root";
322 if (atf::atf_run::is_root())
323 do_check("", metadata
);
325 do_check("Requires root privileges", metadata
);
328 ATF_TEST_CASE(require_user_unprivileged
);
329 ATF_TEST_CASE_HEAD(require_user_unprivileged
) {}
330 ATF_TEST_CASE_BODY(require_user_unprivileged
) {
331 atf::tests::vars_map metadata
;
332 metadata
["require.user"] = "unprivileged";
333 if (atf::atf_run::is_root())
334 do_check("Requires an unprivileged user and the 'unprivileged-user' "
335 "configuration variable is not set", metadata
);
337 do_check("", metadata
);
340 ATF_TEST_CASE(require_user_fail
);
341 ATF_TEST_CASE_HEAD(require_user_fail
) {}
342 ATF_TEST_CASE_BODY(require_user_fail
) {
343 atf::tests::vars_map metadata
;
344 metadata
["require.user"] = "nobody";
345 ATF_REQUIRE_THROW(std::runtime_error
,
346 impl::check_requirements(metadata
, no_config
));
349 // -------------------------------------------------------------------------
351 // -------------------------------------------------------------------------
353 ATF_INIT_TEST_CASES(tcs
)
355 // Add test cases for require.arch.
356 ATF_ADD_TEST_CASE(tcs
, require_arch_one_ok
);
357 ATF_ADD_TEST_CASE(tcs
, require_arch_one_fail
);
358 ATF_ADD_TEST_CASE(tcs
, require_arch_many_ok
);
359 ATF_ADD_TEST_CASE(tcs
, require_arch_many_fail
);
361 // Add test cases for require.config.
362 ATF_ADD_TEST_CASE(tcs
, require_config_one_ok
);
363 ATF_ADD_TEST_CASE(tcs
, require_config_one_fail
);
364 ATF_ADD_TEST_CASE(tcs
, require_config_many_ok
);
365 ATF_ADD_TEST_CASE(tcs
, require_config_many_fail
);
367 // Add test cases for require.files.
368 ATF_ADD_TEST_CASE(tcs
, require_files_one_ok
);
369 ATF_ADD_TEST_CASE(tcs
, require_files_one_missing
);
370 ATF_ADD_TEST_CASE(tcs
, require_files_one_fail
);
371 ATF_ADD_TEST_CASE(tcs
, require_files_many_ok
);
372 ATF_ADD_TEST_CASE(tcs
, require_files_many_missing
);
373 ATF_ADD_TEST_CASE(tcs
, require_files_many_fail
);
375 // Add test cases for require.machine.
376 ATF_ADD_TEST_CASE(tcs
, require_machine_one_ok
);
377 ATF_ADD_TEST_CASE(tcs
, require_machine_one_fail
);
378 ATF_ADD_TEST_CASE(tcs
, require_machine_many_ok
);
379 ATF_ADD_TEST_CASE(tcs
, require_machine_many_fail
);
381 // Add test cases for require.memory.
382 ATF_ADD_TEST_CASE(tcs
, require_memory_ok
);
383 ATF_ADD_TEST_CASE(tcs
, require_memory_not_enough
);
384 ATF_ADD_TEST_CASE(tcs
, require_memory_fail
);
386 // Add test cases for require.progs.
387 ATF_ADD_TEST_CASE(tcs
, require_progs_one_ok
);
388 ATF_ADD_TEST_CASE(tcs
, require_progs_one_missing
);
389 ATF_ADD_TEST_CASE(tcs
, require_progs_one_fail
);
390 ATF_ADD_TEST_CASE(tcs
, require_progs_many_ok
);
391 ATF_ADD_TEST_CASE(tcs
, require_progs_many_missing
);
392 ATF_ADD_TEST_CASE(tcs
, require_progs_many_fail
);
394 // Add test cases for require.user.
395 ATF_ADD_TEST_CASE(tcs
, require_user_root
);
396 ATF_ADD_TEST_CASE(tcs
, require_user_unprivileged
);
397 ATF_ADD_TEST_CASE(tcs
, require_user_fail
);