1*11be35a1SLionel Sambuc // Copyright 2011 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc extern "C" {
30*11be35a1SLionel Sambuc #include <unistd.h>
31*11be35a1SLionel Sambuc }
32*11be35a1SLionel Sambuc
33*11be35a1SLionel Sambuc #include <atf-c++.hpp>
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc #include "engine/config.hpp"
36*11be35a1SLionel Sambuc #include "engine/kyuafile.hpp"
37*11be35a1SLionel Sambuc #include "utils/config/tree.ipp"
38*11be35a1SLionel Sambuc #include "utils/fs/operations.hpp"
39*11be35a1SLionel Sambuc #include "utils/fs/path.hpp"
40*11be35a1SLionel Sambuc #include "utils/optional.ipp"
41*11be35a1SLionel Sambuc #include "utils/passwd.hpp"
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc namespace config = utils::config;
44*11be35a1SLionel Sambuc namespace fs = utils::fs;
45*11be35a1SLionel Sambuc namespace passwd = utils::passwd;
46*11be35a1SLionel Sambuc
47*11be35a1SLionel Sambuc using utils::none;
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambuc namespace {
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc /// Gets the path to an example file.
54*11be35a1SLionel Sambuc ///
55*11be35a1SLionel Sambuc /// \param tc The caller test case. Needed to obtain its 'examplesdir'
56*11be35a1SLionel Sambuc /// property, if any.
57*11be35a1SLionel Sambuc /// \param name The name of the example file.
58*11be35a1SLionel Sambuc ///
59*11be35a1SLionel Sambuc /// \return A path to the desired example file. This can either be inside the
60*11be35a1SLionel Sambuc /// source tree before installing Kyua or in the target installation directory
61*11be35a1SLionel Sambuc /// after installation.
62*11be35a1SLionel Sambuc static fs::path
example_file(const atf::tests::tc * tc,const char * name)63*11be35a1SLionel Sambuc example_file(const atf::tests::tc* tc, const char* name)
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc const fs::path examplesdir =
66*11be35a1SLionel Sambuc tc->has_config_var("examplesdir") ?
67*11be35a1SLionel Sambuc fs::path(tc->get_config_var("examplesdir")) :
68*11be35a1SLionel Sambuc fs::path(KYUA_EXAMPLESDIR);
69*11be35a1SLionel Sambuc return examplesdir / name;
70*11be35a1SLionel Sambuc }
71*11be35a1SLionel Sambuc
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc } // anonymous namespace
74*11be35a1SLionel Sambuc
75*11be35a1SLionel Sambuc
76*11be35a1SLionel Sambuc ATF_TEST_CASE(kyua_conf);
ATF_TEST_CASE_HEAD(kyua_conf)77*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(kyua_conf)
78*11be35a1SLionel Sambuc {
79*11be35a1SLionel Sambuc set_md_var("require.files", example_file(this, "kyua.conf").str());
80*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(kyua_conf)81*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(kyua_conf)
82*11be35a1SLionel Sambuc {
83*11be35a1SLionel Sambuc std::vector< passwd::user > users;
84*11be35a1SLionel Sambuc users.push_back(passwd::user("nobody", 1, 2));
85*11be35a1SLionel Sambuc passwd::set_mock_users_for_testing(users);
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc const config::tree user_config = engine::load_config(
88*11be35a1SLionel Sambuc example_file(this, "kyua.conf"));
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(
91*11be35a1SLionel Sambuc "x86_64",
92*11be35a1SLionel Sambuc user_config.lookup< config::string_node >("architecture"));
93*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(
94*11be35a1SLionel Sambuc "amd64",
95*11be35a1SLionel Sambuc user_config.lookup< config::string_node >("platform"));
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(
98*11be35a1SLionel Sambuc "nobody",
99*11be35a1SLionel Sambuc user_config.lookup< engine::user_node >("unprivileged_user").name);
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc config::properties_map exp_test_suites;
102*11be35a1SLionel Sambuc exp_test_suites["test_suites.FreeBSD.iterations"] = "1000";
103*11be35a1SLionel Sambuc exp_test_suites["test_suites.FreeBSD.run_old_tests"] = "false";
104*11be35a1SLionel Sambuc exp_test_suites["test_suites.NetBSD.file_systems"] = "ffs lfs ext2fs";
105*11be35a1SLionel Sambuc exp_test_suites["test_suites.NetBSD.iterations"] = "100";
106*11be35a1SLionel Sambuc exp_test_suites["test_suites.NetBSD.run_broken_tests"] = "true";
107*11be35a1SLionel Sambuc ATF_REQUIRE(exp_test_suites == user_config.all_properties("test_suites"));
108*11be35a1SLionel Sambuc }
109*11be35a1SLionel Sambuc
110*11be35a1SLionel Sambuc
111*11be35a1SLionel Sambuc ATF_TEST_CASE(kyuafile_top__no_matches);
ATF_TEST_CASE_HEAD(kyuafile_top__no_matches)112*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(kyuafile_top__no_matches)
113*11be35a1SLionel Sambuc {
114*11be35a1SLionel Sambuc set_md_var("require.files", example_file(this, "Kyuafile.top").str());
115*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(kyuafile_top__no_matches)116*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(kyuafile_top__no_matches)
117*11be35a1SLionel Sambuc {
118*11be35a1SLionel Sambuc fs::mkdir(fs::path("root"), 0755);
119*11be35a1SLionel Sambuc const fs::path source_path = example_file(this, "Kyuafile.top");
120*11be35a1SLionel Sambuc ATF_REQUIRE(::symlink(source_path.c_str(), "root/Kyuafile") != -1);
121*11be35a1SLionel Sambuc
122*11be35a1SLionel Sambuc atf::utils::create_file("root/file", "");
123*11be35a1SLionel Sambuc fs::mkdir(fs::path("root/subdir"), 0755);
124*11be35a1SLionel Sambuc
125*11be35a1SLionel Sambuc const engine::kyuafile kyuafile = engine::kyuafile::load(
126*11be35a1SLionel Sambuc fs::path("root/Kyuafile"), none);
127*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(fs::path("root"), kyuafile.source_root());
128*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(fs::path("root"), kyuafile.build_root());
129*11be35a1SLionel Sambuc ATF_REQUIRE(kyuafile.test_programs().empty());
130*11be35a1SLionel Sambuc }
131*11be35a1SLionel Sambuc
132*11be35a1SLionel Sambuc
133*11be35a1SLionel Sambuc ATF_TEST_CASE(kyuafile_top__some_matches);
ATF_TEST_CASE_HEAD(kyuafile_top__some_matches)134*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(kyuafile_top__some_matches)
135*11be35a1SLionel Sambuc {
136*11be35a1SLionel Sambuc set_md_var("require.files", example_file(this, "Kyuafile.top").str());
137*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(kyuafile_top__some_matches)138*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(kyuafile_top__some_matches)
139*11be35a1SLionel Sambuc {
140*11be35a1SLionel Sambuc fs::mkdir(fs::path("root"), 0755);
141*11be35a1SLionel Sambuc const fs::path source_path = example_file(this, "Kyuafile.top");
142*11be35a1SLionel Sambuc ATF_REQUIRE(::symlink(source_path.c_str(), "root/Kyuafile") != -1);
143*11be35a1SLionel Sambuc
144*11be35a1SLionel Sambuc atf::utils::create_file("root/file", "");
145*11be35a1SLionel Sambuc
146*11be35a1SLionel Sambuc fs::mkdir(fs::path("root/subdir1"), 0755);
147*11be35a1SLionel Sambuc atf::utils::create_file("root/subdir1/Kyuafile",
148*11be35a1SLionel Sambuc "syntax(2)\n"
149*11be35a1SLionel Sambuc "atf_test_program{name='a', test_suite='b'}\n");
150*11be35a1SLionel Sambuc atf::utils::create_file("root/subdir1/a", "");
151*11be35a1SLionel Sambuc
152*11be35a1SLionel Sambuc fs::mkdir(fs::path("root/subdir2"), 0755);
153*11be35a1SLionel Sambuc atf::utils::create_file("root/subdir2/Kyuafile",
154*11be35a1SLionel Sambuc "syntax(2)\n"
155*11be35a1SLionel Sambuc "atf_test_program{name='c', test_suite='d'}\n");
156*11be35a1SLionel Sambuc atf::utils::create_file("root/subdir2/c", "");
157*11be35a1SLionel Sambuc atf::utils::create_file("root/subdir2/Kyuafile.etc", "invalid");
158*11be35a1SLionel Sambuc
159*11be35a1SLionel Sambuc const engine::kyuafile kyuafile = engine::kyuafile::load(
160*11be35a1SLionel Sambuc fs::path("root/Kyuafile"), none);
161*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(fs::path("root"), kyuafile.source_root());
162*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(fs::path("root"), kyuafile.build_root());
163*11be35a1SLionel Sambuc
164*11be35a1SLionel Sambuc engine::test_program exp_test_program_a(
165*11be35a1SLionel Sambuc "atf", fs::path("subdir1/a"), fs::path("root"), "b",
166*11be35a1SLionel Sambuc engine::metadata_builder().build());
167*11be35a1SLionel Sambuc engine::test_program exp_test_program_c(
168*11be35a1SLionel Sambuc "atf", fs::path("subdir2/c"), fs::path("root"), "d",
169*11be35a1SLionel Sambuc engine::metadata_builder().build());
170*11be35a1SLionel Sambuc
171*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(2, kyuafile.test_programs().size());
172*11be35a1SLionel Sambuc ATF_REQUIRE((exp_test_program_a == *kyuafile.test_programs()[0] &&
173*11be35a1SLionel Sambuc exp_test_program_c == *kyuafile.test_programs()[1])
174*11be35a1SLionel Sambuc ||
175*11be35a1SLionel Sambuc (exp_test_program_a == *kyuafile.test_programs()[1] &&
176*11be35a1SLionel Sambuc exp_test_program_c == *kyuafile.test_programs()[0]));
177*11be35a1SLionel Sambuc }
178*11be35a1SLionel Sambuc
179*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)180*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
181*11be35a1SLionel Sambuc {
182*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, kyua_conf);
183*11be35a1SLionel Sambuc
184*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, kyuafile_top__no_matches);
185*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, kyuafile_top__some_matches);
186*11be35a1SLionel Sambuc }
187