| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 1 | # encoding: UTF-8 | 
| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 2 | # | 
|  | 3 | # Licensed to the Apache Software Foundation (ASF) under one | 
|  | 4 | # or more contributor license agreements. See the NOTICE file | 
|  | 5 | # distributed with this work for additional information | 
|  | 6 | # regarding copyright ownership. The ASF licenses this file | 
|  | 7 | # to you under the Apache License, Version 2.0 (the | 
|  | 8 | # "License"); you may not use this file except in compliance | 
|  | 9 | # with the License. You may obtain a copy of the License at | 
|  | 10 | # | 
|  | 11 | #   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | # | 
|  | 13 | # Unless required by applicable law or agreed to in writing, | 
|  | 14 | # software distributed under the License is distributed on an | 
|  | 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
|  | 16 | # KIND, either express or implied. See the License for the | 
|  | 17 | # specific language governing permissions and limitations | 
|  | 18 | # under the License. | 
|  | 19 | # | 
|  | 20 |  | 
| Jake Farrell | a87810f | 2012-09-28 01:59:04 +0000 | [diff] [blame] | 21 | require 'spec_helper' | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 22 |  | 
|  | 23 | describe Thrift::CompactProtocol do | 
|  | 24 | TESTS = { | 
|  | 25 | :byte => (-127..127).to_a, | 
|  | 26 | :i16 => (0..14).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort, | 
|  | 27 | :i32 => (0..30).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort, | 
|  | 28 | :i64 => (0..62).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort, | 
| Jake Farrell | b5a18a1 | 2012-10-09 01:10:43 +0000 | [diff] [blame] | 29 | :string => ["", "1", "short", "fourteen123456", "fifteen12345678", "unicode characters: \u20AC \u20AD", "1" * 127, "1" * 3000], | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 30 | :binary => ["", "\001", "\001" * 5, "\001" * 14, "\001" * 15, "\001" * 127, "\001" * 3000], | 
|  | 31 | :double => [0.0, 1.0, -1.0, 1.1, -1.1, 10000000.1, 1.0/0.0, -1.0/0.0], | 
|  | 32 | :bool => [true, false] | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | it "should encode and decode naked primitives correctly" do | 
|  | 36 | TESTS.each_pair do |primitive_type, test_values| | 
|  | 37 | test_values.each do |value| | 
|  | 38 | # puts "testing #{value}" if primitive_type == :i64 | 
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 39 | trans = Thrift::MemoryBufferTransport.new | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 40 | proto = Thrift::CompactProtocol.new(trans) | 
|  | 41 |  | 
|  | 42 | proto.send(writer(primitive_type), value) | 
|  | 43 | # puts "buf: #{trans.inspect_buffer}" if primitive_type == :i64 | 
|  | 44 | read_back = proto.send(reader(primitive_type)) | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 45 | expect(read_back).to eq(value) | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 46 | end | 
|  | 47 | end | 
|  | 48 | end | 
|  | 49 |  | 
|  | 50 | it "should encode and decode primitives in fields correctly" do | 
|  | 51 | TESTS.each_pair do |primitive_type, test_values| | 
|  | 52 | final_primitive_type = primitive_type == :binary ? :string : primitive_type | 
|  | 53 | thrift_type = Thrift::Types.const_get(final_primitive_type.to_s.upcase) | 
|  | 54 | # puts primitive_type | 
|  | 55 | test_values.each do |value| | 
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 56 | trans = Thrift::MemoryBufferTransport.new | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 57 | proto = Thrift::CompactProtocol.new(trans) | 
|  | 58 |  | 
|  | 59 | proto.write_field_begin(nil, thrift_type, 15) | 
|  | 60 | proto.send(writer(primitive_type), value) | 
|  | 61 | proto.write_field_end | 
|  | 62 |  | 
|  | 63 | proto = Thrift::CompactProtocol.new(trans) | 
|  | 64 | name, type, id = proto.read_field_begin | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 65 | expect(type).to eq(thrift_type) | 
|  | 66 | expect(id).to eq(15) | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 67 | read_back = proto.send(reader(primitive_type)) | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 68 | expect(read_back).to eq(value) | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 69 | proto.read_field_end | 
|  | 70 | end | 
|  | 71 | end | 
|  | 72 | end | 
|  | 73 |  | 
|  | 74 | it "should encode and decode a monster struct correctly" do | 
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 75 | trans = Thrift::MemoryBufferTransport.new | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 76 | proto = Thrift::CompactProtocol.new(trans) | 
|  | 77 |  | 
| Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 78 | struct = Thrift::Test::CompactProtoTestStruct.new | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 79 | # sets and maps don't hash well... not sure what to do here. | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 80 | struct.write(proto) | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 81 |  | 
| Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 82 | struct2 = Thrift::Test::CompactProtoTestStruct.new | 
| Bryan Duxbury | 9e4c041 | 2009-04-07 23:45:15 +0000 | [diff] [blame] | 83 | struct2.read(proto) | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 84 | expect(struct2).to eq(struct) | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 85 | end | 
|  | 86 |  | 
|  | 87 | it "should make method calls correctly" do | 
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 88 | client_out_trans = Thrift::MemoryBufferTransport.new | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 89 | client_out_proto = Thrift::CompactProtocol.new(client_out_trans) | 
| Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 90 |  | 
| Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 91 | client_in_trans = Thrift::MemoryBufferTransport.new | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 92 | client_in_proto = Thrift::CompactProtocol.new(client_in_trans) | 
| Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 93 |  | 
| Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 94 | processor = Thrift::Test::Srv::Processor.new(JankyHandler.new) | 
| Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 95 |  | 
| Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 96 | client = Thrift::Test::Srv::Client.new(client_in_proto, client_out_proto) | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 97 | client.send_Janky(1) | 
|  | 98 | # puts client_out_trans.inspect_buffer | 
|  | 99 | processor.process(client_out_proto, client_in_proto) | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 100 | expect(client.recv_Janky).to eq(2) | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 101 | end | 
|  | 102 |  | 
| Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 103 | it "should deal with fields following fields that have non-delta ids" do | 
| Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 104 | brcp = Thrift::Test::BreaksRubyCompactProtocol.new( | 
| Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 105 | :field1 => "blah", | 
| Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 106 | :field2 => Thrift::Test::BigFieldIdStruct.new( | 
| Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 107 | :field1 => "string1", | 
|  | 108 | :field2 => "string2"), | 
|  | 109 | :field3 => 3) | 
|  | 110 | ser = Thrift::Serializer.new(Thrift::CompactProtocolFactory.new) | 
|  | 111 | bytes = ser.serialize(brcp) | 
|  | 112 |  | 
|  | 113 | deser = Thrift::Deserializer.new(Thrift::CompactProtocolFactory.new) | 
| Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 114 | brcp2 = Thrift::Test::BreaksRubyCompactProtocol.new | 
| Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 115 | deser.deserialize(brcp2, bytes) | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 116 | expect(brcp2).to eq(brcp) | 
| Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 117 | end | 
|  | 118 |  | 
| Bryan Duxbury | e80a194 | 2011-09-20 18:45:56 +0000 | [diff] [blame] | 119 | it "should deserialize an empty map to an empty hash" do | 
| Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 120 | struct = Thrift::Test::SingleMapTestStruct.new(:i32_map => {}) | 
| Bryan Duxbury | e80a194 | 2011-09-20 18:45:56 +0000 | [diff] [blame] | 121 | ser = Thrift::Serializer.new(Thrift::CompactProtocolFactory.new) | 
|  | 122 | bytes = ser.serialize(struct) | 
|  | 123 |  | 
|  | 124 | deser = Thrift::Deserializer.new(Thrift::CompactProtocolFactory.new) | 
| Jens Geyer | 123258b | 2015-10-02 00:38:17 +0200 | [diff] [blame] | 125 | struct2 = Thrift::Test::SingleMapTestStruct.new | 
| Bryan Duxbury | e80a194 | 2011-09-20 18:45:56 +0000 | [diff] [blame] | 126 | deser.deserialize(struct2, bytes) | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 127 | expect(struct).to eq(struct2) | 
| Bryan Duxbury | e80a194 | 2011-09-20 18:45:56 +0000 | [diff] [blame] | 128 | end | 
|  | 129 |  | 
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 130 | it "should provide a reasonable to_s" do | 
|  | 131 | trans = Thrift::MemoryBufferTransport.new | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 132 | expect(Thrift::CompactProtocol.new(trans).to_s).to eq("compact(memory)") | 
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 133 | end | 
|  | 134 |  | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 135 | class JankyHandler | 
|  | 136 | def Janky(i32arg) | 
|  | 137 | i32arg * 2 | 
|  | 138 | end | 
|  | 139 | end | 
|  | 140 |  | 
|  | 141 | def writer(sym) | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 142 | "write_#{sym.to_s}" | 
|  | 143 | end | 
|  | 144 |  | 
|  | 145 | def reader(sym) | 
| Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 146 | "read_#{sym.to_s}" | 
|  | 147 | end | 
| Roger Meier | 19dbbef | 2012-12-27 01:24:20 +0100 | [diff] [blame] | 148 | end | 
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 149 |  | 
|  | 150 | describe Thrift::CompactProtocolFactory do | 
|  | 151 | it "should create a CompactProtocol" do | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 152 | expect(Thrift::CompactProtocolFactory.new.get_protocol(double("MockTransport"))).to be_instance_of(Thrift::CompactProtocol) | 
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 153 | end | 
|  | 154 |  | 
|  | 155 | it "should provide a reasonable to_s" do | 
| James E. King III | 2724707 | 2018-03-22 20:50:23 -0400 | [diff] [blame] | 156 | expect(Thrift::CompactProtocolFactory.new.to_s).to eq("compact") | 
| James E. King III | 9aaf295 | 2018-03-20 15:06:08 -0400 | [diff] [blame] | 157 | end | 
|  | 158 | end |