blob: 1d685b65c43c36004d0971fda81628d2a929cc37 [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
91 (-128..127).each do |i|
92 end
93 # handing it numbers out of signed range should clip
94 @trans.rspec_verify
95 (128..255).each do |i|
David Reiss72754e12008-07-25 21:06:03 +000096 @prot.write_byte(i)
Bryan Duxburyc0166282009-02-02 00:48:17 +000097 @trans.read(1).should == [i].pack('c')
David Reiss72754e12008-07-25 21:06:03 +000098 end
99 # and lastly, a Bignum is going to error out
100 lambda { @prot.write_byte(2**65) }.should raise_error(RangeError)
101 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000102
Kevin Clark14fe7912008-08-04 18:46:19 +0000103 it "should error gracefully when trying to write a nil byte" do
104 lambda { @prot.write_byte(nil) }.should raise_error
105 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000106
David Reiss72754e12008-07-25 21:06:03 +0000107 it "should write an i16" do
108 # try a random scattering of values
109 # include the signed i16 minimum/maximum
David Reiss72754e12008-07-25 21:06:03 +0000110 [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i|
111 @prot.write_i16(i)
112 end
113 # and try something out of signed range, it should clip
David Reiss72754e12008-07-25 21:06:03 +0000114 @prot.write_i16(2**15 + 5)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000115
116 @trans.read(1000).should == "\200\000\374\000\000\021\000\000\330\360\006\273\177\377\200\005"
117
David Reiss72754e12008-07-25 21:06:03 +0000118 # a Bignum should error
119 # lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
120 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000121
Kevin Clark14fe7912008-08-04 18:46:19 +0000122 it "should error gracefully when trying to write a nil i16" do
123 lambda { @prot.write_i16(nil) }.should raise_error
124 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000125
David Reiss72754e12008-07-25 21:06:03 +0000126 it "should write an i32" do
127 # try a random scattering of values
128 # include the signed i32 minimum/maximum
David Reiss72754e12008-07-25 21:06:03 +0000129 [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i|
130 @prot.write_i32(i)
131 end
132 # try something out of signed range, it should clip
Bryan Duxburyc0166282009-02-02 00:48:17 +0000133 @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"
134 [2 ** 31 + 5, 2 ** 65 + 5].each do |i|
135 lambda { @prot.write_i32(i) }.should raise_error(RangeError)
136 end
David Reiss72754e12008-07-25 21:06:03 +0000137 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000138
Kevin Clark14fe7912008-08-04 18:46:19 +0000139 it "should error gracefully when trying to write a nil i32" do
140 lambda { @prot.write_i32(nil) }.should raise_error
141 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000142
David Reiss72754e12008-07-25 21:06:03 +0000143 it "should write an i64" do
144 # try a random scattering of values
145 # try the signed i64 minimum/maximum
David Reiss72754e12008-07-25 21:06:03 +0000146 [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i|
147 @prot.write_i64(i)
148 end
149 # try something out of signed range, it should clip
Bryan Duxburyc0166282009-02-02 00:48:17 +0000150 @trans.read(1000).should == ["\200\000\000\000\000\000\000\000",
151 "\377\377\364\303\035\244+]",
152 "\377\377\377\377\376\231:\341",
153 "\377\377\377\377\377\377\377\026",
154 "\000\000\000\000\000\000\000\000",
155 "\000\000\000\000\000\000\004\317",
156 "\000\000\000\000\000#\340\204",
157 "\000\000\000\002\340\311~\365",
158 "\177\377\377\377\377\377\377\377"].join("")
159 lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError)
David Reiss72754e12008-07-25 21:06:03 +0000160 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000161
Kevin Clark14fe7912008-08-04 18:46:19 +0000162 it "should error gracefully when trying to write a nil i64" do
163 lambda { @prot.write_i64(nil) }.should raise_error
164 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000165
David Reiss72754e12008-07-25 21:06:03 +0000166 it "should write a double" do
167 # try a random scattering of values, including min/max
Bryan Duxburyc0166282009-02-02 00:48:17 +0000168 values = [Float::MIN,-1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX]
169 values.each do |f|
David Reiss72754e12008-07-25 21:06:03 +0000170 @prot.write_double(f)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000171 @trans.read(1000).should == [f].pack("G")
David Reiss72754e12008-07-25 21:06:03 +0000172 end
173 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000174
Kevin Clark14fe7912008-08-04 18:46:19 +0000175 it "should error gracefully when trying to write a nil double" do
176 lambda { @prot.write_double(nil) }.should raise_error
177 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000178
David Reiss72754e12008-07-25 21:06:03 +0000179 it "should write a string" do
180 str = "hello world"
David Reiss72754e12008-07-25 21:06:03 +0000181 @prot.write_string(str)
Bryan Duxburyc0166282009-02-02 00:48:17 +0000182 @trans.read(1000).should == [str.size].pack("N") + str
David Reiss72754e12008-07-25 21:06:03 +0000183 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000184
Kevin Clark14fe7912008-08-04 18:46:19 +0000185 it "should error gracefully when trying to write a nil string" do
186 lambda { @prot.write_string(nil) }.should raise_error
187 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000188
David Reiss72754e12008-07-25 21:06:03 +0000189 # message footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000190
David Reiss72754e12008-07-25 21:06:03 +0000191 it "should read a field header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000192 @trans.write([Thrift::Types::STRING, 3].pack("cn"))
David Reiss72754e12008-07-25 21:06:03 +0000193 @prot.read_field_begin.should == [nil, Thrift::Types::STRING, 3]
194 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000195
David Reiss72754e12008-07-25 21:06:03 +0000196 # field footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000197
David Reiss72754e12008-07-25 21:06:03 +0000198 it "should read a stop field" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000199 @trans.write([Thrift::Types::STOP].pack("c"));
David Reiss72754e12008-07-25 21:06:03 +0000200 @prot.read_field_begin.should == [nil, Thrift::Types::STOP, 0]
201 end
202
203 it "should read a map header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000204 @trans.write([Thrift::Types::DOUBLE, Thrift::Types::I64, 42].pack("ccN"))
David Reiss72754e12008-07-25 21:06:03 +0000205 @prot.read_map_begin.should == [Thrift::Types::DOUBLE, Thrift::Types::I64, 42]
206 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000207
David Reiss72754e12008-07-25 21:06:03 +0000208 # map footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000209
David Reiss72754e12008-07-25 21:06:03 +0000210 it "should read a list header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000211 @trans.write([Thrift::Types::STRING, 17].pack("cN"))
David Reiss72754e12008-07-25 21:06:03 +0000212 @prot.read_list_begin.should == [Thrift::Types::STRING, 17]
213 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000214
David Reiss72754e12008-07-25 21:06:03 +0000215 # list footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000216
David Reiss72754e12008-07-25 21:06:03 +0000217 it "should read a set header" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000218 @trans.write([Thrift::Types::STRING, 17].pack("cN"))
219 @prot.read_set_begin.should == [Thrift::Types::STRING, 17]
David Reiss72754e12008-07-25 21:06:03 +0000220 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000221
David Reiss72754e12008-07-25 21:06:03 +0000222 # set footer is a noop
Bryan Duxburyc0166282009-02-02 00:48:17 +0000223
David Reiss72754e12008-07-25 21:06:03 +0000224 it "should read a bool" do
Bryan Duxburyc0166282009-02-02 00:48:17 +0000225 @trans.write("\001\000");
David Reiss72754e12008-07-25 21:06:03 +0000226 @prot.read_bool.should == true
227 @prot.read_bool.should == false
228 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000229
David Reiss72754e12008-07-25 21:06:03 +0000230 it "should read a byte" do
David Reiss72754e12008-07-25 21:06:03 +0000231 [-128, -57, -3, 0, 17, 24, 127].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000232 @trans.write([i].pack("c"))
David Reiss72754e12008-07-25 21:06:03 +0000233 @prot.read_byte.should == i
234 end
235 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000236
David Reiss72754e12008-07-25 21:06:03 +0000237 it "should read an i16" do
238 # try a scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000239 [-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000240 @trans.write([i].pack("n"));
David Reiss72754e12008-07-25 21:06:03 +0000241 @prot.read_i16.should == i
242 end
243 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000244
David Reiss72754e12008-07-25 21:06:03 +0000245 it "should read an i32" do
246 # try a scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000247 [-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000248 @trans.write([i].pack("N"))
David Reiss72754e12008-07-25 21:06:03 +0000249 @prot.read_i32.should == i
250 end
251 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000252
David Reiss72754e12008-07-25 21:06:03 +0000253 it "should read an i64" do
254 # try a scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000255 [-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000256 @trans.write([i >> 32, i & 0xFFFFFFFF].pack("NN"))
David Reiss72754e12008-07-25 21:06:03 +0000257 @prot.read_i64.should == i
258 end
259 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000260
David Reiss72754e12008-07-25 21:06:03 +0000261 it "should read a double" do
262 # try a random scattering of values, including min/max
David Reiss72754e12008-07-25 21:06:03 +0000263 [Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
Bryan Duxburyc0166282009-02-02 00:48:17 +0000264 @trans.write([f].pack("G"));
David Reiss72754e12008-07-25 21:06:03 +0000265 @prot.read_double.should == f
266 end
267 end
Bryan Duxburyc0166282009-02-02 00:48:17 +0000268
David Reiss72754e12008-07-25 21:06:03 +0000269 it "should read a string" do
270 str = "hello world"
Bryan Duxburyc0166282009-02-02 00:48:17 +0000271 @trans.write([str.size].pack("N") + str)
David Reiss72754e12008-07-25 21:06:03 +0000272 @prot.read_string.should == str
273 end
274end