blob: 96bafefb85176b9ac79e8a7457d01f03a0dfffa8 [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
80 @trans.rspec_clear
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)
Kevin Clarkf18b6432008-06-18 01:07:10 +000087 end
88
89 it "should write an i16" do
90 # try a random scattering of values
Kevin Clark5ae03842008-06-18 01:07:37 +000091 # include the signed i16 minimum/maximum
Kevin Clarkf18b6432008-06-18 01:07:10 +000092 @trans.should_receive(:write).with("\200\000").ordered
93 @trans.should_receive(:write).with("\374\000").ordered
94 @trans.should_receive(:write).with("\000\021").ordered
95 @trans.should_receive(:write).with("\000\000").ordered
96 @trans.should_receive(:write).with("\330\360").ordered
97 @trans.should_receive(:write).with("\006\273").ordered
Kevin Clark5ae03842008-06-18 01:07:37 +000098 @trans.should_receive(:write).with("\177\377").ordered
99 [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i|
Kevin Clarkf18b6432008-06-18 01:07:10 +0000100 @prot.write_i16(i)
101 end
Kevin Clark378191a2008-06-18 01:08:19 +0000102 # and try something out of signed range, it should clip
103 @trans.should_receive(:write).with("\200\005").ordered
104 @prot.write_i16(2**15 + 5)
105 # a Bignum should error
106 lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
Kevin Clarkf18b6432008-06-18 01:07:10 +0000107 end
108
109 it "should write an i32" do
110 # try a random scattering of values
Kevin Clark5ae03842008-06-18 01:07:37 +0000111 # include the signed i32 minimum/maximum
Kevin Clarkf18b6432008-06-18 01:07:10 +0000112 @trans.should_receive(:write).with("\200\000\000\000").ordered
113 @trans.should_receive(:write).with("\377\376\037\r").ordered
114 @trans.should_receive(:write).with("\377\377\366\034").ordered
115 @trans.should_receive(:write).with("\377\377\377\375").ordered
116 @trans.should_receive(:write).with("\000\000\000\000").ordered
117 @trans.should_receive(:write).with("\000#\340\203").ordered
118 @trans.should_receive(:write).with("\000\0000+").ordered
Kevin Clark5ae03842008-06-18 01:07:37 +0000119 @trans.should_receive(:write).with("\177\377\377\377").ordered
120 [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i|
Kevin Clarkf18b6432008-06-18 01:07:10 +0000121 @prot.write_i32(i)
122 end
Kevin Clark378191a2008-06-18 01:08:19 +0000123 # try something out of signed range, it should clip
124 @trans.should_receive(:write).with("\200\000\000\005").ordered
125 @prot.write_i32(2 ** 31 + 5)
126 lambda { @prot.write_i32(2 ** 65 + 5) }.should raise_error(RangeError)
Kevin Clarkf18b6432008-06-18 01:07:10 +0000127 end
128
129 it "should write an i64" do
130 # try a random scattering of values
Kevin Clark5ae03842008-06-18 01:07:37 +0000131 # try the signed i64 minimum/maximum
Kevin Clarkf18b6432008-06-18 01:07:10 +0000132 @trans.should_receive(:write).with("\200\000\000\000\000\000\000\000").ordered
133 @trans.should_receive(:write).with("\377\377\364\303\035\244+]").ordered
134 @trans.should_receive(:write).with("\377\377\377\377\376\231:\341").ordered
135 @trans.should_receive(:write).with("\377\377\377\377\377\377\377\026").ordered
136 @trans.should_receive(:write).with("\000\000\000\000\000\000\000\000").ordered
137 @trans.should_receive(:write).with("\000\000\000\000\000\000\004\317").ordered
138 @trans.should_receive(:write).with("\000\000\000\000\000#\340\204").ordered
139 @trans.should_receive(:write).with("\000\000\000\002\340\311~\365").ordered
Kevin Clark5ae03842008-06-18 01:07:37 +0000140 @trans.should_receive(:write).with("\177\377\377\377\377\377\377\377").ordered
141 [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i|
Kevin Clarkf18b6432008-06-18 01:07:10 +0000142 @prot.write_i64(i)
143 end
Kevin Clark378191a2008-06-18 01:08:19 +0000144 # try something out of signed range, it should clip
145 @trans.should_receive(:write).with("\200\000\000\000\000\000\000\005").ordered
146 @prot.write_i64(2**63 + 5)
147 lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError)
Kevin Clarkf18b6432008-06-18 01:07:10 +0000148 end
149
150 it "should write a double" do
Kevin Clark5ae03842008-06-18 01:07:37 +0000151 # try a random scattering of values, including min/max
Kevin Clarkf18b6432008-06-18 01:07:10 +0000152 @trans.should_receive(:write).with("\000\020\000\000\000\000\000\000").ordered
153 @trans.should_receive(:write).with("\300\223<\234\355\221hs").ordered
154 @trans.should_receive(:write).with("\300\376\0173\256\024z\341").ordered
155 @trans.should_receive(:write).with("\3007<2\336\372v\324").ordered
156 @trans.should_receive(:write).with("\000\000\000\000\000\000\000\000").ordered
157 @trans.should_receive(:write).with("@\310\037\220\365\302\217\\").ordered
158 @trans.should_receive(:write).with("@\200Y\327\n=p\244").ordered
159 @trans.should_receive(:write).with("\177\357\377\377\377\377\377\377").ordered
160 [Float::MIN, -1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX].each do |f|
161 @prot.write_double(f)
162 end
163 end
164
165 it "should write a string" do
166 str = "hello world"
167 @prot.should_receive(:write_i32).with(str.length).ordered
168 @trans.should_receive(:write).with(str).ordered
169 @prot.write_string(str)
170 end
Kevin Clark5ae03842008-06-18 01:07:37 +0000171
172 it "should read a message header" do
173 @prot.should_receive(:read_i32).and_return(BinaryProtocol::VERSION_1 | MessageTypes::REPLY, 42)
174 @prot.should_receive(:read_string).and_return('testMessage')
175 @prot.read_message_begin.should == ['testMessage', MessageTypes::REPLY, 42]
176 end
177
Kevin Clarkc13c33b2008-06-18 01:12:48 +0000178 it "should raise an exception if the message header has the wrong version" do
179 @prot.should_receive(:read_i32).and_return(42)
180 lambda { @prot.read_message_begin }.should raise_error(ProtocolException, 'Missing version identifier') { |e| e.type == ProtocolException::BAD_VERSION }
181 end
182
183 # message footer is a noop
Kevin Clark5ae03842008-06-18 01:07:37 +0000184
185 it "should read a field header" do
186 @prot.should_receive(:read_byte).ordered.and_return(Types::STRING)
187 @prot.should_receive(:read_i16).ordered.and_return(3)
188 @prot.read_field_begin.should == [nil, Types::STRING, 3]
189 end
190
191 # field footer is a noop
192
193 it "should read a stop field" do
194 @prot.should_receive(:read_byte).and_return(Types::STOP)
195 @prot.should_not_receive(:read_i16)
196 @prot.read_field_begin.should == [nil, Types::STOP, 0]
197 end
198
199 it "should read a map header" do
200 @prot.should_receive(:read_byte).and_return(Types::DOUBLE, Types::I64)
201 @prot.should_receive(:read_i32).and_return(42)
202 @prot.read_map_begin.should == [Types::DOUBLE, Types::I64, 42]
203 end
204
205 # map footer is a noop
206
207 it "should read a list header" do
208 @prot.should_receive(:read_byte).ordered.and_return(Types::STRING)
209 @prot.should_receive(:read_i32).and_return(17)
210 @prot.read_list_begin.should == [Types::STRING, 17]
211 end
212
213 # list footer is a noop
214
215 it "should read a set header" do
216 @prot.should_receive(:read_byte).ordered.and_return(Types::MAP)
217 @prot.should_receive(:read_i32).ordered.and_return(42)
218 @prot.read_set_begin.should == [Types::MAP, 42]
219 end
220
221 # set footer is a noop
222
223 it "should read a bool" do
224 @prot.should_receive(:read_byte).and_return(1, 0)
225 @prot.read_bool.should == true
226 @prot.read_bool.should == false
227 end
228
229 it "should read a byte" do
230 # try a scattering of values, including min/max
231 @trans.should_receive(:read_all).with(1).and_return(
232 "\200", "\307", "\375",
233 "\000", "\021", "\030", "\177"
234 )
235 [-128, -57, -3, 0, 17, 24, 127].each do |i|
236 @prot.read_byte.should == i
237 end
238 end
239
240 it "should read an i16" do
241 # try a scattering of values, including min/max
242 @trans.should_receive(:read_all).with(2).and_return(
243 "\200\000", "\353\213", "\376\237",
244 "\000\000", "\005\367", "\b\272", "\177\377"
245 )
246 [-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
247 @prot.read_i16.should == i
248 end
249 end
250
251 it "should read an i32" do
252 # try a scattering of values, including min/max
253 @trans.should_receive(:read_all).with(4).and_return(
254 "\200\000\000\000", "\377\374i\213", "\377\377\347\244",
255 "\000\000\000\000", "\000\000\t/", "\000\001\340\363", "\177\377\377\377"
256 )
257 [-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
258 @prot.read_i32.should == i
259 end
260 end
261
262 it "should read an i64" do
263 # try a scattering of values, including min/max
264 @trans.should_receive(:read_all).with(8).and_return(
265 "\200\000\000\000\000\000\000\000", "\377\377\377\377\370\243Z\b",
266 "\377\377\377\377\377\377\3476", "\000\000\000\000\000\000\000\000",
267 "\000\000\000\000\000\000\000 ", "\000\000\000\000\213\332\t\223",
268 "\177\377\377\377\377\377\377\377"
269 )
270 [-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
271 @prot.read_i64.should == i
272 end
273 end
274
275 it "should read a double" do
276 # try a random scattering of values, including min/max
277 @trans.should_receive(:read_all).with(8).and_return(
278 "\000\020\000\000\000\000\000\000", "\301\f9\370\374\362\317\226",
279 "\300t3\274x \243\016", "\000\000\000\000\000\000\000\000", "@^\317\fCo\301Y",
280 "AA\360A\217\317@\260", "\177\357\377\377\377\377\377\377"
281 )
282 [Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
283 @prot.read_double.should == f
284 end
285 end
286
287 it "should read a string" do
288 str = "hello world"
289 @prot.should_receive(:read_i32).and_return(str.length)
290 @trans.should_receive(:read_all).with(str.length).and_return(str)
291 @prot.read_string.should == str
292 end
Kevin Clarkf18b6432008-06-18 01:07:10 +0000293 end
Kevin Clarke977a632008-06-18 01:07:50 +0000294
295 describe BinaryProtocolFactory do
296 it "should create a BinaryProtocol" do
297 BinaryProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of BinaryProtocol
298 end
299 end
Kevin Clarkf18b6432008-06-18 01:07:10 +0000300end