blob: c5ba2467a6b54ee81e02df751724257c08e06ba5 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001#!/usr/bin/env python
2#
3# Protocol Buffers - Google's data interchange format
4# Copyright 2008 Google Inc. All rights reserved.
5# https://developers.google.com/protocol-buffers/
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met:
10#
11# * Redistributions of source code must retain the above copyright
12# notice, this list of conditions and the following disclaimer.
13# * Redistributions in binary form must reproduce the above
14# copyright notice, this list of conditions and the following disclaimer
15# in the documentation and/or other materials provided with the
16# distribution.
17# * Neither the name of Google Inc. nor the names of its
18# contributors may be used to endorse or promote products derived from
19# this software without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33"""A conformance test implementation for the Python protobuf library.
34
35See conformance.proto for more information.
36"""
37
38import struct
39import sys
40import os
Austin Schuh40c16522018-10-28 20:27:54 -070041from google.protobuf import descriptor
42from google.protobuf import descriptor_pool
Brian Silverman9c614bc2016-02-15 20:20:02 -050043from google.protobuf import json_format
Austin Schuh40c16522018-10-28 20:27:54 -070044from google.protobuf import message
45from google.protobuf import test_messages_proto3_pb2
46from google.protobuf import test_messages_proto2_pb2
Brian Silverman9c614bc2016-02-15 20:20:02 -050047import conformance_pb2
48
49sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0)
50sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0)
51
52test_count = 0
53verbose = False
54
55class ProtocolError(Exception):
56 pass
57
58def do_test(request):
Austin Schuh40c16522018-10-28 20:27:54 -070059 isProto3 = (request.message_type == "protobuf_test_messages.proto3.TestAllTypesProto3")
60 isJson = (request.WhichOneof('payload') == 'json_payload')
61 isProto2 = (request.message_type == "protobuf_test_messages.proto2.TestAllTypesProto2")
62
63 if (not isProto3) and (not isJson) and (not isProto2):
64 raise ProtocolError("Protobuf request doesn't have specific payload type")
65
66 test_message = test_messages_proto2_pb2.TestAllTypesProto2() if isProto2 else \
67 test_messages_proto3_pb2.TestAllTypesProto3()
68
Brian Silverman9c614bc2016-02-15 20:20:02 -050069 response = conformance_pb2.ConformanceResponse()
Brian Silverman9c614bc2016-02-15 20:20:02 -050070
71 try:
72 if request.WhichOneof('payload') == 'protobuf_payload':
73 try:
74 test_message.ParseFromString(request.protobuf_payload)
75 except message.DecodeError as e:
76 response.parse_error = str(e)
Austin Schuh40c16522018-10-28 20:27:54 -070077 return response
78
Brian Silverman9c614bc2016-02-15 20:20:02 -050079 elif request.WhichOneof('payload') == 'json_payload':
80 try:
81 json_format.Parse(request.json_payload, test_message)
Austin Schuh40c16522018-10-28 20:27:54 -070082 except Exception as e:
Brian Silverman9c614bc2016-02-15 20:20:02 -050083 response.parse_error = str(e)
84 return response
85
86 else:
87 raise ProtocolError("Request didn't have payload.")
88
89 if request.requested_output_format == conformance_pb2.UNSPECIFIED:
90 raise ProtocolError("Unspecified output format")
91
92 elif request.requested_output_format == conformance_pb2.PROTOBUF:
93 response.protobuf_payload = test_message.SerializeToString()
94
95 elif request.requested_output_format == conformance_pb2.JSON:
Austin Schuh40c16522018-10-28 20:27:54 -070096 try:
97 response.json_payload = json_format.MessageToJson(test_message)
98 except Exception as e:
99 response.serialize_error = str(e)
100 return response
Brian Silverman9c614bc2016-02-15 20:20:02 -0500101
102 except Exception as e:
103 response.runtime_error = str(e)
104
105 return response
106
107def do_test_io():
108 length_bytes = sys.stdin.read(4)
109 if len(length_bytes) == 0:
110 return False # EOF
111 elif len(length_bytes) != 4:
112 raise IOError("I/O error")
113
114 # "I" is "unsigned int", so this depends on running on a platform with
115 # 32-bit "unsigned int" type. The Python struct module unfortunately
116 # has no format specifier for uint32_t.
117 length = struct.unpack("<I", length_bytes)[0]
118 serialized_request = sys.stdin.read(length)
119 if len(serialized_request) != length:
120 raise IOError("I/O error")
121
122 request = conformance_pb2.ConformanceRequest()
123 request.ParseFromString(serialized_request)
124
125 response = do_test(request)
126
127 serialized_response = response.SerializeToString()
128 sys.stdout.write(struct.pack("<I", len(serialized_response)))
129 sys.stdout.write(serialized_response)
130 sys.stdout.flush()
131
132 if verbose:
133 sys.stderr.write("conformance_python: request=%s, response=%s\n" % (
134 request.ShortDebugString().c_str(),
135 response.ShortDebugString().c_str()))
136
137 global test_count
138 test_count += 1
139
140 return True
141
142while True:
143 if not do_test_io():
144 sys.stderr.write("conformance_python: received EOF from test runner " +
145 "after %s tests, exiting\n" % (test_count))
146 sys.exit(0)