conformance_python.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!/usr/bin/env python
  2. # Protocol Buffers - Google's data interchange format
  3. # Copyright 2008 Google Inc. All rights reserved.
  4. # https://developers.google.com/protocol-buffers/
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. #
  10. # * Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # * Redistributions in binary form must reproduce the above
  13. # copyright notice, this list of conditions and the following disclaimer
  14. # in the documentation and/or other materials provided with the
  15. # distribution.
  16. # * Neither the name of Google Inc. nor the names of its
  17. # contributors may be used to endorse or promote products derived from
  18. # this software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. """A conformance test implementation for the Python protobuf library.
  32. See conformance.proto for more information.
  33. """
  34. import struct
  35. import sys
  36. import os
  37. from google.protobuf import json_format
  38. from google.protobuf import message
  39. from google.protobuf import test_messages_proto3_pb2
  40. from google.protobuf import test_messages_proto2_pb2
  41. from google.protobuf import text_format
  42. import conformance_pb2
  43. sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0)
  44. sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0)
  45. test_count = 0
  46. verbose = False
  47. class ProtocolError(Exception):
  48. pass
  49. def do_test(request):
  50. response = conformance_pb2.ConformanceResponse()
  51. if request.message_type == "conformance.FailureSet":
  52. failure_set = conformance_pb2.FailureSet()
  53. failures = []
  54. # TODO(gerbens): Remove, this is a hack to detect if the old vs new
  55. # parser is used by the cpp code. Relying on a bug in the old parser.
  56. hack_proto = test_messages_proto2_pb2.TestAllTypesProto2()
  57. old_parser = True
  58. try:
  59. hack_proto.ParseFromString(b"\322\002\001")
  60. except message.DecodeError as e:
  61. old_parser = False
  62. if old_parser:
  63. # the string above is one of the failing conformance test strings of the
  64. # old parser. If we succeed the c++ implementation is using the old
  65. # parser so we add the list of failing conformance tests.
  66. failures = [
  67. "Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE",
  68. "Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE",
  69. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.BOOL",
  70. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.DOUBLE",
  71. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.ENUM",
  72. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.FIXED32",
  73. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.FIXED64",
  74. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.FLOAT",
  75. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT32",
  76. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT64",
  77. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SFIXED32",
  78. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SFIXED64",
  79. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT32",
  80. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT64",
  81. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT32",
  82. "Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT64",
  83. "Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE",
  84. "Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE",
  85. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.BOOL",
  86. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.DOUBLE",
  87. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.ENUM",
  88. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.FIXED32",
  89. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.FIXED64",
  90. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.FLOAT",
  91. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT32",
  92. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT64",
  93. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SFIXED32",
  94. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SFIXED64",
  95. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT32",
  96. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT64",
  97. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT32",
  98. "Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT64",
  99. ]
  100. for x in failures:
  101. failure_set.failure.append(x)
  102. response.protobuf_payload = failure_set.SerializeToString()
  103. return response
  104. isProto3 = (request.message_type == "protobuf_test_messages.proto3.TestAllTypesProto3")
  105. isJson = (request.WhichOneof('payload') == 'json_payload')
  106. isProto2 = (request.message_type == "protobuf_test_messages.proto2.TestAllTypesProto2")
  107. if (not isProto3) and (not isJson) and (not isProto2):
  108. raise ProtocolError("Protobuf request doesn't have specific payload type")
  109. test_message = test_messages_proto2_pb2.TestAllTypesProto2() if isProto2 else \
  110. test_messages_proto3_pb2.TestAllTypesProto3()
  111. try:
  112. if request.WhichOneof('payload') == 'protobuf_payload':
  113. try:
  114. test_message.ParseFromString(request.protobuf_payload)
  115. except message.DecodeError as e:
  116. response.parse_error = str(e)
  117. return response
  118. elif request.WhichOneof('payload') == 'json_payload':
  119. try:
  120. ignore_unknown_fields = \
  121. request.test_category == \
  122. conformance_pb2.JSON_IGNORE_UNKNOWN_PARSING_TEST
  123. json_format.Parse(request.json_payload, test_message,
  124. ignore_unknown_fields)
  125. except Exception as e:
  126. response.parse_error = str(e)
  127. return response
  128. elif request.WhichOneof('payload') == 'text_payload':
  129. try:
  130. text_format.Parse(request.text_payload, test_message)
  131. except Exception as e:
  132. response.parse_error = str(e)
  133. return response
  134. else:
  135. raise ProtocolError("Request didn't have payload.")
  136. if request.requested_output_format == conformance_pb2.UNSPECIFIED:
  137. raise ProtocolError("Unspecified output format")
  138. elif request.requested_output_format == conformance_pb2.PROTOBUF:
  139. response.protobuf_payload = test_message.SerializeToString()
  140. elif request.requested_output_format == conformance_pb2.JSON:
  141. try:
  142. response.json_payload = json_format.MessageToJson(test_message)
  143. except Exception as e:
  144. response.serialize_error = str(e)
  145. return response
  146. elif request.requested_output_format == conformance_pb2.TEXT_FORMAT:
  147. response.text_payload = text_format.MessageToString(
  148. test_message, print_unknown_fields=request.print_unknown_fields)
  149. except Exception as e:
  150. response.runtime_error = str(e)
  151. return response
  152. def do_test_io():
  153. length_bytes = sys.stdin.read(4)
  154. if len(length_bytes) == 0:
  155. return False # EOF
  156. elif len(length_bytes) != 4:
  157. raise IOError("I/O error")
  158. # "I" is "unsigned int", so this depends on running on a platform with
  159. # 32-bit "unsigned int" type. The Python struct module unfortunately
  160. # has no format specifier for uint32_t.
  161. length = struct.unpack("<I", length_bytes)[0]
  162. serialized_request = sys.stdin.read(length)
  163. if len(serialized_request) != length:
  164. raise IOError("I/O error")
  165. request = conformance_pb2.ConformanceRequest()
  166. request.ParseFromString(serialized_request)
  167. response = do_test(request)
  168. serialized_response = response.SerializeToString()
  169. sys.stdout.write(struct.pack("<I", len(serialized_response)))
  170. sys.stdout.write(serialized_response)
  171. sys.stdout.flush()
  172. if verbose:
  173. sys.stderr.write("conformance_python: request=%s, response=%s\n" % (
  174. request.ShortDebugString().c_str(),
  175. response.ShortDebugString().c_str()))
  176. global test_count
  177. test_count += 1
  178. return True
  179. while True:
  180. if not do_test_io():
  181. sys.stderr.write("conformance_python: received EOF from test runner " +
  182. "after %s tests, exiting\n" % (test_count))
  183. sys.exit(0)