conformance_test_runner.cc 8.9 KB

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