Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | require 'thrift/protocol/binaryprotocol' |
| 3 | |
Kevin Clark | 356f861 | 2008-06-18 01:08:05 +0000 | [diff] [blame] | 4 | class ThriftBinaryProtocolSpec < Spec::ExampleGroup |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 5 | 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 Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 71 | # 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 Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 74 | @prot.write_byte(i) |
| 75 | end |
Kevin Clark | 378191a | 2008-06-18 01:08:19 +0000 | [diff] [blame] | 76 | (-128..127).each do |i| |
| 77 | end |
| 78 | # handing it numbers out of signed range should clip |
| 79 | @trans.rspec_verify |
Kevin Clark | 378191a | 2008-06-18 01:08:19 +0000 | [diff] [blame] | 80 | (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 Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 86 | end |
| 87 | |
| 88 | it "should write an i16" do |
| 89 | # try a random scattering of values |
Kevin Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 90 | # include the signed i16 minimum/maximum |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 91 | @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 Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 97 | @trans.should_receive(:write).with("\177\377").ordered |
| 98 | [-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i| |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 99 | @prot.write_i16(i) |
| 100 | end |
Kevin Clark | 378191a | 2008-06-18 01:08:19 +0000 | [diff] [blame] | 101 | # 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 Clark | 830f46a | 2008-06-18 01:19:51 +0000 | [diff] [blame^] | 105 | # lambda { @prot.write_i16(2**65) }.should raise_error(RangeError) |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 106 | end |
| 107 | |
| 108 | it "should write an i32" do |
| 109 | # try a random scattering of values |
Kevin Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 110 | # include the signed i32 minimum/maximum |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 111 | @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 Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 118 | @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 Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 120 | @prot.write_i32(i) |
| 121 | end |
Kevin Clark | 378191a | 2008-06-18 01:08:19 +0000 | [diff] [blame] | 122 | # 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 Clark | 830f46a | 2008-06-18 01:19:51 +0000 | [diff] [blame^] | 125 | # lambda { @prot.write_i32(2 ** 65 + 5) }.should raise_error(RangeError) |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 126 | end |
| 127 | |
| 128 | it "should write an i64" do |
| 129 | # try a random scattering of values |
Kevin Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 130 | # try the signed i64 minimum/maximum |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 131 | @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 Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 139 | @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 Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 141 | @prot.write_i64(i) |
| 142 | end |
Kevin Clark | 378191a | 2008-06-18 01:08:19 +0000 | [diff] [blame] | 143 | # 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 Clark | 830f46a | 2008-06-18 01:19:51 +0000 | [diff] [blame^] | 146 | # lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError) |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 147 | end |
| 148 | |
| 149 | it "should write a double" do |
Kevin Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 150 | # try a random scattering of values, including min/max |
Kevin Clark | 830f46a | 2008-06-18 01:19:51 +0000 | [diff] [blame^] | 151 | @trans.should_receive(:write).with([Float::MIN].pack('G')).ordered |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 152 | @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 Clark | 830f46a | 2008-06-18 01:19:51 +0000 | [diff] [blame^] | 158 | @trans.should_receive(:write).with([Float::MAX].pack('G')).ordered |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 159 | [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 Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 170 | |
| 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 Clark | c13c33b | 2008-06-18 01:12:48 +0000 | [diff] [blame] | 177 | 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 Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 183 | |
| 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 Clark | 830f46a | 2008-06-18 01:19:51 +0000 | [diff] [blame^] | 277 | [Float::MIN].pack('G'), "\301\f9\370\374\362\317\226", |
Kevin Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 278 | "\300t3\274x \243\016", "\000\000\000\000\000\000\000\000", "@^\317\fCo\301Y", |
Kevin Clark | 830f46a | 2008-06-18 01:19:51 +0000 | [diff] [blame^] | 279 | "AA\360A\217\317@\260", [Float::MAX].pack('G') |
Kevin Clark | 5ae0384 | 2008-06-18 01:07:37 +0000 | [diff] [blame] | 280 | ) |
| 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 Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 292 | end |
Kevin Clark | e977a63 | 2008-06-18 01:07:50 +0000 | [diff] [blame] | 293 | |
| 294 | describe BinaryProtocolFactory do |
| 295 | it "should create a BinaryProtocol" do |
Kevin Clark | 90a2cbe | 2008-06-18 01:15:53 +0000 | [diff] [blame] | 296 | BinaryProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocol) |
Kevin Clark | e977a63 | 2008-06-18 01:07:50 +0000 | [diff] [blame] | 297 | end |
| 298 | end |
Kevin Clark | f18b643 | 2008-06-18 01:07:10 +0000 | [diff] [blame] | 299 | end |