blob: 7ace9b16725c4771891c52fa5e1e6a1aef05653f [file] [log] [blame]
Josh Haberman325392d2015-08-17 12:30:49 -07001#!/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
Josh Habermana3faf082015-12-02 13:21:42 -080041from google.protobuf import json_format
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050042from google.protobuf import message
43from google.protobuf import test_messages_proto3_pb2
Josh Haberman325392d2015-08-17 12:30:49 -070044import conformance_pb2
45
46sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0)
47sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0)
48
49test_count = 0
50verbose = False
51
Josh Haberman874eb362015-12-04 15:03:12 -080052class ProtocolError(Exception):
53 pass
54
Josh Haberman325392d2015-08-17 12:30:49 -070055def do_test(request):
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050056 test_message = test_messages_proto3_pb2.TestAllTypes()
Josh Haberman325392d2015-08-17 12:30:49 -070057 response = conformance_pb2.ConformanceResponse()
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050058 test_message = test_messages_proto3_pb2.TestAllTypes()
Josh Haberman325392d2015-08-17 12:30:49 -070059
60 try:
61 if request.WhichOneof('payload') == 'protobuf_payload':
62 try:
63 test_message.ParseFromString(request.protobuf_payload)
64 except message.DecodeError as e:
65 response.parse_error = str(e)
66 return response
67
68 elif request.WhichOneof('payload') == 'json_payload':
Josh Habermana3faf082015-12-02 13:21:42 -080069 try:
70 json_format.Parse(request.json_payload, test_message)
Bo Yangcc8ca5b2016-09-19 13:45:07 -070071 except Exception as e:
Josh Habermana3faf082015-12-02 13:21:42 -080072 response.parse_error = str(e)
73 return response
Josh Haberman325392d2015-08-17 12:30:49 -070074
75 else:
Josh Haberman874eb362015-12-04 15:03:12 -080076 raise ProtocolError("Request didn't have payload.")
Josh Haberman325392d2015-08-17 12:30:49 -070077
78 if request.requested_output_format == conformance_pb2.UNSPECIFIED:
Josh Haberman874eb362015-12-04 15:03:12 -080079 raise ProtocolError("Unspecified output format")
Josh Haberman325392d2015-08-17 12:30:49 -070080
81 elif request.requested_output_format == conformance_pb2.PROTOBUF:
82 response.protobuf_payload = test_message.SerializeToString()
83
84 elif request.requested_output_format == conformance_pb2.JSON:
Bo Yangcc8ca5b2016-09-19 13:45:07 -070085 try:
86 response.json_payload = json_format.MessageToJson(test_message)
87 except Exception as e:
88 response.serialize_error = str(e)
89 return response
Josh Habermana3faf082015-12-02 13:21:42 -080090
Josh Haberman325392d2015-08-17 12:30:49 -070091 except Exception as e:
92 response.runtime_error = str(e)
93
94 return response
95
96def do_test_io():
97 length_bytes = sys.stdin.read(4)
98 if len(length_bytes) == 0:
99 return False # EOF
100 elif len(length_bytes) != 4:
101 raise IOError("I/O error")
102
103 # "I" is "unsigned int", so this depends on running on a platform with
104 # 32-bit "unsigned int" type. The Python struct module unfortunately
105 # has no format specifier for uint32_t.
106 length = struct.unpack("<I", length_bytes)[0]
107 serialized_request = sys.stdin.read(length)
108 if len(serialized_request) != length:
Josh Haberman874eb362015-12-04 15:03:12 -0800109 raise IOError("I/O error")
Josh Haberman325392d2015-08-17 12:30:49 -0700110
111 request = conformance_pb2.ConformanceRequest()
112 request.ParseFromString(serialized_request)
113
114 response = do_test(request)
115
116 serialized_response = response.SerializeToString()
117 sys.stdout.write(struct.pack("<I", len(serialized_response)))
118 sys.stdout.write(serialized_response)
119 sys.stdout.flush()
120
121 if verbose:
122 sys.stderr.write("conformance_python: request=%s, response=%s\n" % (
123 request.ShortDebugString().c_str(),
124 response.ShortDebugString().c_str()))
125
126 global test_count
127 test_count += 1
128
129 return True
130
131while True:
132 if not do_test_io():
133 sys.stderr.write("conformance_python: received EOF from test runner " +
134 "after %s tests, exiting\n" % (test_count))
135 sys.exit(0)