Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [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 | |
| 33 | require 'conformance' |
| 34 | |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 35 | $test_count = 0 |
| 36 | $verbose = false |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 37 | |
| 38 | def do_test(request) |
| 39 | test_message = Conformance::TestAllTypes.new |
| 40 | response = Conformance::ConformanceResponse.new |
| 41 | |
| 42 | begin |
| 43 | case request.payload |
| 44 | when :protobuf_payload |
| 45 | begin |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 46 | test_message = |
| 47 | Conformance::TestAllTypes.decode(request.protobuf_payload) |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 48 | rescue Google::Protobuf::ParseError => err |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 49 | response.parse_error = err.message.encode('utf-8') |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 50 | return response |
| 51 | end |
| 52 | |
| 53 | when :json_payload |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 54 | begin |
| 55 | test_message = Conformance::TestAllTypes.decode_json(request.json_payload) |
| 56 | rescue Google::Protobuf::ParseError => err |
| 57 | response.parse_error = err.message.encode('utf-8') |
| 58 | return response |
| 59 | end |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 60 | |
| 61 | when nil |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 62 | fail "Request didn't have payload" |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 63 | end |
| 64 | |
| 65 | case request.requested_output_format |
| 66 | when :UNSPECIFIED |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 67 | fail 'Unspecified output format' |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 68 | |
| 69 | when :PROTOBUF |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 70 | response.protobuf_payload = test_message.to_proto |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 71 | |
| 72 | when :JSON |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 73 | response.json_payload = test_message.to_json |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 74 | |
| 75 | when nil |
| 76 | fail "Request didn't have requested output format" |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 77 | end |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 78 | rescue StandardError => err |
| 79 | response.runtime_error = err.message.encode('utf-8') |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 80 | end |
| 81 | |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 82 | response |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 83 | end |
| 84 | |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 85 | # Returns true if the test ran successfully, false on legitimate EOF. |
| 86 | # If EOF is encountered in an unexpected place, raises IOError. |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 87 | def do_test_io |
| 88 | length_bytes = STDIN.read(4) |
| 89 | return false if length_bytes.nil? |
| 90 | |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 91 | length = length_bytes.unpack('V').first |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 92 | serialized_request = STDIN.read(length) |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 93 | if serialized_request.nil? || serialized_request.length != length |
| 94 | fail IOError |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 95 | end |
| 96 | |
| 97 | request = Conformance::ConformanceRequest.decode(serialized_request) |
| 98 | |
| 99 | response = do_test(request) |
| 100 | |
| 101 | serialized_response = Conformance::ConformanceResponse.encode(response) |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 102 | STDOUT.write([serialized_response.length].pack('V')) |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 103 | STDOUT.write(serialized_response) |
| 104 | STDOUT.flush |
| 105 | |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 106 | if $verbose |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 107 | STDERR.puts("conformance_ruby: request=#{request.to_json}, " \ |
| 108 | "response=#{response.to_json}\n") |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 109 | end |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 110 | |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 111 | $test_count += 1 |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 112 | |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 113 | true |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 114 | end |
| 115 | |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 116 | loop do |
| 117 | unless do_test_io |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 118 | STDERR.puts('conformance_ruby: received EOF from test runner ' \ |
Josh Haberman | c2c43a4 | 2015-07-17 16:29:10 -0700 | [diff] [blame] | 119 | "after #{$test_count} tests, exiting") |
| 120 | break |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 121 | end |
| 122 | end |