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

Side by Side Diff: third_party/protobuf/conformance/conformance_cpp.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 #include <errno.h>
32 #include <stdarg.h>
33 #include <unistd.h>
34
35 #include "conformance.pb.h"
36 #include <google/protobuf/util/json_util.h>
37 #include <google/protobuf/util/type_resolver_util.h>
38
39 using conformance::ConformanceRequest;
40 using conformance::ConformanceResponse;
41 using conformance::TestAllTypes;
42 using google::protobuf::Descriptor;
43 using google::protobuf::DescriptorPool;
44 using google::protobuf::internal::scoped_ptr;
45 using google::protobuf::util::BinaryToJsonString;
46 using google::protobuf::util::JsonToBinaryString;
47 using google::protobuf::util::NewTypeResolverForDescriptorPool;
48 using google::protobuf::util::Status;
49 using google::protobuf::util::TypeResolver;
50 using std::string;
51
52 static const char kTypeUrlPrefix[] = "type.googleapis.com";
53
54 static string GetTypeUrl(const Descriptor* message) {
55 return string(kTypeUrlPrefix) + "/" + message->full_name();
56 }
57
58 int test_count = 0;
59 bool verbose = false;
60 TypeResolver* type_resolver;
61 string* type_url;
62
63
64 bool CheckedRead(int fd, void *buf, size_t len) {
65 size_t ofs = 0;
66 while (len > 0) {
67 ssize_t bytes_read = read(fd, (char*)buf + ofs, len);
68
69 if (bytes_read == 0) return false;
70
71 if (bytes_read < 0) {
72 GOOGLE_LOG(FATAL) << "Error reading from test runner: " << strerror(errno );
73 }
74
75 len -= bytes_read;
76 ofs += bytes_read;
77 }
78
79 return true;
80 }
81
82 void CheckedWrite(int fd, const void *buf, size_t len) {
83 if (write(fd, buf, len) != len) {
84 GOOGLE_LOG(FATAL) << "Error writing to test runner: " << strerror(errno);
85 }
86 }
87
88 void DoTest(const ConformanceRequest& request, ConformanceResponse* response) {
89 TestAllTypes test_message;
90
91 switch (request.payload_case()) {
92 case ConformanceRequest::kProtobufPayload:
93 if (!test_message.ParseFromString(request.protobuf_payload())) {
94 // Getting parse details would involve something like:
95 // http://stackoverflow.com/questions/22121922/how-can-i-get-more-deta ils-about-errors-generated-during-protobuf-parsing-c
96 response->set_parse_error("Parse error (no more details available).");
97 return;
98 }
99 break;
100
101 case ConformanceRequest::kJsonPayload: {
102 string proto_binary;
103 Status status = JsonToBinaryString(type_resolver, *type_url,
104 request.json_payload(), &proto_binary);
105 if (!status.ok()) {
106 response->set_parse_error(string("Parse error: ") +
107 status.error_message().as_string());
108 return;
109 }
110
111 GOOGLE_CHECK(test_message.ParseFromString(proto_binary));
112 break;
113 }
114
115 case ConformanceRequest::PAYLOAD_NOT_SET:
116 GOOGLE_LOG(FATAL) << "Request didn't have payload.";
117 break;
118 }
119
120 switch (request.requested_output_format()) {
121 case conformance::UNSPECIFIED:
122 GOOGLE_LOG(FATAL) << "Unspecified output format";
123 break;
124
125 case conformance::PROTOBUF:
126 GOOGLE_CHECK(
127 test_message.SerializeToString(response->mutable_protobuf_payload()));
128 break;
129
130 case conformance::JSON: {
131 string proto_binary;
132 GOOGLE_CHECK(test_message.SerializeToString(&proto_binary));
133 Status status = BinaryToJsonString(type_resolver, *type_url, proto_binary,
134 response->mutable_json_payload());
135 GOOGLE_CHECK(status.ok());
136 break;
137 }
138 }
139 }
140
141 bool DoTestIo() {
142 string serialized_input;
143 string serialized_output;
144 ConformanceRequest request;
145 ConformanceResponse response;
146 uint32_t bytes;
147
148 if (!CheckedRead(STDIN_FILENO, &bytes, sizeof(uint32_t))) {
149 // EOF.
150 return false;
151 }
152
153 serialized_input.resize(bytes);
154
155 if (!CheckedRead(STDIN_FILENO, (char*)serialized_input.c_str(), bytes)) {
156 GOOGLE_LOG(ERROR) << "Unexpected EOF on stdin. " << strerror(errno);
157 }
158
159 if (!request.ParseFromString(serialized_input)) {
160 GOOGLE_LOG(FATAL) << "Parse of ConformanceRequest proto failed.";
161 return false;
162 }
163
164 DoTest(request, &response);
165
166 response.SerializeToString(&serialized_output);
167
168 bytes = serialized_output.size();
169 CheckedWrite(STDOUT_FILENO, &bytes, sizeof(uint32_t));
170 CheckedWrite(STDOUT_FILENO, serialized_output.c_str(), bytes);
171
172 if (verbose) {
173 fprintf(stderr, "conformance-cpp: request=%s, response=%s\n",
174 request.ShortDebugString().c_str(),
175 response.ShortDebugString().c_str());
176 }
177
178 test_count++;
179
180 return true;
181 }
182
183 int main() {
184 type_resolver = NewTypeResolverForDescriptorPool(
185 kTypeUrlPrefix, DescriptorPool::generated_pool());
186 type_url = new string(GetTypeUrl(TestAllTypes::descriptor()));
187 while (1) {
188 if (!DoTestIo()) {
189 fprintf(stderr, "conformance-cpp: received EOF from test runner "
190 "after %d tests, exiting\n", test_count);
191 return 0;
192 }
193 }
194 }
OLDNEW
« no previous file with comments | « third_party/protobuf/conformance/conformance.proto ('k') | third_party/protobuf/conformance/conformance_ruby.rb » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698