File: conformance_ruby.rb

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (132 lines) | stat: -rwxr-xr-x 3,930 bytes parent folder | download | duplicates (5)
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
124
125
126
127
128
129
130
131
132
# 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

# TODO - When rules_ruby supports prefix stripping, use it to obviate the need
# for manual load path manipulation
# https://github.com/bazel-contrib/rules_ruby/issues/219
$LOAD_PATH.unshift ENV['PWD']
$LOAD_PATH.unshift File.join(ENV['PWD'], 'ruby', 'lib')
$LOAD_PATH.unshift File.join(ENV['PWD'], 'ruby')

require 'conformance/conformance_pb'
require 'conformance/test_protos/test_messages_edition2023_pb'
require 'google/protobuf'
require 'google/protobuf/test_messages_proto3_pb'
require 'google/protobuf/test_messages_proto2_pb'
require 'test_messages_proto2_editions_pb'
require 'test_messages_proto3_editions_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.runtime_error = "Unknown message type: " + request.message_type
    return response
  end

  if request.test_category == :TEXT_FORMAT_TEST
    response.skipped = "Ruby doesn't support text format"
    return response
  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 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