xref: /minix3/external/bsd/kyua-cli/dist/engine/testers.cpp (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
111be35a1SLionel Sambuc // Copyright 2012 Google Inc.
211be35a1SLionel Sambuc // All rights reserved.
311be35a1SLionel Sambuc //
411be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
511be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
611be35a1SLionel Sambuc // met:
711be35a1SLionel Sambuc //
811be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
911be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
1011be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
1111be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
1211be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
1311be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
1411be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
1511be35a1SLionel Sambuc //   without specific prior written permission.
1611be35a1SLionel Sambuc //
1711be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1811be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1911be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2011be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2111be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2211be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2311be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2411be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2511be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2611be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2711be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc 
2911be35a1SLionel Sambuc #include "engine/testers.hpp"
3011be35a1SLionel Sambuc 
3111be35a1SLionel Sambuc extern "C" {
3211be35a1SLionel Sambuc #include <dirent.h>
3311be35a1SLionel Sambuc #include <regex.h>
3411be35a1SLionel Sambuc }
3511be35a1SLionel Sambuc 
3611be35a1SLionel Sambuc #include <cerrno>
3711be35a1SLionel Sambuc #include <cstring>
3811be35a1SLionel Sambuc #include <iostream>
3911be35a1SLionel Sambuc #include <map>
4011be35a1SLionel Sambuc #include <string>
4111be35a1SLionel Sambuc 
4211be35a1SLionel Sambuc #include "engine/exceptions.hpp"
4311be35a1SLionel Sambuc #include "utils/env.hpp"
4411be35a1SLionel Sambuc #include "utils/format/macros.hpp"
4511be35a1SLionel Sambuc #include "utils/fs/operations.hpp"
4611be35a1SLionel Sambuc #include "utils/fs/path.hpp"
4711be35a1SLionel Sambuc #include "utils/logging/macros.hpp"
4811be35a1SLionel Sambuc #include "utils/optional.ipp"
4911be35a1SLionel Sambuc #include "utils/passwd.hpp"
5011be35a1SLionel Sambuc #include "utils/process/child.ipp"
5111be35a1SLionel Sambuc #include "utils/process/status.hpp"
5211be35a1SLionel Sambuc #include "utils/stream.hpp"
5311be35a1SLionel Sambuc 
5411be35a1SLionel Sambuc namespace datetime = utils::datetime;
5511be35a1SLionel Sambuc namespace fs = utils::fs;
5611be35a1SLionel Sambuc namespace logging = utils::logging;
5711be35a1SLionel Sambuc namespace passwd = utils::passwd;
5811be35a1SLionel Sambuc namespace process = utils::process;
5911be35a1SLionel Sambuc 
6011be35a1SLionel Sambuc using utils::none;
6111be35a1SLionel Sambuc using utils::optional;
6211be35a1SLionel Sambuc 
6311be35a1SLionel Sambuc 
6411be35a1SLionel Sambuc namespace {
6511be35a1SLionel Sambuc 
6611be35a1SLionel Sambuc 
6711be35a1SLionel Sambuc /// Mapping of interface names to tester binaries.
6811be35a1SLionel Sambuc typedef std::map< std::string, std::string > testers_map;
6911be35a1SLionel Sambuc 
7011be35a1SLionel Sambuc 
7111be35a1SLionel Sambuc /// Collection of known-good interface to tester mappings.
7211be35a1SLionel Sambuc static testers_map interfaces_to_testers;
7311be35a1SLionel Sambuc 
7411be35a1SLionel Sambuc 
7511be35a1SLionel Sambuc /// Drops the trailing newline in a string and replaces others with a literal.
7611be35a1SLionel Sambuc ///
7711be35a1SLionel Sambuc /// \param input The string in which to perform the replacements.
7811be35a1SLionel Sambuc ///
7911be35a1SLionel Sambuc /// \return The modified string.
8011be35a1SLionel Sambuc static std::string
replace_newlines(const std::string input)8111be35a1SLionel Sambuc replace_newlines(const std::string input)
8211be35a1SLionel Sambuc {
8311be35a1SLionel Sambuc     std::string output = input;
8411be35a1SLionel Sambuc 
8511be35a1SLionel Sambuc     while (output.length() > 0 && output[output.length() - 1] == '\n') {
8611be35a1SLionel Sambuc         output.erase(output.end() - 1);
8711be35a1SLionel Sambuc     }
8811be35a1SLionel Sambuc 
8911be35a1SLionel Sambuc     std::string::size_type newline = output.find('\n', 0);
9011be35a1SLionel Sambuc     while (newline != std::string::npos) {
9111be35a1SLionel Sambuc         output.replace(newline, 1, "<<NEWLINE>>");
9211be35a1SLionel Sambuc         newline = output.find('\n', newline + 1);
9311be35a1SLionel Sambuc     }
9411be35a1SLionel Sambuc 
9511be35a1SLionel Sambuc     return output;
9611be35a1SLionel Sambuc }
9711be35a1SLionel Sambuc 
9811be35a1SLionel Sambuc 
9911be35a1SLionel Sambuc /// RAII pattern to invoke a release method on destruction.
10011be35a1SLionel Sambuc ///
10111be35a1SLionel Sambuc /// \todo The existence of this class here is a hack.  We should either
10211be35a1SLionel Sambuc /// generalize the class and use it wherever we need release on destruction
10311be35a1SLionel Sambuc /// semantics, or we should have proper abstractions for the objects below that
10411be35a1SLionel Sambuc /// use this class.
10511be35a1SLionel Sambuc ///
10611be35a1SLionel Sambuc /// \tparam Object The type of the object to be released.  Not a pointer.
10711be35a1SLionel Sambuc /// \tparam ReturnType The return type of the release method.
10811be35a1SLionel Sambuc template< typename Object, typename ReturnType >
10911be35a1SLionel Sambuc class object_releaser {
11011be35a1SLionel Sambuc     /// Pointer to the object being managed.
11111be35a1SLionel Sambuc     Object* _object;
11211be35a1SLionel Sambuc 
11311be35a1SLionel Sambuc     /// Release hook.
11411be35a1SLionel Sambuc     ReturnType (*_free_hook)(Object*);
11511be35a1SLionel Sambuc 
11611be35a1SLionel Sambuc public:
11711be35a1SLionel Sambuc     /// Constructor.
11811be35a1SLionel Sambuc     ///
11911be35a1SLionel Sambuc     /// \param object Pointer to the object being managed.
12011be35a1SLionel Sambuc     /// \param free_hook Release hook.
object_releaser(Object * object,ReturnType (* free_hook)(Object *))12111be35a1SLionel Sambuc     object_releaser(Object* object, ReturnType (*free_hook)(Object*)) :
12211be35a1SLionel Sambuc         _object(object), _free_hook(free_hook)
12311be35a1SLionel Sambuc     {
12411be35a1SLionel Sambuc     }
12511be35a1SLionel Sambuc 
12611be35a1SLionel Sambuc     /// Destructor.
~object_releaser(void)12711be35a1SLionel Sambuc     ~object_releaser(void)
12811be35a1SLionel Sambuc     {
12911be35a1SLionel Sambuc         _free_hook(_object);
13011be35a1SLionel Sambuc     }
13111be35a1SLionel Sambuc };
13211be35a1SLionel Sambuc 
13311be35a1SLionel Sambuc 
13411be35a1SLionel Sambuc /// Finds all available testers and caches their data.
13511be35a1SLionel Sambuc ///
13611be35a1SLionel Sambuc /// \param [out] testers Map into which to store the list of available testers.
13711be35a1SLionel Sambuc static void
load_testers(testers_map & testers)13811be35a1SLionel Sambuc load_testers(testers_map& testers)
13911be35a1SLionel Sambuc {
14011be35a1SLionel Sambuc     PRE(testers.empty());
14111be35a1SLionel Sambuc 
14211be35a1SLionel Sambuc     const fs::path raw_testersdir(utils::getenv_with_default(
14311be35a1SLionel Sambuc         "KYUA_TESTERSDIR", KYUA_TESTERSDIR));
14411be35a1SLionel Sambuc     const fs::path testersdir = raw_testersdir.is_absolute() ?
14511be35a1SLionel Sambuc         raw_testersdir : raw_testersdir.to_absolute();
14611be35a1SLionel Sambuc 
14711be35a1SLionel Sambuc     ::DIR* dir = ::opendir(testersdir.c_str());
14811be35a1SLionel Sambuc     if (dir == NULL) {
14911be35a1SLionel Sambuc         const int original_errno = errno;
15011be35a1SLionel Sambuc         LW(F("Failed to open testers dir %s: %s") % testersdir %
15111be35a1SLionel Sambuc            strerror(original_errno));
15211be35a1SLionel Sambuc         return;  // No testers available in the given location.
15311be35a1SLionel Sambuc     }
15411be35a1SLionel Sambuc     const object_releaser< ::DIR, int > dir_releaser(dir, ::closedir);
15511be35a1SLionel Sambuc 
15611be35a1SLionel Sambuc     ::regex_t preg;
15711be35a1SLionel Sambuc     if (::regcomp(&preg, "^kyua-(.+)-tester$", REG_EXTENDED) != 0)
15811be35a1SLionel Sambuc         throw engine::error("Failed to compile regular expression");
15911be35a1SLionel Sambuc     const object_releaser< ::regex_t, void > preg_releaser(&preg, ::regfree);
16011be35a1SLionel Sambuc 
16111be35a1SLionel Sambuc     ::dirent* de;
16211be35a1SLionel Sambuc     while ((de = readdir(dir)) != NULL) {
16311be35a1SLionel Sambuc         ::regmatch_t matches[2];
16411be35a1SLionel Sambuc         const int ret = ::regexec(&preg, de->d_name, 2, matches, 0);
16511be35a1SLionel Sambuc         if (ret == 0) {
16611be35a1SLionel Sambuc             const std::string interface(de->d_name + matches[1].rm_so,
16711be35a1SLionel Sambuc                                         matches[1].rm_eo - matches[1].rm_so);
16811be35a1SLionel Sambuc             const fs::path path = testersdir / de->d_name;
16911be35a1SLionel Sambuc             LI(F("Found tester for interface %s in %s") % interface % path);
17011be35a1SLionel Sambuc             INV(path.is_absolute());
17111be35a1SLionel Sambuc             testers[interface] = path.str();
17211be35a1SLionel Sambuc         } else if (ret == REG_NOMATCH) {
17311be35a1SLionel Sambuc             // Not a tester; skip.
17411be35a1SLionel Sambuc         } else {
17511be35a1SLionel Sambuc             throw engine::error("Failed to match regular expression");
17611be35a1SLionel Sambuc         }
17711be35a1SLionel Sambuc     }
17811be35a1SLionel Sambuc }
17911be35a1SLionel Sambuc 
18011be35a1SLionel Sambuc 
18111be35a1SLionel Sambuc }  // anonymous namespace
18211be35a1SLionel Sambuc 
18311be35a1SLionel Sambuc 
18411be35a1SLionel Sambuc /// Returns the path to a tester binary.
18511be35a1SLionel Sambuc ///
18611be35a1SLionel Sambuc /// \param interface Name of the interface of the tester being looked for.
18711be35a1SLionel Sambuc ///
18811be35a1SLionel Sambuc /// \return Absolute path to the tester.
18911be35a1SLionel Sambuc fs::path
tester_path(const std::string & interface)19011be35a1SLionel Sambuc engine::tester_path(const std::string& interface)
19111be35a1SLionel Sambuc {
19211be35a1SLionel Sambuc     if (interfaces_to_testers.empty())
19311be35a1SLionel Sambuc         load_testers(interfaces_to_testers);
19411be35a1SLionel Sambuc 
19511be35a1SLionel Sambuc     const testers_map::const_iterator iter = interfaces_to_testers.find(
19611be35a1SLionel Sambuc         interface);
19711be35a1SLionel Sambuc     if (iter == interfaces_to_testers.end())
19811be35a1SLionel Sambuc         throw engine::error("Unknown interface " + interface);
19911be35a1SLionel Sambuc 
20011be35a1SLionel Sambuc     const fs::path path((*iter).second);
20111be35a1SLionel Sambuc     INV(path.is_absolute());
20211be35a1SLionel Sambuc     return path;
20311be35a1SLionel Sambuc }
20411be35a1SLionel Sambuc 
20511be35a1SLionel Sambuc 
20611be35a1SLionel Sambuc /// Constructs a tester.
20711be35a1SLionel Sambuc ///
20811be35a1SLionel Sambuc /// \param interface Name of the interface to use.
20911be35a1SLionel Sambuc /// \param unprivileged_user If not none, the user to switch to when running
21011be35a1SLionel Sambuc ///     the tester.
21111be35a1SLionel Sambuc /// \param timeout If not none, the timeout to pass to the tester.
tester(const std::string & interface,const optional<passwd::user> & unprivileged_user,const optional<datetime::delta> & timeout)21211be35a1SLionel Sambuc engine::tester::tester(const std::string& interface,
21311be35a1SLionel Sambuc                        const optional< passwd::user >& unprivileged_user,
21411be35a1SLionel Sambuc                        const optional< datetime::delta >& timeout) :
21511be35a1SLionel Sambuc     _interface(interface)
21611be35a1SLionel Sambuc {
21711be35a1SLionel Sambuc     if (unprivileged_user) {
21811be35a1SLionel Sambuc         _common_args.push_back(F("-u%s") % unprivileged_user.get().uid);
21911be35a1SLionel Sambuc         _common_args.push_back(F("-g%s") % unprivileged_user.get().gid);
22011be35a1SLionel Sambuc     }
22111be35a1SLionel Sambuc     if (timeout) {
22211be35a1SLionel Sambuc         PRE(timeout.get().useconds == 0);
22311be35a1SLionel Sambuc         _common_args.push_back(F("-t%s") % timeout.get().seconds);
22411be35a1SLionel Sambuc     }
22511be35a1SLionel Sambuc }
22611be35a1SLionel Sambuc 
22711be35a1SLionel Sambuc 
22811be35a1SLionel Sambuc /// Destructor.
~tester(void)22911be35a1SLionel Sambuc engine::tester::~tester(void)
23011be35a1SLionel Sambuc {
23111be35a1SLionel Sambuc }
23211be35a1SLionel Sambuc 
23311be35a1SLionel Sambuc 
23411be35a1SLionel Sambuc /// Executes a list operation on a test program.
23511be35a1SLionel Sambuc ///
23611be35a1SLionel Sambuc /// \param program Path to the test program.
23711be35a1SLionel Sambuc ///
23811be35a1SLionel Sambuc /// \return The output of the tester, which represents a valid list of test
23911be35a1SLionel Sambuc /// cases.
24011be35a1SLionel Sambuc ///
24111be35a1SLionel Sambuc /// \throw error If the tester returns with an unsuccessful exit code.
24211be35a1SLionel Sambuc std::string
list(const fs::path & program) const24311be35a1SLionel Sambuc engine::tester::list(const fs::path& program) const
24411be35a1SLionel Sambuc {
24511be35a1SLionel Sambuc     std::vector< std::string > args = _common_args;
24611be35a1SLionel Sambuc     args.push_back("list");
24711be35a1SLionel Sambuc     args.push_back(program.str());
24811be35a1SLionel Sambuc 
24911be35a1SLionel Sambuc     const fs::path tester_path = engine::tester_path(_interface);
25011be35a1SLionel Sambuc     std::auto_ptr< process::child > child = process::child::spawn_capture(
25111be35a1SLionel Sambuc         tester_path, args);
25211be35a1SLionel Sambuc 
25311be35a1SLionel Sambuc     const std::string output = utils::read_stream(child->output());
25411be35a1SLionel Sambuc 
25511be35a1SLionel Sambuc     const process::status status = child->wait();
25611be35a1SLionel Sambuc     if (!status.exited() || status.exitstatus() != EXIT_SUCCESS)
25711be35a1SLionel Sambuc         throw engine::error("Tester did not exit cleanly: " +
25811be35a1SLionel Sambuc                             replace_newlines(output));
25911be35a1SLionel Sambuc     return output;
26011be35a1SLionel Sambuc }
26111be35a1SLionel Sambuc 
26211be35a1SLionel Sambuc 
26311be35a1SLionel Sambuc /// Executes a test operation on a test case.
26411be35a1SLionel Sambuc ///
26511be35a1SLionel Sambuc /// \param program Path to the test program.
26611be35a1SLionel Sambuc /// \param test_case_name Name of the test case to execute.
26711be35a1SLionel Sambuc /// \param result_file Path to the file in which to leave the result of the
26811be35a1SLionel Sambuc ///     tester invocation.
26911be35a1SLionel Sambuc /// \param stdout_file Path to the file in which to store the stdout.
27011be35a1SLionel Sambuc /// \param stderr_file Path to the file in which to store the stderr.
27111be35a1SLionel Sambuc /// \param vars Collection of configuration variables.
27211be35a1SLionel Sambuc ///
27311be35a1SLionel Sambuc /// \throw error If the tester returns with an unsuccessful exit code.
27411be35a1SLionel Sambuc void
test(const fs::path & program,const std::string & test_case_name,const fs::path & result_file,const fs::path & stdout_file,const fs::path & stderr_file,const std::map<std::string,std::string> & vars) const27511be35a1SLionel Sambuc engine::tester::test(const fs::path& program, const std::string& test_case_name,
27611be35a1SLionel Sambuc                      const fs::path& result_file, const fs::path& stdout_file,
27711be35a1SLionel Sambuc                      const fs::path& stderr_file,
27811be35a1SLionel Sambuc                      const std::map< std::string, std::string >& vars) const
27911be35a1SLionel Sambuc {
28011be35a1SLionel Sambuc     std::vector< std::string > args = _common_args;
28111be35a1SLionel Sambuc     args.push_back("test");
28211be35a1SLionel Sambuc     for (std::map< std::string, std::string >::const_iterator i = vars.begin();
28311be35a1SLionel Sambuc          i != vars.end(); ++i) {
28411be35a1SLionel Sambuc         args.push_back(F("-v%s=%s") % (*i).first % (*i).second);
28511be35a1SLionel Sambuc     }
28611be35a1SLionel Sambuc     args.push_back(program.str());
28711be35a1SLionel Sambuc     args.push_back(test_case_name);
28811be35a1SLionel Sambuc     args.push_back(result_file.str());
28911be35a1SLionel Sambuc 
29011be35a1SLionel Sambuc     const fs::path tester_path = engine::tester_path(_interface);
29111be35a1SLionel Sambuc     std::auto_ptr< process::child > child = process::child::spawn_files(
29211be35a1SLionel Sambuc         tester_path, args, stdout_file, stderr_file);
29311be35a1SLionel Sambuc     const process::status status = child->wait();
29411be35a1SLionel Sambuc 
29511be35a1SLionel Sambuc     if (status.exited()) {
29611be35a1SLionel Sambuc         if (status.exitstatus() == EXIT_SUCCESS) {
29711be35a1SLionel Sambuc             // OK; the tester exited cleanly.
29811be35a1SLionel Sambuc         } else if (status.exitstatus() == EXIT_FAILURE) {
29911be35a1SLionel Sambuc             // OK; the tester reported that the test itself failed and we have
30011be35a1SLionel Sambuc             // the result file to indicate this.
30111be35a1SLionel Sambuc         } else {
30211be35a1SLionel Sambuc             throw engine::error(F("Tester failed with code %s; this is a bug") %
30311be35a1SLionel Sambuc                                 status.exitstatus());
30411be35a1SLionel Sambuc         }
30511be35a1SLionel Sambuc     } else {
30611be35a1SLionel Sambuc         INV(status.signaled());
307*84d9c625SLionel Sambuc         throw engine::error(F("Tester received signal %s; this is a bug") %
308*84d9c625SLionel Sambuc                             status.termsig());
30911be35a1SLionel Sambuc     }
31011be35a1SLionel Sambuc }
311