summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/option
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/socket/option')
-rw-r--r--spec/ruby/library/socket/option/bool_spec.rb14
-rw-r--r--spec/ruby/library/socket/option/initialize_spec.rb83
-rw-r--r--spec/ruby/library/socket/option/inspect_spec.rb5
-rw-r--r--spec/ruby/library/socket/option/int_spec.rb27
-rw-r--r--spec/ruby/library/socket/option/linger_spec.rb32
-rw-r--r--spec/ruby/library/socket/option/new_spec.rb10
6 files changed, 142 insertions, 29 deletions
diff --git a/spec/ruby/library/socket/option/bool_spec.rb b/spec/ruby/library/socket/option/bool_spec.rb
index 74c832a0ad..9992e842b3 100644
--- a/spec/ruby/library/socket/option/bool_spec.rb
+++ b/spec/ruby/library/socket/option/bool_spec.rb
@@ -1,10 +1,10 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+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 be_an_instance_of(Socket::Option)
+ so.should.instance_of?(Socket::Option)
so.family.should == Socket::AF_INET
so.level.should == Socket::SOL_SOCKET
so.optname.should == Socket::SO_KEEPALIVE
@@ -18,8 +18,10 @@ describe "Socket::Option#bool" do
Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, false).bool.should == false
end
- it "raises TypeError if option has not good size" do
- so = Socket::Option.new(:UNSPEC, :SOCKET, :SO_LINGER, [0, 0].pack('i*'))
- lambda { so.bool }.should raise_error(TypeError)
+ 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
index df72f227a9..ebea940d2f 100644
--- a/spec/ruby/library/socket/option/inspect_spec.rb
+++ b/spec/ruby/library/socket/option/inspect_spec.rb
@@ -1,7 +1,6 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
-require 'socket'
describe 'Socket::Option#inspect' do
it 'correctly returns SO_LINGER value' do
diff --git a/spec/ruby/library/socket/option/int_spec.rb b/spec/ruby/library/socket/option/int_spec.rb
index f926ff7968..0cd341f88a 100644
--- a/spec/ruby/library/socket/option/int_spec.rb
+++ b/spec/ruby/library/socket/option/int_spec.rb
@@ -1,15 +1,26 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+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 be_an_instance_of(Socket::Option)
+ 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
@@ -19,10 +30,14 @@ describe "Socket::Option#int" do
so = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 32765)
so.int.should == 32765
+
+ Socket::Option.int(:INET, :IP, :TTL, 4).int.should == 4
end
- it "raises TypeError if option has not good size" do
- so = Socket::Option.new(:UNSPEC, :SOCKET, :SO_LINGER, [0, 0].pack('i*'))
- lambda { so.int }.should raise_error(TypeError)
+ 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
index 687d421af3..87c5e0982e 100644
--- a/spec/ruby/library/socket/option/linger_spec.rb
+++ b/spec/ruby/library/socket/option/linger_spec.rb
@@ -1,5 +1,5 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
option_pack = 'i*'
platform_is :windows do
@@ -9,10 +9,12 @@ end
describe "Socket::Option.linger" do
it "creates a new Socket::Option for SO_LINGER" do
so = Socket::Option.linger(1, 10)
- so.should be_an_instance_of(Socket::Option)
+ 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
@@ -29,34 +31,46 @@ describe "Socket::Option#linger" do
it "returns linger option" do
so = Socket::Option.linger(0, 5)
ary = so.linger
- ary[0].should be_false
+ ary[0].should == false
ary[1].should == 5
so = Socket::Option.linger(false, 4)
ary = so.linger
- ary[0].should be_false
+ ary[0].should == false
ary[1].should == 4
so = Socket::Option.linger(1, 10)
ary = so.linger
- ary[0].should be_true
+ ary[0].should == true
ary[1].should == 10
so = Socket::Option.linger(true, 9)
ary = so.linger
- ary[0].should be_true
+ 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)
- lambda { so.linger }.should raise_error(TypeError)
+ -> { 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)
- lambda { so.linger }.should raise_error(TypeError)
+ -> { 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
index 4f2d0c5386..3721d63ee5 100644
--- a/spec/ruby/library/socket/option/new_spec.rb
+++ b/spec/ruby/library/socket/option/new_spec.rb
@@ -1,5 +1,5 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
describe "Socket::Option.new" do
it "should accept integers" do
@@ -22,14 +22,14 @@ describe "Socket::Option.new" do
end
it "should raise error on unknown family" do
- lambda { Socket::Option.new(:INET4, :SOCKET, :KEEPALIVE, [0].pack('i')) }.should raise_error(SocketError)
+ -> { Socket::Option.new(:INET4, :SOCKET, :KEEPALIVE, [0].pack('i')) }.should.raise(SocketError)
end
it "should raise error on unknown level" do
- lambda { Socket::Option.new(:INET, :ROCKET, :KEEPALIVE, [0].pack('i')) }.should raise_error(SocketError)
+ -> { Socket::Option.new(:INET, :ROCKET, :KEEPALIVE, [0].pack('i')) }.should.raise(SocketError)
end
it "should raise error on unknown option name" do
- lambda { Socket::Option.new(:INET, :SOCKET, :ALIVE, [0].pack('i')) }.should raise_error(SocketError)
+ -> { Socket::Option.new(:INET, :SOCKET, :ALIVE, [0].pack('i')) }.should.raise(SocketError)
end
end