blob: 1ff8d84c39dc0681f92f2c0014fa58e42830cd86 [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
Kevin Clarkead33822009-02-04 22:43:59 +00009 it "should define the proper VERSION_1, VERSION_MASK AND TYPE_MASK" do
David Reiss72754e12008-07-25 21:06:03 +000010 protocol_class.const_get(:VERSION_MASK).should == 0xffff0000
11 protocol_class.const_get(:VERSION_1).should == 0x80010000
Kevin Clarkead33822009-02-04 22:43:59 +000012 protocol_class.const_get(:TYPE_MASK).should == 0x000000ff
David Reiss72754e12008-07-25 21:06:03 +000013 end
14
Kevin Clarkead33822009-02-04 22:43:59 +000015 it "should make strict_read readable" do
16 @prot.strict_read.should eql(true)
17 end
18
19 it "should make strict_write readable" do
20 @prot.strict_write.should eql(true)
21 end
22
David Reiss72754e12008-07-25 21:06:03 +000023 it "should write the message header" do
David Reiss72754e12008-07-25 21:06:03 +000024 @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
Bryan Duxburyc0166282009-02-02 00:48:17 +000025 @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 +000026 end
Kevin Clarkead33822009-02-04 22:43:59 +000027
28 it "should write the message header without version when writes are not strict" do
29 @prot = protocol_class.new(@trans, true, false) # no strict write
30 @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
31 @trans.read(1000).should == "\000\000\000\vtestMessage\001\000\000\000\021"
32 end
33
34 it "should write the message header with a version when writes are strict" do
35 @prot = protocol_class.new(@trans) # strict write
36 @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
37 @trans.read(1000).should == "\200\001\000\001\000\000\000\vtestMessage\000\000\000\021"
38 end
39
David Reiss72754e12008-07-25 21:06:03 +000040
41 # message footer is a noop
42
43 it "should write the field header" do
David Reiss72754e12008-07-25 21:06:03 +000044 @prot.write_field_begin('foo', Thrift::Types::DOUBLE, 3)
Bryan Duxburyc0166282009-02-02 00:48:17 +000045 @trans.read(1000).should == [Thrift::Types::DOUBLE, 3].pack("cn")
David Reiss72754e12008-07-25 21:06:03 +000046 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000047
David Reiss72754e12008-07-25 21:06:03 +000048 # field footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +000049
David Reiss72754e12008-07-25 21:06:03 +000050 it "should write the STOP field" do
David Reiss72754e12008-07-25 21:06:03 +000051 @prot.write_field_stop
Bryan Duxburyc0166282009-02-02 00:48:17 +000052 @trans.read(1).should == "\000"
David Reiss72754e12008-07-25 21:06:03 +000053 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000054
David Reiss72754e12008-07-25 21:06:03 +000055 it "should write the map header" do
David Reiss72754e12008-07-25 21:06:03 +000056 @prot.write_map_begin(Thrift::Types::STRING, Thrift::Types::LIST, 17)
Bryan Duxburyc0166282009-02-02 00:48:17 +000057 @trans.read(1000).should == [Thrift::Types::STRING, Thrift::Types::LIST, 17].pack("ccN");
David Reiss72754e12008-07-25 21:06:03 +000058 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000059
David Reiss72754e12008-07-25 21:06:03 +000060 # map footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +000061
David Reiss72754e12008-07-25 21:06:03 +000062 it "should write the list header" do
David Reiss72754e12008-07-25 21:06:03 +000063 @prot.write_list_begin(Thrift::Types::I16, 42)
Bryan Duxburyc0166282009-02-02 00:48:17 +000064 @trans.read(1000).should == [Thrift::Types::I16, 42].pack("cN")
David Reiss72754e12008-07-25 21:06:03 +000065 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000066
David Reiss72754e12008-07-25 21:06:03 +000067 # list footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +000068
David Reiss72754e12008-07-25 21:06:03 +000069 it "should write the set header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +000070 @prot.write_set_begin(Thrift::Types::I16, 42)
71 @trans.read(1000).should == [Thrift::Types::I16, 42].pack("cN")
David Reiss72754e12008-07-25 21:06:03 +000072 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000073
David Reiss72754e12008-07-25 21:06:03 +000074 it "should write a bool" do
David Reiss72754e12008-07-25 21:06:03 +000075 @prot.write_bool(true)
David Reiss72754e12008-07-25 21:06:03 +000076 @prot.write_bool(false)
Bryan Duxburyc0166282009-02-02 00:48:17 +000077 @trans.read(1000).should == "\001\000"
David Reiss72754e12008-07-25 21:06:03 +000078 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000079
Kevin Clark14fe7912008-08-04 18:46:19 +000080 it "should treat a nil bool as false" do
Kevin Clark14fe7912008-08-04 18:46:19 +000081 @prot.write_bool(nil)
Bryan Duxburyc0166282009-02-02 00:48:17 +000082 @trans.read(1).should == "\000"
Kevin Clark14fe7912008-08-04 18:46:19 +000083 end
Bryan Duxburyc0166282009-02-02 00:48:17 +000084
David Reiss72754e12008-07-25 21:06:03 +000085 it "should write a byte" do
86 # byte is small enough, let's check -128..127
87 (-128..127).each do |i|
David Reiss72754e12008-07-25 21:06:03 +000088 @prot.write_byte(i)
Bryan Duxburyc0166282009-02-02 00:48:17 +000089 @trans.read(1).should == [i].pack('c')
David Reiss72754e12008-07-25 21:06:03 +000090 end
David Reiss72754e12008-07-25 21:06:03 +000091 # handing it numbers out of signed range should clip
92 @trans.rspec_verify
93 (128..255).each do |i|
David Reiss72754e12008-07-25 21:06:03 +000094 @prot.write_byte(i)
Bryan Duxburyc0166282009-02-02 00:48:17 +000095 @trans.read(1).should == [i].pack('c')
David Reiss72754e12008-07-25 21:06:03 +000096 end
97 # and lastly, a Bignum is going to error out
98 lambda { @prot.write_byte(2**65) }.should raise_error(RangeError)
99 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000100
Kevin Clark14fe7912008-08-04 18:46:19 +0000101 it "should error gracefully when trying to write a nil byte" do
102 lambda { @prot.write_byte(nil) }.should raise_error
103 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000104
David Reiss72754e12008-07-25 21:06:03 +0000105 it "should write an i16" do
106 # try a random scattering of values
107 # include the signed i16 minimum/maximum
David Reiss72754e12008-07-25 21:06:03 +0000108 [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i|
109 @prot.write_i16(i)
110 end
111 # and try something out of signed range, it should clip
David Reiss72754e12008-07-25 21:06:03 +0000112 @prot.write_i16(2**15 + 5)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000113
114 @trans.read(1000).should == "\200\000\374\000\000\021\000\000\330\360\006\273\177\377\200\005"
115
David Reiss72754e12008-07-25 21:06:03 +0000116 # a Bignum should error
117 # lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
118 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000119
Kevin Clark14fe7912008-08-04 18:46:19 +0000120 it "should error gracefully when trying to write a nil i16" do
121 lambda { @prot.write_i16(nil) }.should raise_error
122 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000123
David Reiss72754e12008-07-25 21:06:03 +0000124 it "should write an i32" do
125 # try a random scattering of values
126 # include the signed i32 minimum/maximum
David Reiss72754e12008-07-25 21:06:03 +0000127 [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i|
128 @prot.write_i32(i)
129 end
130 # try something out of signed range, it should clip
Bryan Duxburyc0166282009-02-02 00:48:17 +0000131 @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"
132 [2 ** 31 + 5, 2 ** 65 + 5].each do |i|
133 lambda { @prot.write_i32(i) }.should raise_error(RangeError)
134 end
David Reiss72754e12008-07-25 21:06:03 +0000135 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000136
Kevin Clark14fe7912008-08-04 18:46:19 +0000137 it "should error gracefully when trying to write a nil i32" do
138 lambda { @prot.write_i32(nil) }.should raise_error
139 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000140
David Reiss72754e12008-07-25 21:06:03 +0000141 it "should write an i64" do
142 # try a random scattering of values
143 # try the signed i64 minimum/maximum
David Reiss72754e12008-07-25 21:06:03 +0000144 [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i|
145 @prot.write_i64(i)
146 end
147 # try something out of signed range, it should clip
Bryan Duxburyc0166282009-02-02 00:48:17 +0000148 @trans.read(1000).should == ["\200\000\000\000\000\000\000\000",
149 "\377\377\364\303\035\244+]",
150 "\377\377\377\377\376\231:\341",
151 "\377\377\377\377\377\377\377\026",
152 "\000\000\000\000\000\000\000\000",
153 "\000\000\000\000\000\000\004\317",
154 "\000\000\000\000\000#\340\204",
155 "\000\000\000\002\340\311~\365",
156 "\177\377\377\377\377\377\377\377"].join("")
157 lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError)
David Reiss72754e12008-07-25 21:06:03 +0000158 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000159
Kevin Clark14fe7912008-08-04 18:46:19 +0000160 it "should error gracefully when trying to write a nil i64" do
161 lambda { @prot.write_i64(nil) }.should raise_error
162 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000163
David Reiss72754e12008-07-25 21:06:03 +0000164 it "should write a double" do
165 # try a random scattering of values, including min/max
Bryan Duxburyc0166282009-02-02 00:48:17 +0000166 values = [Float::MIN,-1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX]
167 values.each do |f|
David Reiss72754e12008-07-25 21:06:03 +0000168 @prot.write_double(f)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000169 @trans.read(1000).should == [f].pack("G")
David Reiss72754e12008-07-25 21:06:03 +0000170 end
171 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000172
Kevin Clark14fe7912008-08-04 18:46:19 +0000173 it "should error gracefully when trying to write a nil double" do
174 lambda { @prot.write_double(nil) }.should raise_error
175 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000176
David Reiss72754e12008-07-25 21:06:03 +0000177 it "should write a string" do
178 str = "hello world"
David Reiss72754e12008-07-25 21:06:03 +0000179 @prot.write_string(str)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000180 @trans.read(1000).should == [str.size].pack("N") + str
David Reiss72754e12008-07-25 21:06:03 +0000181 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000182
Kevin Clark14fe7912008-08-04 18:46:19 +0000183 it "should error gracefully when trying to write a nil string" do
184 lambda { @prot.write_string(nil) }.should raise_error
185 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000186
David Reiss72754e12008-07-25 21:06:03 +0000187 # message footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000188
David Reiss72754e12008-07-25 21:06:03 +0000189 it "should read a field header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000190 @trans.write([Thrift::Types::STRING, 3].pack("cn"))
David Reiss72754e12008-07-25 21:06:03 +0000191 @prot.read_field_begin.should == [nil, Thrift::Types::STRING, 3]
192 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000193
David Reiss72754e12008-07-25 21:06:03 +0000194 # field footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000195
David Reiss72754e12008-07-25 21:06:03 +0000196 it "should read a stop field" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000197 @trans.write([Thrift::Types::STOP].pack("c"));
David Reiss72754e12008-07-25 21:06:03 +0000198 @prot.read_field_begin.should == [nil, Thrift::Types::STOP, 0]
199 end
200
201 it "should read a map header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000202 @trans.write([Thrift::Types::DOUBLE, Thrift::Types::I64, 42].pack("ccN"))
David Reiss72754e12008-07-25 21:06:03 +0000203 @prot.read_map_begin.should == [Thrift::Types::DOUBLE, Thrift::Types::I64, 42]
204 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000205
David Reiss72754e12008-07-25 21:06:03 +0000206 # map footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000207
David Reiss72754e12008-07-25 21:06:03 +0000208 it "should read a list header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000209 @trans.write([Thrift::Types::STRING, 17].pack("cN"))
David Reiss72754e12008-07-25 21:06:03 +0000210 @prot.read_list_begin.should == [Thrift::Types::STRING, 17]
211 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000212
David Reiss72754e12008-07-25 21:06:03 +0000213 # list footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000214
David Reiss72754e12008-07-25 21:06:03 +0000215 it "should read a set header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000216 @trans.write([Thrift::Types::STRING, 17].pack("cN"))
217 @prot.read_set_begin.should == [Thrift::Types::STRING, 17]
David Reiss72754e12008-07-25 21:06:03 +0000218 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000219
David Reiss72754e12008-07-25 21:06:03 +0000220 # set footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000221
David Reiss72754e12008-07-25 21:06:03 +0000222 it "should read a bool" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000223 @trans.write("\001\000");
David Reiss72754e12008-07-25 21:06:03 +0000224 @prot.read_bool.should == true
225 @prot.read_bool.should == false
226 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000227
David Reiss72754e12008-07-25 21:06:03 +0000228 it "should read a byte" do
David Reiss72754e12008-07-25 21:06:03 +0000229 [-128, -57, -3, 0, 17, 24, 127].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000230 @trans.write([i].pack("c"))
David Reiss72754e12008-07-25 21:06:03 +0000231 @prot.read_byte.should == i
232 end
233 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000234
David Reiss72754e12008-07-25 21:06:03 +0000235 it "should read an i16" do
236 # try a scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000237 [-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000238 @trans.write([i].pack("n"));
David Reiss72754e12008-07-25 21:06:03 +0000239 @prot.read_i16.should == i
240 end
241 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000242
David Reiss72754e12008-07-25 21:06:03 +0000243 it "should read an i32" do
244 # try a scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000245 [-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000246 @trans.write([i].pack("N"))
David Reiss72754e12008-07-25 21:06:03 +0000247 @prot.read_i32.should == i
248 end
249 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000250
David Reiss72754e12008-07-25 21:06:03 +0000251 it "should read an i64" do
252 # try a scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000253 [-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000254 @trans.write([i >> 32, i & 0xFFFFFFFF].pack("NN"))
David Reiss72754e12008-07-25 21:06:03 +0000255 @prot.read_i64.should == i
256 end
257 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000258
David Reiss72754e12008-07-25 21:06:03 +0000259 it "should read a double" do
260 # try a random scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000261 [Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000262 @trans.write([f].pack("G"));
David Reiss72754e12008-07-25 21:06:03 +0000263 @prot.read_double.should == f
264 end
265 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000266
David Reiss72754e12008-07-25 21:06:03 +0000267 it "should read a string" do
268 str = "hello world"
Bryan Duxburyc0166282009-02-02 00:48:17 +0000269 @trans.write([str.size].pack("N") + str)
David Reiss72754e12008-07-25 21:06:03 +0000270 @prot.read_string.should == str
271 end
272end