blob: ced15b4234877cffe236c877bd88cea77fce3624 [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
5 @trans = mock("MockTransport")
6 @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
15 @prot.should_receive(:write_i32).with(protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::CALL).ordered
16 @prot.should_receive(:write_string).with('testMessage').ordered
17 @prot.should_receive(:write_i32).with(17).ordered
18 @prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
19 end
20
21 # message footer is a noop
22
23 it "should write the field header" do
24 @prot.should_receive(:write_byte).with(Thrift::Types::DOUBLE).ordered
25 @prot.should_receive(:write_i16).with(3).ordered
26 @prot.write_field_begin('foo', Thrift::Types::DOUBLE, 3)
27 end
28
29 # field footer is a noop
30
31 it "should write the STOP field" do
32 @prot.should_receive(:write_byte).with(Thrift::Types::STOP)
33 @prot.write_field_stop
34 end
35
36 it "should write the map header" do
37 @prot.should_receive(:write_byte).with(Thrift::Types::STRING).ordered
38 @prot.should_receive(:write_byte).with(Thrift::Types::LIST).ordered
39 @prot.should_receive(:write_i32).with(17).ordered
40 @prot.write_map_begin(Thrift::Types::STRING, Thrift::Types::LIST, 17)
41 end
42
43 # map footer is a noop
44
45 it "should write the list header" do
46 @prot.should_receive(:write_byte).with(Thrift::Types::I16).ordered
47 @prot.should_receive(:write_i32).with(42).ordered
48 @prot.write_list_begin(Thrift::Types::I16, 42)
49 end
50
51 # list footer is a noop
52
53 it "should write the set header" do
54 @prot.should_receive(:write_byte).with(Thrift::Types::BOOL).ordered
55 @prot.should_receive(:write_i32).with(2).ordered
56 @prot.write_set_begin(Thrift::Types::BOOL, 2)
57 end
58
59 it "should write a bool" do
60 @prot.should_receive(:write_byte).with(1).ordered
61 @prot.write_bool(true)
62 @prot.should_receive(:write_byte).with(0).ordered
63 @prot.write_bool(false)
64 end
65
Kevin Clark14fe7912008-08-04 18:46:19 +000066 it "should treat a nil bool as false" do
67 @prot.should_receive(:write_byte).with(0)
68 @prot.write_bool(nil)
69 end
70
David Reiss72754e12008-07-25 21:06:03 +000071 it "should write a byte" do
72 # byte is small enough, let's check -128..127
73 (-128..127).each do |i|
74 @trans.should_receive(:write).with([i].pack('c')).ordered
75 @prot.write_byte(i)
76 end
77 (-128..127).each do |i|
78 end
79 # handing it numbers out of signed range should clip
80 @trans.rspec_verify
81 (128..255).each do |i|
82 @trans.should_receive(:write).with([i].pack('c')).ordered
83 @prot.write_byte(i)
84 end
85 # and lastly, a Bignum is going to error out
86 lambda { @prot.write_byte(2**65) }.should raise_error(RangeError)
87 end
88
Kevin Clark14fe7912008-08-04 18:46:19 +000089 it "should error gracefully when trying to write a nil byte" do
90 lambda { @prot.write_byte(nil) }.should raise_error
91 end
92
David Reiss72754e12008-07-25 21:06:03 +000093 it "should write an i16" do
94 # try a random scattering of values
95 # include the signed i16 minimum/maximum
96 @trans.should_receive(:write).with("\200\000").ordered
97 @trans.should_receive(:write).with("\374\000").ordered
98 @trans.should_receive(:write).with("\000\021").ordered
99 @trans.should_receive(:write).with("\000\000").ordered
100 @trans.should_receive(:write).with("\330\360").ordered
101 @trans.should_receive(:write).with("\006\273").ordered
102 @trans.should_receive(:write).with("\177\377").ordered
103 [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i|
104 @prot.write_i16(i)
105 end
106 # and try something out of signed range, it should clip
107 @trans.should_receive(:write).with("\200\005").ordered
108 @prot.write_i16(2**15 + 5)
109 # a Bignum should error
110 # lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
111 end
112
Kevin Clark14fe7912008-08-04 18:46:19 +0000113 it "should error gracefully when trying to write a nil i16" do
114 lambda { @prot.write_i16(nil) }.should raise_error
115 end
116
David Reiss72754e12008-07-25 21:06:03 +0000117 it "should write an i32" do
118 # try a random scattering of values
119 # include the signed i32 minimum/maximum
120 @trans.should_receive(:write).with("\200\000\000\000").ordered
121 @trans.should_receive(:write).with("\377\376\037\r").ordered
122 @trans.should_receive(:write).with("\377\377\366\034").ordered
123 @trans.should_receive(:write).with("\377\377\377\375").ordered
124 @trans.should_receive(:write).with("\000\000\000\000").ordered
125 @trans.should_receive(:write).with("\000#\340\203").ordered
126 @trans.should_receive(:write).with("\000\0000+").ordered
127 @trans.should_receive(:write).with("\177\377\377\377").ordered
128 [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i|
129 @prot.write_i32(i)
130 end
131 # try something out of signed range, it should clip
132 @trans.should_receive(:write).with("\200\000\000\005").ordered
133 @prot.write_i32(2 ** 31 + 5)
134 # lambda { @prot.write_i32(2 ** 65 + 5) }.should raise_error(RangeError)
135 end
136
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
140
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
144 @trans.should_receive(:write).with("\200\000\000\000\000\000\000\000").ordered
145 @trans.should_receive(:write).with("\377\377\364\303\035\244+]").ordered
146 @trans.should_receive(:write).with("\377\377\377\377\376\231:\341").ordered
147 @trans.should_receive(:write).with("\377\377\377\377\377\377\377\026").ordered
148 @trans.should_receive(:write).with("\000\000\000\000\000\000\000\000").ordered
149 @trans.should_receive(:write).with("\000\000\000\000\000\000\004\317").ordered
150 @trans.should_receive(:write).with("\000\000\000\000\000#\340\204").ordered
151 @trans.should_receive(:write).with("\000\000\000\002\340\311~\365").ordered
152 @trans.should_receive(:write).with("\177\377\377\377\377\377\377\377").ordered
153 [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i|
154 @prot.write_i64(i)
155 end
156 # try something out of signed range, it should clip
157 @trans.should_receive(:write).with("\200\000\000\000\000\000\000\005").ordered
158 @prot.write_i64(2**63 + 5)
159 # lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError)
160 end
161
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
165
David Reiss72754e12008-07-25 21:06:03 +0000166 it "should write a double" do
167 # try a random scattering of values, including min/max
168 @trans.should_receive(:write).with([Float::MIN].pack('G')).ordered
169 @trans.should_receive(:write).with("\300\223<\234\355\221hs").ordered
170 @trans.should_receive(:write).with("\300\376\0173\256\024z\341").ordered
171 @trans.should_receive(:write).with("\3007<2\336\372v\324").ordered
172 @trans.should_receive(:write).with("\000\000\000\000\000\000\000\000").ordered
173 @trans.should_receive(:write).with("@\310\037\220\365\302\217\\").ordered
174 @trans.should_receive(:write).with("@\200Y\327\n=p\244").ordered
175 @trans.should_receive(:write).with([Float::MAX].pack('G')).ordered
176 [Float::MIN, -1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX].each do |f|
177 @prot.write_double(f)
178 end
179 end
180
Kevin Clark14fe7912008-08-04 18:46:19 +0000181 it "should error gracefully when trying to write a nil double" do
182 lambda { @prot.write_double(nil) }.should raise_error
183 end
184
David Reiss72754e12008-07-25 21:06:03 +0000185 it "should write a string" do
186 str = "hello world"
187 @prot.should_receive(:write_i32).with(str.length).ordered
188 @trans.should_receive(:write).with(str).ordered
189 @prot.write_string(str)
190 end
191
Kevin Clark14fe7912008-08-04 18:46:19 +0000192 it "should error gracefully when trying to write a nil string" do
193 lambda { @prot.write_string(nil) }.should raise_error
194 end
195
David Reiss72754e12008-07-25 21:06:03 +0000196 # message footer is a noop
197
198 it "should read a field header" do
199 @prot.should_receive(:read_byte).ordered.and_return(Thrift::Types::STRING)
200 @prot.should_receive(:read_i16).ordered.and_return(3)
201 @prot.read_field_begin.should == [nil, Thrift::Types::STRING, 3]
202 end
203
204 # field footer is a noop
205
206 it "should read a stop field" do
207 @prot.should_receive(:read_byte).and_return(Thrift::Types::STOP)
208 @prot.should_not_receive(:read_i16)
209 @prot.read_field_begin.should == [nil, Thrift::Types::STOP, 0]
210 end
211
212 it "should read a map header" do
213 @prot.should_receive(:read_byte).and_return(Thrift::Types::DOUBLE, Thrift::Types::I64)
214 @prot.should_receive(:read_i32).and_return(42)
215 @prot.read_map_begin.should == [Thrift::Types::DOUBLE, Thrift::Types::I64, 42]
216 end
217
218 # map footer is a noop
219
220 it "should read a list header" do
221 @prot.should_receive(:read_byte).ordered.and_return(Thrift::Types::STRING)
222 @prot.should_receive(:read_i32).and_return(17)
223 @prot.read_list_begin.should == [Thrift::Types::STRING, 17]
224 end
225
226 # list footer is a noop
227
228 it "should read a set header" do
229 @prot.should_receive(:read_byte).ordered.and_return(Thrift::Types::MAP)
230 @prot.should_receive(:read_i32).ordered.and_return(42)
231 @prot.read_set_begin.should == [Thrift::Types::MAP, 42]
232 end
233
234 # set footer is a noop
235
236 it "should read a bool" do
237 @prot.should_receive(:read_byte).and_return(1, 0)
238 @prot.read_bool.should == true
239 @prot.read_bool.should == false
240 end
241
242 it "should read a byte" do
243 # try a scattering of values, including min/max
244 @trans.should_receive(:read_all).with(1).and_return(
245 "\200", "\307", "\375",
246 "\000", "\021", "\030", "\177"
247 )
248 [-128, -57, -3, 0, 17, 24, 127].each do |i|
249 @prot.read_byte.should == i
250 end
251 end
252
253 it "should read an i16" do
254 # try a scattering of values, including min/max
255 @trans.should_receive(:read_all).with(2).and_return(
256 "\200\000", "\353\213", "\376\237",
257 "\000\000", "\005\367", "\b\272", "\177\377"
258 )
259 [-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
260 @prot.read_i16.should == i
261 end
262 end
263
264 it "should read an i32" do
265 # try a scattering of values, including min/max
266 @trans.should_receive(:read_all).with(4).and_return(
267 "\200\000\000\000", "\377\374i\213", "\377\377\347\244",
268 "\000\000\000\000", "\000\000\t/", "\000\001\340\363", "\177\377\377\377"
269 )
270 [-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
271 @prot.read_i32.should == i
272 end
273 end
274
275 it "should read an i64" do
276 # try a scattering of values, including min/max
277 @trans.should_receive(:read_all).with(8).and_return(
278 "\200\000\000\000\000\000\000\000", "\377\377\377\377\370\243Z\b",
279 "\377\377\377\377\377\377\3476", "\000\000\000\000\000\000\000\000",
280 "\000\000\000\000\000\000\000 ", "\000\000\000\000\213\332\t\223",
281 "\177\377\377\377\377\377\377\377"
282 )
283 [-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
284 @prot.read_i64.should == i
285 end
286 end
287
288 it "should read a double" do
289 # try a random scattering of values, including min/max
290 @trans.should_receive(:read_all).with(8).and_return(
291 [Float::MIN].pack('G'), "\301\f9\370\374\362\317\226",
292 "\300t3\274x \243\016", "\000\000\000\000\000\000\000\000", "@^\317\fCo\301Y",
293 "AA\360A\217\317@\260", [Float::MAX].pack('G')
294 )
295 [Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
296 @prot.read_double.should == f
297 end
298 end
299
300 it "should read a string" do
301 str = "hello world"
302 @prot.should_receive(:read_i32).and_return(str.length)
303 @trans.should_receive(:read_all).with(str.length).and_return(str)
304 @prot.read_string.should == str
305 end
306end