conformance_ruby.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. require 'conformance_pb'
  33. require 'google/protobuf/test_messages_proto3_pb'
  34. require 'google/protobuf/test_messages_proto2_pb'
  35. $test_count = 0
  36. $verbose = false
  37. def do_test(request)
  38. test_message = ProtobufTestMessages::Proto3::TestAllTypesProto3.new
  39. response = Conformance::ConformanceResponse.new
  40. descriptor = Google::Protobuf::DescriptorPool.generated_pool.lookup(request.message_type)
  41. unless descriptor
  42. response.skipped = "Unknown message type: " + request.message_type
  43. end
  44. begin
  45. case request.payload
  46. when :protobuf_payload
  47. begin
  48. test_message = descriptor.msgclass.decode(request.protobuf_payload)
  49. rescue Google::Protobuf::ParseError => err
  50. response.parse_error = err.message.encode('utf-8')
  51. return response
  52. end
  53. when :json_payload
  54. begin
  55. options = {}
  56. if request.test_category == :JSON_IGNORE_UNKNOWN_PARSING_TEST
  57. options[:ignore_unknown_fields] = true
  58. end
  59. test_message = descriptor.msgclass.decode_json(request.json_payload, options)
  60. rescue Google::Protobuf::ParseError => err
  61. response.parse_error = err.message.encode('utf-8')
  62. return response
  63. end
  64. when :text_payload
  65. begin
  66. response.skipped = "Ruby doesn't support text format"
  67. return response
  68. end
  69. when nil
  70. fail "Request didn't have payload"
  71. end
  72. case request.requested_output_format
  73. when :UNSPECIFIED
  74. fail 'Unspecified output format'
  75. when :PROTOBUF
  76. begin
  77. response.protobuf_payload = test_message.to_proto
  78. rescue Google::Protobuf::ParseError => err
  79. response.serialize_error = err.message.encode('utf-8')
  80. end
  81. when :JSON
  82. begin
  83. response.json_payload = test_message.to_json
  84. rescue Google::Protobuf::ParseError => err
  85. response.serialize_error = err.message.encode('utf-8')
  86. end
  87. when nil
  88. fail "Request didn't have requested output format"
  89. end
  90. rescue StandardError => err
  91. response.runtime_error = err.message.encode('utf-8')
  92. end
  93. response
  94. end
  95. # Returns true if the test ran successfully, false on legitimate EOF.
  96. # If EOF is encountered in an unexpected place, raises IOError.
  97. def do_test_io
  98. length_bytes = STDIN.read(4)
  99. return false if length_bytes.nil?
  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. request = Conformance::ConformanceRequest.decode(serialized_request)
  106. response = do_test(request)
  107. serialized_response = Conformance::ConformanceResponse.encode(response)
  108. STDOUT.write([serialized_response.length].pack('V'))
  109. STDOUT.write(serialized_response)
  110. STDOUT.flush
  111. if $verbose
  112. STDERR.puts("conformance_ruby: request=#{request.to_json}, " \
  113. "response=#{response.to_json}\n")
  114. end
  115. $test_count += 1
  116. true
  117. end
  118. loop do
  119. unless do_test_io
  120. STDERR.puts('conformance_ruby: received EOF from test runner ' \
  121. "after #{$test_count} tests, exiting")
  122. break
  123. end
  124. end