1*11be35a1SLionel Sambuc // Copyright 2010 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 #include "engine/config.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc #if defined(HAVE_CONFIG_H)
32*11be35a1SLionel Sambuc # include "config.h"
33*11be35a1SLionel Sambuc #endif
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc #include <stdexcept>
36*11be35a1SLionel Sambuc #include <vector>
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc #include <atf-c++.hpp>
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc #include "engine/exceptions.hpp"
41*11be35a1SLionel Sambuc #include "utils/cmdline/exceptions.hpp"
42*11be35a1SLionel Sambuc #include "utils/cmdline/parser.hpp"
43*11be35a1SLionel Sambuc #include "utils/config/tree.ipp"
44*11be35a1SLionel Sambuc #include "utils/passwd.hpp"
45*11be35a1SLionel Sambuc
46*11be35a1SLionel Sambuc namespace config = utils::config;
47*11be35a1SLionel Sambuc namespace fs = utils::fs;
48*11be35a1SLionel Sambuc namespace passwd = utils::passwd;
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambuc using utils::none;
51*11be35a1SLionel Sambuc using utils::optional;
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc
54*11be35a1SLionel Sambuc namespace {
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc
57*11be35a1SLionel Sambuc /// Replaces the system user database with a fake one for testing purposes.
58*11be35a1SLionel Sambuc static void
set_mock_users(void)59*11be35a1SLionel Sambuc set_mock_users(void)
60*11be35a1SLionel Sambuc {
61*11be35a1SLionel Sambuc std::vector< passwd::user > users;
62*11be35a1SLionel Sambuc users.push_back(passwd::user("user1", 100, 150));
63*11be35a1SLionel Sambuc users.push_back(passwd::user("user2", 200, 250));
64*11be35a1SLionel Sambuc passwd::set_mock_users_for_testing(users);
65*11be35a1SLionel Sambuc }
66*11be35a1SLionel Sambuc
67*11be35a1SLionel Sambuc
68*11be35a1SLionel Sambuc /// Checks that the default values of a config object match our expectations.
69*11be35a1SLionel Sambuc ///
70*11be35a1SLionel Sambuc /// This fails the test case if any field of the input config object is not
71*11be35a1SLionel Sambuc /// what we expect.
72*11be35a1SLionel Sambuc ///
73*11be35a1SLionel Sambuc /// \param config The configuration to validate.
74*11be35a1SLionel Sambuc static void
validate_defaults(const config::tree & config)75*11be35a1SLionel Sambuc validate_defaults(const config::tree& config)
76*11be35a1SLionel Sambuc {
77*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(
78*11be35a1SLionel Sambuc KYUA_ARCHITECTURE,
79*11be35a1SLionel Sambuc config.lookup< config::string_node >("architecture"));
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(
82*11be35a1SLionel Sambuc KYUA_PLATFORM,
83*11be35a1SLionel Sambuc config.lookup< config::string_node >("platform"));
84*11be35a1SLionel Sambuc
85*11be35a1SLionel Sambuc ATF_REQUIRE(!config.is_set("unprivileged_user"));
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc ATF_REQUIRE(config.all_properties("test_suites").empty());
88*11be35a1SLionel Sambuc }
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc
91*11be35a1SLionel Sambuc } // anonymous namespace
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc
94*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(config__defaults);
ATF_TEST_CASE_BODY(config__defaults)95*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(config__defaults)
96*11be35a1SLionel Sambuc {
97*11be35a1SLionel Sambuc const config::tree user_config = engine::default_config();
98*11be35a1SLionel Sambuc validate_defaults(user_config);
99*11be35a1SLionel Sambuc }
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc
102*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(config__load__defaults);
ATF_TEST_CASE_BODY(config__load__defaults)103*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(config__load__defaults)
104*11be35a1SLionel Sambuc {
105*11be35a1SLionel Sambuc atf::utils::create_file("config", "syntax(2)\n");
106*11be35a1SLionel Sambuc
107*11be35a1SLionel Sambuc const config::tree user_config = engine::load_config(fs::path("config"));
108*11be35a1SLionel Sambuc validate_defaults(user_config);
109*11be35a1SLionel Sambuc }
110*11be35a1SLionel Sambuc
111*11be35a1SLionel Sambuc
112*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(config__load__overrides);
ATF_TEST_CASE_BODY(config__load__overrides)113*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(config__load__overrides)
114*11be35a1SLionel Sambuc {
115*11be35a1SLionel Sambuc set_mock_users();
116*11be35a1SLionel Sambuc
117*11be35a1SLionel Sambuc atf::utils::create_file(
118*11be35a1SLionel Sambuc "config",
119*11be35a1SLionel Sambuc "syntax(2)\n"
120*11be35a1SLionel Sambuc "architecture = 'test-architecture'\n"
121*11be35a1SLionel Sambuc "platform = 'test-platform'\n"
122*11be35a1SLionel Sambuc "unprivileged_user = 'user2'\n"
123*11be35a1SLionel Sambuc "test_suites.mysuite.myvar = 'myvalue'\n");
124*11be35a1SLionel Sambuc
125*11be35a1SLionel Sambuc const config::tree user_config = engine::load_config(fs::path("config"));
126*11be35a1SLionel Sambuc
127*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("test-architecture",
128*11be35a1SLionel Sambuc user_config.lookup_string("architecture"));
129*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("test-platform",
130*11be35a1SLionel Sambuc user_config.lookup_string("platform"));
131*11be35a1SLionel Sambuc
132*11be35a1SLionel Sambuc const passwd::user& user = user_config.lookup< engine::user_node >(
133*11be35a1SLionel Sambuc "unprivileged_user");
134*11be35a1SLionel Sambuc ATF_REQUIRE_EQ("user2", user.name);
135*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(200, user.uid);
136*11be35a1SLionel Sambuc
137*11be35a1SLionel Sambuc config::properties_map exp_test_suites;
138*11be35a1SLionel Sambuc exp_test_suites["test_suites.mysuite.myvar"] = "myvalue";
139*11be35a1SLionel Sambuc
140*11be35a1SLionel Sambuc ATF_REQUIRE(exp_test_suites == user_config.all_properties("test_suites"));
141*11be35a1SLionel Sambuc }
142*11be35a1SLionel Sambuc
143*11be35a1SLionel Sambuc
144*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(config__load__lua_error);
ATF_TEST_CASE_BODY(config__load__lua_error)145*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(config__load__lua_error)
146*11be35a1SLionel Sambuc {
147*11be35a1SLionel Sambuc atf::utils::create_file("config", "this syntax is invalid\n");
148*11be35a1SLionel Sambuc
149*11be35a1SLionel Sambuc ATF_REQUIRE_THROW(engine::load_error, engine::load_config(
150*11be35a1SLionel Sambuc fs::path("config")));
151*11be35a1SLionel Sambuc }
152*11be35a1SLionel Sambuc
153*11be35a1SLionel Sambuc
154*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(config__load__bad_syntax__version);
ATF_TEST_CASE_BODY(config__load__bad_syntax__version)155*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(config__load__bad_syntax__version)
156*11be35a1SLionel Sambuc {
157*11be35a1SLionel Sambuc atf::utils::create_file("config", "syntax(123)\n");
158*11be35a1SLionel Sambuc
159*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(engine::load_error,
160*11be35a1SLionel Sambuc "Unsupported config version 123",
161*11be35a1SLionel Sambuc engine::load_config(fs::path("config")));
162*11be35a1SLionel Sambuc }
163*11be35a1SLionel Sambuc
164*11be35a1SLionel Sambuc
165*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(config__load__missing_file);
ATF_TEST_CASE_BODY(config__load__missing_file)166*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(config__load__missing_file)
167*11be35a1SLionel Sambuc {
168*11be35a1SLionel Sambuc ATF_REQUIRE_THROW_RE(engine::load_error, "Load of 'missing' failed",
169*11be35a1SLionel Sambuc engine::load_config(fs::path("missing")));
170*11be35a1SLionel Sambuc }
171*11be35a1SLionel Sambuc
172*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)173*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
174*11be35a1SLionel Sambuc {
175*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, config__defaults);
176*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, config__load__defaults);
177*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, config__load__overrides);
178*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, config__load__lua_error);
179*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, config__load__bad_syntax__version);
180*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, config__load__missing_file);
181*11be35a1SLionel Sambuc }
182