blob: 6e744d3a86e6223e1688432d85efc8e80303c3e8 [file] [log] [blame]
Kevin Clarkf18b6432008-06-18 01:07:10 +00001require File.dirname(__FILE__) + '/spec_helper'
2require 'thrift/protocol/binaryprotocol'
3
Kevin Clark356f8612008-06-18 01:08:05 +00004class ThriftBinaryProtocolSpec < Spec::ExampleGroup
Kevin Clarkf18b6432008-06-18 01:07:10 +00005 include Thrift
6
7 describe BinaryProtocol do
8 before(:each) do
9 @trans = mock("MockTransport", :null_object => true)
10 @prot = BinaryProtocol.new(@trans)
11 end
12
13 it "should define the proper VERSION_1 and VERSION_MASK" do
14 BinaryProtocol::VERSION_MASK.should == 0xffff0000
15 BinaryProtocol::VERSION_1.should == 0x80010000
16 end
17
18 it "should write the message header" do
19 @prot.should_receive(:write_i32).with(BinaryProtocol::VERSION_1 | MessageTypes::CALL).ordered
20 @prot.should_receive(:write_string).with('testMessage').ordered
21 @prot.should_receive(:write_i32).with(17).ordered
22 @prot.write_message_begin('testMessage', MessageTypes::CALL, 17)
23 end
24
25 # message footer is a noop
26
27 it "should write the field header" do
28 @prot.should_receive(:write_byte).with(Types::DOUBLE).ordered
29 @prot.should_receive(:write_i16).with(3).ordered
30 @prot.write_field_begin('foo', Types::DOUBLE, 3)
31 end
32
33 # field footer is a noop
34
35 it "should write the STOP field" do
36 @prot.should_receive(:write_byte).with(Types::STOP)
37 @prot.write_field_stop
38 end
39
40 it "should write the map header" do
41 @prot.should_receive(:write_byte).with(Types::STRING).ordered
42 @prot.should_receive(:write_byte).with(Types::LIST).ordered
43 @prot.should_receive(:write_i32).with(17).ordered
44 @prot.write_map_begin(Types::STRING, Types::LIST, 17)
45 end
46
47 # map footer is a noop
48
49 it "should write the list header" do
50 @prot.should_receive(:write_byte).with(Types::I16).ordered
51 @prot.should_receive(:write_i32).with(42).ordered
52 @prot.write_list_begin(Types::I16, 42)
53 end
54
55 # list footer is a noop
56
57 it "should write the set header" do
58 @prot.should_receive(:write_byte).with(Types::BOOL).ordered
59 @prot.should_receive(:write_i32).with(2).ordered
60 @prot.write_set_begin(Types::BOOL, 2)
61 end
62
63 it "should write a bool" do
64 @prot.should_receive(:write_byte).with(1).ordered
65 @prot.write_bool(true)
66 @prot.should_receive(:write_byte).with(0).ordered
67 @prot.write_bool(false)
68 end
69
70 it "should write a byte" do
Kevin Clark5ae03842008-06-18 01:07:37 +000071 # byte is small enough, let's check -128..127
72 (-128..127).each do |i|
73 @trans.should_receive(:write).with([i].pack('c')).ordered
Kevin Clark5ae03842008-06-18 01:07:37 +000074 @prot.write_byte(i)
75 end
Kevin Clark378191a2008-06-18 01:08:19 +000076 (-128..127).each do |i|
77 end
78 # handing it numbers out of signed range should clip
79 @trans.rspec_verify
Kevin Clark378191a2008-06-18 01:08:19 +000080 (128..255).each do |i|
81 @trans.should_receive(:write).with([i].pack('c')).ordered
82 @prot.write_byte(i)
83 end
84 # and lastly, a Bignum is going to error out
85 lambda { @prot.write_byte(2**65) }.should raise_error(RangeError)
Kevin Clarkf18b6432008-06-18 01:07:10 +000086 end
87
88 it "should write an i16" do
89 # try a random scattering of values
Kevin Clark5ae03842008-06-18 01:07:37 +000090 # include the signed i16 minimum/maximum
Kevin Clarkf18b6432008-06-18 01:07:10 +000091 @trans.should_receive(:write).with("\200\000").ordered
92 @trans.should_receive(:write).with("\374\000").ordered
93 @trans.should_receive(:write).with("\000\021").ordered
94 @trans.should_receive(:write).with("\000\000").ordered
95 @trans.should_receive(:write).with("\330\360").ordered
96 @trans.should_receive(:write).with("\006\273").ordered
Kevin Clark5ae03842008-06-18 01:07:37 +000097 @trans.should_receive(:write).with("\177\377").ordered
98 [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i|
Kevin Clarkf18b6432008-06-18 01:07:10 +000099 @prot.write_i16(i)
100 end
Kevin Clark378191a2008-06-18 01:08:19 +0000101 # and try something out of signed range, it should clip
102 @trans.should_receive(:write).with("\200\005").ordered
103 @prot.write_i16(2**15 + 5)
104 # a Bignum should error
Kevin Clark830f46a2008-06-18 01:19:51 +0000105 # lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
Kevin Clarkf18b6432008-06-18 01:07:10 +0000106 end
107
108 it "should write an i32" do
109 # try a random scattering of values
Kevin Clark5ae03842008-06-18 01:07:37 +0000110 # include the signed i32 minimum/maximum
Kevin Clarkf18b6432008-06-18 01:07:10 +0000111 @trans.should_receive(:write).with("\200\000\000\000").ordered
112 @trans.should_receive(:write).with("\377\376\037\r").ordered
113 @trans.should_receive(:write).with("\377\377\366\034").ordered
114 @trans.should_receive(:write).with("\377\377\377\375").ordered
115 @trans.should_receive(:write).with("\000\000\000\000").ordered
116 @trans.should_receive(:write).with("\000#\340\203").ordered
117 @trans.should_receive(:write).with("\000\0000+").ordered
Kevin Clark5ae03842008-06-18 01:07:37 +0000118 @trans.should_receive(:write).with("\177\377\377\377").ordered
119 [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i|
Kevin Clarkf18b6432008-06-18 01:07:10 +0000120 @prot.write_i32(i)
121 end
Kevin Clark378191a2008-06-18 01:08:19 +0000122 # try something out of signed range, it should clip
123 @trans.should_receive(:write).with("\200\000\000\005").ordered
124 @prot.write_i32(2 ** 31 + 5)
Kevin Clark830f46a2008-06-18 01:19:51 +0000125 # lambda { @prot.write_i32(2 ** 65 + 5) }.should raise_error(RangeError)
Kevin Clarkf18b6432008-06-18 01:07:10 +0000126 end
127
128 it "should write an i64" do
129 # try a random scattering of values
Kevin Clark5ae03842008-06-18 01:07:37 +0000130 # try the signed i64 minimum/maximum
Kevin Clarkf18b6432008-06-18 01:07:10 +0000131 @trans.should_receive(:write).with("\200\000\000\000\000\000\000\000").ordered
132 @trans.should_receive(:write).with("\377\377\364\303\035\244+]").ordered
133 @trans.should_receive(:write).with("\377\377\377\377\376\231:\341").ordered
134 @trans.should_receive(:write).with("\377\377\377\377\377\377\377\026").ordered
135 @trans.should_receive(:write).with("\000\000\000\000\000\000\000\000").ordered
136 @trans.should_receive(:write).with("\000\000\000\000\000\000\004\317").ordered
137 @trans.should_receive(:write).with("\000\000\000\000\000#\340\204").ordered
138 @trans.should_receive(:write).with("\000\000\000\002\340\311~\365").ordered
Kevin Clark5ae03842008-06-18 01:07:37 +0000139 @trans.should_receive(:write).with("\177\377\377\377\377\377\377\377").ordered
140 [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i|
Kevin Clarkf18b6432008-06-18 01:07:10 +0000141 @prot.write_i64(i)
142 end
Kevin Clark378191a2008-06-18 01:08:19 +0000143 # try something out of signed range, it should clip
144 @trans.should_receive(:write).with("\200\000\000\000\000\000\000\005").ordered
145 @prot.write_i64(2**63 + 5)
Kevin Clark830f46a2008-06-18 01:19:51 +0000146 # lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError)
Kevin Clarkf18b6432008-06-18 01:07:10 +0000147 end
148
149 it "should write a double" do
Kevin Clark5ae03842008-06-18 01:07:37 +0000150 # try a random scattering of values, including min/max
Kevin Clark830f46a2008-06-18 01:19:51 +0000151 @trans.should_receive(:write).with([Float::MIN].pack('G')).ordered
Kevin Clarkf18b6432008-06-18 01:07:10 +0000152 @trans.should_receive(:write).with("\300\223<\234\355\221hs").ordered
153 @trans.should_receive(:write).with("\300\376\0173\256\024z\341").ordered
154 @trans.should_receive(:write).with("\3007<2\336\372v\324").ordered
155 @trans.should_receive(:write).with("\000\000\000\000\000\000\000\000").ordered
156 @trans.should_receive(:write).with("@\310\037\220\365\302\217\\").ordered
157 @trans.should_receive(:write).with("@\200Y\327\n=p\244").ordered
Kevin Clark830f46a2008-06-18 01:19:51 +0000158 @trans.should_receive(:write).with([Float::MAX].pack('G')).ordered
Kevin Clarkf18b6432008-06-18 01:07:10 +0000159 [Float::MIN, -1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX].each do |f|
160 @prot.write_double(f)
161 end
162 end
163
164 it "should write a string" do
165 str = "hello world"
166 @prot.should_receive(:write_i32).with(str.length).ordered
167 @trans.should_receive(:write).with(str).ordered
168 @prot.write_string(str)
169 end
Kevin Clark5ae03842008-06-18 01:07:37 +0000170
171 it "should read a message header" do
172 @prot.should_receive(:read_i32).and_return(BinaryProtocol::VERSION_1 | MessageTypes::REPLY, 42)
173 @prot.should_receive(:read_string).and_return('testMessage')
174 @prot.read_message_begin.should == ['testMessage', MessageTypes::REPLY, 42]
175 end
176
Kevin Clarkc13c33b2008-06-18 01:12:48 +0000177 it "should raise an exception if the message header has the wrong version" do
178 @prot.should_receive(:read_i32).and_return(42)
179 lambda { @prot.read_message_begin }.should raise_error(ProtocolException, 'Missing version identifier') { |e| e.type == ProtocolException::BAD_VERSION }
180 end
181
182 # message footer is a noop
Kevin Clark5ae03842008-06-18 01:07:37 +0000183
184 it "should read a field header" do
185 @prot.should_receive(:read_byte).ordered.and_return(Types::STRING)
186 @prot.should_receive(:read_i16).ordered.and_return(3)
187 @prot.read_field_begin.should == [nil, Types::STRING, 3]
188 end
189
190 # field footer is a noop
191
192 it "should read a stop field" do
193 @prot.should_receive(:read_byte).and_return(Types::STOP)
194 @prot.should_not_receive(:read_i16)
195 @prot.read_field_begin.should == [nil, Types::STOP, 0]
196 end
197
198 it "should read a map header" do
199 @prot.should_receive(:read_byte).and_return(Types::DOUBLE, Types::I64)
200 @prot.should_receive(:read_i32).and_return(42)
201 @prot.read_map_begin.should == [Types::DOUBLE, Types::I64, 42]
202 end
203
204 # map footer is a noop
205
206 it "should read a list header" do
207 @prot.should_receive(:read_byte).ordered.and_return(Types::STRING)
208 @prot.should_receive(:read_i32).and_return(17)
209 @prot.read_list_begin.should == [Types::STRING, 17]
210 end
211
212 # list footer is a noop
213
214 it "should read a set header" do
215 @prot.should_receive(:read_byte).ordered.and_return(Types::MAP)
216 @prot.should_receive(:read_i32).ordered.and_return(42)
217 @prot.read_set_begin.should == [Types::MAP, 42]
218 end
219
220 # set footer is a noop
221
222 it "should read a bool" do
223 @prot.should_receive(:read_byte).and_return(1, 0)
224 @prot.read_bool.should == true
225 @prot.read_bool.should == false
226 end
227
228 it "should read a byte" do
229 # try a scattering of values, including min/max
230 @trans.should_receive(:read_all).with(1).and_return(
231 "\200", "\307", "\375",
232 "\000", "\021", "\030", "\177"
233 )
234 [-128, -57, -3, 0, 17, 24, 127].each do |i|
235 @prot.read_byte.should == i
236 end
237 end
238
239 it "should read an i16" do
240 # try a scattering of values, including min/max
241 @trans.should_receive(:read_all).with(2).and_return(
242 "\200\000", "\353\213", "\376\237",
243 "\000\000", "\005\367", "\b\272", "\177\377"
244 )
245 [-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
246 @prot.read_i16.should == i
247 end
248 end
249
250 it "should read an i32" do
251 # try a scattering of values, including min/max
252 @trans.should_receive(:read_all).with(4).and_return(
253 "\200\000\000\000", "\377\374i\213", "\377\377\347\244",
254 "\000\000\000\000", "\000\000\t/", "\000\001\340\363", "\177\377\377\377"
255 )
256 [-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
257 @prot.read_i32.should == i
258 end
259 end
260
261 it "should read an i64" do
262 # try a scattering of values, including min/max
263 @trans.should_receive(:read_all).with(8).and_return(
264 "\200\000\000\000\000\000\000\000", "\377\377\377\377\370\243Z\b",
265 "\377\377\377\377\377\377\3476", "\000\000\000\000\000\000\000\000",
266 "\000\000\000\000\000\000\000 ", "\000\000\000\000\213\332\t\223",
267 "\177\377\377\377\377\377\377\377"
268 )
269 [-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
270 @prot.read_i64.should == i
271 end
272 end
273
274 it "should read a double" do
275 # try a random scattering of values, including min/max
276 @trans.should_receive(:read_all).with(8).and_return(
Kevin Clark830f46a2008-06-18 01:19:51 +0000277 [Float::MIN].pack('G'), "\301\f9\370\374\362\317\226",
Kevin Clark5ae03842008-06-18 01:07:37 +0000278 "\300t3\274x \243\016", "\000\000\000\000\000\000\000\000", "@^\317\fCo\301Y",
Kevin Clark830f46a2008-06-18 01:19:51 +0000279 "AA\360A\217\317@\260", [Float::MAX].pack('G')
Kevin Clark5ae03842008-06-18 01:07:37 +0000280 )
281 [Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
282 @prot.read_double.should == f
283 end
284 end
285
286 it "should read a string" do
287 str = "hello world"
288 @prot.should_receive(:read_i32).and_return(str.length)
289 @trans.should_receive(:read_all).with(str.length).and_return(str)
290 @prot.read_string.should == str
291 end
Kevin Clarkf18b6432008-06-18 01:07:10 +0000292 end
Kevin Clarke977a632008-06-18 01:07:50 +0000293
294 describe BinaryProtocolFactory do
295 it "should create a BinaryProtocol" do
Kevin Clark90a2cbe2008-06-18 01:15:53 +0000296 BinaryProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocol)
Kevin Clarke977a632008-06-18 01:07:50 +0000297 end
298 end
Kevin Clarkf18b6432008-06-18 01:07:10 +0000299end