1// Copyright 2010 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 <cstdlib> 30#include <cstring> 31#include <iostream> 32 33#include "utils/defs.hpp" 34#include "utils/test_utils.ipp" 35 36 37namespace { 38 39 40/// Prints a fake but valid test case list and then aborts. 41/// 42/// \param argv The original arguments of the program. 43/// 44/// \return Nothing because this dies before returning. 45static int 46helper_abort_test_cases_list(int /* argc */, char** argv) 47{ 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; " 51 "version=\"1\"\n\n"; 52 std::cout << "ident: foo\n"; 53 } 54 } 55 utils::abort_without_coredump(); 56} 57 58 59/// Just returns without printing anything as the test case list. 60/// 61/// \return Always 0, as required for test programs. 62static int 63helper_empty_test_cases_list(int /* argc */, char** /* argv */) 64{ 65 return EXIT_SUCCESS; 66} 67 68 69/// Prints a correctly-formatted test case list but empty. 70/// 71/// \param argv The original arguments of the program. 72/// 73/// \return Always 0, as required for test programs. 74static int 75helper_zero_test_cases(int /* argc */, char** argv) 76{ 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; " 80 "version=\"1\"\n\n"; 81 } 82 return EXIT_SUCCESS; 83} 84 85 86/// Mapping of the name of a helper to its implementation. 87struct helper { 88 /// The name of the helper, as will be provided by the user on the CLI. 89 const char* name; 90 91 /// A pointer to the function implementing the helper. 92 int (*hook)(int, char**); 93}; 94 95 96/// NULL-terminated table mapping helper names to their implementations. 97static 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, }, 101 { NULL, NULL, }, 102}; 103 104 105} // anonymous namespace 106 107 108/// Entry point to the ATF-less helpers. 109/// 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 113/// binaries. 114/// 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. 118/// 119/// \param argc The user argument count; delegated to the helper. 120/// \param argv The user arguments; delegated to the helper. 121/// 122/// \return The exit code of the helper, which depends on the requested helper. 123int 124main(int argc, char** argv) 125{ 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); 130 } 131 132 const struct helper* iter = helpers; 133 for (; iter->name != NULL && std::strcmp(iter->name, command) != 0; iter++) 134 ; 135 if (iter->name == NULL) { 136 std::cerr << "Usage error: unknown command " << command << "\n"; 137 std::exit(EXIT_FAILURE); 138 } 139 140 return iter->hook(argc, argv); 141} 142