Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // This file contains a program for running the test suite in a separate |
| 32 | // process. The other alternative is to run the suite in-process. See |
| 33 | // conformance.proto for pros/cons of these two options. |
| 34 | // |
| 35 | // This program will fork the process under test and communicate with it over |
| 36 | // its stdin/stdout: |
| 37 | // |
| 38 | // +--------+ pipe +----------+ |
| 39 | // | tester | <------> | testee | |
| 40 | // | | | | |
| 41 | // | C++ | | any lang | |
| 42 | // +--------+ +----------+ |
| 43 | // |
| 44 | // The tester contains all of the test cases and their expected output. |
| 45 | // The testee is a simple program written in the target language that reads |
| 46 | // each test case and attempts to produce acceptable output for it. |
| 47 | // |
| 48 | // Every test consists of a ConformanceRequest/ConformanceResponse |
| 49 | // request/reply pair. The protocol on the pipe is simply: |
| 50 | // |
| 51 | // 1. tester sends 4-byte length N (little endian) |
| 52 | // 2. tester sends N bytes representing a ConformanceRequest proto |
| 53 | // 3. testee sends 4-byte length M (little endian) |
| 54 | // 4. testee sends M bytes representing a ConformanceResponse proto |
| 55 | |
| 56 | #include <algorithm> |
| 57 | #include <errno.h> |
| 58 | #include <fstream> |
| 59 | #include <sys/types.h> |
| 60 | #include <sys/wait.h> |
| 61 | #include <unistd.h> |
| 62 | #include <vector> |
| 63 | |
| 64 | #include <google/protobuf/stubs/stringprintf.h> |
| 65 | |
| 66 | #include "conformance.pb.h" |
| 67 | #include "conformance_test.h" |
| 68 | |
| 69 | using conformance::ConformanceRequest; |
| 70 | using conformance::ConformanceResponse; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 71 | using google::protobuf::StringAppendF; |
| 72 | using std::string; |
| 73 | using std::vector; |
| 74 | |
| 75 | #define STRINGIFY(x) #x |
| 76 | #define TOSTRING(x) STRINGIFY(x) |
| 77 | #define CHECK_SYSCALL(call) \ |
| 78 | if (call < 0) { \ |
| 79 | perror(#call " " __FILE__ ":" TOSTRING(__LINE__)); \ |
| 80 | exit(1); \ |
| 81 | } |
| 82 | |
| 83 | // Test runner that spawns the process being tested and communicates with it |
| 84 | // over a pipe. |
| 85 | class ForkPipeRunner : public google::protobuf::ConformanceTestRunner { |
| 86 | public: |
| 87 | ForkPipeRunner(const std::string &executable) |
| 88 | : child_pid_(-1), executable_(executable) {} |
| 89 | |
| 90 | virtual ~ForkPipeRunner() {} |
| 91 | |
| 92 | void RunTest(const std::string& test_name, |
| 93 | const std::string& request, |
| 94 | std::string* response) { |
| 95 | if (child_pid_ < 0) { |
| 96 | SpawnTestProgram(); |
| 97 | } |
| 98 | |
| 99 | current_test_name_ = test_name; |
| 100 | |
| 101 | uint32_t len = request.size(); |
| 102 | CheckedWrite(write_fd_, &len, sizeof(uint32_t)); |
| 103 | CheckedWrite(write_fd_, request.c_str(), request.size()); |
| 104 | |
| 105 | if (!TryRead(read_fd_, &len, sizeof(uint32_t))) { |
| 106 | // We failed to read from the child, assume a crash and try to reap. |
| 107 | GOOGLE_LOG(INFO) << "Trying to reap child, pid=" << child_pid_; |
| 108 | |
| 109 | int status; |
| 110 | waitpid(child_pid_, &status, WEXITED); |
| 111 | |
| 112 | string error_msg; |
| 113 | if (WIFEXITED(status)) { |
| 114 | StringAppendF(&error_msg, |
| 115 | "child exited, status=%d", WEXITSTATUS(status)); |
| 116 | } else if (WIFSIGNALED(status)) { |
| 117 | StringAppendF(&error_msg, |
| 118 | "child killed by signal %d", WTERMSIG(status)); |
| 119 | } |
| 120 | GOOGLE_LOG(INFO) << error_msg; |
| 121 | child_pid_ = -1; |
| 122 | |
| 123 | conformance::ConformanceResponse response_obj; |
| 124 | response_obj.set_runtime_error(error_msg); |
| 125 | response_obj.SerializeToString(response); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | response->resize(len); |
| 130 | CheckedRead(read_fd_, (void*)response->c_str(), len); |
| 131 | } |
| 132 | |
| 133 | private: |
| 134 | // TODO(haberman): make this work on Windows, instead of using these |
| 135 | // UNIX-specific APIs. |
| 136 | // |
| 137 | // There is a platform-agnostic API in |
| 138 | // src/google/protobuf/compiler/subprocess.h |
| 139 | // |
| 140 | // However that API only supports sending a single message to the subprocess. |
| 141 | // We really want to be able to send messages and receive responses one at a |
| 142 | // time: |
| 143 | // |
| 144 | // 1. Spawning a new process for each test would take way too long for thousands |
| 145 | // of tests and subprocesses like java that can take 100ms or more to start |
| 146 | // up. |
| 147 | // |
| 148 | // 2. Sending all the tests in one big message and receiving all results in one |
| 149 | // big message would take away our visibility about which test(s) caused a |
| 150 | // crash or other fatal error. It would also give us only a single failure |
| 151 | // instead of all of them. |
| 152 | void SpawnTestProgram() { |
| 153 | int toproc_pipe_fd[2]; |
| 154 | int fromproc_pipe_fd[2]; |
| 155 | if (pipe(toproc_pipe_fd) < 0 || pipe(fromproc_pipe_fd) < 0) { |
| 156 | perror("pipe"); |
| 157 | exit(1); |
| 158 | } |
| 159 | |
| 160 | pid_t pid = fork(); |
| 161 | if (pid < 0) { |
| 162 | perror("fork"); |
| 163 | exit(1); |
| 164 | } |
| 165 | |
| 166 | if (pid) { |
| 167 | // Parent. |
| 168 | CHECK_SYSCALL(close(toproc_pipe_fd[0])); |
| 169 | CHECK_SYSCALL(close(fromproc_pipe_fd[1])); |
| 170 | write_fd_ = toproc_pipe_fd[1]; |
| 171 | read_fd_ = fromproc_pipe_fd[0]; |
| 172 | child_pid_ = pid; |
| 173 | } else { |
| 174 | // Child. |
| 175 | CHECK_SYSCALL(close(STDIN_FILENO)); |
| 176 | CHECK_SYSCALL(close(STDOUT_FILENO)); |
| 177 | CHECK_SYSCALL(dup2(toproc_pipe_fd[0], STDIN_FILENO)); |
| 178 | CHECK_SYSCALL(dup2(fromproc_pipe_fd[1], STDOUT_FILENO)); |
| 179 | |
| 180 | CHECK_SYSCALL(close(toproc_pipe_fd[0])); |
| 181 | CHECK_SYSCALL(close(fromproc_pipe_fd[1])); |
| 182 | CHECK_SYSCALL(close(toproc_pipe_fd[1])); |
| 183 | CHECK_SYSCALL(close(fromproc_pipe_fd[0])); |
| 184 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 185 | std::unique_ptr<char[]> executable(new char[executable_.size() + 1]); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 186 | memcpy(executable.get(), executable_.c_str(), executable_.size()); |
| 187 | executable[executable_.size()] = '\0'; |
| 188 | |
| 189 | char *const argv[] = {executable.get(), NULL}; |
| 190 | CHECK_SYSCALL(execv(executable.get(), argv)); // Never returns. |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void CheckedWrite(int fd, const void *buf, size_t len) { |
| 195 | if (write(fd, buf, len) != len) { |
| 196 | GOOGLE_LOG(FATAL) << current_test_name_ |
| 197 | << ": error writing to test program: " |
| 198 | << strerror(errno); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | bool TryRead(int fd, void *buf, size_t len) { |
| 203 | size_t ofs = 0; |
| 204 | while (len > 0) { |
| 205 | ssize_t bytes_read = read(fd, (char*)buf + ofs, len); |
| 206 | |
| 207 | if (bytes_read == 0) { |
| 208 | GOOGLE_LOG(ERROR) << current_test_name_ |
| 209 | << ": unexpected EOF from test program"; |
| 210 | return false; |
| 211 | } else if (bytes_read < 0) { |
| 212 | GOOGLE_LOG(ERROR) << current_test_name_ |
| 213 | << ": error reading from test program: " |
| 214 | << strerror(errno); |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | len -= bytes_read; |
| 219 | ofs += bytes_read; |
| 220 | } |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | void CheckedRead(int fd, void *buf, size_t len) { |
| 226 | if (!TryRead(fd, buf, len)) { |
| 227 | GOOGLE_LOG(FATAL) << current_test_name_ |
| 228 | << ": error reading from test program: " |
| 229 | << strerror(errno); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | int write_fd_; |
| 234 | int read_fd_; |
| 235 | pid_t child_pid_; |
| 236 | std::string executable_; |
| 237 | std::string current_test_name_; |
| 238 | }; |
| 239 | |
| 240 | void UsageError() { |
| 241 | fprintf(stderr, |
| 242 | "Usage: conformance-test-runner [options] <test-program>\n"); |
| 243 | fprintf(stderr, "\n"); |
| 244 | fprintf(stderr, "Options:\n"); |
| 245 | fprintf(stderr, |
| 246 | " --failure_list <filename> Use to specify list of tests\n"); |
| 247 | fprintf(stderr, |
| 248 | " that are expected to fail. File\n"); |
| 249 | fprintf(stderr, |
| 250 | " should contain one test name per\n"); |
| 251 | fprintf(stderr, |
| 252 | " line. Use '#' for comments.\n"); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 253 | fprintf(stderr, |
| 254 | " --enforce_recommended Enforce that recommended test\n"); |
| 255 | fprintf(stderr, |
| 256 | " cases are also passing. Specify\n"); |
| 257 | fprintf(stderr, |
| 258 | " this flag if you want to be\n"); |
| 259 | fprintf(stderr, |
| 260 | " strictly conforming to protobuf\n"); |
| 261 | fprintf(stderr, |
| 262 | " spec.\n"); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 263 | exit(1); |
| 264 | } |
| 265 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 266 | void ParseFailureList(const char *filename, std::vector<string>* failure_list) { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 267 | std::ifstream infile(filename); |
| 268 | |
| 269 | if (!infile.is_open()) { |
| 270 | fprintf(stderr, "Couldn't open failure list file: %s\n", filename); |
| 271 | exit(1); |
| 272 | } |
| 273 | |
| 274 | for (string line; getline(infile, line);) { |
| 275 | // Remove whitespace. |
| 276 | line.erase(std::remove_if(line.begin(), line.end(), ::isspace), |
| 277 | line.end()); |
| 278 | |
| 279 | // Remove comments. |
| 280 | line = line.substr(0, line.find("#")); |
| 281 | |
| 282 | if (!line.empty()) { |
| 283 | failure_list->push_back(line); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | int main(int argc, char *argv[]) { |
| 289 | char *program; |
| 290 | google::protobuf::ConformanceTestSuite suite; |
| 291 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 292 | string failure_list_filename; |
| 293 | std::vector<string> failure_list; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 294 | |
| 295 | for (int arg = 1; arg < argc; ++arg) { |
| 296 | if (strcmp(argv[arg], "--failure_list") == 0) { |
| 297 | if (++arg == argc) UsageError(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 298 | failure_list_filename = argv[arg]; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 299 | ParseFailureList(argv[arg], &failure_list); |
| 300 | } else if (strcmp(argv[arg], "--verbose") == 0) { |
| 301 | suite.SetVerbose(true); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 302 | } else if (strcmp(argv[arg], "--enforce_recommended") == 0) { |
| 303 | suite.SetEnforceRecommended(true); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 304 | } else if (argv[arg][0] == '-') { |
| 305 | fprintf(stderr, "Unknown option: %s\n", argv[arg]); |
| 306 | UsageError(); |
| 307 | } else { |
| 308 | if (arg != argc - 1) { |
| 309 | fprintf(stderr, "Too many arguments.\n"); |
| 310 | UsageError(); |
| 311 | } |
| 312 | program = argv[arg]; |
| 313 | } |
| 314 | } |
| 315 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 316 | suite.SetFailureList(failure_list_filename, failure_list); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 317 | ForkPipeRunner runner(program); |
| 318 | |
| 319 | std::string output; |
| 320 | bool ok = suite.RunSuite(&runner, &output); |
| 321 | |
| 322 | fwrite(output.c_str(), 1, output.size(), stderr); |
| 323 | |
| 324 | return ok ? EXIT_SUCCESS : EXIT_FAILURE; |
| 325 | } |