conformance_test_runner.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 <algorithm>
  55. #include <errno.h>
  56. #include <fstream>
  57. #include <sys/types.h>
  58. #include <sys/wait.h>
  59. #include <unistd.h>
  60. #include <vector>
  61. #include <google/protobuf/stubs/stringprintf.h>
  62. #include "conformance.pb.h"
  63. #include "conformance_test.h"
  64. using conformance::ConformanceRequest;
  65. using conformance::ConformanceResponse;
  66. using google::protobuf::internal::scoped_array;
  67. using google::protobuf::StringAppendF;
  68. using std::string;
  69. using std::vector;
  70. #define STRINGIFY(x) #x
  71. #define TOSTRING(x) STRINGIFY(x)
  72. #define CHECK_SYSCALL(call) \
  73. if (call < 0) { \
  74. perror(#call " " __FILE__ ":" TOSTRING(__LINE__)); \
  75. exit(1); \
  76. }
  77. // Test runner that spawns the process being tested and communicates with it
  78. // over a pipe.
  79. class ForkPipeRunner : public google::protobuf::ConformanceTestRunner {
  80. public:
  81. ForkPipeRunner(const std::string &executable)
  82. : child_pid_(-1), executable_(executable) {}
  83. virtual ~ForkPipeRunner() {}
  84. void RunTest(const std::string& test_name,
  85. const std::string& request,
  86. std::string* response) {
  87. if (child_pid_ < 0) {
  88. SpawnTestProgram();
  89. }
  90. current_test_name_ = test_name;
  91. uint32_t len = request.size();
  92. CheckedWrite(write_fd_, &len, sizeof(uint32_t));
  93. CheckedWrite(write_fd_, request.c_str(), request.size());
  94. if (!TryRead(read_fd_, &len, sizeof(uint32_t))) {
  95. // We failed to read from the child, assume a crash and try to reap.
  96. GOOGLE_LOG(INFO) << "Trying to reap child, pid=" << child_pid_;
  97. int status;
  98. waitpid(child_pid_, &status, WEXITED);
  99. string error_msg;
  100. if (WIFEXITED(status)) {
  101. StringAppendF(&error_msg,
  102. "child exited, status=%d", WEXITSTATUS(status));
  103. } else if (WIFSIGNALED(status)) {
  104. StringAppendF(&error_msg,
  105. "child killed by signal %d", WTERMSIG(status));
  106. }
  107. GOOGLE_LOG(INFO) << error_msg;
  108. child_pid_ = -1;
  109. conformance::ConformanceResponse response_obj;
  110. response_obj.set_runtime_error(error_msg);
  111. response_obj.SerializeToString(response);
  112. return;
  113. }
  114. response->resize(len);
  115. CheckedRead(read_fd_, (void*)response->c_str(), len);
  116. }
  117. private:
  118. // TODO(haberman): make this work on Windows, instead of using these
  119. // UNIX-specific APIs.
  120. //
  121. // There is a platform-agnostic API in
  122. // src/google/protobuf/compiler/subprocess.h
  123. //
  124. // However that API only supports sending a single message to the subprocess.
  125. // We really want to be able to send messages and receive responses one at a
  126. // time:
  127. //
  128. // 1. Spawning a new process for each test would take way too long for thousands
  129. // of tests and subprocesses like java that can take 100ms or more to start
  130. // up.
  131. //
  132. // 2. Sending all the tests in one big message and receiving all results in one
  133. // big message would take away our visibility about which test(s) caused a
  134. // crash or other fatal error. It would also give us only a single failure
  135. // instead of all of them.
  136. void SpawnTestProgram() {
  137. int toproc_pipe_fd[2];
  138. int fromproc_pipe_fd[2];
  139. if (pipe(toproc_pipe_fd) < 0 || pipe(fromproc_pipe_fd) < 0) {
  140. perror("pipe");
  141. exit(1);
  142. }
  143. pid_t pid = fork();
  144. if (pid < 0) {
  145. perror("fork");
  146. exit(1);
  147. }
  148. if (pid) {
  149. // Parent.
  150. CHECK_SYSCALL(close(toproc_pipe_fd[0]));
  151. CHECK_SYSCALL(close(fromproc_pipe_fd[1]));
  152. write_fd_ = toproc_pipe_fd[1];
  153. read_fd_ = fromproc_pipe_fd[0];
  154. child_pid_ = pid;
  155. } else {
  156. // Child.
  157. CHECK_SYSCALL(close(STDIN_FILENO));
  158. CHECK_SYSCALL(close(STDOUT_FILENO));
  159. CHECK_SYSCALL(dup2(toproc_pipe_fd[0], STDIN_FILENO));
  160. CHECK_SYSCALL(dup2(fromproc_pipe_fd[1], STDOUT_FILENO));
  161. CHECK_SYSCALL(close(toproc_pipe_fd[0]));
  162. CHECK_SYSCALL(close(fromproc_pipe_fd[1]));
  163. CHECK_SYSCALL(close(toproc_pipe_fd[1]));
  164. CHECK_SYSCALL(close(fromproc_pipe_fd[0]));
  165. scoped_array<char> executable(new char[executable_.size() + 1]);
  166. memcpy(executable.get(), executable_.c_str(), executable_.size());
  167. executable[executable_.size()] = '\0';
  168. char *const argv[] = {executable.get(), NULL};
  169. CHECK_SYSCALL(execv(executable.get(), argv)); // Never returns.
  170. }
  171. }
  172. void CheckedWrite(int fd, const void *buf, size_t len) {
  173. if (write(fd, buf, len) != len) {
  174. GOOGLE_LOG(FATAL) << current_test_name_
  175. << ": error writing to test program: "
  176. << strerror(errno);
  177. }
  178. }
  179. bool TryRead(int fd, void *buf, size_t len) {
  180. size_t ofs = 0;
  181. while (len > 0) {
  182. ssize_t bytes_read = read(fd, (char*)buf + ofs, len);
  183. if (bytes_read == 0) {
  184. GOOGLE_LOG(ERROR) << current_test_name_
  185. << ": unexpected EOF from test program";
  186. return false;
  187. } else if (bytes_read < 0) {
  188. GOOGLE_LOG(ERROR) << current_test_name_
  189. << ": error reading from test program: "
  190. << strerror(errno);
  191. return false;
  192. }
  193. len -= bytes_read;
  194. ofs += bytes_read;
  195. }
  196. return true;
  197. }
  198. void CheckedRead(int fd, void *buf, size_t len) {
  199. if (!TryRead(fd, buf, len)) {
  200. GOOGLE_LOG(FATAL) << current_test_name_
  201. << ": error reading from test program: "
  202. << strerror(errno);
  203. }
  204. }
  205. int write_fd_;
  206. int read_fd_;
  207. pid_t child_pid_;
  208. std::string executable_;
  209. std::string current_test_name_;
  210. };
  211. void UsageError() {
  212. fprintf(stderr,
  213. "Usage: conformance-test-runner [options] <test-program>\n");
  214. fprintf(stderr, "\n");
  215. fprintf(stderr, "Options:\n");
  216. fprintf(stderr,
  217. " --failure_list <filename> Use to specify list of tests\n");
  218. fprintf(stderr,
  219. " that are expected to fail. File\n");
  220. fprintf(stderr,
  221. " should contain one test name per\n");
  222. fprintf(stderr,
  223. " line. Use '#' for comments.\n");
  224. fprintf(stderr,
  225. " --enforce_recommended Enforce that recommended test\n");
  226. fprintf(stderr,
  227. " cases are also passing. Specify\n");
  228. fprintf(stderr,
  229. " this flag if you want to be\n");
  230. fprintf(stderr,
  231. " strictly conforming to protobuf\n");
  232. fprintf(stderr,
  233. " spec.\n");
  234. exit(1);
  235. }
  236. void ParseFailureList(const char *filename, std::vector<string>* failure_list) {
  237. std::ifstream infile(filename);
  238. if (!infile.is_open()) {
  239. fprintf(stderr, "Couldn't open failure list file: %s\n", filename);
  240. exit(1);
  241. }
  242. for (string line; getline(infile, line);) {
  243. // Remove whitespace.
  244. line.erase(std::remove_if(line.begin(), line.end(), ::isspace),
  245. line.end());
  246. // Remove comments.
  247. line = line.substr(0, line.find("#"));
  248. if (!line.empty()) {
  249. failure_list->push_back(line);
  250. }
  251. }
  252. }
  253. int main(int argc, char *argv[]) {
  254. char *program;
  255. google::protobuf::ConformanceTestSuite suite;
  256. string failure_list_filename;
  257. std::vector<string> failure_list;
  258. for (int arg = 1; arg < argc; ++arg) {
  259. if (strcmp(argv[arg], "--failure_list") == 0) {
  260. if (++arg == argc) UsageError();
  261. failure_list_filename = argv[arg];
  262. ParseFailureList(argv[arg], &failure_list);
  263. } else if (strcmp(argv[arg], "--verbose") == 0) {
  264. suite.SetVerbose(true);
  265. } else if (strcmp(argv[arg], "--enforce_recommended") == 0) {
  266. suite.SetEnforceRecommended(true);
  267. } else if (argv[arg][0] == '-') {
  268. fprintf(stderr, "Unknown option: %s\n", argv[arg]);
  269. UsageError();
  270. } else {
  271. if (arg != argc - 1) {
  272. fprintf(stderr, "Too many arguments.\n");
  273. UsageError();
  274. }
  275. program = argv[arg];
  276. }
  277. }
  278. suite.SetFailureList(failure_list_filename, failure_list);
  279. ForkPipeRunner runner(program);
  280. std::string output;
  281. bool ok = suite.RunSuite(&runner, &output);
  282. fwrite(output.c_str(), 1, output.size(), stderr);
  283. return ok ? EXIT_SUCCESS : EXIT_FAILURE;
  284. }