1// Copyright 2012 The Kyua Authors.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9//   notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11//   notice, this list of conditions and the following disclaimer in the
12//   documentation and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors
14//   may be used to endorse or promote products derived from this software
15//   without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include "engine/requirements.hpp"
30
31#include "model/metadata.hpp"
32#include "model/types.hpp"
33#include "utils/config/nodes.ipp"
34#include "utils/config/tree.ipp"
35#include "utils/format/macros.hpp"
36#include "utils/fs/operations.hpp"
37#include "utils/fs/path.hpp"
38#include "utils/memory.hpp"
39#include "utils/passwd.hpp"
40#include "utils/sanity.hpp"
41#include "utils/units.hpp"
42
43namespace config = utils::config;
44namespace fs = utils::fs;
45namespace passwd = utils::passwd;
46namespace units = utils::units;
47
48
49namespace {
50
51
52/// Checks if all required configuration variables are present.
53///
54/// \param required_configs Set of required variable names.
55/// \param user_config Runtime user configuration.
56/// \param test_suite_name Name of the test suite the test belongs to.
57///
58/// \return Empty if all variables are present or an error message otherwise.
59static std::string
60check_required_configs(const model::strings_set& required_configs,
61                       const config::tree& user_config,
62                       const std::string& test_suite_name)
63{
64    for (model::strings_set::const_iterator iter = required_configs.begin();
65         iter != required_configs.end(); iter++) {
66        std::string property;
67        // TODO(jmmv): All this rewrite logic belongs in the ATF interface.
68        if ((*iter) == "unprivileged-user" || (*iter) == "unprivileged_user")
69            property = "unprivileged_user";
70        else
71            property = F("test_suites.%s.%s") % test_suite_name % (*iter);
72
73        if (!user_config.is_set(property))
74            return F("Required configuration property '%s' not defined") %
75                (*iter);
76    }
77    return "";
78}
79
80
81/// Checks if the allowed architectures match the current architecture.
82///
83/// \param allowed_architectures Set of allowed architectures.
84/// \param user_config Runtime user configuration.
85///
86/// \return Empty if the current architecture is in the list or an error
87/// message otherwise.
88static std::string
89check_allowed_architectures(const model::strings_set& allowed_architectures,
90                            const config::tree& user_config)
91{
92    if (!allowed_architectures.empty()) {
93        const std::string architecture =
94            user_config.lookup< config::string_node >("architecture");
95        if (allowed_architectures.find(architecture) ==
96            allowed_architectures.end())
97            return F("Current architecture '%s' not supported") % architecture;
98    }
99    return "";
100}
101
102
103/// Checks if the allowed platforms match the current architecture.
104///
105/// \param allowed_platforms Set of allowed platforms.
106/// \param user_config Runtime user configuration.
107///
108/// \return Empty if the current platform is in the list or an error message
109/// otherwise.
110static std::string
111check_allowed_platforms(const model::strings_set& allowed_platforms,
112                        const config::tree& user_config)
113{
114    if (!allowed_platforms.empty()) {
115        const std::string platform =
116            user_config.lookup< config::string_node >("platform");
117        if (allowed_platforms.find(platform) == allowed_platforms.end())
118            return F("Current platform '%s' not supported") % platform;
119    }
120    return "";
121}
122
123
124/// Checks if the current user matches the required user.
125///
126/// \param required_user Name of the required user category.
127/// \param user_config Runtime user configuration.
128///
129/// \return Empty if the current user fits the required user characteristics or
130/// an error message otherwise.
131static std::string
132check_required_user(const std::string& required_user,
133                    const config::tree& user_config)
134{
135    if (!required_user.empty()) {
136        const passwd::user user = passwd::current_user();
137        if (required_user == "root") {
138            if (!user.is_root())
139                return "Requires root privileges";
140        } else if (required_user == "unprivileged") {
141            if (user.is_root())
142                if (!user_config.is_set("unprivileged_user"))
143                    return "Requires an unprivileged user but the "
144                        "unprivileged-user configuration variable is not "
145                        "defined";
146        } else
147            UNREACHABLE_MSG("Value of require.user not properly validated");
148    }
149    return "";
150}
151
152
153/// Checks if all required files exist.
154///
155/// \param required_files Set of paths.
156///
157/// \return Empty if the required files all exist or an error message otherwise.
158static std::string
159check_required_files(const model::paths_set& required_files)
160{
161    for (model::paths_set::const_iterator iter = required_files.begin();
162         iter != required_files.end(); iter++) {
163        INV((*iter).is_absolute());
164        if (!fs::exists(*iter))
165            return F("Required file '%s' not found") % *iter;
166    }
167    return "";
168}
169
170
171/// Checks if all required programs exist.
172///
173/// \param required_programs Set of paths.
174///
175/// \return Empty if the required programs all exist or an error message
176/// otherwise.
177static std::string
178check_required_programs(const model::paths_set& required_programs)
179{
180    for (model::paths_set::const_iterator iter = required_programs.begin();
181         iter != required_programs.end(); iter++) {
182        if ((*iter).is_absolute()) {
183            if (!fs::exists(*iter))
184                return F("Required program '%s' not found") % *iter;
185        } else {
186            if (!fs::find_in_path((*iter).c_str()))
187                return F("Required program '%s' not found in PATH") % *iter;
188        }
189    }
190    return "";
191}
192
193
194/// Checks if the current system has the specified amount of memory.
195///
196/// \param required_memory Amount of required physical memory, or zero if not
197///     applicable.
198///
199/// \return Empty if the current system has the required amount of memory or an
200/// error message otherwise.
201static std::string
202check_required_memory(const units::bytes& required_memory)
203{
204    if (required_memory > 0) {
205        const units::bytes physical_memory = utils::physical_memory();
206        if (physical_memory > 0 && physical_memory < required_memory)
207            return F("Requires %s bytes of physical memory but only %s "
208                     "available") %
209                required_memory.format() % physical_memory.format();
210    }
211    return "";
212}
213
214
215/// Checks if the work directory's file system has enough free disk space.
216///
217/// \param required_disk_space Amount of required free disk space, or zero if
218///     not applicable.
219/// \param work_directory Path to where the test case will be run.
220///
221/// \return Empty if the file system where the work directory is hosted has
222/// enough free disk space or an error message otherwise.
223static std::string
224check_required_disk_space(const units::bytes& required_disk_space,
225                          const fs::path& work_directory)
226{
227    if (required_disk_space > 0) {
228        const units::bytes free_disk_space = fs::free_disk_space(
229            work_directory);
230        if (free_disk_space < required_disk_space)
231            return F("Requires %s bytes of free disk space but only %s "
232                     "available") %
233                required_disk_space.format() % free_disk_space.format();
234    }
235    return "";
236}
237
238
239}  // anonymous namespace
240
241
242/// Checks if all the requirements specified by the test case are met.
243///
244/// \param md The test metadata.
245/// \param cfg The engine configuration.
246/// \param test_suite Name of the test suite the test belongs to.
247/// \param work_directory Path to where the test case will be run.
248///
249/// \return A string describing the reason for skipping the test, or empty if
250/// the test should be executed.
251std::string
252engine::check_reqs(const model::metadata& md, const config::tree& cfg,
253                   const std::string& test_suite,
254                   const fs::path& work_directory)
255{
256    std::string reason;
257
258    reason = check_required_configs(md.required_configs(), cfg, test_suite);
259    if (!reason.empty())
260        return reason;
261
262    reason = check_allowed_architectures(md.allowed_architectures(), cfg);
263    if (!reason.empty())
264        return reason;
265
266    reason = check_allowed_platforms(md.allowed_platforms(), cfg);
267    if (!reason.empty())
268        return reason;
269
270    reason = check_required_user(md.required_user(), cfg);
271    if (!reason.empty())
272        return reason;
273
274    reason = check_required_files(md.required_files());
275    if (!reason.empty())
276        return reason;
277
278    reason = check_required_programs(md.required_programs());
279    if (!reason.empty())
280        return reason;
281
282    reason = check_required_memory(md.required_memory());
283    if (!reason.empty())
284        return reason;
285
286    reason = check_required_disk_space(md.required_disk_space(),
287                                       work_directory);
288    if (!reason.empty())
289        return reason;
290
291    INV(reason.empty());
292    return reason;
293}
294