1097141ebrooks// Copyright 2014 The Kyua Authors.
2097141ebrooks// All rights reserved.
3097141ebrooks//
4097141ebrooks// Redistribution and use in source and binary forms, with or without
5097141ebrooks// modification, are permitted provided that the following conditions are
6097141ebrooks// met:
7097141ebrooks//
8097141ebrooks// * Redistributions of source code must retain the above copyright
9097141ebrooks//   notice, this list of conditions and the following disclaimer.
10097141ebrooks// * Redistributions in binary form must reproduce the above copyright
11097141ebrooks//   notice, this list of conditions and the following disclaimer in the
12097141ebrooks//   documentation and/or other materials provided with the distribution.
13097141ebrooks// * Neither the name of Google Inc. nor the names of its contributors
14097141ebrooks//   may be used to endorse or promote products derived from this software
15097141ebrooks//   without specific prior written permission.
16097141ebrooks//
17097141ebrooks// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18097141ebrooks// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19097141ebrooks// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20097141ebrooks// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21097141ebrooks// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22097141ebrooks// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23097141ebrooks// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24097141ebrooks// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25097141ebrooks// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26097141ebrooks// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27097141ebrooks// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28097141ebrooks
29097141ebrooks#include "engine/atf.hpp"
30097141ebrooks
31097141ebrooksextern "C" {
32097141ebrooks#include <unistd.h>
33097141ebrooks}
34097141ebrooks
35097141ebrooks#include <cerrno>
36097141ebrooks#include <cstdlib>
37097141ebrooks#include <fstream>
38097141ebrooks
39097141ebrooks#include "engine/atf_list.hpp"
40097141ebrooks#include "engine/atf_result.hpp"
41097141ebrooks#include "engine/exceptions.hpp"
42097141ebrooks#include "model/test_case.hpp"
43097141ebrooks#include "model/test_program.hpp"
44097141ebrooks#include "model/test_result.hpp"
45097141ebrooks#include "utils/defs.hpp"
46097141ebrooks#include "utils/env.hpp"
47097141ebrooks#include "utils/format/macros.hpp"
48097141ebrooks#include "utils/fs/path.hpp"
49097141ebrooks#include "utils/logging/macros.hpp"
50097141ebrooks#include "utils/optional.ipp"
51097141ebrooks#include "utils/process/exceptions.hpp"
52097141ebrooks#include "utils/process/operations.hpp"
53097141ebrooks#include "utils/process/status.hpp"
54097141ebrooks#include "utils/stream.hpp"
55097141ebrooks
56097141ebrooksnamespace config = utils::config;
57097141ebrooksnamespace fs = utils::fs;
58097141ebrooksnamespace process = utils::process;
59097141ebrooks
60097141ebrooksusing utils::optional;
61097141ebrooks
62097141ebrooks
63097141ebrooksnamespace {
64097141ebrooks
65097141ebrooks
66097141ebrooks/// Basename of the file containing the result written by the ATF test case.
67097141ebrooksstatic const char* result_name = "result.atf";
68097141ebrooks
69097141ebrooks
70097141ebrooks/// Magic numbers returned by exec_list when exec(2) fails.
71097141ebrooksenum list_exit_code {
72097141ebrooks    exit_eacces = 90,
73097141ebrooks    exit_enoent,
74097141ebrooks    exit_enoexec,
75097141ebrooks};
76097141ebrooks
77097141ebrooks
78097141ebrooks}  // anonymous namespace
79097141ebrooks
80097141ebrooks
81097141ebrooks/// Executes a test program's list operation.
82097141ebrooks///
83097141ebrooks/// This method is intended to be called within a subprocess and is expected
84097141ebrooks/// to terminate execution either by exec(2)ing the test program or by
85097141ebrooks/// exiting with a failure.
86097141ebrooks///
87097141ebrooks/// \param test_program The test program to execute.
88097141ebrooks/// \param vars User-provided variables to pass to the test program.
89097141ebrooksvoid
90097141ebrooksengine::atf_interface::exec_list(const model::test_program& test_program,
91097141ebrooks                                 const config::properties_map& vars) const
92097141ebrooks{
93097141ebrooks    utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
94097141ebrooks
95097141ebrooks    process::args_vector args;
96097141ebrooks    for (config::properties_map::const_iterator iter = vars.begin();
97097141ebrooks         iter != vars.end(); ++iter) {
98097141ebrooks        args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
99097141ebrooks    }
100097141ebrooks
101097141ebrooks    args.push_back("-l");
102097141ebrooks    try {
103097141ebrooks        process::exec_unsafe(test_program.absolute_path(), args);
104097141ebrooks    } catch (const process::system_error& e) {
105097141ebrooks        if (e.original_errno() == EACCES)
106097141ebrooks            ::_exit(exit_eacces);
107097141ebrooks        else if (e.original_errno() == ENOENT)
108097141ebrooks            ::_exit(exit_enoent);
109097141ebrooks        else if (e.original_errno() == ENOEXEC)
110097141ebrooks            ::_exit(exit_enoexec);
111097141ebrooks        throw;
112097141ebrooks    }
113097141ebrooks}
114097141ebrooks
115097141ebrooks
116097141ebrooks/// Computes the test cases list of a test program.
117097141ebrooks///
118097141ebrooks/// \param status The termination status of the subprocess used to execute
119097141ebrooks///     the exec_test() method or none if the test timed out.
120097141ebrooks/// \param stdout_path Path to the file containing the stdout of the test.
121097141ebrooks/// \param stderr_path Path to the file containing the stderr of the test.
122097141ebrooks///
123097141ebrooks/// \return A list of test cases.
124097141ebrooks///
125097141ebrooks/// \throw error If there is a problem parsing the test case list.
126097141ebrooksmodel::test_cases_map
127097141ebrooksengine::atf_interface::parse_list(const optional< process::status >& status,
128097141ebrooks                                  const fs::path& stdout_path,
129097141ebrooks                                  const fs::path& stderr_path) const
130097141ebrooks{
131097141ebrooks    const std::string stderr_contents = utils::read_file(stderr_path);
132097141ebrooks    if (!stderr_contents.empty())
133097141ebrooks        LW("Test case list wrote to stderr: " + stderr_contents);
134097141ebrooks
135097141ebrooks    if (!status)
136097141ebrooks        throw engine::error("Test case list timed out");
137097141ebrooks    if (status.get().exited()) {
138097141ebrooks        const int exitstatus = status.get().exitstatus();
139097141ebrooks        if (exitstatus == EXIT_SUCCESS) {
140097141ebrooks            // Nothing to do; fall through.
141097141ebrooks        } else if (exitstatus == exit_eacces) {
142097141ebrooks            throw engine::error("Permission denied to run test program");
143097141ebrooks        } else if (exitstatus == exit_enoent) {
144097141ebrooks            throw engine::error("Cannot find test program");
145097141ebrooks        } else if (exitstatus == exit_enoexec) {
146097141ebrooks            throw engine::error("Invalid test program format");
147097141ebrooks        } else {
148097141ebrooks            throw engine::error("Test program did not exit cleanly");
149097141ebrooks        }
150097141ebrooks    } else {
151097141ebrooks        throw engine::error("Test program received signal");
152097141ebrooks    }
153097141ebrooks
154097141ebrooks    std::ifstream input(stdout_path.c_str());
155097141ebrooks    if (!input)
156097141ebrooks        throw engine::load_error(stdout_path, "Cannot open file for read");
157097141ebrooks    const model::test_cases_map test_cases = parse_atf_list(input);
158097141ebrooks
159097141ebrooks    if (!stderr_contents.empty())
160097141ebrooks        throw engine::error("Test case list wrote to stderr");
161097141ebrooks
162097141ebrooks    return test_cases;
163097141ebrooks}
164097141ebrooks
165097141ebrooks
166097141ebrooks/// Executes a test case of the test program.
167097141ebrooks///
168097141ebrooks/// This method is intended to be called within a subprocess and is expected
169097141ebrooks/// to terminate execution either by exec(2)ing the test program or by
170097141ebrooks/// exiting with a failure.
171097141ebrooks///
172097141ebrooks/// \param test_program The test program to execute.
173097141ebrooks/// \param test_case_name Name of the test case to invoke.
174097141ebrooks/// \param vars User-provided variables to pass to the test program.
175097141ebrooks/// \param control_directory Directory where the interface may place control
176097141ebrooks///     files.
177097141ebrooksvoid
178097141ebrooksengine::atf_interface::exec_test(const model::test_program& test_program,
179097141ebrooks                                 const std::string& test_case_name,
180097141ebrooks                                 const config::properties_map& vars,
181097141ebrooks                                 const fs::path& control_directory) const
182097141ebrooks{
183097141ebrooks    utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
184097141ebrooks
185097141ebrooks    process::args_vector args;
186097141ebrooks    for (config::properties_map::const_iterator iter = vars.begin();
187097141ebrooks         iter != vars.end(); ++iter) {
188097141ebrooks        args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
189097141ebrooks    }
190097141ebrooks
191097141ebrooks    args.push_back(F("-r%s") % (control_directory / result_name));
192097141ebrooks    args.push_back(test_case_name);
193097141ebrooks    process::exec(test_program.absolute_path(), args);
194097141ebrooks}
195097141ebrooks
196097141ebrooks
197097141ebrooks/// Executes a test cleanup routine of the test program.
198097141ebrooks///
199097141ebrooks/// This method is intended to be called within a subprocess and is expected
200097141ebrooks/// to terminate execution either by exec(2)ing the test program or by
201097141ebrooks/// exiting with a failure.
202097141ebrooks///
203097141ebrooks/// \param test_program The test program to execute.
204097141ebrooks/// \param test_case_name Name of the test case to invoke.
205097141ebrooks/// \param vars User-provided variables to pass to the test program.
206097141ebrooksvoid
207097141ebrooksengine::atf_interface::exec_cleanup(
208097141ebrooks    const model::test_program& test_program,
209097141ebrooks    const std::string& test_case_name,
210097141ebrooks    const config::properties_map& vars,
211097141ebrooks    const fs::path& /* control_directory */) const
212097141ebrooks{
213097141ebrooks    utils::setenv("__RUNNING_INSIDE_ATF_RUN", "internal-yes-value");
214097141ebrooks
215097141ebrooks    process::args_vector args;
216097141ebrooks    for (config::properties_map::const_iterator iter = vars.begin();
217097141ebrooks         iter != vars.end(); ++iter) {
218097141ebrooks        args.push_back(F("-v%s=%s") % (*iter).first % (*iter).second);
219097141ebrooks    }
220097141ebrooks
221097141ebrooks    args.push_back(F("%s:cleanup") % test_case_name);
222097141ebrooks    process::exec(test_program.absolute_path(), args);
223097141ebrooks}
224097141ebrooks
225097141ebrooks
226097141ebrooks/// Computes the result of a test case based on its termination status.
227097141ebrooks///
228097141ebrooks/// \param status The termination status of the subprocess used to execute
229097141ebrooks///     the exec_test() method or none if the test timed out.
230097141ebrooks/// \param control_directory Directory where the interface may have placed
231097141ebrooks///     control files.
232097141ebrooks///
233097141ebrooks/// \return A test result.
234097141ebrooksmodel::test_result
235097141ebrooksengine::atf_interface::compute_result(
236097141ebrooks    const optional< process::status >& status,
237097141ebrooks    const fs::path& control_directory,
238097141ebrooks    const fs::path& /* stdout_path */,
239097141ebrooks    const fs::path& /* stderr_path */) const
240097141ebrooks{
241097141ebrooks    return calculate_atf_result(status, control_directory / result_name);
242097141ebrooks}
243