Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 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 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 33 | require 'conformance_pb' |
| 34 | require 'google/protobuf/test_messages_proto3_pb' |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 35 | |
| 36 | $test_count = 0 |
| 37 | $verbose = false |
| 38 | |
| 39 | def do_test(request) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 40 | test_message = ProtobufTestMessages::Proto3::TestAllTypesProto3.new |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 41 | response = Conformance::ConformanceResponse.new |
| 42 | |
| 43 | begin |
| 44 | case request.payload |
| 45 | when :protobuf_payload |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 46 | if request.message_type.eql?('protobuf_test_messages.proto3.TestAllTypesProto3') |
| 47 | begin |
| 48 | test_message = ProtobufTestMessages::Proto3::TestAllTypesProto3.decode( |
| 49 | request.protobuf_payload) |
| 50 | rescue Google::Protobuf::ParseError => err |
| 51 | response.parse_error = err.message.encode('utf-8') |
| 52 | return response |
| 53 | end |
| 54 | elsif request.message_type.eql?('protobuf_test_messages.proto2.TestAllTypesProto2') |
| 55 | response.skipped = "Ruby doesn't support proto2" |
| 56 | return response |
| 57 | else |
| 58 | fail "Protobuf request doesn't have specific payload type" |
| 59 | end |
| 60 | |
| 61 | when :json_payload |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 62 | begin |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 63 | test_message = ProtobufTestMessages::Proto3::TestAllTypesProto3.decode_json( |
| 64 | request.json_payload) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 65 | rescue Google::Protobuf::ParseError => err |
| 66 | response.parse_error = err.message.encode('utf-8') |
| 67 | return response |
| 68 | end |
| 69 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 70 | when nil |
| 71 | fail "Request didn't have payload" |
| 72 | end |
| 73 | |
| 74 | case request.requested_output_format |
| 75 | when :UNSPECIFIED |
| 76 | fail 'Unspecified output format' |
| 77 | |
| 78 | when :PROTOBUF |
| 79 | response.protobuf_payload = test_message.to_proto |
| 80 | |
| 81 | when :JSON |
| 82 | response.json_payload = test_message.to_json |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 83 | |
| 84 | when nil |
| 85 | fail "Request didn't have requested output format" |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 86 | end |
| 87 | rescue StandardError => err |
| 88 | response.runtime_error = err.message.encode('utf-8') |
| 89 | end |
| 90 | |
| 91 | response |
| 92 | end |
| 93 | |
| 94 | # Returns true if the test ran successfully, false on legitimate EOF. |
| 95 | # If EOF is encountered in an unexpected place, raises IOError. |
| 96 | def do_test_io |
| 97 | length_bytes = STDIN.read(4) |
| 98 | return false if length_bytes.nil? |
| 99 | |
| 100 | length = length_bytes.unpack('V').first |
| 101 | serialized_request = STDIN.read(length) |
| 102 | if serialized_request.nil? || serialized_request.length != length |
| 103 | fail IOError |
| 104 | end |
| 105 | |
| 106 | request = Conformance::ConformanceRequest.decode(serialized_request) |
| 107 | |
| 108 | response = do_test(request) |
| 109 | |
| 110 | serialized_response = Conformance::ConformanceResponse.encode(response) |
| 111 | STDOUT.write([serialized_response.length].pack('V')) |
| 112 | STDOUT.write(serialized_response) |
| 113 | STDOUT.flush |
| 114 | |
| 115 | if $verbose |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 116 | STDERR.puts("conformance_ruby: request=#{request.to_json}, " \ |
| 117 | "response=#{response.to_json}\n") |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 118 | end |
| 119 | |
| 120 | $test_count += 1 |
| 121 | |
| 122 | true |
| 123 | end |
| 124 | |
| 125 | loop do |
| 126 | unless do_test_io |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 127 | STDERR.puts('conformance_ruby: received EOF from test runner ' \ |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 128 | "after #{$test_count} tests, exiting") |
| 129 | break |
| 130 | end |
| 131 | end |