File: conformance_ruby.rb

package info (click to toggle)
protobuf 3.25.4-2
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 45,944 kB
  • sloc: cpp: 204,199; java: 87,622; ansic: 81,204; objc: 58,434; cs: 27,303; python: 22,799; php: 11,340; ruby: 8,637; pascal: 3,325; xml: 2,333; sh: 1,331; makefile: 538; lisp: 86; awk: 17
file content (123 lines) | stat: -rwxr-xr-x 3,442 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env ruby
#
# Protocol Buffers - Google's data interchange format
# Copyright 2008 Google Inc.  All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

require 'conformance/conformance_pb'
require 'google/protobuf/test_messages_proto3_pb'
require 'google/protobuf/test_messages_proto2_pb'

$test_count = 0
$verbose = false

def do_test(request)
  test_message = ProtobufTestMessages::Proto3::TestAllTypesProto3.new
  response = Conformance::ConformanceResponse.new
  descriptor = Google::Protobuf::DescriptorPool.generated_pool.lookup(request.message_type)

  unless descriptor
    response.skipped = "Unknown message type: " + request.message_type
  end

  begin
    case request.payload
    when :protobuf_payload
      begin
        test_message = descriptor.msgclass.decode(request.protobuf_payload)
      rescue Google::Protobuf::ParseError => err
        response.parse_error = err.message.encode('utf-8')
        return response
      end

    when :json_payload
      begin
        options = {}
        if request.test_category == :JSON_IGNORE_UNKNOWN_PARSING_TEST
          options[:ignore_unknown_fields] = true
        end
        test_message = descriptor.msgclass.decode_json(request.json_payload, options)
      rescue Google::Protobuf::ParseError => err
        response.parse_error = err.message.encode('utf-8')
        return response
      end

    when :text_payload
      begin
        response.skipped = "Ruby doesn't support text format"
        return response
      end

    when nil
      fail "Request didn't have payload"
    end

    case request.requested_output_format
    when :UNSPECIFIED
      fail 'Unspecified output format'

    when :PROTOBUF
      begin
        response.protobuf_payload = test_message.to_proto
      rescue Google::Protobuf::ParseError => err
        response.serialize_error = err.message.encode('utf-8')
      end

    when :JSON
      begin
        response.json_payload = test_message.to_json
      rescue Google::Protobuf::ParseError => err
        response.serialize_error = err.message.encode('utf-8')
      end

    when nil
      fail "Request didn't have requested output format"
    end
  rescue StandardError => err
    response.runtime_error = err.message.encode('utf-8')
  end

  response
end

# Returns true if the test ran successfully, false on legitimate EOF.
# If EOF is encountered in an unexpected place, raises IOError.
def do_test_io
  length_bytes = STDIN.read(4)
  return false if length_bytes.nil?

  length = length_bytes.unpack('V').first
  serialized_request = STDIN.read(length)
  if serialized_request.nil? || serialized_request.length != length
    fail IOError
  end

  request = Conformance::ConformanceRequest.decode(serialized_request)

  response = do_test(request)

  serialized_response = Conformance::ConformanceResponse.encode(response)
  STDOUT.write([serialized_response.length].pack('V'))
  STDOUT.write(serialized_response)
  STDOUT.flush

  if $verbose
    STDERR.puts("conformance_ruby: request=#{request.to_json}, " \
                                 "response=#{response.to_json}\n")
  end

  $test_count += 1

  true
end

loop do
  unless do_test_io
    STDERR.puts('conformance_ruby: received EOF from test runner ' \
                "after #{$test_count} tests, exiting")
    break
  end
end