blob: 78e2ccbacc2ce8642b80c7b69da476b1be625579 [file] [log] [blame]
David Reiss72754e12008-07-25 21:06:03 +00001require File.dirname(__FILE__) + '/spec_helper'
2
3shared_examples_for 'a binary protocol' do
4 before(:each) do
Bryan Duxburyc0166282009-02-02 00:48:17 +00005 @trans = Thrift::MemoryBuffer.new
David Reiss72754e12008-07-25 21:06:03 +00006 @prot = protocol_class.new(@trans)
7 end
8
9 it "should define the proper VERSION_1 and VERSION_MASK" do
10 protocol_class.const_get(:VERSION_MASK).should == 0xffff0000
11 protocol_class.const_get(:VERSION_1).should == 0x80010000
12 end
13
14 it "should write the message header" do
David Reiss72754e12008-07-25 21:06:03 +000015 @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
Bryan Duxburyc0166282009-02-02 00:48:17 +000016 @trans.read(1000).should == [protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::CALL, "testMessage".size, "testMessage", 17].pack("NNa11N")
David Reiss72754e12008-07-25 21:06:03 +000017 end
18
19 # message footer is a noop
20
21 it "should write the field header" do
David Reiss72754e12008-07-25 21:06:03 +000022 @prot.write_field_begin('foo', Thrift::Types::DOUBLE, 3)
Bryan Duxburyc0166282009-02-02 00:48:17 +000023 @trans.read(1000).should == [Thrift::Types::DOUBLE, 3].pack("cn")
David Reiss72754e12008-07-25 21:06:03 +000024 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000025
David Reiss72754e12008-07-25 21:06:03 +000026 # field footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +000027
David Reiss72754e12008-07-25 21:06:03 +000028 it "should write the STOP field" do
David Reiss72754e12008-07-25 21:06:03 +000029 @prot.write_field_stop
Bryan Duxburyc0166282009-02-02 00:48:17 +000030 @trans.read(1).should == "\000"
David Reiss72754e12008-07-25 21:06:03 +000031 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000032
David Reiss72754e12008-07-25 21:06:03 +000033 it "should write the map header" do
David Reiss72754e12008-07-25 21:06:03 +000034 @prot.write_map_begin(Thrift::Types::STRING, Thrift::Types::LIST, 17)
Bryan Duxburyc0166282009-02-02 00:48:17 +000035 @trans.read(1000).should == [Thrift::Types::STRING, Thrift::Types::LIST, 17].pack("ccN");
David Reiss72754e12008-07-25 21:06:03 +000036 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000037
David Reiss72754e12008-07-25 21:06:03 +000038 # map footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +000039
David Reiss72754e12008-07-25 21:06:03 +000040 it "should write the list header" do
David Reiss72754e12008-07-25 21:06:03 +000041 @prot.write_list_begin(Thrift::Types::I16, 42)
Bryan Duxburyc0166282009-02-02 00:48:17 +000042 @trans.read(1000).should == [Thrift::Types::I16, 42].pack("cN")
David Reiss72754e12008-07-25 21:06:03 +000043 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000044
David Reiss72754e12008-07-25 21:06:03 +000045 # list footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +000046
David Reiss72754e12008-07-25 21:06:03 +000047 it "should write the set header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +000048 @prot.write_set_begin(Thrift::Types::I16, 42)
49 @trans.read(1000).should == [Thrift::Types::I16, 42].pack("cN")
David Reiss72754e12008-07-25 21:06:03 +000050 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000051
David Reiss72754e12008-07-25 21:06:03 +000052 it "should write a bool" do
David Reiss72754e12008-07-25 21:06:03 +000053 @prot.write_bool(true)
David Reiss72754e12008-07-25 21:06:03 +000054 @prot.write_bool(false)
Bryan Duxburyc0166282009-02-02 00:48:17 +000055 @trans.read(1000).should == "\001\000"
David Reiss72754e12008-07-25 21:06:03 +000056 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000057
Kevin Clark14fe7912008-08-04 18:46:19 +000058 it "should treat a nil bool as false" do
Kevin Clark14fe7912008-08-04 18:46:19 +000059 @prot.write_bool(nil)
Bryan Duxburyc0166282009-02-02 00:48:17 +000060 @trans.read(1).should == "\000"
Kevin Clark14fe7912008-08-04 18:46:19 +000061 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000062
David Reiss72754e12008-07-25 21:06:03 +000063 it "should write a byte" do
64 # byte is small enough, let's check -128..127
65 (-128..127).each do |i|
David Reiss72754e12008-07-25 21:06:03 +000066 @prot.write_byte(i)
Bryan Duxburyc0166282009-02-02 00:48:17 +000067 @trans.read(1).should == [i].pack('c')
David Reiss72754e12008-07-25 21:06:03 +000068 end
69 (-128..127).each do |i|
70 end
71 # handing it numbers out of signed range should clip
72 @trans.rspec_verify
73 (128..255).each do |i|
David Reiss72754e12008-07-25 21:06:03 +000074 @prot.write_byte(i)
Bryan Duxburyc0166282009-02-02 00:48:17 +000075 @trans.read(1).should == [i].pack('c')
David Reiss72754e12008-07-25 21:06:03 +000076 end
77 # and lastly, a Bignum is going to error out
78 lambda { @prot.write_byte(2**65) }.should raise_error(RangeError)
79 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000080
Kevin Clark14fe7912008-08-04 18:46:19 +000081 it "should error gracefully when trying to write a nil byte" do
82 lambda { @prot.write_byte(nil) }.should raise_error
83 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000084
David Reiss72754e12008-07-25 21:06:03 +000085 it "should write an i16" do
86 # try a random scattering of values
87 # include the signed i16 minimum/maximum
David Reiss72754e12008-07-25 21:06:03 +000088 [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i|
89 @prot.write_i16(i)
90 end
91 # and try something out of signed range, it should clip
David Reiss72754e12008-07-25 21:06:03 +000092 @prot.write_i16(2**15 + 5)
Bryan Duxburyc0166282009-02-02 00:48:17 +000093
94 @trans.read(1000).should == "\200\000\374\000\000\021\000\000\330\360\006\273\177\377\200\005"
95
David Reiss72754e12008-07-25 21:06:03 +000096 # a Bignum should error
97 # lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
98 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000099
Kevin Clark14fe7912008-08-04 18:46:19 +0000100 it "should error gracefully when trying to write a nil i16" do
101 lambda { @prot.write_i16(nil) }.should raise_error
102 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000103
David Reiss72754e12008-07-25 21:06:03 +0000104 it "should write an i32" do
105 # try a random scattering of values
106 # include the signed i32 minimum/maximum
David Reiss72754e12008-07-25 21:06:03 +0000107 [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i|
108 @prot.write_i32(i)
109 end
110 # try something out of signed range, it should clip
Bryan Duxburyc0166282009-02-02 00:48:17 +0000111 @trans.read(1000).should == "\200\000\000\000" + "\377\376\037\r" + "\377\377\366\034" + "\377\377\377\375" + "\000\000\000\000" + "\000#\340\203" + "\000\0000+" + "\177\377\377\377"
112 [2 ** 31 + 5, 2 ** 65 + 5].each do |i|
113 lambda { @prot.write_i32(i) }.should raise_error(RangeError)
114 end
David Reiss72754e12008-07-25 21:06:03 +0000115 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000116
Kevin Clark14fe7912008-08-04 18:46:19 +0000117 it "should error gracefully when trying to write a nil i32" do
118 lambda { @prot.write_i32(nil) }.should raise_error
119 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000120
David Reiss72754e12008-07-25 21:06:03 +0000121 it "should write an i64" do
122 # try a random scattering of values
123 # try the signed i64 minimum/maximum
David Reiss72754e12008-07-25 21:06:03 +0000124 [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i|
125 @prot.write_i64(i)
126 end
127 # try something out of signed range, it should clip
Bryan Duxburyc0166282009-02-02 00:48:17 +0000128 @trans.read(1000).should == ["\200\000\000\000\000\000\000\000",
129 "\377\377\364\303\035\244+]",
130 "\377\377\377\377\376\231:\341",
131 "\377\377\377\377\377\377\377\026",
132 "\000\000\000\000\000\000\000\000",
133 "\000\000\000\000\000\000\004\317",
134 "\000\000\000\000\000#\340\204",
135 "\000\000\000\002\340\311~\365",
136 "\177\377\377\377\377\377\377\377"].join("")
137 lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError)
David Reiss72754e12008-07-25 21:06:03 +0000138 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000139
Kevin Clark14fe7912008-08-04 18:46:19 +0000140 it "should error gracefully when trying to write a nil i64" do
141 lambda { @prot.write_i64(nil) }.should raise_error
142 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000143
David Reiss72754e12008-07-25 21:06:03 +0000144 it "should write a double" do
145 # try a random scattering of values, including min/max
Bryan Duxburyc0166282009-02-02 00:48:17 +0000146 values = [Float::MIN,-1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX]
147 values.each do |f|
David Reiss72754e12008-07-25 21:06:03 +0000148 @prot.write_double(f)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000149 @trans.read(1000).should == [f].pack("G")
David Reiss72754e12008-07-25 21:06:03 +0000150 end
151 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000152
Kevin Clark14fe7912008-08-04 18:46:19 +0000153 it "should error gracefully when trying to write a nil double" do
154 lambda { @prot.write_double(nil) }.should raise_error
155 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000156
David Reiss72754e12008-07-25 21:06:03 +0000157 it "should write a string" do
158 str = "hello world"
David Reiss72754e12008-07-25 21:06:03 +0000159 @prot.write_string(str)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000160 @trans.read(1000).should == [str.size].pack("N") + str
David Reiss72754e12008-07-25 21:06:03 +0000161 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000162
Kevin Clark14fe7912008-08-04 18:46:19 +0000163 it "should error gracefully when trying to write a nil string" do
164 lambda { @prot.write_string(nil) }.should raise_error
165 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000166
David Reiss72754e12008-07-25 21:06:03 +0000167 # message footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000168
David Reiss72754e12008-07-25 21:06:03 +0000169 it "should read a field header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000170 @trans.write([Thrift::Types::STRING, 3].pack("cn"))
David Reiss72754e12008-07-25 21:06:03 +0000171 @prot.read_field_begin.should == [nil, Thrift::Types::STRING, 3]
172 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000173
David Reiss72754e12008-07-25 21:06:03 +0000174 # field footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000175
David Reiss72754e12008-07-25 21:06:03 +0000176 it "should read a stop field" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000177 @trans.write([Thrift::Types::STOP].pack("c"));
David Reiss72754e12008-07-25 21:06:03 +0000178 @prot.read_field_begin.should == [nil, Thrift::Types::STOP, 0]
179 end
180
181 it "should read a map header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000182 @trans.write([Thrift::Types::DOUBLE, Thrift::Types::I64, 42].pack("ccN"))
David Reiss72754e12008-07-25 21:06:03 +0000183 @prot.read_map_begin.should == [Thrift::Types::DOUBLE, Thrift::Types::I64, 42]
184 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000185
David Reiss72754e12008-07-25 21:06:03 +0000186 # map footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000187
David Reiss72754e12008-07-25 21:06:03 +0000188 it "should read a list header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000189 @trans.write([Thrift::Types::STRING, 17].pack("cN"))
David Reiss72754e12008-07-25 21:06:03 +0000190 @prot.read_list_begin.should == [Thrift::Types::STRING, 17]
191 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000192
David Reiss72754e12008-07-25 21:06:03 +0000193 # list footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000194
David Reiss72754e12008-07-25 21:06:03 +0000195 it "should read a set header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000196 @trans.write([Thrift::Types::STRING, 17].pack("cN"))
197 @prot.read_set_begin.should == [Thrift::Types::STRING, 17]
David Reiss72754e12008-07-25 21:06:03 +0000198 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000199
David Reiss72754e12008-07-25 21:06:03 +0000200 # set footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000201
David Reiss72754e12008-07-25 21:06:03 +0000202 it "should read a bool" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000203 @trans.write("\001\000");
David Reiss72754e12008-07-25 21:06:03 +0000204 @prot.read_bool.should == true
205 @prot.read_bool.should == false
206 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000207
David Reiss72754e12008-07-25 21:06:03 +0000208 it "should read a byte" do
David Reiss72754e12008-07-25 21:06:03 +0000209 [-128, -57, -3, 0, 17, 24, 127].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000210 @trans.write([i].pack("c"))
David Reiss72754e12008-07-25 21:06:03 +0000211 @prot.read_byte.should == i
212 end
213 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000214
David Reiss72754e12008-07-25 21:06:03 +0000215 it "should read an i16" do
216 # try a scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000217 [-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000218 @trans.write([i].pack("n"));
David Reiss72754e12008-07-25 21:06:03 +0000219 @prot.read_i16.should == i
220 end
221 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000222
David Reiss72754e12008-07-25 21:06:03 +0000223 it "should read an i32" do
224 # try a scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000225 [-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000226 @trans.write([i].pack("N"))
David Reiss72754e12008-07-25 21:06:03 +0000227 @prot.read_i32.should == i
228 end
229 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000230
David Reiss72754e12008-07-25 21:06:03 +0000231 it "should read an i64" do
232 # try a scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000233 [-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000234 @trans.write([i >> 32, i & 0xFFFFFFFF].pack("NN"))
David Reiss72754e12008-07-25 21:06:03 +0000235 @prot.read_i64.should == i
236 end
237 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000238
David Reiss72754e12008-07-25 21:06:03 +0000239 it "should read a double" do
240 # try a random scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000241 [Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000242 @trans.write([f].pack("G"));
David Reiss72754e12008-07-25 21:06:03 +0000243 @prot.read_double.should == f
244 end
245 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000246
David Reiss72754e12008-07-25 21:06:03 +0000247 it "should read a string" do
248 str = "hello world"
Bryan Duxburyc0166282009-02-02 00:48:17 +0000249 @trans.write([str.size].pack("N") + str)
David Reiss72754e12008-07-25 21:06:03 +0000250 @prot.read_string.should == str
251 end
252end