conformance_ruby.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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'
  33. $test_count = 0
  34. $verbose = false
  35. def do_test(request)
  36. test_message = Conformance::TestAllTypes.new
  37. response = Conformance::ConformanceResponse.new
  38. begin
  39. case request.payload
  40. when :protobuf_payload
  41. begin
  42. test_message =
  43. Conformance::TestAllTypes.decode(request.protobuf_payload)
  44. rescue Google::Protobuf::ParseError => err
  45. response.parse_error = err.message.encode('utf-8')
  46. return response
  47. end
  48. when :json_payload
  49. begin
  50. test_message = Conformance::TestAllTypes.decode_json(request.json_payload)
  51. rescue Google::Protobuf::ParseError => err
  52. response.parse_error = err.message.encode('utf-8')
  53. return response
  54. end
  55. when nil
  56. fail "Request didn't have payload"
  57. end
  58. case request.requested_output_format
  59. when :UNSPECIFIED
  60. fail 'Unspecified output format'
  61. when :PROTOBUF
  62. response.protobuf_payload = test_message.to_proto
  63. when :JSON
  64. response.json_payload = test_message.to_json
  65. when nil
  66. fail "Request didn't have requested output format"
  67. end
  68. rescue StandardError => err
  69. response.runtime_error = err.message.encode('utf-8')
  70. end
  71. response
  72. end
  73. # Returns true if the test ran successfully, false on legitimate EOF.
  74. # If EOF is encountered in an unexpected place, raises IOError.
  75. def do_test_io
  76. length_bytes = STDIN.read(4)
  77. return false if length_bytes.nil?
  78. length = length_bytes.unpack('V').first
  79. serialized_request = STDIN.read(length)
  80. if serialized_request.nil? || serialized_request.length != length
  81. fail IOError
  82. end
  83. request = Conformance::ConformanceRequest.decode(serialized_request)
  84. response = do_test(request)
  85. serialized_response = Conformance::ConformanceResponse.encode(response)
  86. STDOUT.write([serialized_response.length].pack('V'))
  87. STDOUT.write(serialized_response)
  88. STDOUT.flush
  89. if $verbose
  90. STDERR.puts("conformance_ruby: request=#{request.to_json}, " \
  91. "response=#{response.to_json}\n")
  92. end
  93. $test_count += 1
  94. true
  95. end
  96. loop do
  97. unless do_test_io
  98. STDERR.puts('conformance_ruby: received EOF from test runner ' \
  99. "after #{$test_count} tests, exiting")
  100. break
  101. end
  102. end