David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | require 'thrift/protocol/binaryprotocolaccelerated' |
| 3 | require File.dirname(__FILE__) + '/binaryprotocol_spec_shared' |
| 4 | require File.dirname(__FILE__) + '/gen-rb/ThriftSpec_types' |
| 5 | |
| 6 | class ThriftBinaryProtocolAcceleratedSpec < Spec::ExampleGroup |
| 7 | include Thrift |
| 8 | |
| 9 | describe BinaryProtocolAccelerated do |
| 10 | # given that BinaryProtocolAccelerated only actually overrides read_message_begin |
| 11 | # this shared spec isn't going to do much, but it's still worth including |
| 12 | # for future-proofing in case we start overriding individual methods |
| 13 | it_should_behave_like 'a binary protocol' |
| 14 | |
| 15 | def protocol_class |
| 16 | BinaryProtocolAccelerated |
| 17 | end |
| 18 | |
| 19 | before(:each) do |
| 20 | @buffer = "" |
| 21 | @trans.stub!(:borrow).and_return { @buffer } |
| 22 | @trans.stub!(:consume!).and_return do |*args| |
| 23 | n = args.first || 0 |
| 24 | @buffer.slice!(0,n) |
| 25 | end |
| 26 | end |
| 27 | |
| 28 | it "should read a message header" do |
| 29 | @buffer = "\200\001\000\002\000\000\000\vtestMessage\000\000\000*" |
| 30 | # @prot.should_receive(:read_i32).and_return(protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::REPLY, 42) |
| 31 | # @prot.should_receive(:read_string).and_return('testMessage') |
| 32 | @prot.read_message_begin.should == ['testMessage', Thrift::MessageTypes::REPLY, 42] |
| 33 | end |
| 34 | |
| 35 | it "should raise an exception if the message header has the wrong version" do |
| 36 | @buffer = "\000\000\000\v" |
| 37 | # @prot.should_receive(:read_i32).and_return(42) |
| 38 | lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'Missing version identifier') do |e| |
| 39 | e.type == Thrift::ProtocolException::BAD_VERSION |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | it "should encode a struct with all fields set identically to Thrift::BinaryProtocol" do |
| 44 | foo = SpecNamespace::Foo.new(:complex => {5 => {"foo" => 1.2}, 17 => {"bar" => 3.14159, "baz" => 5.8}}) |
| 45 | @prot.encode_binary(foo).should == "\r\000\005\b\r\000\000\000\002\000\000\000\005\v\004\000\000\000\001\000\ |
| 46 | \000\000\003foo?\363333333\000\000\000\021\v\004\000\000\000\002\000\000\000\003baz@\027333333\000\000\000\003bar@\ |
| 47 | \t!\371\360\e\206n\016\000\006\006\000\000\000\003\000\005\000\021\000\357\b\000\001\000\000\0005\v\000\002\000\000\ |
| 48 | \000\005words\f\000\003\v\000\001\000\000\000\rhello, world!\000\017\000\004\b\000\000\000\004\000\000\000\001\000\ |
| 49 | \000\000\002\000\000\000\002\000\000\000\003\000" |
| 50 | end |
| 51 | |
| 52 | it "should encode a struct with missing fields identically to Thrift::BinaryProtocol" do |
| 53 | foo = SpecNamespace::Foo.new(:simple => nil, :ints => nil) |
| 54 | @prot.encode_binary(foo).should == "\016\000\006\006\000\000\000\003\000\005\000\021\000\357\v\000\002\000\000\ |
| 55 | \000\005words\f\000\003\v\000\001\000\000\000\rhello, world!\000\000" |
| 56 | end |
| 57 | |
| 58 | it "should decode a struct with all fields set identically to Thrift::BinaryProtocol" do |
| 59 | foo = SpecNamespace::Foo.new(:complex => {5 => {"foo" => 1.2}, 17 => {"bar" => 3.14159, "baz" => 5.8}}) |
| 60 | trans = Thrift::MemoryBuffer.new("\r\000\005\b\r\000\000\000\002\000\000\000\005\v\004\000\000\000\001\000\ |
| 61 | \000\000\003foo?\363333333\000\000\000\021\v\004\000\000\000\002\000\000\000\003baz@\027333333\000\000\000\003bar@\ |
| 62 | \t!\371\360\e\206n\016\000\006\006\000\000\000\003\000\005\000\021\000\357\b\000\001\000\000\0005\v\000\002\000\000\ |
| 63 | \000\005words\f\000\003\v\000\001\000\000\000\rhello, world!\000\017\000\004\b\000\000\000\004\000\000\000\001\000\ |
| 64 | \000\000\002\000\000\000\002\000\000\000\003\000") |
| 65 | @prot.decode_binary(SpecNamespace::Foo.new, trans).should == foo |
| 66 | end |
| 67 | |
| 68 | it "should decode a struct with missing fields identically to Thrift::BinaryProtocol" do |
| 69 | trans = Thrift::MemoryBuffer.new("\016\000\006\006\000\000\000\003\000\005\000\021\000\357\v\000\002\000\000\ |
| 70 | \000\005words\f\000\003\v\000\001\000\000\000\rhello, world!\000\000") |
| 71 | @prot.decode_binary(SpecNamespace::Foo.new, trans).should == SpecNamespace::Foo.new |
| 72 | end |
David Reiss | 0c7d38c | 2008-07-25 21:06:06 +0000 | [diff] [blame] | 73 | |
| 74 | it "should encode a string with null bytes in it" do |
| 75 | foo = SpecNamespace::Hello.new(:greeting => "Hello\000World!") |
| 76 | @prot.encode_binary(foo).should == "\v\000\001\000\000\000\fHello\000World!\000" |
| 77 | end |
| 78 | |
| 79 | it "should decode a string with null bytes in it" do |
| 80 | trans = Thrift::MemoryBuffer.new("\v\000\001\000\000\000\fHello\000World!\000") |
| 81 | @prot.decode_binary(SpecNamespace::Hello.new, trans).should == SpecNamespace::Hello.new(:greeting => "Hello\000World!") |
| 82 | end |
Kevin Clark | 14fe791 | 2008-08-04 18:46:19 +0000 | [diff] [blame] | 83 | |
| 84 | it "should error when encoding a struct with a nil value in a list" do |
| 85 | Thrift.type_checking = false |
| 86 | sl = SpecNamespace::SimpleList |
| 87 | hello = SpecNamespace::Hello |
| 88 | # nil counts as false for bools |
| 89 | # lambda { @prot.encode_binary(sl.new(:bools => [true, false, nil, false])) }.should raise_error |
| 90 | lambda { @prot.encode_binary(sl.new(:bytes => [1, 2, nil, 3])) }.should raise_error |
| 91 | lambda { @prot.encode_binary(sl.new(:i16s => [1, 2, nil, 3])) }.should raise_error |
| 92 | lambda { @prot.encode_binary(sl.new(:i32s => [1, 2, nil, 3])) }.should raise_error |
| 93 | lambda { @prot.encode_binary(sl.new(:i64s => [1, 2, nil, 3])) }.should raise_error |
| 94 | lambda { @prot.encode_binary(sl.new(:doubles => [1.0, 2.0, nil, 3.0])) }.should raise_error |
| 95 | lambda { @prot.encode_binary(sl.new(:strings => ["one", "two", nil, "three"])) }.should raise_error |
| 96 | lambda { @prot.encode_binary(sl.new(:lists => [[1, 2], nil, [3, 4]])) }.should raise_error |
| 97 | lambda { @prot.encode_binary(sl.new(:maps => [{1 => 2}, nil, {3 => 4}])) }.should raise_error |
| 98 | lambda { @prot.encode_binary(sl.new(:sets => [Set.new([1, 2]), nil, Set.new([3, 4])])) }.should raise_error |
| 99 | lambda { @prot.encode_binary(sl.new(:structs => [hello.new, nil, hello.new(:greeting => "hi")])) }.should raise_error |
| 100 | end |
| 101 | |
| 102 | it "should error when encoding a non-nil, non-correctly-typed value in a list" do |
| 103 | Thrift.type_checking = false |
| 104 | sl = SpecNamespace::SimpleList |
| 105 | hello = SpecNamespace::Hello |
| 106 | # bool should accept any value |
| 107 | # lambda { @prot.encode_binary(sl.new(:bools => [true, false, 3])) }.should raise_error |
| 108 | lambda { @prot.encode_binary(sl.new(:bytes => [1, 2, "3", 5])) }.should raise_error |
| 109 | lambda { @prot.encode_binary(sl.new(:i16s => ["one", 2, 3])) }.should raise_error |
| 110 | lambda { @prot.encode_binary(sl.new(:i32s => [[1,2], 3, 4])) }.should raise_error |
| 111 | lambda { @prot.encode_binary(sl.new(:i64s => [{1 => 2}, 3, 4])) }.should raise_error |
| 112 | lambda { @prot.encode_binary(sl.new(:doubles => ["one", 2.3, 3.4])) }.should raise_error |
| 113 | lambda { @prot.encode_binary(sl.new(:strings => ["one", "two", 3, 4])) }.should raise_error |
| 114 | lambda { @prot.encode_binary(sl.new(:lists => [{1 => 2}, [3, 4]])) }.should raise_error |
| 115 | lambda { @prot.encode_binary(sl.new(:maps => [{1 => 2}, [3, 4]])) }.should raise_error |
| 116 | lambda { @prot.encode_binary(sl.new(:sets => [Set.new([1, 2]), 3, 4])) }.should raise_error |
| 117 | lambda { @prot.encode_binary(sl.new(:structs => [3, "four"])) }.should raise_error |
| 118 | end |
| 119 | |
| 120 | it "should error when given nil to encode" do |
| 121 | lambda { @prot.encode_binary(nil) }.should raise_error |
| 122 | end |
| 123 | |
| 124 | it "should error when encoding an improper object where a container is expected" do |
| 125 | Thrift.type_checking = false |
| 126 | sl = SpecNamespace::SimpleList |
| 127 | lambda { @prot.encode_binary(sl.new(:strings => {"one" => "two", nil => "three"})) }.should raise_error |
| 128 | lambda { @prot.encode_binary(sl.new(:maps => [[1, 2]])) }.should raise_error |
| 129 | end |
| 130 | |
| 131 | it "should accept arrays and hashes as sets" do |
| 132 | Thrift.type_checking = false |
| 133 | sl = SpecNamespace::SimpleList |
| 134 | lambda { @prot.encode_binary(sl.new(:sets => [[1, 2], {3 => true, 4 => true}])) }.should_not raise_error |
| 135 | end |
David Reiss | 72754e1 | 2008-07-25 21:06:03 +0000 | [diff] [blame] | 136 | end |
| 137 | |
| 138 | describe BinaryProtocolAcceleratedFactory do |
| 139 | it "should create a BinaryProtocolAccelerated" do |
| 140 | BinaryProtocolAcceleratedFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(BinaryProtocolAccelerated) |
| 141 | end |
| 142 | end |
| 143 | end |