conformance_cpp.cc 7.7 KB

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