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