blob: df63bf7cedde92429c5c679572922eead22627ee [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001#!/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 Schuh40c16522018-10-28 20:27:54 -070033require 'conformance_pb'
34require 'google/protobuf/test_messages_proto3_pb'
Brian Silverman9c614bc2016-02-15 20:20:02 -050035
36$test_count = 0
37$verbose = false
38
39def do_test(request)
Austin Schuh40c16522018-10-28 20:27:54 -070040 test_message = ProtobufTestMessages::Proto3::TestAllTypesProto3.new
Brian Silverman9c614bc2016-02-15 20:20:02 -050041 response = Conformance::ConformanceResponse.new
42
43 begin
44 case request.payload
45 when :protobuf_payload
Austin Schuh40c16522018-10-28 20:27:54 -070046 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 Silverman9c614bc2016-02-15 20:20:02 -050062 begin
Austin Schuh40c16522018-10-28 20:27:54 -070063 test_message = ProtobufTestMessages::Proto3::TestAllTypesProto3.decode_json(
64 request.json_payload)
Brian Silverman9c614bc2016-02-15 20:20:02 -050065 rescue Google::Protobuf::ParseError => err
66 response.parse_error = err.message.encode('utf-8')
67 return response
68 end
69
Brian Silverman9c614bc2016-02-15 20:20:02 -050070 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 Schuh40c16522018-10-28 20:27:54 -070083
84 when nil
85 fail "Request didn't have requested output format"
Brian Silverman9c614bc2016-02-15 20:20:02 -050086 end
87 rescue StandardError => err
88 response.runtime_error = err.message.encode('utf-8')
89 end
90
91 response
92end
93
94# Returns true if the test ran successfully, false on legitimate EOF.
95# If EOF is encountered in an unexpected place, raises IOError.
96def 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 Schuh40c16522018-10-28 20:27:54 -0700116 STDERR.puts("conformance_ruby: request=#{request.to_json}, " \
117 "response=#{response.to_json}\n")
Brian Silverman9c614bc2016-02-15 20:20:02 -0500118 end
119
120 $test_count += 1
121
122 true
123end
124
125loop do
126 unless do_test_io
Austin Schuh40c16522018-10-28 20:27:54 -0700127 STDERR.puts('conformance_ruby: received EOF from test runner ' \
Brian Silverman9c614bc2016-02-15 20:20:02 -0500128 "after #{$test_count} tests, exiting")
129 break
130 end
131end