blob: cd065673062f7a1c6ab573891db186e3e6dc33de [file] [log] [blame]
Josh Haberman181c7f22015-07-15 11:05:10 -07001#!/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
33require 'conformance'
34
Josh Habermanc2c43a42015-07-17 16:29:10 -070035$test_count = 0
36$verbose = false
Josh Haberman181c7f22015-07-15 11:05:10 -070037
38def 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 Habermanc2c43a42015-07-17 16:29:10 -070046 test_message =
47 Conformance::TestAllTypes.decode(request.protobuf_payload)
Josh Haberman181c7f22015-07-15 11:05:10 -070048 rescue Google::Protobuf::ParseError => err
Josh Habermanc2c43a42015-07-17 16:29:10 -070049 response.parse_error = err.message.encode('utf-8')
Josh Haberman181c7f22015-07-15 11:05:10 -070050 return response
51 end
52
53 when :json_payload
54 test_message = Conformance::TestAllTypes.decode_json(request.json_payload)
55
56 when nil
Josh Habermanc2c43a42015-07-17 16:29:10 -070057 fail "Request didn't have payload"
Josh Haberman181c7f22015-07-15 11:05:10 -070058 end
59
60 case request.requested_output_format
61 when :UNSPECIFIED
Josh Habermanc2c43a42015-07-17 16:29:10 -070062 fail 'Unspecified output format'
Josh Haberman181c7f22015-07-15 11:05:10 -070063
64 when :PROTOBUF
Josh Habermanc2c43a42015-07-17 16:29:10 -070065 response.protobuf_payload = test_message.to_proto
Josh Haberman181c7f22015-07-15 11:05:10 -070066
67 when :JSON
Josh Habermanc2c43a42015-07-17 16:29:10 -070068 response.json_payload = test_message.to_json
Josh Haberman181c7f22015-07-15 11:05:10 -070069 end
Josh Habermanc2c43a42015-07-17 16:29:10 -070070 rescue StandardError => err
71 response.runtime_error = err.message.encode('utf-8')
Josh Haberman181c7f22015-07-15 11:05:10 -070072 end
73
Josh Habermanc2c43a42015-07-17 16:29:10 -070074 response
Josh Haberman181c7f22015-07-15 11:05:10 -070075end
76
Josh Habermanc2c43a42015-07-17 16:29:10 -070077# Returns true if the test ran successfully, false on legitimate EOF.
78# If EOF is encountered in an unexpected place, raises IOError.
Josh Haberman181c7f22015-07-15 11:05:10 -070079def do_test_io
80 length_bytes = STDIN.read(4)
81 return false if length_bytes.nil?
82
Josh Habermanc2c43a42015-07-17 16:29:10 -070083 length = length_bytes.unpack('V').first
Josh Haberman181c7f22015-07-15 11:05:10 -070084 serialized_request = STDIN.read(length)
Josh Habermanc2c43a42015-07-17 16:29:10 -070085 if serialized_request.nil? || serialized_request.length != length
86 fail IOError
Josh Haberman181c7f22015-07-15 11:05:10 -070087 end
88
89 request = Conformance::ConformanceRequest.decode(serialized_request)
90
91 response = do_test(request)
92
93 serialized_response = Conformance::ConformanceResponse.encode(response)
Josh Habermanc2c43a42015-07-17 16:29:10 -070094 STDOUT.write([serialized_response.length].pack('V'))
Josh Haberman181c7f22015-07-15 11:05:10 -070095 STDOUT.write(serialized_response)
96 STDOUT.flush
97
Josh Habermanc2c43a42015-07-17 16:29:10 -070098 if $verbose
99 STDERR.puts("conformance-cpp: request={request.to_json}, " \
100 "response={response.to_json}\n")
101 end
Josh Haberman181c7f22015-07-15 11:05:10 -0700102
Josh Habermanc2c43a42015-07-17 16:29:10 -0700103 $test_count += 1
Josh Haberman181c7f22015-07-15 11:05:10 -0700104
Josh Habermanc2c43a42015-07-17 16:29:10 -0700105 true
Josh Haberman181c7f22015-07-15 11:05:10 -0700106end
107
Josh Habermanc2c43a42015-07-17 16:29:10 -0700108loop do
109 unless do_test_io
110 STDERR.puts('conformance-cpp: received EOF from test runner ' \
111 "after #{$test_count} tests, exiting")
112 break
Josh Haberman181c7f22015-07-15 11:05:10 -0700113 end
114end