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 | |
| 35 | test_count = 0; |
| 36 | verbose = false; |
| 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 |
| 46 | test_message = Conformance::TestAllTypes.decode(request.protobuf_payload) |
| 47 | rescue Google::Protobuf::ParseError => err |
| 48 | response.parse_error = err.message.encode("utf-8") |
| 49 | return response |
| 50 | end |
| 51 | |
| 52 | when :json_payload |
| 53 | test_message = Conformance::TestAllTypes.decode_json(request.json_payload) |
| 54 | |
| 55 | when nil |
| 56 | raise "Request didn't have payload."; |
| 57 | end |
| 58 | |
| 59 | case request.requested_output_format |
| 60 | when :UNSPECIFIED |
| 61 | raise "Unspecified output format" |
| 62 | |
| 63 | when :PROTOBUF |
| 64 | response.protobuf_payload = Conformance::TestAllTypes.encode(test_message) |
| 65 | |
| 66 | when :JSON |
| 67 | response.json_payload = Conformance::TestAllTypes.encode_json(test_message) |
| 68 | end |
| 69 | rescue Exception => err |
| 70 | response.runtime_error = err.message.encode("utf-8") + err.backtrace.join("\n") |
| 71 | end |
| 72 | |
| 73 | return response |
| 74 | end |
| 75 | |
| 76 | def do_test_io |
| 77 | length_bytes = STDIN.read(4) |
| 78 | return false if length_bytes.nil? |
| 79 | |
| 80 | length = length_bytes.unpack("V").first |
| 81 | serialized_request = STDIN.read(length) |
| 82 | if serialized_request.nil? or serialized_request.length != length |
| 83 | raise "I/O error" |
| 84 | end |
| 85 | |
| 86 | request = Conformance::ConformanceRequest.decode(serialized_request) |
| 87 | |
| 88 | response = do_test(request) |
| 89 | |
| 90 | serialized_response = Conformance::ConformanceResponse.encode(response) |
| 91 | STDOUT.write([serialized_response.length].pack("V")) |
| 92 | STDOUT.write(serialized_response) |
| 93 | STDOUT.flush |
| 94 | |
| 95 | #if verbose |
| 96 | # fprintf(stderr, "conformance-cpp: request=%s, response=%s\n", |
| 97 | # request.ShortDebugString().c_str(), |
| 98 | # response.ShortDebugString().c_str()); |
| 99 | |
| 100 | #test_count++; |
| 101 | |
| 102 | return true; |
| 103 | end |
| 104 | |
| 105 | while true |
| 106 | if not do_test_io() |
| 107 | STDERR.puts("conformance-cpp: received EOF from test runner " + |
| 108 | "after #{test_count} tests, exiting") |
| 109 | exit 0 |
| 110 | end |
| 111 | end |