conformance_test_runner.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <unistd.h>
  56. #include "conformance.pb.h"
  57. #include "conformance_test.h"
  58. using conformance::ConformanceRequest;
  59. using conformance::ConformanceResponse;
  60. using google::protobuf::internal::scoped_array;
  61. #define STRINGIFY(x) #x
  62. #define TOSTRING(x) STRINGIFY(x)
  63. #define CHECK_SYSCALL(call) \
  64. if (call < 0) { \
  65. perror(#call " " __FILE__ ":" TOSTRING(__LINE__)); \
  66. exit(1); \
  67. }
  68. // Test runner that spawns the process being tested and communicates with it
  69. // over a pipe.
  70. class ForkPipeRunner : public google::protobuf::ConformanceTestRunner {
  71. public:
  72. ForkPipeRunner(const std::string &executable)
  73. : executable_(executable), running_(false) {}
  74. void RunTest(const std::string& request, std::string* response) {
  75. if (!running_) {
  76. SpawnTestProgram();
  77. }
  78. uint32_t len = request.size();
  79. CheckedWrite(write_fd_, &len, sizeof(uint32_t));
  80. CheckedWrite(write_fd_, request.c_str(), request.size());
  81. CheckedRead(read_fd_, &len, sizeof(uint32_t));
  82. response->resize(len);
  83. CheckedRead(read_fd_, (void*)response->c_str(), len);
  84. }
  85. private:
  86. // TODO(haberman): make this work on Windows, instead of using these
  87. // UNIX-specific APIs.
  88. //
  89. // There is a platform-agnostic API in
  90. // src/google/protobuf/compiler/subprocess.h
  91. //
  92. // However that API only supports sending a single message to the subprocess.
  93. // We really want to be able to send messages and receive responses one at a
  94. // time:
  95. //
  96. // 1. Spawning a new process for each test would take way too long for thousands
  97. // of tests and subprocesses like java that can take 100ms or more to start
  98. // up.
  99. //
  100. // 2. Sending all the tests in one big message and receiving all results in one
  101. // big message would take away our visibility about which test(s) caused a
  102. // crash or other fatal error. It would also give us only a single failure
  103. // instead of all of them.
  104. void SpawnTestProgram() {
  105. int toproc_pipe_fd[2];
  106. int fromproc_pipe_fd[2];
  107. if (pipe(toproc_pipe_fd) < 0 || pipe(fromproc_pipe_fd) < 0) {
  108. perror("pipe");
  109. exit(1);
  110. }
  111. pid_t pid = fork();
  112. if (pid < 0) {
  113. perror("fork");
  114. exit(1);
  115. }
  116. if (pid) {
  117. // Parent.
  118. CHECK_SYSCALL(close(toproc_pipe_fd[0]));
  119. CHECK_SYSCALL(close(fromproc_pipe_fd[1]));
  120. write_fd_ = toproc_pipe_fd[1];
  121. read_fd_ = fromproc_pipe_fd[0];
  122. running_ = true;
  123. } else {
  124. // Child.
  125. CHECK_SYSCALL(close(STDIN_FILENO));
  126. CHECK_SYSCALL(close(STDOUT_FILENO));
  127. CHECK_SYSCALL(dup2(toproc_pipe_fd[0], STDIN_FILENO));
  128. CHECK_SYSCALL(dup2(fromproc_pipe_fd[1], STDOUT_FILENO));
  129. CHECK_SYSCALL(close(toproc_pipe_fd[0]));
  130. CHECK_SYSCALL(close(fromproc_pipe_fd[1]));
  131. CHECK_SYSCALL(close(toproc_pipe_fd[1]));
  132. CHECK_SYSCALL(close(fromproc_pipe_fd[0]));
  133. scoped_array<char> executable(new char[executable_.size()]);
  134. memcpy(executable.get(), executable_.c_str(), executable_.size());
  135. char *const argv[] = {executable.get(), NULL};
  136. CHECK_SYSCALL(execv(executable.get(), argv)); // Never returns.
  137. }
  138. }
  139. void CheckedWrite(int fd, const void *buf, size_t len) {
  140. if (write(fd, buf, len) != len) {
  141. GOOGLE_LOG(FATAL) << "Error writing to test program: " << strerror(errno);
  142. }
  143. }
  144. void CheckedRead(int fd, void *buf, size_t len) {
  145. size_t ofs = 0;
  146. while (len > 0) {
  147. ssize_t bytes_read = read(fd, (char*)buf + ofs, len);
  148. if (bytes_read == 0) {
  149. GOOGLE_LOG(FATAL) << "Unexpected EOF from test program";
  150. } else if (bytes_read < 0) {
  151. GOOGLE_LOG(FATAL) << "Error reading from test program: " << strerror(errno);
  152. }
  153. len -= bytes_read;
  154. ofs += bytes_read;
  155. }
  156. }
  157. int write_fd_;
  158. int read_fd_;
  159. bool running_;
  160. std::string executable_;
  161. };
  162. int main(int argc, char *argv[]) {
  163. if (argc < 2) {
  164. fprintf(stderr, "Usage: conformance-test-runner <test-program>\n");
  165. exit(1);
  166. }
  167. ForkPipeRunner runner(argv[1]);
  168. google::protobuf::ConformanceTestSuite suite;
  169. std::string output;
  170. suite.RunSuite(&runner, &output);
  171. fwrite(output.c_str(), 1, output.size(), stderr);
  172. }