encode_decode_test.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/ruby
  2. # generated_code.rb is in the same directory as this test.
  3. $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
  4. require 'generated_code_pb'
  5. require 'google/protobuf/well_known_types'
  6. require 'test/unit'
  7. def hex2bin(s)
  8. s.scan(/../).map { |x| x.hex.chr }.join
  9. end
  10. class EncodeDecodeTest < Test::Unit::TestCase
  11. def test_discard_unknown
  12. # Test discard unknown in message.
  13. unknown_msg = A::B::C::TestUnknown.new(:unknown_field => 1)
  14. from = A::B::C::TestUnknown.encode(unknown_msg)
  15. m = A::B::C::TestMessage.decode(from)
  16. Google::Protobuf.discard_unknown(m)
  17. to = A::B::C::TestMessage.encode(m)
  18. assert_equal '', to
  19. # Test discard unknown for singular message field.
  20. unknown_msg = A::B::C::TestUnknown.new(
  21. :optional_unknown =>
  22. A::B::C::TestUnknown.new(:unknown_field => 1))
  23. from = A::B::C::TestUnknown.encode(unknown_msg)
  24. m = A::B::C::TestMessage.decode(from)
  25. Google::Protobuf.discard_unknown(m)
  26. to = A::B::C::TestMessage.encode(m.optional_msg)
  27. assert_equal '', to
  28. # Test discard unknown for repeated message field.
  29. unknown_msg = A::B::C::TestUnknown.new(
  30. :repeated_unknown =>
  31. [A::B::C::TestUnknown.new(:unknown_field => 1)])
  32. from = A::B::C::TestUnknown.encode(unknown_msg)
  33. m = A::B::C::TestMessage.decode(from)
  34. Google::Protobuf.discard_unknown(m)
  35. to = A::B::C::TestMessage.encode(m.repeated_msg[0])
  36. assert_equal '', to
  37. # Test discard unknown for map value message field.
  38. unknown_msg = A::B::C::TestUnknown.new(
  39. :map_unknown =>
  40. {"" => A::B::C::TestUnknown.new(:unknown_field => 1)})
  41. from = A::B::C::TestUnknown.encode(unknown_msg)
  42. m = A::B::C::TestMessage.decode(from)
  43. Google::Protobuf.discard_unknown(m)
  44. to = A::B::C::TestMessage.encode(m.map_string_msg[''])
  45. assert_equal '', to
  46. # Test discard unknown for oneof message field.
  47. unknown_msg = A::B::C::TestUnknown.new(
  48. :oneof_unknown =>
  49. A::B::C::TestUnknown.new(:unknown_field => 1))
  50. from = A::B::C::TestUnknown.encode(unknown_msg)
  51. m = A::B::C::TestMessage.decode(from)
  52. Google::Protobuf.discard_unknown(m)
  53. to = A::B::C::TestMessage.encode(m.oneof_msg)
  54. assert_equal '', to
  55. end
  56. def test_encode_json
  57. msg = A::B::C::TestMessage.new({ optional_int32: 22 })
  58. json = msg.to_json
  59. to = A::B::C::TestMessage.decode_json(json)
  60. assert_equal to.optional_int32, 22
  61. msg = A::B::C::TestMessage.new({ optional_int32: 22 })
  62. json = msg.to_json({ preserve_proto_fieldnames: true })
  63. assert_match 'optional_int32', json
  64. to = A::B::C::TestMessage.decode_json(json)
  65. assert_equal 22, to.optional_int32
  66. msg = A::B::C::TestMessage.new({ optional_int32: 22 })
  67. json = A::B::C::TestMessage.encode_json(
  68. msg,
  69. { preserve_proto_fieldnames: true, emit_defaults: true }
  70. )
  71. assert_match 'optional_int32', json
  72. end
  73. def test_encode_wrong_msg
  74. assert_raise ::ArgumentError do
  75. m = A::B::C::TestMessage.new(
  76. :optional_int32 => 1,
  77. )
  78. Google::Protobuf::Any.encode(m)
  79. end
  80. end
  81. def test_json_name
  82. msg = A::B::C::TestJsonName.new(:value => 42)
  83. json = msg.to_json
  84. assert_match json, "{\"CustomJsonName\":42}"
  85. end
  86. def test_decode_depth_limit
  87. msg = A::B::C::TestMessage.new(
  88. optional_msg: A::B::C::TestMessage.new(
  89. optional_msg: A::B::C::TestMessage.new(
  90. optional_msg: A::B::C::TestMessage.new(
  91. optional_msg: A::B::C::TestMessage.new(
  92. optional_msg: A::B::C::TestMessage.new(
  93. )
  94. )
  95. )
  96. )
  97. )
  98. )
  99. msg_encoded = A::B::C::TestMessage.encode(msg)
  100. msg_out = A::B::C::TestMessage.decode(msg_encoded)
  101. assert_match msg.to_json, msg_out.to_json
  102. assert_raise Google::Protobuf::ParseError do
  103. A::B::C::TestMessage.decode(msg_encoded, { recursion_limit: 4 })
  104. end
  105. msg_out = A::B::C::TestMessage.decode(msg_encoded, { recursion_limit: 5 })
  106. assert_match msg.to_json, msg_out.to_json
  107. end
  108. def test_encode_depth_limit
  109. msg = A::B::C::TestMessage.new(
  110. optional_msg: A::B::C::TestMessage.new(
  111. optional_msg: A::B::C::TestMessage.new(
  112. optional_msg: A::B::C::TestMessage.new(
  113. optional_msg: A::B::C::TestMessage.new(
  114. optional_msg: A::B::C::TestMessage.new(
  115. )
  116. )
  117. )
  118. )
  119. )
  120. )
  121. msg_encoded = A::B::C::TestMessage.encode(msg)
  122. msg_out = A::B::C::TestMessage.decode(msg_encoded)
  123. assert_match msg.to_json, msg_out.to_json
  124. assert_raise RuntimeError do
  125. A::B::C::TestMessage.encode(msg, { recursion_limit: 5 })
  126. end
  127. msg_encoded = A::B::C::TestMessage.encode(msg, { recursion_limit: 6 })
  128. msg_out = A::B::C::TestMessage.decode(msg_encoded)
  129. assert_match msg.to_json, msg_out.to_json
  130. end
  131. end