blob: 7519810ec30abf8bebdccfe08ef6a227d8a94ed1 [file] [log] [blame]
Jake Farrellb5a18a12012-10-09 01:10:43 +00001# encoding: UTF-8
David Reissea2cba82009-03-30 21:35:00 +00002#
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 Farrella87810f2012-09-28 01:59:04 +000021require 'spec_helper'
Bryan Duxburyd815c212009-03-19 18:57:43 +000022
23describe 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 Farrellb5a18a12012-10-09 01:10:43 +000029 :string => ["", "1", "short", "fourteen123456", "fifteen12345678", "unicode characters: \u20AC \u20AD", "1" * 127, "1" * 3000],
Bryan Duxburyd815c212009-03-19 18:57:43 +000030 :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 Duxburyd1d15422009-04-04 00:58:03 +000039 trans = Thrift::MemoryBufferTransport.new
Bryan Duxburyd815c212009-03-19 18:57:43 +000040 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 III27247072018-03-22 20:50:23 -040045 expect(read_back).to eq(value)
Bryan Duxburyd815c212009-03-19 18:57:43 +000046 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 Duxburyd1d15422009-04-04 00:58:03 +000056 trans = Thrift::MemoryBufferTransport.new
Bryan Duxburyd815c212009-03-19 18:57:43 +000057 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 III27247072018-03-22 20:50:23 -040065 expect(type).to eq(thrift_type)
66 expect(id).to eq(15)
Bryan Duxburyd815c212009-03-19 18:57:43 +000067 read_back = proto.send(reader(primitive_type))
James E. King III27247072018-03-22 20:50:23 -040068 expect(read_back).to eq(value)
Bryan Duxburyd815c212009-03-19 18:57:43 +000069 proto.read_field_end
70 end
71 end
72 end
73
Dmytro Shteflyuke9ac8e32025-11-19 23:33:23 -050074 it "should write a uuid" do
75 trans = Thrift::MemoryBufferTransport.new
76 proto = Thrift::CompactProtocol.new(trans)
77
78 proto.write_uuid("00112233-4455-6677-8899-aabbccddeeff")
79 a = trans.read(trans.available)
80 expect(a.unpack('C*')).to eq([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])
81 end
82
83 it "should read a uuid" do
84 trans = Thrift::MemoryBufferTransport.new
85 proto = Thrift::CompactProtocol.new(trans)
86
87 trans.write([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff].pack('C*'))
88 uuid = proto.read_uuid
89 expect(uuid).to eq("00112233-4455-6677-8899-aabbccddeeff")
90 end
91
92 it "should error gracefully when trying to write an invalid uuid" do
93 trans = Thrift::MemoryBufferTransport.new
94 proto = Thrift::CompactProtocol.new(trans)
95 expect { proto.write_uuid("invalid") }.to raise_error(Thrift::ProtocolException)
96 end
97
Bryan Duxburyd815c212009-03-19 18:57:43 +000098 it "should encode and decode a monster struct correctly" do
Bryan Duxburyd1d15422009-04-04 00:58:03 +000099 trans = Thrift::MemoryBufferTransport.new
Bryan Duxburyd815c212009-03-19 18:57:43 +0000100 proto = Thrift::CompactProtocol.new(trans)
101
Jens Geyer123258b2015-10-02 00:38:17 +0200102 struct = Thrift::Test::CompactProtoTestStruct.new
Bryan Duxburyd815c212009-03-19 18:57:43 +0000103 # sets and maps don't hash well... not sure what to do here.
Bryan Duxburyd815c212009-03-19 18:57:43 +0000104 struct.write(proto)
Bryan Duxburyd815c212009-03-19 18:57:43 +0000105
Jens Geyer123258b2015-10-02 00:38:17 +0200106 struct2 = Thrift::Test::CompactProtoTestStruct.new
Bryan Duxbury9e4c0412009-04-07 23:45:15 +0000107 struct2.read(proto)
James E. King III27247072018-03-22 20:50:23 -0400108 expect(struct2).to eq(struct)
Bryan Duxburyd815c212009-03-19 18:57:43 +0000109 end
110
111 it "should make method calls correctly" do
Bryan Duxburyd1d15422009-04-04 00:58:03 +0000112 client_out_trans = Thrift::MemoryBufferTransport.new
Bryan Duxburyd815c212009-03-19 18:57:43 +0000113 client_out_proto = Thrift::CompactProtocol.new(client_out_trans)
Bryan Duxbury35565a42010-01-06 23:12:09 +0000114
Bryan Duxburyd1d15422009-04-04 00:58:03 +0000115 client_in_trans = Thrift::MemoryBufferTransport.new
Bryan Duxburyd815c212009-03-19 18:57:43 +0000116 client_in_proto = Thrift::CompactProtocol.new(client_in_trans)
Bryan Duxbury35565a42010-01-06 23:12:09 +0000117
Jens Geyer123258b2015-10-02 00:38:17 +0200118 processor = Thrift::Test::Srv::Processor.new(JankyHandler.new)
Bryan Duxbury35565a42010-01-06 23:12:09 +0000119
Jens Geyer123258b2015-10-02 00:38:17 +0200120 client = Thrift::Test::Srv::Client.new(client_in_proto, client_out_proto)
Bryan Duxburyd815c212009-03-19 18:57:43 +0000121 client.send_Janky(1)
122 # puts client_out_trans.inspect_buffer
123 processor.process(client_out_proto, client_in_proto)
James E. King III27247072018-03-22 20:50:23 -0400124 expect(client.recv_Janky).to eq(2)
Bryan Duxburyd815c212009-03-19 18:57:43 +0000125 end
126
Bryan Duxbury35565a42010-01-06 23:12:09 +0000127 it "should deal with fields following fields that have non-delta ids" do
Jens Geyer123258b2015-10-02 00:38:17 +0200128 brcp = Thrift::Test::BreaksRubyCompactProtocol.new(
Bryan Duxbury35565a42010-01-06 23:12:09 +0000129 :field1 => "blah",
Jens Geyer123258b2015-10-02 00:38:17 +0200130 :field2 => Thrift::Test::BigFieldIdStruct.new(
Bryan Duxbury35565a42010-01-06 23:12:09 +0000131 :field1 => "string1",
132 :field2 => "string2"),
133 :field3 => 3)
134 ser = Thrift::Serializer.new(Thrift::CompactProtocolFactory.new)
135 bytes = ser.serialize(brcp)
136
137 deser = Thrift::Deserializer.new(Thrift::CompactProtocolFactory.new)
Jens Geyer123258b2015-10-02 00:38:17 +0200138 brcp2 = Thrift::Test::BreaksRubyCompactProtocol.new
Bryan Duxbury35565a42010-01-06 23:12:09 +0000139 deser.deserialize(brcp2, bytes)
James E. King III27247072018-03-22 20:50:23 -0400140 expect(brcp2).to eq(brcp)
Bryan Duxbury35565a42010-01-06 23:12:09 +0000141 end
142
Bryan Duxburye80a1942011-09-20 18:45:56 +0000143 it "should deserialize an empty map to an empty hash" do
Jens Geyer123258b2015-10-02 00:38:17 +0200144 struct = Thrift::Test::SingleMapTestStruct.new(:i32_map => {})
Bryan Duxburye80a1942011-09-20 18:45:56 +0000145 ser = Thrift::Serializer.new(Thrift::CompactProtocolFactory.new)
146 bytes = ser.serialize(struct)
147
148 deser = Thrift::Deserializer.new(Thrift::CompactProtocolFactory.new)
Jens Geyer123258b2015-10-02 00:38:17 +0200149 struct2 = Thrift::Test::SingleMapTestStruct.new
Bryan Duxburye80a1942011-09-20 18:45:56 +0000150 deser.deserialize(struct2, bytes)
James E. King III27247072018-03-22 20:50:23 -0400151 expect(struct).to eq(struct2)
Bryan Duxburye80a1942011-09-20 18:45:56 +0000152 end
153
James E. King III9aaf2952018-03-20 15:06:08 -0400154 it "should provide a reasonable to_s" do
155 trans = Thrift::MemoryBufferTransport.new
James E. King III27247072018-03-22 20:50:23 -0400156 expect(Thrift::CompactProtocol.new(trans).to_s).to eq("compact(memory)")
James E. King III9aaf2952018-03-20 15:06:08 -0400157 end
158
Bryan Duxburyd815c212009-03-19 18:57:43 +0000159 class JankyHandler
160 def Janky(i32arg)
161 i32arg * 2
162 end
163 end
164
165 def writer(sym)
Bryan Duxburyd815c212009-03-19 18:57:43 +0000166 "write_#{sym.to_s}"
167 end
168
169 def reader(sym)
Bryan Duxburyd815c212009-03-19 18:57:43 +0000170 "read_#{sym.to_s}"
171 end
Roger Meier19dbbef2012-12-27 01:24:20 +0100172end
James E. King III9aaf2952018-03-20 15:06:08 -0400173
174describe Thrift::CompactProtocolFactory do
175 it "should create a CompactProtocol" do
James E. King III27247072018-03-22 20:50:23 -0400176 expect(Thrift::CompactProtocolFactory.new.get_protocol(double("MockTransport"))).to be_instance_of(Thrift::CompactProtocol)
James E. King III9aaf2952018-03-20 15:06:08 -0400177 end
178
179 it "should provide a reasonable to_s" do
James E. King III27247072018-03-22 20:50:23 -0400180 expect(Thrift::CompactProtocolFactory.new.to_s).to eq("compact")
James E. King III9aaf2952018-03-20 15:06:08 -0400181 end
182end