blob: 9a4af6bc8f0ea72bb3d2cbd036d8d8f68cb3848a [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
66 it "should write a byte" do
67 # byte is small enough, let's check -128..127
68 (-128..127).each do |i|
69 @trans.should_receive(:write).with([i].pack('c')).ordered
70 @prot.write_byte(i)
71 end
72 (-128..127).each do |i|
73 end
74 # handing it numbers out of signed range should clip
75 @trans.rspec_verify
76 (128..255).each do |i|
77 @trans.should_receive(:write).with([i].pack('c')).ordered
78 @prot.write_byte(i)
79 end
80 # and lastly, a Bignum is going to error out
81 lambda { @prot.write_byte(2**65) }.should raise_error(RangeError)
82 end
83
84 it "should write an i16" do
85 # try a random scattering of values
86 # include the signed i16 minimum/maximum
87 @trans.should_receive(:write).with("\200\000").ordered
88 @trans.should_receive(:write).with("\374\000").ordered
89 @trans.should_receive(:write).with("\000\021").ordered
90 @trans.should_receive(:write).with("\000\000").ordered
91 @trans.should_receive(:write).with("\330\360").ordered
92 @trans.should_receive(:write).with("\006\273").ordered
93 @trans.should_receive(:write).with("\177\377").ordered
94 [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i|
95 @prot.write_i16(i)
96 end
97 # and try something out of signed range, it should clip
98 @trans.should_receive(:write).with("\200\005").ordered
99 @prot.write_i16(2**15 + 5)
100 # a Bignum should error
101 # lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
102 end
103
104 it "should write an i32" do
105 # try a random scattering of values
106 # include the signed i32 minimum/maximum
107 @trans.should_receive(:write).with("\200\000\000\000").ordered
108 @trans.should_receive(:write).with("\377\376\037\r").ordered
109 @trans.should_receive(:write).with("\377\377\366\034").ordered
110 @trans.should_receive(:write).with("\377\377\377\375").ordered
111 @trans.should_receive(:write).with("\000\000\000\000").ordered
112 @trans.should_receive(:write).with("\000#\340\203").ordered
113 @trans.should_receive(:write).with("\000\0000+").ordered
114 @trans.should_receive(:write).with("\177\377\377\377").ordered
115 [-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i|
116 @prot.write_i32(i)
117 end
118 # try something out of signed range, it should clip
119 @trans.should_receive(:write).with("\200\000\000\005").ordered
120 @prot.write_i32(2 ** 31 + 5)
121 # lambda { @prot.write_i32(2 ** 65 + 5) }.should raise_error(RangeError)
122 end
123
124 it "should write an i64" do
125 # try a random scattering of values
126 # try the signed i64 minimum/maximum
127 @trans.should_receive(:write).with("\200\000\000\000\000\000\000\000").ordered
128 @trans.should_receive(:write).with("\377\377\364\303\035\244+]").ordered
129 @trans.should_receive(:write).with("\377\377\377\377\376\231:\341").ordered
130 @trans.should_receive(:write).with("\377\377\377\377\377\377\377\026").ordered
131 @trans.should_receive(:write).with("\000\000\000\000\000\000\000\000").ordered
132 @trans.should_receive(:write).with("\000\000\000\000\000\000\004\317").ordered
133 @trans.should_receive(:write).with("\000\000\000\000\000#\340\204").ordered
134 @trans.should_receive(:write).with("\000\000\000\002\340\311~\365").ordered
135 @trans.should_receive(:write).with("\177\377\377\377\377\377\377\377").ordered
136 [-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i|
137 @prot.write_i64(i)
138 end
139 # try something out of signed range, it should clip
140 @trans.should_receive(:write).with("\200\000\000\000\000\000\000\005").ordered
141 @prot.write_i64(2**63 + 5)
142 # lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError)
143 end
144
145 it "should write a double" do
146 # try a random scattering of values, including min/max
147 @trans.should_receive(:write).with([Float::MIN].pack('G')).ordered
148 @trans.should_receive(:write).with("\300\223<\234\355\221hs").ordered
149 @trans.should_receive(:write).with("\300\376\0173\256\024z\341").ordered
150 @trans.should_receive(:write).with("\3007<2\336\372v\324").ordered
151 @trans.should_receive(:write).with("\000\000\000\000\000\000\000\000").ordered
152 @trans.should_receive(:write).with("@\310\037\220\365\302\217\\").ordered
153 @trans.should_receive(:write).with("@\200Y\327\n=p\244").ordered
154 @trans.should_receive(:write).with([Float::MAX].pack('G')).ordered
155 [Float::MIN, -1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX].each do |f|
156 @prot.write_double(f)
157 end
158 end
159
160 it "should write a string" do
161 str = "hello world"
162 @prot.should_receive(:write_i32).with(str.length).ordered
163 @trans.should_receive(:write).with(str).ordered
164 @prot.write_string(str)
165 end
166
167 # message footer is a noop
168
169 it "should read a field header" do
170 @prot.should_receive(:read_byte).ordered.and_return(Thrift::Types::STRING)
171 @prot.should_receive(:read_i16).ordered.and_return(3)
172 @prot.read_field_begin.should == [nil, Thrift::Types::STRING, 3]
173 end
174
175 # field footer is a noop
176
177 it "should read a stop field" do
178 @prot.should_receive(:read_byte).and_return(Thrift::Types::STOP)
179 @prot.should_not_receive(:read_i16)
180 @prot.read_field_begin.should == [nil, Thrift::Types::STOP, 0]
181 end
182
183 it "should read a map header" do
184 @prot.should_receive(:read_byte).and_return(Thrift::Types::DOUBLE, Thrift::Types::I64)
185 @prot.should_receive(:read_i32).and_return(42)
186 @prot.read_map_begin.should == [Thrift::Types::DOUBLE, Thrift::Types::I64, 42]
187 end
188
189 # map footer is a noop
190
191 it "should read a list header" do
192 @prot.should_receive(:read_byte).ordered.and_return(Thrift::Types::STRING)
193 @prot.should_receive(:read_i32).and_return(17)
194 @prot.read_list_begin.should == [Thrift::Types::STRING, 17]
195 end
196
197 # list footer is a noop
198
199 it "should read a set header" do
200 @prot.should_receive(:read_byte).ordered.and_return(Thrift::Types::MAP)
201 @prot.should_receive(:read_i32).ordered.and_return(42)
202 @prot.read_set_begin.should == [Thrift::Types::MAP, 42]
203 end
204
205 # set footer is a noop
206
207 it "should read a bool" do
208 @prot.should_receive(:read_byte).and_return(1, 0)
209 @prot.read_bool.should == true
210 @prot.read_bool.should == false
211 end
212
213 it "should read a byte" do
214 # try a scattering of values, including min/max
215 @trans.should_receive(:read_all).with(1).and_return(
216 "\200", "\307", "\375",
217 "\000", "\021", "\030", "\177"
218 )
219 [-128, -57, -3, 0, 17, 24, 127].each do |i|
220 @prot.read_byte.should == i
221 end
222 end
223
224 it "should read an i16" do
225 # try a scattering of values, including min/max
226 @trans.should_receive(:read_all).with(2).and_return(
227 "\200\000", "\353\213", "\376\237",
228 "\000\000", "\005\367", "\b\272", "\177\377"
229 )
230 [-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
231 @prot.read_i16.should == i
232 end
233 end
234
235 it "should read an i32" do
236 # try a scattering of values, including min/max
237 @trans.should_receive(:read_all).with(4).and_return(
238 "\200\000\000\000", "\377\374i\213", "\377\377\347\244",
239 "\000\000\000\000", "\000\000\t/", "\000\001\340\363", "\177\377\377\377"
240 )
241 [-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
242 @prot.read_i32.should == i
243 end
244 end
245
246 it "should read an i64" do
247 # try a scattering of values, including min/max
248 @trans.should_receive(:read_all).with(8).and_return(
249 "\200\000\000\000\000\000\000\000", "\377\377\377\377\370\243Z\b",
250 "\377\377\377\377\377\377\3476", "\000\000\000\000\000\000\000\000",
251 "\000\000\000\000\000\000\000 ", "\000\000\000\000\213\332\t\223",
252 "\177\377\377\377\377\377\377\377"
253 )
254 [-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
255 @prot.read_i64.should == i
256 end
257 end
258
259 it "should read a double" do
260 # try a random scattering of values, including min/max
261 @trans.should_receive(:read_all).with(8).and_return(
262 [Float::MIN].pack('G'), "\301\f9\370\374\362\317\226",
263 "\300t3\274x \243\016", "\000\000\000\000\000\000\000\000", "@^\317\fCo\301Y",
264 "AA\360A\217\317@\260", [Float::MAX].pack('G')
265 )
266 [Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
267 @prot.read_double.should == f
268 end
269 end
270
271 it "should read a string" do
272 str = "hello world"
273 @prot.should_receive(:read_i32).and_return(str.length)
274 @trans.should_receive(:read_all).with(str.length).and_return(str)
275 @prot.read_string.should == str
276 end
277end