Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 1 | #!/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 | |
| 35 | See conformance.proto for more information. |
| 36 | """ |
| 37 | |
| 38 | import struct |
| 39 | import sys |
| 40 | import os |
Josh Haberman | a3faf08 | 2015-12-02 13:21:42 -0800 | [diff] [blame] | 41 | from google.protobuf import json_format |
Joshua Haberman | f1ce60e | 2016-12-03 11:51:25 -0500 | [diff] [blame^] | 42 | from google.protobuf import message |
| 43 | from google.protobuf import test_messages_proto3_pb2 |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 44 | import conformance_pb2 |
| 45 | |
| 46 | sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) |
| 47 | sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0) |
| 48 | |
| 49 | test_count = 0 |
| 50 | verbose = False |
| 51 | |
Josh Haberman | 874eb36 | 2015-12-04 15:03:12 -0800 | [diff] [blame] | 52 | class ProtocolError(Exception): |
| 53 | pass |
| 54 | |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 55 | def do_test(request): |
Joshua Haberman | f1ce60e | 2016-12-03 11:51:25 -0500 | [diff] [blame^] | 56 | test_message = test_messages_proto3_pb2.TestAllTypes() |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 57 | response = conformance_pb2.ConformanceResponse() |
Joshua Haberman | f1ce60e | 2016-12-03 11:51:25 -0500 | [diff] [blame^] | 58 | test_message = test_messages_proto3_pb2.TestAllTypes() |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 59 | |
| 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 Haberman | a3faf08 | 2015-12-02 13:21:42 -0800 | [diff] [blame] | 69 | try: |
| 70 | json_format.Parse(request.json_payload, test_message) |
Bo Yang | cc8ca5b | 2016-09-19 13:45:07 -0700 | [diff] [blame] | 71 | except Exception as e: |
Josh Haberman | a3faf08 | 2015-12-02 13:21:42 -0800 | [diff] [blame] | 72 | response.parse_error = str(e) |
| 73 | return response |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 74 | |
| 75 | else: |
Josh Haberman | 874eb36 | 2015-12-04 15:03:12 -0800 | [diff] [blame] | 76 | raise ProtocolError("Request didn't have payload.") |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 77 | |
| 78 | if request.requested_output_format == conformance_pb2.UNSPECIFIED: |
Josh Haberman | 874eb36 | 2015-12-04 15:03:12 -0800 | [diff] [blame] | 79 | raise ProtocolError("Unspecified output format") |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 80 | |
| 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 Yang | cc8ca5b | 2016-09-19 13:45:07 -0700 | [diff] [blame] | 85 | 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 Haberman | a3faf08 | 2015-12-02 13:21:42 -0800 | [diff] [blame] | 90 | |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 91 | except Exception as e: |
| 92 | response.runtime_error = str(e) |
| 93 | |
| 94 | return response |
| 95 | |
| 96 | def 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 Haberman | 874eb36 | 2015-12-04 15:03:12 -0800 | [diff] [blame] | 109 | raise IOError("I/O error") |
Josh Haberman | 325392d | 2015-08-17 12:30:49 -0700 | [diff] [blame] | 110 | |
| 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 | |
| 131 | while 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) |