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

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

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