1 // Copyright 2010 Google Inc. 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 35 36 namespace { 37 38 39 /// Prints a fake but valid test case list and then aborts. 40 /// 41 /// \param unused_argc The original argument count of the program. 42 /// \param argv The original arguments of the program. 43 /// 44 /// \return Nothing because this dies before returning. 45 static int 46 helper_abort_test_cases_list(int UTILS_UNUSED_PARAM(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 std::abort(); 56 } 57 58 59 /// Just returns without printing anything as the test case list. 60 /// 61 /// \param unused_argc The original argument count of the program. 62 /// \param unused_argv The original arguments of the program. 63 /// 64 /// \return Always 0, as required for test programs. 65 static int 66 helper_empty_test_cases_list(int UTILS_UNUSED_PARAM(argc), 67 char** UTILS_UNUSED_PARAM(argv)) 68 { 69 return EXIT_SUCCESS; 70 } 71 72 73 /// Prints a correctly-formatted test case list but empty. 74 /// 75 /// \param unused_argc The original argument count of the program. 76 /// \param argv The original arguments of the program. 77 /// 78 /// \return Always 0, as required for test programs. 79 static int 80 helper_zero_test_cases(int UTILS_UNUSED_PARAM(argc), char** argv) 81 { 82 for (const char* const* arg = argv; *arg != NULL; arg++) { 83 if (std::strcmp(*arg, "-l") == 0) 84 std::cout << "Content-Type: application/X-atf-tp; " 85 "version=\"1\"\n\n"; 86 } 87 return EXIT_SUCCESS; 88 } 89 90 91 /// Mapping of the name of a helper to its implementation. 92 struct helper { 93 /// The name of the helper, as will be provided by the user on the CLI. 94 const char* name; 95 96 /// A pointer to the function implementing the helper. 97 int (*hook)(int, char**); 98 }; 99 100 101 /// NULL-terminated table mapping helper names to their implementations. 102 static const helper helpers[] = { 103 { "abort_test_cases_list", helper_abort_test_cases_list, }, 104 { "empty_test_cases_list", helper_empty_test_cases_list, }, 105 { "zero_test_cases", helper_zero_test_cases, }, 106 { NULL, NULL, }, 107 }; 108 109 110 } // anonymous namespace 111 112 113 /// Entry point to the ATF-less helpers. 114 /// 115 /// The caller must select a helper to execute by defining the HELPER 116 /// environment variable to the name of the desired helper. Think of this main 117 /// method as a subprogram dispatcher, to avoid having many individual helper 118 /// binaries. 119 /// 120 /// \todo Maybe we should really have individual helper binaries. It would 121 /// avoid a significant amount of complexity here and in the tests, at the 122 /// expense of some extra files and extra build logic. 123 /// 124 /// \param argc The user argument count; delegated to the helper. 125 /// \param argv The user arguments; delegated to the helper. 126 /// 127 /// \return The exit code of the helper, which depends on the requested helper. 128 int 129 main(int argc, char** argv) 130 { 131 const char* command = std::getenv("HELPER"); 132 if (command == NULL) { 133 std::cerr << "Usage error: HELPER must be set to a helper name\n"; 134 std::exit(EXIT_FAILURE); 135 } 136 137 const struct helper* iter = helpers; 138 for (; iter->name != NULL && std::strcmp(iter->name, command) != 0; iter++) 139 ; 140 if (iter->name == NULL) { 141 std::cerr << "Usage error: unknown command " << command << "\n"; 142 std::exit(EXIT_FAILURE); 143 } 144 145 return iter->hook(argc, argv); 146 } 147