conformance_test_runner.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. // This file contains a program for running the test suite in a separate
  31. // process. The other alternative is to run the suite in-process. See
  32. // conformance.proto for pros/cons of these two options.
  33. //
  34. // This program will fork the process under test and communicate with it over
  35. // its stdin/stdout:
  36. //
  37. // +--------+ pipe +----------+
  38. // | tester | <------> | testee |
  39. // | | | |
  40. // | C++ | | any lang |
  41. // +--------+ +----------+
  42. //
  43. // The tester contains all of the test cases and their expected output.
  44. // The testee is a simple program written in the target language that reads
  45. // each test case and attempts to produce acceptable output for it.
  46. //
  47. // Every test consists of a ConformanceRequest/ConformanceResponse
  48. // request/reply pair. The protocol on the pipe is simply:
  49. //
  50. // 1. tester sends 4-byte length N (little endian)
  51. // 2. tester sends N bytes representing a ConformanceRequest proto
  52. // 3. testee sends 4-byte length M (little endian)
  53. // 4. testee sends M bytes representing a ConformanceResponse proto
  54. #include <errno.h>
  55. #include <sys/types.h>
  56. #include <sys/wait.h>
  57. #include <unistd.h>
  58. #include <algorithm>
  59. #include <fstream>
  60. #include <vector>
  61. #include <google/protobuf/stubs/stringprintf.h>
  62. #include "conformance.pb.h"
  63. #include "conformance_test.h"
  64. using conformance::ConformanceResponse;
  65. using google::protobuf::ConformanceTestSuite;
  66. using std::string;
  67. using std::vector;
  68. #define STRINGIFY(x) #x
  69. #define TOSTRING(x) STRINGIFY(x)
  70. #define GOOGLE_CHECK_SYSCALL(call) \
  71. if (call < 0) { \
  72. perror(#call " " __FILE__ ":" TOSTRING(__LINE__)); \
  73. exit(1); \
  74. }
  75. namespace google {
  76. namespace protobuf {
  77. void ParseFailureList(const char *filename,
  78. conformance::FailureSet *failure_list) {
  79. std::ifstream infile(filename);
  80. if (!infile.is_open()) {
  81. fprintf(stderr, "Couldn't open failure list file: %s\n", filename);
  82. exit(1);
  83. }
  84. for (string line; getline(infile, line);) {
  85. // Remove whitespace.
  86. line.erase(std::remove_if(line.begin(), line.end(), ::isspace),
  87. line.end());
  88. // Remove comments.
  89. line = line.substr(0, line.find("#"));
  90. if (!line.empty()) {
  91. failure_list->add_failure(line);
  92. }
  93. }
  94. }
  95. void UsageError() {
  96. fprintf(stderr,
  97. "Usage: conformance-test-runner [options] <test-program>\n");
  98. fprintf(stderr, "\n");
  99. fprintf(stderr, "Options:\n");
  100. fprintf(stderr,
  101. " --failure_list <filename> Use to specify list of tests\n");
  102. fprintf(stderr,
  103. " that are expected to fail. File\n");
  104. fprintf(stderr,
  105. " should contain one test name per\n");
  106. fprintf(stderr,
  107. " line. Use '#' for comments.\n");
  108. fprintf(stderr,
  109. " --text_format_failure_list <filename> Use to specify list \n");
  110. fprintf(stderr,
  111. " of tests that are expected to \n");
  112. fprintf(stderr,
  113. " fail in the \n");
  114. fprintf(stderr,
  115. " text_format_conformance_suite. \n");
  116. fprintf(stderr,
  117. " File should contain one test name \n");
  118. fprintf(stderr,
  119. " per line. Use '#' for comments.\n");
  120. fprintf(stderr,
  121. " --enforce_recommended Enforce that recommended test\n");
  122. fprintf(stderr,
  123. " cases are also passing. Specify\n");
  124. fprintf(stderr,
  125. " this flag if you want to be\n");
  126. fprintf(stderr,
  127. " strictly conforming to protobuf\n");
  128. fprintf(stderr,
  129. " spec.\n");
  130. exit(1);
  131. }
  132. void ForkPipeRunner::RunTest(
  133. const std::string& test_name,
  134. const std::string& request,
  135. std::string* response) {
  136. if (child_pid_ < 0) {
  137. SpawnTestProgram();
  138. }
  139. current_test_name_ = test_name;
  140. uint32_t len = request.size();
  141. CheckedWrite(write_fd_, &len, sizeof(uint32_t));
  142. CheckedWrite(write_fd_, request.c_str(), request.size());
  143. if (!TryRead(read_fd_, &len, sizeof(uint32_t))) {
  144. // We failed to read from the child, assume a crash and try to reap.
  145. GOOGLE_LOG(INFO) << "Trying to reap child, pid=" << child_pid_;
  146. int status;
  147. waitpid(child_pid_, &status, WEXITED);
  148. string error_msg;
  149. if (WIFEXITED(status)) {
  150. StringAppendF(&error_msg,
  151. "child exited, status=%d", WEXITSTATUS(status));
  152. } else if (WIFSIGNALED(status)) {
  153. StringAppendF(&error_msg,
  154. "child killed by signal %d", WTERMSIG(status));
  155. }
  156. GOOGLE_LOG(INFO) << error_msg;
  157. child_pid_ = -1;
  158. conformance::ConformanceResponse response_obj;
  159. response_obj.set_runtime_error(error_msg);
  160. response_obj.SerializeToString(response);
  161. return;
  162. }
  163. response->resize(len);
  164. CheckedRead(read_fd_, (void*)response->c_str(), len);
  165. }
  166. int ForkPipeRunner::Run(
  167. int argc, char *argv[], const std::vector<ConformanceTestSuite*>& suites) {
  168. if (suites.empty()) {
  169. fprintf(stderr, "No test suites found.\n");
  170. return EXIT_FAILURE;
  171. }
  172. bool all_ok = true;
  173. for (ConformanceTestSuite* suite : suites) {
  174. string program;
  175. std::vector<string> program_args;
  176. string failure_list_filename;
  177. conformance::FailureSet failure_list;
  178. for (int arg = 1; arg < argc; ++arg) {
  179. if (strcmp(argv[arg], suite->GetFailureListFlagName().c_str()) == 0) {
  180. if (++arg == argc) UsageError();
  181. failure_list_filename = argv[arg];
  182. ParseFailureList(argv[arg], &failure_list);
  183. } else if (strcmp(argv[arg], "--verbose") == 0) {
  184. suite->SetVerbose(true);
  185. } else if (strcmp(argv[arg], "--enforce_recommended") == 0) {
  186. suite->SetEnforceRecommended(true);
  187. } else if (argv[arg][0] == '-') {
  188. bool recognized_flag = false;
  189. for (ConformanceTestSuite* suite : suites) {
  190. if (strcmp(argv[arg], suite->GetFailureListFlagName().c_str()) == 0) {
  191. if (++arg == argc) UsageError();
  192. recognized_flag = true;
  193. }
  194. }
  195. if (!recognized_flag) {
  196. fprintf(stderr, "Unknown option: %s\n", argv[arg]);
  197. UsageError();
  198. }
  199. } else {
  200. program += argv[arg];
  201. while (arg < argc) {
  202. program_args.push_back(argv[arg]);
  203. arg++;
  204. }
  205. }
  206. }
  207. ForkPipeRunner runner(program, program_args);
  208. std::string output;
  209. all_ok = all_ok &&
  210. suite->RunSuite(&runner, &output, failure_list_filename, &failure_list);
  211. fwrite(output.c_str(), 1, output.size(), stderr);
  212. }
  213. return all_ok ? EXIT_SUCCESS : EXIT_FAILURE;
  214. }
  215. // TODO(haberman): make this work on Windows, instead of using these
  216. // UNIX-specific APIs.
  217. //
  218. // There is a platform-agnostic API in
  219. // src/google/protobuf/compiler/subprocess.h
  220. //
  221. // However that API only supports sending a single message to the subprocess.
  222. // We really want to be able to send messages and receive responses one at a
  223. // time:
  224. //
  225. // 1. Spawning a new process for each test would take way too long for thousands
  226. // of tests and subprocesses like java that can take 100ms or more to start
  227. // up.
  228. //
  229. // 2. Sending all the tests in one big message and receiving all results in one
  230. // big message would take away our visibility about which test(s) caused a
  231. // crash or other fatal error. It would also give us only a single failure
  232. // instead of all of them.
  233. void ForkPipeRunner::SpawnTestProgram() {
  234. int toproc_pipe_fd[2];
  235. int fromproc_pipe_fd[2];
  236. if (pipe(toproc_pipe_fd) < 0 || pipe(fromproc_pipe_fd) < 0) {
  237. perror("pipe");
  238. exit(1);
  239. }
  240. pid_t pid = fork();
  241. if (pid < 0) {
  242. perror("fork");
  243. exit(1);
  244. }
  245. if (pid) {
  246. // Parent.
  247. GOOGLE_CHECK_SYSCALL(close(toproc_pipe_fd[0]));
  248. GOOGLE_CHECK_SYSCALL(close(fromproc_pipe_fd[1]));
  249. write_fd_ = toproc_pipe_fd[1];
  250. read_fd_ = fromproc_pipe_fd[0];
  251. child_pid_ = pid;
  252. } else {
  253. // Child.
  254. GOOGLE_CHECK_SYSCALL(close(STDIN_FILENO));
  255. GOOGLE_CHECK_SYSCALL(close(STDOUT_FILENO));
  256. GOOGLE_CHECK_SYSCALL(dup2(toproc_pipe_fd[0], STDIN_FILENO));
  257. GOOGLE_CHECK_SYSCALL(dup2(fromproc_pipe_fd[1], STDOUT_FILENO));
  258. GOOGLE_CHECK_SYSCALL(close(toproc_pipe_fd[0]));
  259. GOOGLE_CHECK_SYSCALL(close(fromproc_pipe_fd[1]));
  260. GOOGLE_CHECK_SYSCALL(close(toproc_pipe_fd[1]));
  261. GOOGLE_CHECK_SYSCALL(close(fromproc_pipe_fd[0]));
  262. std::unique_ptr<char[]> executable(new char[executable_.size() + 1]);
  263. memcpy(executable.get(), executable_.c_str(), executable_.size());
  264. executable[executable_.size()] = '\0';
  265. std::vector<const char *> argv;
  266. argv.push_back(executable.get());
  267. for (size_t i = 0; i < executable_args_.size(); ++i) {
  268. argv.push_back(executable_args_[i].c_str());
  269. }
  270. argv.push_back(nullptr);
  271. // Never returns.
  272. GOOGLE_CHECK_SYSCALL(execv(executable.get(), const_cast<char **>(argv.data())));
  273. }
  274. }
  275. void ForkPipeRunner::CheckedWrite(int fd, const void *buf, size_t len) {
  276. if (static_cast<size_t>(write(fd, buf, len)) != len) {
  277. GOOGLE_LOG(FATAL) << current_test_name_
  278. << ": error writing to test program: " << strerror(errno);
  279. }
  280. }
  281. bool ForkPipeRunner::TryRead(int fd, void *buf, size_t len) {
  282. size_t ofs = 0;
  283. while (len > 0) {
  284. ssize_t bytes_read = read(fd, (char*)buf + ofs, len);
  285. if (bytes_read == 0) {
  286. GOOGLE_LOG(ERROR) << current_test_name_ << ": unexpected EOF from test program";
  287. return false;
  288. } else if (bytes_read < 0) {
  289. GOOGLE_LOG(ERROR) << current_test_name_
  290. << ": error reading from test program: " << strerror(errno);
  291. return false;
  292. }
  293. len -= bytes_read;
  294. ofs += bytes_read;
  295. }
  296. return true;
  297. }
  298. void ForkPipeRunner::CheckedRead(int fd, void *buf, size_t len) {
  299. if (!TryRead(fd, buf, len)) {
  300. GOOGLE_LOG(FATAL) << current_test_name_
  301. << ": error reading from test program: " << strerror(errno);
  302. }
  303. }
  304. } // namespace protobuf
  305. } // namespace google