Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 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 | |
Yilun Chong | 020a24d | 2017-06-29 11:33:22 -0700 | [diff] [blame] | 35 | #include <google/protobuf/message.h> |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 36 | #include <google/protobuf/text_format.h> |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 37 | #include <google/protobuf/util/json_util.h> |
| 38 | #include <google/protobuf/util/type_resolver_util.h> |
Joshua Haberman | 5c028d6 | 2020-11-20 10:44:58 -0800 | [diff] [blame] | 39 | #include <google/protobuf/stubs/status.h> |
Rafi Kamal | 58d4420 | 2019-11-11 17:06:56 -0800 | [diff] [blame] | 40 | #include "conformance.pb.h" |
| 41 | #include <google/protobuf/test_messages_proto2.pb.h> |
| 42 | #include <google/protobuf/test_messages_proto3.pb.h> |
| 43 | #include <google/protobuf/stubs/status.h> |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 44 | |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 45 | using conformance::ConformanceRequest; |
| 46 | using conformance::ConformanceResponse; |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 47 | using google::protobuf::Descriptor; |
| 48 | using google::protobuf::DescriptorPool; |
Yilun Chong | 020a24d | 2017-06-29 11:33:22 -0700 | [diff] [blame] | 49 | using google::protobuf::Message; |
Yilun Chong | 3adb054 | 2017-06-30 17:22:32 -0700 | [diff] [blame] | 50 | using google::protobuf::MessageFactory; |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 51 | using google::protobuf::TextFormat; |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 52 | using google::protobuf::util::BinaryToJsonString; |
Feng Xiao | 6bbe197 | 2018-08-08 17:00:41 -0700 | [diff] [blame] | 53 | using google::protobuf::util::JsonParseOptions; |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 54 | using google::protobuf::util::JsonToBinaryString; |
| 55 | using google::protobuf::util::NewTypeResolverForDescriptorPool; |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 56 | using google::protobuf::util::TypeResolver; |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 57 | using protobuf_test_messages::proto3::TestAllTypesProto3; |
Joshua Haberman | 9df4275 | 2021-02-26 10:37:37 -0800 | [diff] [blame] | 58 | using protobuf_test_messages::proto2::TestAllTypesProto2; |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 59 | using std::string; |
| 60 | |
| 61 | static const char kTypeUrlPrefix[] = "type.googleapis.com"; |
| 62 | |
Yilun Chong | 0adb74c | 2019-01-08 15:06:30 -0800 | [diff] [blame] | 63 | const char* kFailures[] = { |
Yilun Chong | 0adb74c | 2019-01-08 15:06:30 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 66 | static string GetTypeUrl(const Descriptor* message) { |
| 67 | return string(kTypeUrlPrefix) + "/" + message->full_name(); |
| 68 | } |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 69 | |
| 70 | int test_count = 0; |
| 71 | bool verbose = false; |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 72 | TypeResolver* type_resolver; |
| 73 | string* type_url; |
| 74 | |
Rafi Kamal | 58d4420 | 2019-11-11 17:06:56 -0800 | [diff] [blame] | 75 | namespace google { |
| 76 | namespace protobuf { |
| 77 | |
| 78 | using util::Status; |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 79 | |
| 80 | bool CheckedRead(int fd, void *buf, size_t len) { |
| 81 | size_t ofs = 0; |
| 82 | while (len > 0) { |
| 83 | ssize_t bytes_read = read(fd, (char*)buf + ofs, len); |
| 84 | |
| 85 | if (bytes_read == 0) return false; |
| 86 | |
| 87 | if (bytes_read < 0) { |
Rafi Kamal | 58d4420 | 2019-11-11 17:06:56 -0800 | [diff] [blame] | 88 | GOOGLE_LOG(FATAL) << "Error reading from test runner: " << strerror(errno); |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | len -= bytes_read; |
| 92 | ofs += bytes_read; |
| 93 | } |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | void CheckedWrite(int fd, const void *buf, size_t len) { |
| 99 | if (write(fd, buf, len) != len) { |
| 100 | GOOGLE_LOG(FATAL) << "Error writing to test runner: " << strerror(errno); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void DoTest(const ConformanceRequest& request, ConformanceResponse* response) { |
Yilun Chong | 020a24d | 2017-06-29 11:33:22 -0700 | [diff] [blame] | 105 | Message *test_message; |
Joshua Haberman | 9df4275 | 2021-02-26 10:37:37 -0800 | [diff] [blame] | 106 | google::protobuf::LinkMessageReflection<TestAllTypesProto2>(); |
| 107 | google::protobuf::LinkMessageReflection<TestAllTypesProto3>(); |
Yilun Chong | 3adb054 | 2017-06-30 17:22:32 -0700 | [diff] [blame] | 108 | const Descriptor *descriptor = DescriptorPool::generated_pool()->FindMessageTypeByName( |
| 109 | request.message_type()); |
| 110 | if (!descriptor) { |
| 111 | GOOGLE_LOG(FATAL) << "No such message type: " << request.message_type(); |
Yilun Chong | 020a24d | 2017-06-29 11:33:22 -0700 | [diff] [blame] | 112 | } |
Yilun Chong | 3adb054 | 2017-06-30 17:22:32 -0700 | [diff] [blame] | 113 | test_message = MessageFactory::generated_factory()->GetPrototype(descriptor)->New(); |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 114 | |
| 115 | switch (request.payload_case()) { |
Yilun Chong | 18a0c2c | 2017-06-27 18:24:15 -0700 | [diff] [blame] | 116 | case ConformanceRequest::kProtobufPayload: { |
Yilun Chong | 020a24d | 2017-06-29 11:33:22 -0700 | [diff] [blame] | 117 | if (!test_message->ParseFromString(request.protobuf_payload())) { |
| 118 | // Getting parse details would involve something like: |
| 119 | // http://stackoverflow.com/questions/22121922/how-can-i-get-more-details-about-errors-generated-during-protobuf-parsing-c |
| 120 | response->set_parse_error("Parse error (no more details available)."); |
| 121 | return; |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 122 | } |
| 123 | break; |
Yilun Chong | 18a0c2c | 2017-06-27 18:24:15 -0700 | [diff] [blame] | 124 | } |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 125 | |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 126 | case ConformanceRequest::kJsonPayload: { |
| 127 | string proto_binary; |
Feng Xiao | 6bbe197 | 2018-08-08 17:00:41 -0700 | [diff] [blame] | 128 | JsonParseOptions options; |
| 129 | options.ignore_unknown_fields = |
| 130 | (request.test_category() == |
| 131 | conformance::JSON_IGNORE_UNKNOWN_PARSING_TEST); |
Joshua Haberman | 5c028d6 | 2020-11-20 10:44:58 -0800 | [diff] [blame] | 132 | util::Status status = |
| 133 | JsonToBinaryString(type_resolver, *type_url, request.json_payload(), |
| 134 | &proto_binary, options); |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 135 | if (!status.ok()) { |
| 136 | response->set_parse_error(string("Parse error: ") + |
Yannic Bonenberger | aa5cb98 | 2021-03-10 10:05:31 +0100 | [diff] [blame] | 137 | std::string(status.message())); |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
Yilun Chong | 020a24d | 2017-06-29 11:33:22 -0700 | [diff] [blame] | 141 | if (!test_message->ParseFromString(proto_binary)) { |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 142 | response->set_runtime_error( |
| 143 | "Parsing JSON generates invalid proto output."); |
| 144 | return; |
| 145 | } |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 146 | break; |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 147 | } |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 148 | |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 149 | case ConformanceRequest::kTextPayload: { |
| 150 | if (!TextFormat::ParseFromString(request.text_payload(), test_message)) { |
| 151 | response->set_parse_error("Parse error"); |
| 152 | return; |
| 153 | } |
| 154 | break; |
| 155 | } |
| 156 | |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 157 | case ConformanceRequest::PAYLOAD_NOT_SET: |
| 158 | GOOGLE_LOG(FATAL) << "Request didn't have payload."; |
| 159 | break; |
Paul Yang | cecba29 | 2018-12-14 16:05:03 -0800 | [diff] [blame] | 160 | |
| 161 | default: |
Rafi Kamal | 58d4420 | 2019-11-11 17:06:56 -0800 | [diff] [blame] | 162 | GOOGLE_LOG(FATAL) << "unknown payload type: " << request.payload_case(); |
Paul Yang | cecba29 | 2018-12-14 16:05:03 -0800 | [diff] [blame] | 163 | break; |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Yilun Chong | 0adb74c | 2019-01-08 15:06:30 -0800 | [diff] [blame] | 166 | conformance::FailureSet failures; |
| 167 | if (descriptor == failures.GetDescriptor()) { |
| 168 | for (const char* s : kFailures) failures.add_failure(s); |
| 169 | test_message = &failures; |
| 170 | } |
| 171 | |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 172 | switch (request.requested_output_format()) { |
| 173 | case conformance::UNSPECIFIED: |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 174 | GOOGLE_LOG(FATAL) << "Unspecified output format"; |
| 175 | break; |
| 176 | |
Yilun Chong | 18a0c2c | 2017-06-27 18:24:15 -0700 | [diff] [blame] | 177 | case conformance::PROTOBUF: { |
Rafi Kamal | 58d4420 | 2019-11-11 17:06:56 -0800 | [diff] [blame] | 178 | GOOGLE_CHECK(test_message->SerializeToString( |
| 179 | response->mutable_protobuf_payload())); |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 180 | break; |
Yilun Chong | 18a0c2c | 2017-06-27 18:24:15 -0700 | [diff] [blame] | 181 | } |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 182 | |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 183 | case conformance::JSON: { |
| 184 | string proto_binary; |
Yilun Chong | 020a24d | 2017-06-29 11:33:22 -0700 | [diff] [blame] | 185 | GOOGLE_CHECK(test_message->SerializeToString(&proto_binary)); |
Joshua Haberman | 5c028d6 | 2020-11-20 10:44:58 -0800 | [diff] [blame] | 186 | util::Status status = |
| 187 | BinaryToJsonString(type_resolver, *type_url, proto_binary, |
| 188 | response->mutable_json_payload()); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 189 | if (!status.ok()) { |
| 190 | response->set_serialize_error( |
| 191 | string("Failed to serialize JSON output: ") + |
Yannic Bonenberger | aa5cb98 | 2021-03-10 10:05:31 +0100 | [diff] [blame] | 192 | std::string(status.message())); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 193 | return; |
| 194 | } |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 195 | break; |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 196 | } |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 197 | |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 198 | case conformance::TEXT_FORMAT: { |
Hao Nguyen | 2f864fd | 2019-03-20 11:45:01 -0700 | [diff] [blame] | 199 | TextFormat::Printer printer; |
| 200 | printer.SetHideUnknownFields(!request.print_unknown_fields()); |
| 201 | GOOGLE_CHECK(printer.PrintToString(*test_message, |
Rafi Kamal | 58d4420 | 2019-11-11 17:06:56 -0800 | [diff] [blame] | 202 | response->mutable_text_payload())); |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 203 | break; |
| 204 | } |
| 205 | |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 206 | default: |
| 207 | GOOGLE_LOG(FATAL) << "Unknown output format: " |
Rafi Kamal | 58d4420 | 2019-11-11 17:06:56 -0800 | [diff] [blame] | 208 | << request.requested_output_format(); |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | bool DoTestIo() { |
| 213 | string serialized_input; |
| 214 | string serialized_output; |
| 215 | ConformanceRequest request; |
| 216 | ConformanceResponse response; |
| 217 | uint32_t bytes; |
| 218 | |
| 219 | if (!CheckedRead(STDIN_FILENO, &bytes, sizeof(uint32_t))) { |
| 220 | // EOF. |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | serialized_input.resize(bytes); |
| 225 | |
| 226 | if (!CheckedRead(STDIN_FILENO, (char*)serialized_input.c_str(), bytes)) { |
| 227 | GOOGLE_LOG(ERROR) << "Unexpected EOF on stdin. " << strerror(errno); |
| 228 | } |
| 229 | |
| 230 | if (!request.ParseFromString(serialized_input)) { |
| 231 | GOOGLE_LOG(FATAL) << "Parse of ConformanceRequest proto failed."; |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | DoTest(request, &response); |
| 236 | |
| 237 | response.SerializeToString(&serialized_output); |
| 238 | |
| 239 | bytes = serialized_output.size(); |
| 240 | CheckedWrite(STDOUT_FILENO, &bytes, sizeof(uint32_t)); |
| 241 | CheckedWrite(STDOUT_FILENO, serialized_output.c_str(), bytes); |
| 242 | |
| 243 | if (verbose) { |
| 244 | fprintf(stderr, "conformance-cpp: request=%s, response=%s\n", |
| 245 | request.ShortDebugString().c_str(), |
| 246 | response.ShortDebugString().c_str()); |
| 247 | } |
| 248 | |
| 249 | test_count++; |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | |
Rafi Kamal | 58d4420 | 2019-11-11 17:06:56 -0800 | [diff] [blame] | 254 | } // namespace protobuf |
| 255 | } // namespace google |
| 256 | |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 257 | int main() { |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 258 | type_resolver = NewTypeResolverForDescriptorPool( |
| 259 | kTypeUrlPrefix, DescriptorPool::generated_pool()); |
Yilun Chong | 3adb054 | 2017-06-30 17:22:32 -0700 | [diff] [blame] | 260 | type_url = new string(GetTypeUrl(TestAllTypesProto3::descriptor())); |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 261 | while (1) { |
Rafi Kamal | 58d4420 | 2019-11-11 17:06:56 -0800 | [diff] [blame] | 262 | if (!google::protobuf::DoTestIo()) { |
Josh Haberman | 35a1cc7 | 2015-04-01 17:23:48 -0700 | [diff] [blame] | 263 | fprintf(stderr, "conformance-cpp: received EOF from test runner " |
| 264 | "after %d tests, exiting\n", test_count); |
| 265 | return 0; |
| 266 | } |
| 267 | } |
| 268 | } |