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