blob: a490c8e8e54bd68e26ef4d10a5f4685dff51b2c4 [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
41from google.protobuf import message
Josh Habermana3faf082015-12-02 13:21:42 -080042from google.protobuf import json_format
Josh Haberman325392d2015-08-17 12:30:49 -070043import conformance_pb2
44
45sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0)
46sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0)
47
48test_count = 0
49verbose = False
50
Josh Haberman874eb362015-12-04 15:03:12 -080051class ProtocolError(Exception):
52 pass
53
Josh Haberman325392d2015-08-17 12:30:49 -070054def do_test(request):
55 test_message = conformance_pb2.TestAllTypes()
56 response = conformance_pb2.ConformanceResponse()
57 test_message = conformance_pb2.TestAllTypes()
58
59 try:
60 if request.WhichOneof('payload') == 'protobuf_payload':
61 try:
62 test_message.ParseFromString(request.protobuf_payload)
63 except message.DecodeError as e:
64 response.parse_error = str(e)
65 return response
66
67 elif request.WhichOneof('payload') == 'json_payload':
Josh Habermana3faf082015-12-02 13:21:42 -080068 try:
69 json_format.Parse(request.json_payload, test_message)
70 except json_format.ParseError as e:
71 response.parse_error = str(e)
72 return response
Josh Haberman325392d2015-08-17 12:30:49 -070073
74 else:
Josh Haberman874eb362015-12-04 15:03:12 -080075 raise ProtocolError("Request didn't have payload.")
Josh Haberman325392d2015-08-17 12:30:49 -070076
77 if request.requested_output_format == conformance_pb2.UNSPECIFIED:
Josh Haberman874eb362015-12-04 15:03:12 -080078 raise ProtocolError("Unspecified output format")
Josh Haberman325392d2015-08-17 12:30:49 -070079
80 elif request.requested_output_format == conformance_pb2.PROTOBUF:
81 response.protobuf_payload = test_message.SerializeToString()
82
83 elif request.requested_output_format == conformance_pb2.JSON:
Josh Habermana3faf082015-12-02 13:21:42 -080084 response.json_payload = json_format.MessageToJson(test_message)
85
Josh Haberman325392d2015-08-17 12:30:49 -070086 except Exception as e:
87 response.runtime_error = str(e)
88
89 return response
90
91def do_test_io():
92 length_bytes = sys.stdin.read(4)
93 if len(length_bytes) == 0:
94 return False # EOF
95 elif len(length_bytes) != 4:
96 raise IOError("I/O error")
97
98 # "I" is "unsigned int", so this depends on running on a platform with
99 # 32-bit "unsigned int" type. The Python struct module unfortunately
100 # has no format specifier for uint32_t.
101 length = struct.unpack("<I", length_bytes)[0]
102 serialized_request = sys.stdin.read(length)
103 if len(serialized_request) != length:
Josh Haberman874eb362015-12-04 15:03:12 -0800104 raise IOError("I/O error")
Josh Haberman325392d2015-08-17 12:30:49 -0700105
106 request = conformance_pb2.ConformanceRequest()
107 request.ParseFromString(serialized_request)
108
109 response = do_test(request)
110
111 serialized_response = response.SerializeToString()
112 sys.stdout.write(struct.pack("<I", len(serialized_response)))
113 sys.stdout.write(serialized_response)
114 sys.stdout.flush()
115
116 if verbose:
117 sys.stderr.write("conformance_python: request=%s, response=%s\n" % (
118 request.ShortDebugString().c_str(),
119 response.ShortDebugString().c_str()))
120
121 global test_count
122 test_count += 1
123
124 return True
125
126while True:
127 if not do_test_io():
128 sys.stderr.write("conformance_python: received EOF from test runner " +
129 "after %s tests, exiting\n" % (test_count))
130 sys.exit(0)