diff options
Diffstat (limited to 'spec/ruby/library/socket/option')
| -rw-r--r-- | spec/ruby/library/socket/option/bool_spec.rb | 27 | ||||
| -rw-r--r-- | spec/ruby/library/socket/option/initialize_spec.rb | 83 | ||||
| -rw-r--r-- | spec/ruby/library/socket/option/inspect_spec.rb | 19 | ||||
| -rw-r--r-- | spec/ruby/library/socket/option/int_spec.rb | 43 | ||||
| -rw-r--r-- | spec/ruby/library/socket/option/linger_spec.rb | 76 | ||||
| -rw-r--r-- | spec/ruby/library/socket/option/new_spec.rb | 35 |
6 files changed, 283 insertions, 0 deletions
diff --git a/spec/ruby/library/socket/option/bool_spec.rb b/spec/ruby/library/socket/option/bool_spec.rb new file mode 100644 index 0000000000..9992e842b3 --- /dev/null +++ b/spec/ruby/library/socket/option/bool_spec.rb @@ -0,0 +1,27 @@ +require_relative '../spec_helper' +require_relative '../fixtures/classes' + +describe "Socket::Option.bool" do + it "creates a new Socket::Option" do + so = Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, true) + so.should.instance_of?(Socket::Option) + so.family.should == Socket::AF_INET + so.level.should == Socket::SOL_SOCKET + so.optname.should == Socket::SO_KEEPALIVE + so.data.should == [1].pack('i') + end +end + +describe "Socket::Option#bool" do + it "returns boolean value" do + Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, true).bool.should == true + Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, false).bool.should == false + end + + platform_is_not :windows do + it 'raises TypeError when called on a non boolean option' do + opt = Socket::Option.linger(1, 4) + -> { opt.bool }.should.raise(TypeError) + end + end +end diff --git a/spec/ruby/library/socket/option/initialize_spec.rb b/spec/ruby/library/socket/option/initialize_spec.rb new file mode 100644 index 0000000000..5af274f332 --- /dev/null +++ b/spec/ruby/library/socket/option/initialize_spec.rb @@ -0,0 +1,83 @@ +require_relative '../spec_helper' + +describe 'Socket::Option#initialize' do + before do + @bool = [0].pack('i') + end + + describe 'using Integers' do + it 'returns a Socket::Option' do + opt = Socket::Option + .new(Socket::AF_INET, Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, @bool) + + opt.should.instance_of?(Socket::Option) + + opt.family.should == Socket::AF_INET + opt.level.should == Socket::SOL_SOCKET + opt.optname.should == Socket::SO_KEEPALIVE + opt.data.should == @bool + end + end + + describe 'using Symbols' do + it 'returns a Socket::Option' do + opt = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, @bool) + + opt.should.instance_of?(Socket::Option) + + opt.family.should == Socket::AF_INET + opt.level.should == Socket::SOL_SOCKET + opt.optname.should == Socket::SO_KEEPALIVE + opt.data.should == @bool + end + + it 'raises when using an invalid address family' do + -> { + Socket::Option.new(:INET2, :SOCKET, :KEEPALIVE, @bool) + }.should.raise(SocketError) + end + + it 'raises when using an invalid level' do + -> { + Socket::Option.new(:INET, :CATS, :KEEPALIVE, @bool) + }.should.raise(SocketError) + end + + it 'raises when using an invalid option name' do + -> { + Socket::Option.new(:INET, :SOCKET, :CATS, @bool) + }.should.raise(SocketError) + end + end + + describe 'using Strings' do + it 'returns a Socket::Option' do + opt = Socket::Option.new('INET', 'SOCKET', 'KEEPALIVE', @bool) + + opt.should.instance_of?(Socket::Option) + + opt.family.should == Socket::AF_INET + opt.level.should == Socket::SOL_SOCKET + opt.optname.should == Socket::SO_KEEPALIVE + opt.data.should == @bool + end + + it 'raises when using an invalid address family' do + -> { + Socket::Option.new('INET2', 'SOCKET', 'KEEPALIVE', @bool) + }.should.raise(SocketError) + end + + it 'raises when using an invalid level' do + -> { + Socket::Option.new('INET', 'CATS', 'KEEPALIVE', @bool) + }.should.raise(SocketError) + end + + it 'raises when using an invalid option name' do + -> { + Socket::Option.new('INET', 'SOCKET', 'CATS', @bool) + }.should.raise(SocketError) + end + end +end diff --git a/spec/ruby/library/socket/option/inspect_spec.rb b/spec/ruby/library/socket/option/inspect_spec.rb new file mode 100644 index 0000000000..ebea940d2f --- /dev/null +++ b/spec/ruby/library/socket/option/inspect_spec.rb @@ -0,0 +1,19 @@ +require_relative '../spec_helper' +require_relative '../fixtures/classes' + + +describe 'Socket::Option#inspect' do + it 'correctly returns SO_LINGER value' do + value = Socket::Option.linger(nil, 0).inspect + value.should == '#<Socket::Option: UNSPEC SOCKET LINGER off 0sec>' + + value = Socket::Option.linger(false, 30).inspect + value.should == '#<Socket::Option: UNSPEC SOCKET LINGER off 30sec>' + + value = Socket::Option.linger(true, 0).inspect + value.should == '#<Socket::Option: UNSPEC SOCKET LINGER on 0sec>' + + value = Socket::Option.linger(true, 30).inspect + value.should == '#<Socket::Option: UNSPEC SOCKET LINGER on 30sec>' + end +end diff --git a/spec/ruby/library/socket/option/int_spec.rb b/spec/ruby/library/socket/option/int_spec.rb new file mode 100644 index 0000000000..0cd341f88a --- /dev/null +++ b/spec/ruby/library/socket/option/int_spec.rb @@ -0,0 +1,43 @@ +require_relative '../spec_helper' +require_relative '../fixtures/classes' + +describe "Socket::Option.int" do + it "creates a new Socket::Option" do + so = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 5) + so.should.instance_of?(Socket::Option) + so.family.should == Socket::Constants::AF_INET + so.level.should == Socket::Constants::SOL_SOCKET + so.optname.should == Socket::Constants::SO_KEEPALIVE + so.data.should == [5].pack('i') + end + + it 'returns a Socket::Option' do + opt = Socket::Option.int(:INET, :IP, :TTL, 4) + + opt.should.instance_of?(Socket::Option) + + opt.family.should == Socket::AF_INET + opt.level.should == Socket::IPPROTO_IP + opt.optname.should == Socket::IP_TTL + opt.data.should == [4].pack('i') + end +end + +describe "Socket::Option#int" do + it "returns int value" do + so = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 17) + so.int.should == 17 + + so = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 32765) + so.int.should == 32765 + + Socket::Option.int(:INET, :IP, :TTL, 4).int.should == 4 + end + + platform_is_not :windows do + it 'raises TypeError when called on a non integer option' do + opt = Socket::Option.linger(1, 4) + -> { opt.int }.should.raise(TypeError) + end + end +end diff --git a/spec/ruby/library/socket/option/linger_spec.rb b/spec/ruby/library/socket/option/linger_spec.rb new file mode 100644 index 0000000000..87c5e0982e --- /dev/null +++ b/spec/ruby/library/socket/option/linger_spec.rb @@ -0,0 +1,76 @@ +require_relative '../spec_helper' +require_relative '../fixtures/classes' + +option_pack = 'i*' +platform_is :windows do + option_pack = 's*' +end + +describe "Socket::Option.linger" do + it "creates a new Socket::Option for SO_LINGER" do + so = Socket::Option.linger(1, 10) + so.should.instance_of?(Socket::Option) + + so.family.should == Socket::Constants::AF_UNSPEC + so.level.should == Socket::Constants::SOL_SOCKET + so.optname.should == Socket::Constants::SO_LINGER + + so.data.should == [1, 10].pack(option_pack) + end + + it "accepts boolean as onoff argument" do + so = Socket::Option.linger(false, 0) + so.data.should == [0, 0].pack(option_pack) + + so = Socket::Option.linger(true, 1) + so.data.should == [1, 1].pack(option_pack) + end +end + +describe "Socket::Option#linger" do + it "returns linger option" do + so = Socket::Option.linger(0, 5) + ary = so.linger + ary[0].should == false + ary[1].should == 5 + + so = Socket::Option.linger(false, 4) + ary = so.linger + ary[0].should == false + ary[1].should == 4 + + so = Socket::Option.linger(1, 10) + ary = so.linger + ary[0].should == true + ary[1].should == 10 + + so = Socket::Option.linger(true, 9) + ary = so.linger + ary[0].should == true + ary[1].should == 9 + end + + it "raises TypeError if not a SO_LINGER" do + so = Socket::Option.int(:AF_UNSPEC, :SOL_SOCKET, :KEEPALIVE, 1) + -> { so.linger }.should.raise(TypeError) + end + + it 'raises TypeError when called on a non SOL_SOCKET/SO_LINGER option' do + opt = Socket::Option.int(:INET, :IP, :TTL, 4) + + -> { opt.linger }.should.raise(TypeError) + end + + platform_is_not :windows do + it "raises TypeError if option has not good size" do + so = Socket::Option.int(:AF_UNSPEC, :SOL_SOCKET, :LINGER, 1) + -> { so.linger }.should.raise(TypeError) + end + end + + it 'raises TypeError when called on a non linger option' do + opt = Socket::Option.new(:INET, :SOCKET, :LINGER, '') + + -> { opt.linger }.should.raise(TypeError) + end +end diff --git a/spec/ruby/library/socket/option/new_spec.rb b/spec/ruby/library/socket/option/new_spec.rb new file mode 100644 index 0000000000..3721d63ee5 --- /dev/null +++ b/spec/ruby/library/socket/option/new_spec.rb @@ -0,0 +1,35 @@ +require_relative '../spec_helper' +require_relative '../fixtures/classes' + +describe "Socket::Option.new" do + it "should accept integers" do + so = Socket::Option.new(Socket::AF_INET, Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, [0].pack('i')) + so.family.should == Socket::AF_INET + so.level.should == Socket::SOL_SOCKET + so.optname.should == Socket::SO_KEEPALIVE + end + + it "should accept symbols" do + so = Socket::Option.new(:AF_INET, :SOL_SOCKET, :SO_KEEPALIVE, [0].pack('i')) + so.family.should == Socket::AF_INET + so.level.should == Socket::SOL_SOCKET + so.optname.should == Socket::SO_KEEPALIVE + + so = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [0].pack('i')) + so.family.should == Socket::AF_INET + so.level.should == Socket::SOL_SOCKET + so.optname.should == Socket::SO_KEEPALIVE + end + + it "should raise error on unknown family" do + -> { Socket::Option.new(:INET4, :SOCKET, :KEEPALIVE, [0].pack('i')) }.should.raise(SocketError) + end + + it "should raise error on unknown level" do + -> { Socket::Option.new(:INET, :ROCKET, :KEEPALIVE, [0].pack('i')) }.should.raise(SocketError) + end + + it "should raise error on unknown option name" do + -> { Socket::Option.new(:INET, :SOCKET, :ALIVE, [0].pack('i')) }.should.raise(SocketError) + end +end |
