1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
33 #include "utils/defs.hpp"
34 #include "utils/test_utils.ipp"
40 /// Prints a fake but valid test case list and then aborts.
42 /// \param argv The original arguments of the program.
44 /// \return Nothing because this dies before returning.
46 helper_abort_test_cases_list(int /* argc */, char** argv)
48 for (const char* const* arg = argv; *arg != NULL; arg++) {
49 if (std::strcmp(*arg, "-l") == 0) {
50 std::cout << "Content-Type: application/X-atf-tp; "
52 std::cout << "ident: foo\n";
55 utils::abort_without_coredump();
59 /// Just returns without printing anything as the test case list.
61 /// \return Always 0, as required for test programs.
63 helper_empty_test_cases_list(int /* argc */, char** /* argv */)
69 /// Prints a correctly-formatted test case list but empty.
71 /// \param argv The original arguments of the program.
73 /// \return Always 0, as required for test programs.
75 helper_zero_test_cases(int /* argc */, char** argv)
77 for (const char* const* arg = argv; *arg != NULL; arg++) {
78 if (std::strcmp(*arg, "-l") == 0)
79 std::cout << "Content-Type: application/X-atf-tp; "
86 /// Mapping of the name of a helper to its implementation.
88 /// The name of the helper, as will be provided by the user on the CLI.
91 /// A pointer to the function implementing the helper.
92 int (*hook)(int, char**);
96 /// NULL-terminated table mapping helper names to their implementations.
97 static const helper helpers[] = {
98 { "abort_test_cases_list", helper_abort_test_cases_list, },
99 { "empty_test_cases_list", helper_empty_test_cases_list, },
100 { "zero_test_cases", helper_zero_test_cases, },
105 } // anonymous namespace
108 /// Entry point to the ATF-less helpers.
110 /// The caller must select a helper to execute by defining the HELPER
111 /// environment variable to the name of the desired helper. Think of this main
112 /// method as a subprogram dispatcher, to avoid having many individual helper
115 /// \todo Maybe we should really have individual helper binaries. It would
116 /// avoid a significant amount of complexity here and in the tests, at the
117 /// expense of some extra files and extra build logic.
119 /// \param argc The user argument count; delegated to the helper.
120 /// \param argv The user arguments; delegated to the helper.
122 /// \return The exit code of the helper, which depends on the requested helper.
124 main(int argc, char** argv)
126 const char* command = std::getenv("HELPER");
127 if (command == NULL) {
128 std::cerr << "Usage error: HELPER must be set to a helper name\n";
129 std::exit(EXIT_FAILURE);
132 const struct helper* iter = helpers;
133 for (; iter->name != NULL && std::strcmp(iter->name, command) != 0; iter++)
135 if (iter->name == NULL) {
136 std::cerr << "Usage error: unknown command " << command << "\n";
137 std::exit(EXIT_FAILURE);
140 return iter->hook(argc, argv);