conformance_cpp.cc 7.2 KB

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