Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: third_party/protobuf/conformance/conformance_test_runner.cc

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

Powered by Google App Engine
This is Rietveld 408576698