conformance_cpp.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include <errno.h>
  31. #include <stdarg.h>
  32. #include <unistd.h>
  33. #include <google/protobuf/message.h>
  34. #include <google/protobuf/text_format.h>
  35. #include <google/protobuf/util/json_util.h>
  36. #include <google/protobuf/util/type_resolver_util.h>
  37. #include <google/protobuf/stubs/status.h>
  38. #include "conformance.pb.h"
  39. #include <google/protobuf/test_messages_proto2.pb.h>
  40. #include <google/protobuf/test_messages_proto3.pb.h>
  41. #include <google/protobuf/stubs/status.h>
  42. using conformance::ConformanceRequest;
  43. using conformance::ConformanceResponse;
  44. using google::protobuf::Descriptor;
  45. using google::protobuf::DescriptorPool;
  46. using google::protobuf::Message;
  47. using google::protobuf::MessageFactory;
  48. using google::protobuf::TextFormat;
  49. using google::protobuf::util::BinaryToJsonString;
  50. using google::protobuf::util::JsonParseOptions;
  51. using google::protobuf::util::JsonToBinaryString;
  52. using google::protobuf::util::NewTypeResolverForDescriptorPool;
  53. using google::protobuf::util::TypeResolver;
  54. using protobuf_test_messages::proto3::TestAllTypesProto3;
  55. using std::string;
  56. static const char kTypeUrlPrefix[] = "type.googleapis.com";
  57. const char* kFailures[] = {
  58. };
  59. static string GetTypeUrl(const Descriptor* message) {
  60. return string(kTypeUrlPrefix) + "/" + message->full_name();
  61. }
  62. int test_count = 0;
  63. bool verbose = false;
  64. TypeResolver* type_resolver;
  65. string* type_url;
  66. namespace google {
  67. namespace protobuf {
  68. using util::Status;
  69. bool CheckedRead(int fd, void *buf, size_t len) {
  70. size_t ofs = 0;
  71. while (len > 0) {
  72. ssize_t bytes_read = read(fd, (char*)buf + ofs, len);
  73. if (bytes_read == 0) return false;
  74. if (bytes_read < 0) {
  75. GOOGLE_LOG(FATAL) << "Error reading from test runner: " << strerror(errno);
  76. }
  77. len -= bytes_read;
  78. ofs += bytes_read;
  79. }
  80. return true;
  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. void DoTest(const ConformanceRequest& request, ConformanceResponse* response) {
  88. Message *test_message;
  89. const Descriptor *descriptor = DescriptorPool::generated_pool()->FindMessageTypeByName(
  90. request.message_type());
  91. if (!descriptor) {
  92. GOOGLE_LOG(FATAL) << "No such message type: " << request.message_type();
  93. }
  94. test_message = MessageFactory::generated_factory()->GetPrototype(descriptor)->New();
  95. switch (request.payload_case()) {
  96. case ConformanceRequest::kProtobufPayload: {
  97. if (!test_message->ParseFromString(request.protobuf_payload())) {
  98. // Getting parse details would involve something like:
  99. // http://stackoverflow.com/questions/22121922/how-can-i-get-more-details-about-errors-generated-during-protobuf-parsing-c
  100. response->set_parse_error("Parse error (no more details available).");
  101. return;
  102. }
  103. break;
  104. }
  105. case ConformanceRequest::kJsonPayload: {
  106. string proto_binary;
  107. JsonParseOptions options;
  108. options.ignore_unknown_fields =
  109. (request.test_category() ==
  110. conformance::JSON_IGNORE_UNKNOWN_PARSING_TEST);
  111. util::Status status =
  112. JsonToBinaryString(type_resolver, *type_url, request.json_payload(),
  113. &proto_binary, options);
  114. if (!status.ok()) {
  115. response->set_parse_error(string("Parse error: ") +
  116. std::string(status.error_message()));
  117. return;
  118. }
  119. if (!test_message->ParseFromString(proto_binary)) {
  120. response->set_runtime_error(
  121. "Parsing JSON generates invalid proto output.");
  122. return;
  123. }
  124. break;
  125. }
  126. case ConformanceRequest::kTextPayload: {
  127. if (!TextFormat::ParseFromString(request.text_payload(), test_message)) {
  128. response->set_parse_error("Parse error");
  129. return;
  130. }
  131. break;
  132. }
  133. case ConformanceRequest::PAYLOAD_NOT_SET:
  134. GOOGLE_LOG(FATAL) << "Request didn't have payload.";
  135. break;
  136. default:
  137. GOOGLE_LOG(FATAL) << "unknown payload type: " << request.payload_case();
  138. break;
  139. }
  140. conformance::FailureSet failures;
  141. if (descriptor == failures.GetDescriptor()) {
  142. for (const char* s : kFailures) failures.add_failure(s);
  143. test_message = &failures;
  144. }
  145. switch (request.requested_output_format()) {
  146. case conformance::UNSPECIFIED:
  147. GOOGLE_LOG(FATAL) << "Unspecified output format";
  148. break;
  149. case conformance::PROTOBUF: {
  150. GOOGLE_CHECK(test_message->SerializeToString(
  151. response->mutable_protobuf_payload()));
  152. break;
  153. }
  154. case conformance::JSON: {
  155. string proto_binary;
  156. GOOGLE_CHECK(test_message->SerializeToString(&proto_binary));
  157. util::Status status =
  158. BinaryToJsonString(type_resolver, *type_url, proto_binary,
  159. response->mutable_json_payload());
  160. if (!status.ok()) {
  161. response->set_serialize_error(
  162. string("Failed to serialize JSON output: ") +
  163. std::string(status.error_message()));
  164. return;
  165. }
  166. break;
  167. }
  168. case conformance::TEXT_FORMAT: {
  169. TextFormat::Printer printer;
  170. printer.SetHideUnknownFields(!request.print_unknown_fields());
  171. GOOGLE_CHECK(printer.PrintToString(*test_message,
  172. response->mutable_text_payload()));
  173. break;
  174. }
  175. default:
  176. GOOGLE_LOG(FATAL) << "Unknown output format: "
  177. << request.requested_output_format();
  178. }
  179. }
  180. bool DoTestIo() {
  181. string serialized_input;
  182. string serialized_output;
  183. ConformanceRequest request;
  184. ConformanceResponse response;
  185. uint32_t bytes;
  186. if (!CheckedRead(STDIN_FILENO, &bytes, sizeof(uint32_t))) {
  187. // EOF.
  188. return false;
  189. }
  190. serialized_input.resize(bytes);
  191. if (!CheckedRead(STDIN_FILENO, (char*)serialized_input.c_str(), bytes)) {
  192. GOOGLE_LOG(ERROR) << "Unexpected EOF on stdin. " << strerror(errno);
  193. }
  194. if (!request.ParseFromString(serialized_input)) {
  195. GOOGLE_LOG(FATAL) << "Parse of ConformanceRequest proto failed.";
  196. return false;
  197. }
  198. DoTest(request, &response);
  199. response.SerializeToString(&serialized_output);
  200. bytes = serialized_output.size();
  201. CheckedWrite(STDOUT_FILENO, &bytes, sizeof(uint32_t));
  202. CheckedWrite(STDOUT_FILENO, serialized_output.c_str(), bytes);
  203. if (verbose) {
  204. fprintf(stderr, "conformance-cpp: request=%s, response=%s\n",
  205. request.ShortDebugString().c_str(),
  206. response.ShortDebugString().c_str());
  207. }
  208. test_count++;
  209. return true;
  210. }
  211. } // namespace protobuf
  212. } // namespace google
  213. int main() {
  214. type_resolver = NewTypeResolverForDescriptorPool(
  215. kTypeUrlPrefix, DescriptorPool::generated_pool());
  216. type_url = new string(GetTypeUrl(TestAllTypesProto3::descriptor()));
  217. while (1) {
  218. if (!google::protobuf::DoTestIo()) {
  219. fprintf(stderr, "conformance-cpp: received EOF from test runner "
  220. "after %d tests, exiting\n", test_count);
  221. return 0;
  222. }
  223. }
  224. }