Debian Codegraph @

extra/protobuf/protobuf-24.4/conformance/conformance_python.py

raw
1 #!/usr/bin/env python3
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
32 """A conformance test implementation for the Python protobuf library.
33
34 See conformance.proto for more information.
35 """
36
37 import struct
38 import sys
39 from google.protobuf import json_format
40 from google.protobuf import message
41 from google.protobuf import text_format
42 from google.protobuf import test_messages_proto2_pb2
43 from google.protobuf import test_messages_proto3_pb2
44 from conformance import conformance_pb2
45
46 test_count = 0
47 verbose = False
48
49
50 class ProtocolError(Exception):
51 pass
52
53
54 def do_test(request):
55 response = conformance_pb2.ConformanceResponse()
56
57 if request.message_type == "conformance.FailureSet":
58 failure_set = conformance_pb2.FailureSet()
59 failures = []
60 # TODO(gerbens): Remove, this is a hack to detect if the old vs new
61 # parser is used by the cpp code. Relying on a bug in the old parser.
62 hack_proto = test_messages_proto2_pb2.TestAllTypesProto2()
63 old_parser = True
64 try:
65 hack_proto.ParseFromString(b"\322\002\001")
66 except message.DecodeError as e:
67 old_parser = False
68 if old_parser:
69 # the string above is one of the failing conformance test strings of the
70 # old parser. If we succeed the c++ implementation is using the old
71 # parser so we add the list of failing conformance tests.
72 failures = [
73 "Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE",
74 "Required.Proto3.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE",
75 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.BOOL",
76 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.DOUBLE",
77 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.ENUM",
78 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.FIXED32",
79 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.FIXED64",
80 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.FLOAT",
81 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT32",
82 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.INT64",
83 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SFIXED32",
84 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SFIXED64",
85 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT32",
86 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.SINT64",
87 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT32",
88 "Required.Proto3.ProtobufInput.PrematureEofInPackedField.UINT64",
89 "Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownNonRepeatedValue.MESSAGE",
90 "Required.Proto2.ProtobufInput.PrematureEofInDelimitedDataForKnownRepeatedValue.MESSAGE",
91 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.BOOL",
92 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.DOUBLE",
93 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.ENUM",
94 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.FIXED32",
95 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.FIXED64",
96 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.FLOAT",
97 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT32",
98 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.INT64",
99 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SFIXED32",
100 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SFIXED64",
101 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT32",
102 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.SINT64",
103 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT32",
104 "Required.Proto2.ProtobufInput.PrematureEofInPackedField.UINT64",
105 ]
106 for x in failures:
107 failure_set.failure.append(x)
108 response.protobuf_payload = failure_set.SerializeToString()
109 return response
110
111 isProto3 = (request.message_type == "protobuf_test_messages.proto3.TestAllTypesProto3")
112 isJson = (request.WhichOneof('payload') == 'json_payload')
113 isProto2 = (request.message_type == "protobuf_test_messages.proto2.TestAllTypesProto2")
114
115 if (not isProto3) and (not isJson) and (not isProto2):
116 raise ProtocolError("Protobuf request doesn't have specific payload type")
117
118 test_message = test_messages_proto2_pb2.TestAllTypesProto2() if isProto2 else \
119 test_messages_proto3_pb2.TestAllTypesProto3()
120
121 try:
122 if request.WhichOneof('payload') == 'protobuf_payload':
123 try:
124 test_message.ParseFromString(request.protobuf_payload)
125 except message.DecodeError as e:
126 response.parse_error = str(e)
127 return response
128
129 elif request.WhichOneof('payload') == 'json_payload':
130 try:
131 ignore_unknown_fields = \
132 request.test_category == \
133 conformance_pb2.JSON_IGNORE_UNKNOWN_PARSING_TEST
134 json_format.Parse(request.json_payload, test_message,
135 ignore_unknown_fields)
136 except Exception as e:
137 response.parse_error = str(e)
138 return response
139
140 elif request.WhichOneof('payload') == 'text_payload':
141 try:
142 text_format.Parse(request.text_payload, test_message)
143 except Exception as e:
144 response.parse_error = str(e)
145 return response
146
147 else:
148 raise ProtocolError("Request didn't have payload.")
149
150 if request.requested_output_format == conformance_pb2.UNSPECIFIED:
151 raise ProtocolError("Unspecified output format")
152
153 elif request.requested_output_format == conformance_pb2.PROTOBUF:
154 response.protobuf_payload = test_message.SerializeToString()
155
156 elif request.requested_output_format == conformance_pb2.JSON:
157 try:
158 response.json_payload = json_format.MessageToJson(test_message)
159 except Exception as e:
160 response.serialize_error = str(e)
161 return response
162
163 elif request.requested_output_format == conformance_pb2.TEXT_FORMAT:
164 response.text_payload = text_format.MessageToString(
165 test_message, print_unknown_fields=request.print_unknown_fields)
166
167 except Exception as e:
168 response.runtime_error = str(e)
169
170 return response
171
172
173 def do_test_io():
174 length_bytes = sys.stdin.buffer.read(4)
175 if len(length_bytes) == 0:
176 return False # EOF
177 elif len(length_bytes) != 4:
178 raise IOError("I/O error")
179
180 length = struct.unpack("<I", length_bytes)[0]
181 serialized_request = sys.stdin.buffer.read(length)
182 if len(serialized_request) != length:
183 raise IOError("I/O error")
184
185 request = conformance_pb2.ConformanceRequest()
186 request.ParseFromString(serialized_request)
187
188 response = do_test(request)
189
190 serialized_response = response.SerializeToString()
191 sys.stdout.buffer.write(struct.pack("<I", len(serialized_response)))
192 sys.stdout.buffer.write(serialized_response)
193 sys.stdout.buffer.flush()
194
195 if verbose:
196 sys.stderr.write("conformance_python: request=%s, response=%s\n" % (
197 request.ShortDebugString().c_str(),
198 response.ShortDebugString().c_str()))
199
200 global test_count
201 test_count += 1
202
203 return True
204
205 while True:
206 if not do_test_io():
207 sys.stderr.write("conformance_python: received EOF from test runner " +
208 "after %s tests, exiting\n" % (test_count))
209 sys.exit(0)