diff options
Diffstat (limited to 'spec/ruby/library/socket')
163 files changed, 2978 insertions, 1636 deletions
diff --git a/spec/ruby/library/socket/addrinfo/afamily_spec.rb b/spec/ruby/library/socket/addrinfo/afamily_spec.rb index 7229dab9de..5d075be057 100644 --- a/spec/ruby/library/socket/addrinfo/afamily_spec.rb +++ b/spec/ruby/library/socket/addrinfo/afamily_spec.rb @@ -23,15 +23,13 @@ describe "Addrinfo#afamily" do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end - - it "returns Socket::AF_UNIX" do - @addrinfo.afamily.should == Socket::AF_UNIX - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end + + it "returns Socket::AF_UNIX" do + @addrinfo.afamily.should == Socket::AF_UNIX end end end diff --git a/spec/ruby/library/socket/addrinfo/family_addrinfo_spec.rb b/spec/ruby/library/socket/addrinfo/family_addrinfo_spec.rb index d3419daaaf..3c2f9f73d8 100644 --- a/spec/ruby/library/socket/addrinfo/family_addrinfo_spec.rb +++ b/spec/ruby/library/socket/addrinfo/family_addrinfo_spec.rb @@ -4,7 +4,7 @@ describe 'Addrinfo#family_addrinfo' do it 'raises ArgumentError if no arguments are given' do addr = Addrinfo.tcp('127.0.0.1', 0) - lambda { addr.family_addrinfo }.should raise_error(ArgumentError) + -> { addr.family_addrinfo }.should raise_error(ArgumentError) end describe 'using multiple arguments' do @@ -14,11 +14,11 @@ describe 'Addrinfo#family_addrinfo' do end it 'raises ArgumentError if only 1 argument is given' do - lambda { @source.family_addrinfo('127.0.0.1') }.should raise_error(ArgumentError) + -> { @source.family_addrinfo('127.0.0.1') }.should raise_error(ArgumentError) end it 'raises ArgumentError if more than 2 arguments are given' do - lambda { @source.family_addrinfo('127.0.0.1', 0, 666) }.should raise_error(ArgumentError) + -> { @source.family_addrinfo('127.0.0.1', 0, 666) }.should raise_error(ArgumentError) end it 'returns an Addrinfo when a host and port are given' do @@ -50,38 +50,36 @@ describe 'Addrinfo#family_addrinfo' do end end - with_feature :unix_socket do - describe 'with a UNIX Addrinfo' do - before do - @source = Addrinfo.unix('cats') - end + describe 'with a UNIX Addrinfo' do + before do + @source = Addrinfo.unix('cats') + end - it 'raises ArgumentError if more than 1 argument is given' do - lambda { @source.family_addrinfo('foo', 'bar') }.should raise_error(ArgumentError) - end + it 'raises ArgumentError if more than 1 argument is given' do + -> { @source.family_addrinfo('foo', 'bar') }.should raise_error(ArgumentError) + end - it 'returns an Addrinfo when a UNIX socket path is given' do - addr = @source.family_addrinfo('dogs') + it 'returns an Addrinfo when a UNIX socket path is given' do + addr = @source.family_addrinfo('dogs') - addr.should be_an_instance_of(Addrinfo) - end + addr.should be_an_instance_of(Addrinfo) + end - describe 'the returned Addrinfo' do - before do - @addr = @source.family_addrinfo('dogs') - end + describe 'the returned Addrinfo' do + before do + @addr = @source.family_addrinfo('dogs') + end - it 'uses AF_UNIX as the address family' do - @addr.afamily.should == Socket::AF_UNIX - end + it 'uses AF_UNIX as the address family' do + @addr.afamily.should == Socket::AF_UNIX + end - it 'uses PF_UNIX as the protocol family' do - @addr.pfamily.should == Socket::PF_UNIX - end + it 'uses PF_UNIX as the protocol family' do + @addr.pfamily.should == Socket::PF_UNIX + end - it 'uses the given socket path' do - @addr.unix_path.should == 'dogs' - end + it 'uses the given socket path' do + @addr.unix_path.should == 'dogs' end end end @@ -99,17 +97,17 @@ describe 'Addrinfo#family_addrinfo' do it 'raises ArgumentError if more than 1 argument is given' do input = Addrinfo.tcp('127.0.0.2', 0) - lambda { @source.family_addrinfo(input, 666) }.should raise_error(ArgumentError) + -> { @source.family_addrinfo(input, 666) }.should raise_error(ArgumentError) end it "raises ArgumentError if the protocol families don't match" do input = Addrinfo.tcp('::1', 0) - lambda { @source.family_addrinfo(input) }.should raise_error(ArgumentError) + -> { @source.family_addrinfo(input) }.should raise_error(ArgumentError) end it "raises ArgumentError if the socket types don't match" do input = Addrinfo.udp('127.0.0.1', 0) - lambda { @source.family_addrinfo(input) }.should raise_error(ArgumentError) + -> { @source.family_addrinfo(input) }.should raise_error(ArgumentError) end end end diff --git a/spec/ruby/library/socket/addrinfo/getaddrinfo_spec.rb b/spec/ruby/library/socket/addrinfo/getaddrinfo_spec.rb index 67fad73815..e05fe9967a 100644 --- a/spec/ruby/library/socket/addrinfo/getaddrinfo_spec.rb +++ b/spec/ruby/library/socket/addrinfo/getaddrinfo_spec.rb @@ -73,19 +73,15 @@ describe 'Addrinfo.getaddrinfo' do end end - platform_is_not :'solaris2.10' do # i386-solaris - it 'sets a custom socket protocol of the Addrinfo instances' do - array = Addrinfo.getaddrinfo('127.0.0.1', 80, nil, nil, Socket::IPPROTO_UDP) + it 'sets a custom socket protocol of the Addrinfo instances' do + array = Addrinfo.getaddrinfo('127.0.0.1', 80, nil, nil, Socket::IPPROTO_UDP) - array[0].protocol.should == Socket::IPPROTO_UDP - end + array[0].protocol.should == Socket::IPPROTO_UDP end - platform_is_not :solaris do - it 'sets the canonical name when AI_CANONNAME is given as a flag' do - array = Addrinfo.getaddrinfo('localhost', 80, nil, nil, nil, Socket::AI_CANONNAME) + it 'sets the canonical name when AI_CANONNAME is given as a flag' do + array = Addrinfo.getaddrinfo('localhost', 80, nil, nil, nil, Socket::AI_CANONNAME) - array[0].canonname.should be_an_instance_of(String) - end + array[0].canonname.should be_an_instance_of(String) end end diff --git a/spec/ruby/library/socket/addrinfo/getnameinfo_spec.rb b/spec/ruby/library/socket/addrinfo/getnameinfo_spec.rb index c5284f1c0f..43b5a2000a 100644 --- a/spec/ruby/library/socket/addrinfo/getnameinfo_spec.rb +++ b/spec/ruby/library/socket/addrinfo/getnameinfo_spec.rb @@ -21,7 +21,7 @@ describe 'Addrinfo#getnameinfo' do end platform_is :linux do - with_feature :unix_socket do + platform_is_not :android do describe 'using a UNIX Addrinfo' do before do @addr = Addrinfo.unix('cats') diff --git a/spec/ruby/library/socket/addrinfo/initialize_spec.rb b/spec/ruby/library/socket/addrinfo/initialize_spec.rb index 460fc28d49..1f16531aaa 100644 --- a/spec/ruby/library/socket/addrinfo/initialize_spec.rb +++ b/spec/ruby/library/socket/addrinfo/initialize_spec.rb @@ -17,7 +17,7 @@ describe "Addrinfo#initialize" do @addrinfo.ip_port.should == 25 end - it "returns the Socket::UNSPEC pfamily" do + it "returns the UNSPEC pfamily" do @addrinfo.pfamily.should == Socket::PF_UNSPEC end @@ -53,7 +53,7 @@ describe "Addrinfo#initialize" do @addrinfo.ip_port.should == 25 end - it "returns the Socket::UNSPEC pfamily" do + it "returns the INET6 pfamily" do @addrinfo.pfamily.should == Socket::PF_INET6 end @@ -83,7 +83,7 @@ describe "Addrinfo#initialize" do @addrinfo.ip_port.should == 25 end - it "returns the Socket::UNSPEC pfamily" do + it "returns the INET6 pfamily" do @addrinfo.pfamily.should == Socket::PF_INET6 end @@ -91,7 +91,7 @@ describe "Addrinfo#initialize" do @addrinfo.afamily.should == Socket::AF_INET6 end - it "returns the 0 socket type" do + it "returns the specified socket type" do @addrinfo.socktype.should == Socket::SOCK_STREAM end @@ -113,7 +113,7 @@ describe "Addrinfo#initialize" do @addrinfo.ip_port.should == 25 end - it "returns the Socket::UNSPEC pfamily" do + it "returns the INET6 pfamily" do @addrinfo.pfamily.should == Socket::PF_INET6 end @@ -147,11 +147,11 @@ describe "Addrinfo#initialize" do @addrinfo.ip_port.should == 46102 end - it "returns the Socket::PF_INET pfamily" do + it "returns the INET pfamily" do @addrinfo.pfamily.should == Socket::PF_INET end - it "returns the INET6 afamily" do + it "returns the INET afamily" do @addrinfo.afamily.should == Socket::AF_INET end @@ -198,7 +198,7 @@ describe "Addrinfo#initialize" do describe 'with an invalid IP address' do it 'raises SocketError' do - block = lambda { Addrinfo.new(['AF_INET6', 80, 'hostname', '127.0.0.1']) } + block = -> { Addrinfo.new(['AF_INET6', 80, 'hostname', '127.0.0.1']) } block.should raise_error(SocketError) end @@ -217,11 +217,11 @@ describe "Addrinfo#initialize" do @addrinfo.ip_port.should == 46102 end - it "returns the Socket::UNSPEC pfamily" do + it "returns the INET pfamily" do @addrinfo.pfamily.should == Socket::PF_INET end - it "returns the INET6 afamily" do + it "returns the INET afamily" do @addrinfo.afamily.should == Socket::AF_INET end @@ -247,11 +247,11 @@ describe "Addrinfo#initialize" do @addrinfo.ip_port.should == 46102 end - it "returns the Socket::UNSPEC pfamily" do + it "returns the INET pfamily" do @addrinfo.pfamily.should == Socket::PF_INET end - it "returns the INET6 afamily" do + it "returns the INET afamily" do @addrinfo.afamily.should == Socket::AF_INET end @@ -274,15 +274,17 @@ describe "Addrinfo#initialize" do end end - with_feature :sock_packet do - [:SOCK_SEQPACKET].each do |type| - it "overwrites the socket type #{type}" do - sockaddr = ['AF_INET', 80, 'hostname', '127.0.0.1'] + platform_is_not :android do + with_feature :sock_packet do + [:SOCK_SEQPACKET].each do |type| + it "overwrites the socket type #{type}" do + sockaddr = ['AF_INET', 80, 'hostname', '127.0.0.1'] - value = Socket.const_get(type) - addr = Addrinfo.new(sockaddr, nil, value) + value = Socket.const_get(type) + addr = Addrinfo.new(sockaddr, nil, value) - addr.socktype.should == value + addr.socktype.should == value + end end end end @@ -290,7 +292,7 @@ describe "Addrinfo#initialize" do it "raises SocketError when using SOCK_RDM" do sockaddr = ['AF_INET', 80, 'hostname', '127.0.0.1'] value = Socket::SOCK_RDM - block = lambda { Addrinfo.new(sockaddr, nil, value) } + block = -> { Addrinfo.new(sockaddr, nil, value) } block.should raise_error(SocketError) end @@ -309,11 +311,11 @@ describe "Addrinfo#initialize" do @addrinfo.ip_port.should == 46102 end - it "returns the Socket::UNSPEC pfamily" do + it "returns the INET pfamily" do @addrinfo.pfamily.should == Socket::PF_INET end - it "returns the INET6 afamily" do + it "returns the INET afamily" do @addrinfo.afamily.should == Socket::AF_INET end @@ -333,7 +335,7 @@ describe "Addrinfo#initialize" do @sockaddr = ['AF_INET6', 80, 'hostname', '127.0.0.1'] end - it "raises SocketError when using any Socket constant except except AF_INET(6)/PF_INET(6)" do + it "raises SocketError when using any Socket constant except AF_INET(6)/PF_INET(6)" do Socket.constants.grep(/(^AF_|^PF_)(?!INET)/).each do |constant| value = Socket.const_get(constant) -> { @@ -360,11 +362,11 @@ describe "Addrinfo#initialize" do end end - platform_is_not :windows, :aix, :solaris do + platform_is_not :windows, :aix do (Socket.constants.grep(/^IPPROTO/) - valid).each do |type| it "raises SocketError when using #{type}" do value = Socket.const_get(type) - block = lambda { Addrinfo.new(@sockaddr, nil, nil, value) } + block = -> { Addrinfo.new(@sockaddr, nil, nil, value) } block.should raise_error(SocketError) end @@ -388,11 +390,11 @@ describe "Addrinfo#initialize" do end end - platform_is_not :windows, :aix, :solaris do + platform_is_not :windows, :aix do (Socket.constants.grep(/^IPPROTO/) - valid).each do |type| it "raises SocketError when using #{type}" do value = Socket.const_get(type) - block = lambda { Addrinfo.new(@sockaddr, nil, @socktype, value) } + block = -> { Addrinfo.new(@sockaddr, nil, @socktype, value) } block.should raise_error(SocketError) end @@ -409,7 +411,7 @@ describe "Addrinfo#initialize" do Socket.constants.grep(/^IPPROTO/).each do |type| it "raises SocketError when using #{type}" do value = Socket.const_get(type) - block = lambda { Addrinfo.new(@sockaddr, nil, @socktype, value) } + block = -> { Addrinfo.new(@sockaddr, nil, @socktype, value) } block.should raise_error(SocketError) end @@ -440,7 +442,7 @@ describe "Addrinfo#initialize" do Socket.constants.grep(/^IPPROTO/).each do |type| it "raises SocketError when using #{type}" do value = Socket.const_get(type) - block = lambda { Addrinfo.new(@sockaddr, nil, @socktype, value) } + block = -> { Addrinfo.new(@sockaddr, nil, @socktype, value) } block.should raise_error(SocketError) end @@ -448,28 +450,30 @@ describe "Addrinfo#initialize" do end platform_is :linux do - describe 'and the socket type is set to SOCK_SEQPACKET' do - before do - @socktype = Socket::SOCK_SEQPACKET - end + platform_is_not :android do + describe 'and the socket type is set to SOCK_SEQPACKET' do + before do + @socktype = Socket::SOCK_SEQPACKET + end - valid = [:IPPROTO_IP, :IPPROTO_HOPOPTS] + valid = [:IPPROTO_IP, :IPPROTO_HOPOPTS] - valid.each do |type| - it "overwrites the protocol when using #{type}" do - value = Socket.const_get(type) - addr = Addrinfo.new(@sockaddr, nil, @socktype, value) + valid.each do |type| + it "overwrites the protocol when using #{type}" do + value = Socket.const_get(type) + addr = Addrinfo.new(@sockaddr, nil, @socktype, value) - addr.protocol.should == value + addr.protocol.should == value + end end - end - (Socket.constants.grep(/^IPPROTO/) - valid).each do |type| - it "raises SocketError when using #{type}" do - value = Socket.const_get(type) - block = lambda { Addrinfo.new(@sockaddr, nil, @socktype, value) } + (Socket.constants.grep(/^IPPROTO/) - valid).each do |type| + it "raises SocketError when using #{type}" do + value = Socket.const_get(type) + block = -> { Addrinfo.new(@sockaddr, nil, @socktype, value) } - block.should raise_error(SocketError) + block.should raise_error(SocketError) + end end end end @@ -491,11 +495,11 @@ describe "Addrinfo#initialize" do end end - platform_is_not :windows, :aix, :solaris do + platform_is_not :windows, :aix do (Socket.constants.grep(/^IPPROTO/) - valid).each do |type| it "raises SocketError when using #{type}" do value = Socket.const_get(type) - block = lambda { Addrinfo.new(@sockaddr, nil, @socktype, value) } + block = -> { Addrinfo.new(@sockaddr, nil, @socktype, value) } block.should raise_error(SocketError) end @@ -510,13 +514,13 @@ describe "Addrinfo#initialize" do @sockaddr = Socket.sockaddr_in(80, '127.0.0.1') end - it 'returns an Addrinfo with :PF_INET family' do + it 'returns an Addrinfo with :PF_INET family' do addr = Addrinfo.new(@sockaddr, :PF_INET) addr.pfamily.should == Socket::PF_INET end - it 'returns an Addrinfo with :INET family' do + it 'returns an Addrinfo with :INET family' do addr = Addrinfo.new(@sockaddr, :INET) addr.pfamily.should == Socket::PF_INET @@ -540,13 +544,13 @@ describe "Addrinfo#initialize" do @sockaddr = Socket.sockaddr_in(80, '127.0.0.1') end - it 'returns an Addrinfo with "PF_INET" family' do + it 'returns an Addrinfo with "PF_INET" family' do addr = Addrinfo.new(@sockaddr, 'PF_INET') addr.pfamily.should == Socket::PF_INET end - it 'returns an Addrinfo with "INET" family' do + it 'returns an Addrinfo with "INET" family' do addr = Addrinfo.new(@sockaddr, 'INET') addr.pfamily.should == Socket::PF_INET @@ -565,23 +569,21 @@ describe "Addrinfo#initialize" do end end - with_feature :unix_socket do - describe 'using separate arguments for a Unix socket' do - before do - @sockaddr = Socket.pack_sockaddr_un('socket') - end + describe 'using separate arguments for a Unix socket' do + before do + @sockaddr = Socket.pack_sockaddr_un('socket') + end - it 'returns an Addrinfo with the correct unix path' do - Addrinfo.new(@sockaddr).unix_path.should == 'socket' - end + it 'returns an Addrinfo with the correct unix path' do + Addrinfo.new(@sockaddr).unix_path.should == 'socket' + end - it 'returns an Addrinfo with the correct protocol family' do - Addrinfo.new(@sockaddr).pfamily.should == Socket::PF_UNSPEC - end + it 'returns an Addrinfo with the correct protocol family' do + Addrinfo.new(@sockaddr).pfamily.should == Socket::PF_UNSPEC + end - it 'returns an Addrinfo with the correct address family' do - Addrinfo.new(@sockaddr).afamily.should == Socket::AF_UNIX - end + it 'returns an Addrinfo with the correct address family' do + Addrinfo.new(@sockaddr).afamily.should == Socket::AF_UNIX end end end diff --git a/spec/ruby/library/socket/addrinfo/inspect_sockaddr_spec.rb b/spec/ruby/library/socket/addrinfo/inspect_sockaddr_spec.rb index 70ca4dd4d7..6b18c79469 100644 --- a/spec/ruby/library/socket/addrinfo/inspect_sockaddr_spec.rb +++ b/spec/ruby/library/socket/addrinfo/inspect_sockaddr_spec.rb @@ -32,19 +32,17 @@ describe 'Addrinfo#inspect_sockaddr' do end end - with_feature :unix_socket do - describe 'using a UNIX path' do - it 'returns a String containing the UNIX path' do - addr = Addrinfo.unix('/foo/bar') + describe 'using a UNIX path' do + it 'returns a String containing the UNIX path' do + addr = Addrinfo.unix('/foo/bar') - addr.inspect_sockaddr.should == '/foo/bar' - end + addr.inspect_sockaddr.should == '/foo/bar' + end - it 'returns a String containing the UNIX path when using a relative path' do - addr = Addrinfo.unix('foo') + it 'returns a String containing the UNIX path when using a relative path' do + addr = Addrinfo.unix('foo') - addr.inspect_sockaddr.should == 'UNIX foo' - end + addr.inspect_sockaddr.should == 'UNIX foo' end end end diff --git a/spec/ruby/library/socket/addrinfo/inspect_spec.rb b/spec/ruby/library/socket/addrinfo/inspect_spec.rb index 98e1e83ffa..1442af6162 100644 --- a/spec/ruby/library/socket/addrinfo/inspect_spec.rb +++ b/spec/ruby/library/socket/addrinfo/inspect_spec.rb @@ -41,25 +41,23 @@ describe 'Addrinfo#inspect' do end end - with_feature :unix_socket do - describe 'using a UNIX Addrinfo' do - it 'returns a String' do - addr = Addrinfo.unix('/foo') + describe 'using a UNIX Addrinfo' do + it 'returns a String' do + addr = Addrinfo.unix('/foo') - addr.inspect.should == '#<Addrinfo: /foo SOCK_STREAM>' - end + addr.inspect.should == '#<Addrinfo: /foo SOCK_STREAM>' + end - it 'returns a String when using a relative UNIX path' do - addr = Addrinfo.unix('foo') + it 'returns a String when using a relative UNIX path' do + addr = Addrinfo.unix('foo') - addr.inspect.should == '#<Addrinfo: UNIX foo SOCK_STREAM>' - end + addr.inspect.should == '#<Addrinfo: UNIX foo SOCK_STREAM>' + end - it 'returns a String when using a DGRAM socket' do - addr = Addrinfo.unix('/foo', Socket::SOCK_DGRAM) + it 'returns a String when using a DGRAM socket' do + addr = Addrinfo.unix('/foo', Socket::SOCK_DGRAM) - addr.inspect.should == '#<Addrinfo: /foo SOCK_DGRAM>' - end + addr.inspect.should == '#<Addrinfo: /foo SOCK_DGRAM>' end end end diff --git a/spec/ruby/library/socket/addrinfo/ip_address_spec.rb b/spec/ruby/library/socket/addrinfo/ip_address_spec.rb index d53c00267a..193432e861 100644 --- a/spec/ruby/library/socket/addrinfo/ip_address_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ip_address_spec.rb @@ -21,15 +21,13 @@ describe "Addrinfo#ip_address" do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "raises an exception" do - lambda { @addrinfo.ip_address }.should raise_error(SocketError) - end + it "raises an exception" do + -> { @addrinfo.ip_address }.should raise_error(SocketError) end end diff --git a/spec/ruby/library/socket/addrinfo/ip_port_spec.rb b/spec/ruby/library/socket/addrinfo/ip_port_spec.rb index 6cf9e7a18e..f10ce35143 100644 --- a/spec/ruby/library/socket/addrinfo/ip_port_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ip_port_spec.rb @@ -21,15 +21,13 @@ describe "Addrinfo#ip_port" do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "raises an exception" do - lambda { @addrinfo.ip_port }.should raise_error(SocketError) - end + it "raises an exception" do + -> { @addrinfo.ip_port }.should raise_error(SocketError) end end end diff --git a/spec/ruby/library/socket/addrinfo/ip_spec.rb b/spec/ruby/library/socket/addrinfo/ip_spec.rb index 80e7a62df7..09b9341605 100644 --- a/spec/ruby/library/socket/addrinfo/ip_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ip_spec.rb @@ -22,15 +22,13 @@ describe "Addrinfo#ip?" do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "returns false" do - @addrinfo.ip?.should be_false - end + it "returns false" do + @addrinfo.ip?.should be_false end end end diff --git a/spec/ruby/library/socket/addrinfo/ip_unpack_spec.rb b/spec/ruby/library/socket/addrinfo/ip_unpack_spec.rb index 57ae79a6c8..58260c4557 100644 --- a/spec/ruby/library/socket/addrinfo/ip_unpack_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ip_unpack_spec.rb @@ -21,15 +21,13 @@ describe "Addrinfo#ip_unpack" do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "raises an exception" do - lambda { @addrinfo.ip_unpack }.should raise_error(SocketError) - end + it "raises an exception" do + -> { @addrinfo.ip_unpack }.should raise_error(SocketError) end end end diff --git a/spec/ruby/library/socket/addrinfo/ipv4_loopback_spec.rb b/spec/ruby/library/socket/addrinfo/ipv4_loopback_spec.rb index f5bab711db..3a584d4f52 100644 --- a/spec/ruby/library/socket/addrinfo/ipv4_loopback_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv4_loopback_spec.rb @@ -3,10 +3,10 @@ require_relative '../spec_helper' describe "Addrinfo#ipv4_loopback?" do describe "for an ipv4 socket" do it "returns true for the loopback address" do - Addrinfo.ip('127.0.0.1').ipv4_loopback?.should == true - Addrinfo.ip('127.0.0.2').ipv4_loopback?.should == true - Addrinfo.ip('127.255.0.1').ipv4_loopback?.should == true - Addrinfo.ip('127.255.255.255').ipv4_loopback?.should == true + Addrinfo.ip('127.0.0.1').should.ipv4_loopback? + Addrinfo.ip('127.0.0.2').should.ipv4_loopback? + Addrinfo.ip('127.255.0.1').should.ipv4_loopback? + Addrinfo.ip('127.255.255.255').should.ipv4_loopback? end it "returns false for another address" do @@ -29,15 +29,13 @@ describe "Addrinfo#ipv4_loopback?" do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "returns false" do - @addrinfo.ipv4_loopback?.should be_false - end + it "returns false" do + @addrinfo.ipv4_loopback?.should be_false end end end diff --git a/spec/ruby/library/socket/addrinfo/ipv4_multicast_spec.rb b/spec/ruby/library/socket/addrinfo/ipv4_multicast_spec.rb index bdd14c1b00..e4b4cfcc84 100644 --- a/spec/ruby/library/socket/addrinfo/ipv4_multicast_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv4_multicast_spec.rb @@ -2,28 +2,26 @@ require_relative '../spec_helper' describe "Addrinfo#ipv4_multicast?" do it 'returns true for a multicast address' do - Addrinfo.ip('224.0.0.0').ipv4_multicast?.should == true - Addrinfo.ip('224.0.0.9').ipv4_multicast?.should == true - Addrinfo.ip('239.255.255.250').ipv4_multicast?.should == true + Addrinfo.ip('224.0.0.0').should.ipv4_multicast? + Addrinfo.ip('224.0.0.9').should.ipv4_multicast? + Addrinfo.ip('239.255.255.250').should.ipv4_multicast? end it 'returns false for a regular address' do - Addrinfo.ip('8.8.8.8').ipv4_multicast?.should == false + Addrinfo.ip('8.8.8.8').should_not.ipv4_multicast? end it 'returns false for an IPv6 address' do - Addrinfo.ip('::1').ipv4_multicast?.should == false + Addrinfo.ip('::1').should_not.ipv4_multicast? end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "returns false" do - @addrinfo.ipv4_multicast?.should be_false - end + it "returns false" do + @addrinfo.ipv4_multicast?.should be_false end end end diff --git a/spec/ruby/library/socket/addrinfo/ipv4_private_spec.rb b/spec/ruby/library/socket/addrinfo/ipv4_private_spec.rb index 733577609e..97218b5ba3 100644 --- a/spec/ruby/library/socket/addrinfo/ipv4_private_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv4_private_spec.rb @@ -8,14 +8,14 @@ describe "Addrinfo#ipv4_private?" do end it "returns true for a private address" do - Addrinfo.ip('10.0.0.0').ipv4_private?.should == true - Addrinfo.ip('10.0.0.5').ipv4_private?.should == true + Addrinfo.ip('10.0.0.0').should.ipv4_private? + Addrinfo.ip('10.0.0.5').should.ipv4_private? - Addrinfo.ip('172.16.0.0').ipv4_private?.should == true - Addrinfo.ip('172.16.0.5').ipv4_private?.should == true + Addrinfo.ip('172.16.0.0').should.ipv4_private? + Addrinfo.ip('172.16.0.5').should.ipv4_private? - Addrinfo.ip('192.168.0.0').ipv4_private?.should == true - Addrinfo.ip('192.168.0.5').ipv4_private?.should == true + Addrinfo.ip('192.168.0.0').should.ipv4_private? + Addrinfo.ip('192.168.0.5').should.ipv4_private? end it "returns false for a public address" do @@ -33,15 +33,13 @@ describe "Addrinfo#ipv4_private?" do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "returns false" do - @addrinfo.ipv4_private?.should be_false - end + it "returns false" do + @addrinfo.ipv4_private?.should be_false end end end diff --git a/spec/ruby/library/socket/addrinfo/ipv4_spec.rb b/spec/ruby/library/socket/addrinfo/ipv4_spec.rb index 7cba8209b6..61f7759b10 100644 --- a/spec/ruby/library/socket/addrinfo/ipv4_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv4_spec.rb @@ -21,15 +21,13 @@ describe "Addrinfo#ipv4?" do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "returns false" do - @addrinfo.ipv4?.should be_false - end + it "returns false" do + @addrinfo.ipv4?.should be_false end end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_linklocal_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_linklocal_spec.rb index 291b4d7d6b..bfef396381 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_linklocal_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_linklocal_spec.rb @@ -5,19 +5,19 @@ guard -> { SocketSpecs.ipv6_available? } do describe 'Addrinfo#ipv6_linklocal?' do platform_is_not :aix do it 'returns true for a link-local address' do - Addrinfo.ip('fe80::').ipv6_linklocal?.should == true - Addrinfo.ip('fe81::').ipv6_linklocal?.should == true - Addrinfo.ip('fe8f::').ipv6_linklocal?.should == true - Addrinfo.ip('fe80::1').ipv6_linklocal?.should == true + Addrinfo.ip('fe80::').should.ipv6_linklocal? + Addrinfo.ip('fe81::').should.ipv6_linklocal? + Addrinfo.ip('fe8f::').should.ipv6_linklocal? + Addrinfo.ip('fe80::1').should.ipv6_linklocal? end end it 'returns false for a regular address' do - Addrinfo.ip('::1').ipv6_linklocal?.should == false + Addrinfo.ip('::1').should_not.ipv6_linklocal? end it 'returns false for an IPv4 address' do - Addrinfo.ip('127.0.0.1').ipv6_linklocal?.should == false + Addrinfo.ip('127.0.0.1').should_not.ipv6_linklocal? end end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_loopback_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_loopback_spec.rb index 9ff8f107bf..ffc75185ea 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_loopback_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_loopback_spec.rb @@ -31,15 +31,13 @@ describe "Addrinfo#ipv6_loopback?" do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end - - it "returns false" do - @addrinfo.ipv6_loopback?.should be_false - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end + + it "returns false" do + @addrinfo.ipv6_loopback?.should be_false end end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_mc_global_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_mc_global_spec.rb index 9c8e4dccb4..01fa0992ba 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_mc_global_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_mc_global_spec.rb @@ -2,19 +2,19 @@ require_relative '../spec_helper' describe 'Addrinfo#ipv6_mc_global?' do it 'returns true for a multi-cast address in the global scope' do - Addrinfo.ip('ff1e::').ipv6_mc_global?.should == true - Addrinfo.ip('fffe::').ipv6_mc_global?.should == true - Addrinfo.ip('ff0e::').ipv6_mc_global?.should == true - Addrinfo.ip('ff1e::1').ipv6_mc_global?.should == true + Addrinfo.ip('ff1e::').should.ipv6_mc_global? + Addrinfo.ip('fffe::').should.ipv6_mc_global? + Addrinfo.ip('ff0e::').should.ipv6_mc_global? + Addrinfo.ip('ff1e::1').should.ipv6_mc_global? end it 'returns false for a regular IPv6 address' do - Addrinfo.ip('::1').ipv6_mc_global?.should == false - Addrinfo.ip('ff1a::').ipv6_mc_global?.should == false - Addrinfo.ip('ff1f::1').ipv6_mc_global?.should == false + Addrinfo.ip('::1').should_not.ipv6_mc_global? + Addrinfo.ip('ff1a::').should_not.ipv6_mc_global? + Addrinfo.ip('ff1f::1').should_not.ipv6_mc_global? end it 'returns false for an IPv4 address' do - Addrinfo.ip('127.0.0.1').ipv6_mc_global?.should == false + Addrinfo.ip('127.0.0.1').should_not.ipv6_mc_global? end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_mc_linklocal_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_mc_linklocal_spec.rb index dd52a9ad8b..a1298919eb 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_mc_linklocal_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_mc_linklocal_spec.rb @@ -2,18 +2,18 @@ require_relative '../spec_helper' describe 'Addrinfo#ipv6_mc_linklocal?' do it 'returns true for a multi-cast link-local address' do - Addrinfo.ip('ff12::').ipv6_mc_linklocal?.should == true - Addrinfo.ip('ff02::').ipv6_mc_linklocal?.should == true - Addrinfo.ip('fff2::').ipv6_mc_linklocal?.should == true - Addrinfo.ip('ff12::1').ipv6_mc_linklocal?.should == true + Addrinfo.ip('ff12::').should.ipv6_mc_linklocal? + Addrinfo.ip('ff02::').should.ipv6_mc_linklocal? + Addrinfo.ip('fff2::').should.ipv6_mc_linklocal? + Addrinfo.ip('ff12::1').should.ipv6_mc_linklocal? end it 'returns false for a regular IPv6 address' do - Addrinfo.ip('::1').ipv6_mc_linklocal?.should == false - Addrinfo.ip('fff1::').ipv6_mc_linklocal?.should == false + Addrinfo.ip('::1').should_not.ipv6_mc_linklocal? + Addrinfo.ip('fff1::').should_not.ipv6_mc_linklocal? end it 'returns false for an IPv4 address' do - Addrinfo.ip('127.0.0.1').ipv6_mc_linklocal?.should == false + Addrinfo.ip('127.0.0.1').should_not.ipv6_mc_linklocal? end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_mc_nodelocal_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_mc_nodelocal_spec.rb index b3cf5ffbab..0aee952d88 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_mc_nodelocal_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_mc_nodelocal_spec.rb @@ -2,17 +2,17 @@ require_relative '../spec_helper' describe 'Addrinfo#ipv6_mc_nodelocal?' do it 'returns true for a multi-cast node-local address' do - Addrinfo.ip('ff11::').ipv6_mc_nodelocal?.should == true - Addrinfo.ip('ff01::').ipv6_mc_nodelocal?.should == true - Addrinfo.ip('fff1::').ipv6_mc_nodelocal?.should == true - Addrinfo.ip('ff11::1').ipv6_mc_nodelocal?.should == true + Addrinfo.ip('ff11::').should.ipv6_mc_nodelocal? + Addrinfo.ip('ff01::').should.ipv6_mc_nodelocal? + Addrinfo.ip('fff1::').should.ipv6_mc_nodelocal? + Addrinfo.ip('ff11::1').should.ipv6_mc_nodelocal? end it 'returns false for a regular IPv6 address' do - Addrinfo.ip('::1').ipv6_mc_nodelocal?.should == false + Addrinfo.ip('::1').should_not.ipv6_mc_nodelocal? end it 'returns false for an IPv4 address' do - Addrinfo.ip('127.0.0.1').ipv6_mc_nodelocal?.should == false + Addrinfo.ip('127.0.0.1').should_not.ipv6_mc_nodelocal? end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_mc_orglocal_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_mc_orglocal_spec.rb index 90653dd49c..2977a98d30 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_mc_orglocal_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_mc_orglocal_spec.rb @@ -2,17 +2,17 @@ require_relative '../spec_helper' describe 'Addrinfo#ipv6_mc_orglocal?' do it 'returns true for a multi-cast org-local address' do - Addrinfo.ip('ff18::').ipv6_mc_orglocal?.should == true - Addrinfo.ip('ff08::').ipv6_mc_orglocal?.should == true - Addrinfo.ip('fff8::').ipv6_mc_orglocal?.should == true - Addrinfo.ip('ff18::1').ipv6_mc_orglocal?.should == true + Addrinfo.ip('ff18::').should.ipv6_mc_orglocal? + Addrinfo.ip('ff08::').should.ipv6_mc_orglocal? + Addrinfo.ip('fff8::').should.ipv6_mc_orglocal? + Addrinfo.ip('ff18::1').should.ipv6_mc_orglocal? end it 'returns false for a regular IPv6 address' do - Addrinfo.ip('::1').ipv6_mc_orglocal?.should == false + Addrinfo.ip('::1').should_not.ipv6_mc_orglocal? end it 'returns false for an IPv4 address' do - Addrinfo.ip('127.0.0.1').ipv6_mc_orglocal?.should == false + Addrinfo.ip('127.0.0.1').should_not.ipv6_mc_orglocal? end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_mc_sitelocal_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_mc_sitelocal_spec.rb index 686b625e0d..58e5976a40 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_mc_sitelocal_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_mc_sitelocal_spec.rb @@ -2,17 +2,17 @@ require_relative '../spec_helper' describe 'Addrinfo#ipv6_mc_sitelocal?' do it 'returns true for a multi-cast site-local address' do - Addrinfo.ip('ff15::').ipv6_mc_sitelocal?.should == true - Addrinfo.ip('ff05::').ipv6_mc_sitelocal?.should == true - Addrinfo.ip('fff5::').ipv6_mc_sitelocal?.should == true - Addrinfo.ip('ff15::1').ipv6_mc_sitelocal?.should == true + Addrinfo.ip('ff15::').should.ipv6_mc_sitelocal? + Addrinfo.ip('ff05::').should.ipv6_mc_sitelocal? + Addrinfo.ip('fff5::').should.ipv6_mc_sitelocal? + Addrinfo.ip('ff15::1').should.ipv6_mc_sitelocal? end it 'returns false for a regular IPv6 address' do - Addrinfo.ip('::1').ipv6_mc_sitelocal?.should == false + Addrinfo.ip('::1').should_not.ipv6_mc_sitelocal? end it 'returns false for an IPv4 address' do - Addrinfo.ip('127.0.0.1').ipv6_mc_sitelocal?.should == false + Addrinfo.ip('127.0.0.1').should_not.ipv6_mc_sitelocal? end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_multicast_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_multicast_spec.rb index c25322869c..99d4e8cf4d 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_multicast_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_multicast_spec.rb @@ -18,31 +18,29 @@ describe "Addrinfo#ipv6_multicast?" do describe "for an ipv6 socket" do it "returns true for a multicast address" do - Addrinfo.ip('ff00::').ipv6_multicast?.should == true - Addrinfo.ip('ff00::1').ipv6_multicast?.should == true - Addrinfo.ip('ff08::1').ipv6_multicast?.should == true - Addrinfo.ip('fff8::1').ipv6_multicast?.should == true - - Addrinfo.ip('ff02::').ipv6_multicast?.should == true - Addrinfo.ip('ff02::1').ipv6_multicast?.should == true - Addrinfo.ip('ff0f::').ipv6_multicast?.should == true + Addrinfo.ip('ff00::').should.ipv6_multicast? + Addrinfo.ip('ff00::1').should.ipv6_multicast? + Addrinfo.ip('ff08::1').should.ipv6_multicast? + Addrinfo.ip('fff8::1').should.ipv6_multicast? + + Addrinfo.ip('ff02::').should.ipv6_multicast? + Addrinfo.ip('ff02::1').should.ipv6_multicast? + Addrinfo.ip('ff0f::').should.ipv6_multicast? end it "returns false for another address" do - Addrinfo.ip('::1').ipv6_multicast?.should == false - Addrinfo.ip('fe80::').ipv6_multicast?.should == false + Addrinfo.ip('::1').should_not.ipv6_multicast? + Addrinfo.ip('fe80::').should_not.ipv6_multicast? end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "returns false" do - @addrinfo.ipv6_multicast?.should be_false - end + it "returns false" do + @addrinfo.ipv6_multicast?.should be_false end end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_sitelocal_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_sitelocal_spec.rb index dd202a1749..9158eb5809 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_sitelocal_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_sitelocal_spec.rb @@ -5,19 +5,19 @@ guard -> { SocketSpecs.ipv6_available? } do describe 'Addrinfo#ipv6_sitelocal?' do platform_is_not :aix do it 'returns true for a site-local address' do - Addrinfo.ip('feef::').ipv6_sitelocal?.should == true - Addrinfo.ip('fee0::').ipv6_sitelocal?.should == true - Addrinfo.ip('fee2::').ipv6_sitelocal?.should == true - Addrinfo.ip('feef::1').ipv6_sitelocal?.should == true + Addrinfo.ip('feef::').should.ipv6_sitelocal? + Addrinfo.ip('fee0::').should.ipv6_sitelocal? + Addrinfo.ip('fee2::').should.ipv6_sitelocal? + Addrinfo.ip('feef::1').should.ipv6_sitelocal? end end it 'returns false for a regular IPv6 address' do - Addrinfo.ip('::1').ipv6_sitelocal?.should == false + Addrinfo.ip('::1').should_not.ipv6_sitelocal? end it 'returns false for an IPv4 address' do - Addrinfo.ip('127.0.0.1').ipv6_sitelocal?.should == false + Addrinfo.ip('127.0.0.1').should_not.ipv6_sitelocal? end end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_spec.rb index 131e38849c..436d5e930b 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_spec.rb @@ -21,15 +21,13 @@ describe "Addrinfo#ipv6?" do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "returns false" do - @addrinfo.ipv6?.should be_false - end + it "returns false" do + @addrinfo.ipv6?.should be_false end end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_to_ipv4_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_to_ipv4_spec.rb index 6dfaf531ae..29050bec20 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_to_ipv4_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_to_ipv4_spec.rb @@ -62,7 +62,7 @@ guard -> { SocketSpecs.ipv6_available? } do Addrinfo.ip('192.168.1.1').ipv6_to_ipv4.should be_nil end - with_feature :unix_socket do + describe 'for a unix socket' do it 'returns nil for a UNIX Addrinfo' do Addrinfo.unix('foo').ipv6_to_ipv4.should be_nil end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_unique_local_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_unique_local_spec.rb index b80a15fcb0..22f0fa3b75 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_unique_local_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_unique_local_spec.rb @@ -2,17 +2,17 @@ require_relative '../spec_helper' describe 'Addrinfo#ipv6_unique_local?' do it 'returns true for an unique local IPv6 address' do - Addrinfo.ip('fc00::').ipv6_unique_local?.should == true - Addrinfo.ip('fd00::').ipv6_unique_local?.should == true - Addrinfo.ip('fcff::').ipv6_unique_local?.should == true + Addrinfo.ip('fc00::').should.ipv6_unique_local? + Addrinfo.ip('fd00::').should.ipv6_unique_local? + Addrinfo.ip('fcff::').should.ipv6_unique_local? end it 'returns false for a regular IPv6 address' do - Addrinfo.ip('::1').ipv6_unique_local?.should == false - Addrinfo.ip('fe00::').ipv6_unique_local?.should == false + Addrinfo.ip('::1').should_not.ipv6_unique_local? + Addrinfo.ip('fe00::').should_not.ipv6_unique_local? end it 'returns false for an IPv4 address' do - Addrinfo.ip('127.0.0.1').ipv6_unique_local?.should == false + Addrinfo.ip('127.0.0.1').should_not.ipv6_unique_local? end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_unspecified_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_unspecified_spec.rb index dd79d57503..d63979ceda 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_unspecified_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_unspecified_spec.rb @@ -2,14 +2,14 @@ require_relative '../spec_helper' describe 'Addrinfo#ipv6_unspecified?' do it 'returns true for an unspecified IPv6 address' do - Addrinfo.ip('::').ipv6_unspecified?.should == true + Addrinfo.ip('::').should.ipv6_unspecified? end it 'returns false for a regular IPv6 address' do - Addrinfo.ip('::1').ipv6_unspecified?.should == false + Addrinfo.ip('::1').should_not.ipv6_unspecified? end it 'returns false for an IPv4 address' do - Addrinfo.ip('127.0.0.1').ipv6_unspecified?.should == false + Addrinfo.ip('127.0.0.1').should_not.ipv6_unspecified? end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_v4compat_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_v4compat_spec.rb index ab8388a21b..21ca85af99 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_v4compat_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_v4compat_spec.rb @@ -2,19 +2,19 @@ require_relative '../spec_helper' describe 'Addrinfo#ipv6_v4compat?' do it 'returns true for an IPv4 compatible address' do - Addrinfo.ip('::127.0.0.1').ipv6_v4compat?.should == true - Addrinfo.ip('::192.168.1.1').ipv6_v4compat?.should == true + Addrinfo.ip('::127.0.0.1').should.ipv6_v4compat? + Addrinfo.ip('::192.168.1.1').should.ipv6_v4compat? end it 'returns false for an IPv4 mapped address' do - Addrinfo.ip('::ffff:192.168.1.1').ipv6_v4compat?.should == false + Addrinfo.ip('::ffff:192.168.1.1').should_not.ipv6_v4compat? end it 'returns false for a regular IPv6 address' do - Addrinfo.ip('::1').ipv6_v4compat?.should == false + Addrinfo.ip('::1').should_not.ipv6_v4compat? end it 'returns false for an IPv4 address' do - Addrinfo.ip('127.0.0.1').ipv6_v4compat?.should == false + Addrinfo.ip('127.0.0.1').should_not.ipv6_v4compat? end end diff --git a/spec/ruby/library/socket/addrinfo/ipv6_v4mapped_spec.rb b/spec/ruby/library/socket/addrinfo/ipv6_v4mapped_spec.rb index 82b272c165..7dac0e75db 100644 --- a/spec/ruby/library/socket/addrinfo/ipv6_v4mapped_spec.rb +++ b/spec/ruby/library/socket/addrinfo/ipv6_v4mapped_spec.rb @@ -2,19 +2,19 @@ require_relative '../spec_helper' describe 'Addrinfo#ipv6_v4mapped?' do it 'returns true for an IPv4 compatible address' do - Addrinfo.ip('::ffff:192.168.1.1').ipv6_v4mapped?.should == true + Addrinfo.ip('::ffff:192.168.1.1').should.ipv6_v4mapped? end it 'returns false for an IPv4 compatible address' do - Addrinfo.ip('::192.168.1.1').ipv6_v4mapped?.should == false - Addrinfo.ip('::127.0.0.1').ipv6_v4mapped?.should == false + Addrinfo.ip('::192.168.1.1').should_not.ipv6_v4mapped? + Addrinfo.ip('::127.0.0.1').should_not.ipv6_v4mapped? end it 'returns false for a regular IPv6 address' do - Addrinfo.ip('::1').ipv6_v4mapped?.should == false + Addrinfo.ip('::1').should_not.ipv6_v4mapped? end it 'returns false for an IPv4 address' do - Addrinfo.ip('127.0.0.1').ipv6_v4mapped?.should == false + Addrinfo.ip('127.0.0.1').should_not.ipv6_v4mapped? end end diff --git a/spec/ruby/library/socket/addrinfo/listen_spec.rb b/spec/ruby/library/socket/addrinfo/listen_spec.rb index 714a96ed6c..931093f732 100644 --- a/spec/ruby/library/socket/addrinfo/listen_spec.rb +++ b/spec/ruby/library/socket/addrinfo/listen_spec.rb @@ -29,6 +29,6 @@ describe 'Addrinfo#listen' do socket = sock end - socket.closed?.should == true + socket.should.closed? end end diff --git a/spec/ruby/library/socket/addrinfo/marshal_dump_spec.rb b/spec/ruby/library/socket/addrinfo/marshal_dump_spec.rb index c4220a6f3e..e2c3497f7f 100644 --- a/spec/ruby/library/socket/addrinfo/marshal_dump_spec.rb +++ b/spec/ruby/library/socket/addrinfo/marshal_dump_spec.rb @@ -32,10 +32,8 @@ describe 'Addrinfo#marshal_dump' do @array[3].should == 'SOCK_STREAM' end - platform_is_not :'solaris2.10' do # i386-solaris - it 'includes the protocol as the 5th value' do - @array[4].should == 'IPPROTO_TCP' - end + it 'includes the protocol as the 5th value' do + @array[4].should == 'IPPROTO_TCP' end it 'includes the canonical name as the 6th value' do @@ -44,40 +42,38 @@ describe 'Addrinfo#marshal_dump' do end end - with_feature :unix_socket do - describe 'using a UNIX Addrinfo' do + describe 'using a UNIX Addrinfo' do + before do + @addr = Addrinfo.unix('foo') + end + + it 'returns an Array' do + @addr.marshal_dump.should be_an_instance_of(Array) + end + + describe 'the returned Array' do before do - @addr = Addrinfo.unix('foo') + @array = @addr.marshal_dump end - it 'returns an Array' do - @addr.marshal_dump.should be_an_instance_of(Array) + it 'includes the address family as the 1st value' do + @array[0].should == 'AF_UNIX' end - describe 'the returned Array' do - before do - @array = @addr.marshal_dump - end - - it 'includes the address family as the 1st value' do - @array[0].should == 'AF_UNIX' - end - - it 'includes the UNIX path as the 2nd value' do - @array[1].should == @addr.unix_path - end + it 'includes the UNIX path as the 2nd value' do + @array[1].should == @addr.unix_path + end - it 'includes the protocol family as the 3rd value' do - @array[2].should == 'PF_UNIX' - end + it 'includes the protocol family as the 3rd value' do + @array[2].should == 'PF_UNIX' + end - it 'includes the socket type as the 4th value' do - @array[3].should == 'SOCK_STREAM' - end + it 'includes the socket type as the 4th value' do + @array[3].should == 'SOCK_STREAM' + end - it 'includes the protocol as the 5th value' do - @array[4].should == 0 - end + it 'includes the protocol as the 5th value' do + @array[4].should == 0 end end end diff --git a/spec/ruby/library/socket/addrinfo/marshal_load_spec.rb b/spec/ruby/library/socket/addrinfo/marshal_load_spec.rb index aa20865224..02cef90115 100644 --- a/spec/ruby/library/socket/addrinfo/marshal_load_spec.rb +++ b/spec/ruby/library/socket/addrinfo/marshal_load_spec.rb @@ -18,18 +18,16 @@ describe 'Addrinfo#marshal_load' do end end - with_feature :unix_socket do - describe 'using a UNIX socket' do - it 'returns a new Addrinfo' do - source = Addrinfo.unix('foo') - addr = Marshal.load(Marshal.dump(source)) + describe 'using a UNIX socket' do + it 'returns a new Addrinfo' do + source = Addrinfo.unix('foo') + addr = Marshal.load(Marshal.dump(source)) - addr.afamily.should == source.afamily - addr.pfamily.should == source.pfamily - addr.socktype.should == source.socktype - addr.protocol.should == source.protocol - addr.unix_path.should == source.unix_path - end + addr.afamily.should == source.afamily + addr.pfamily.should == source.pfamily + addr.socktype.should == source.socktype + addr.protocol.should == source.protocol + addr.unix_path.should == source.unix_path end end end diff --git a/spec/ruby/library/socket/addrinfo/pfamily_spec.rb b/spec/ruby/library/socket/addrinfo/pfamily_spec.rb index 984744a964..da530b7fdc 100644 --- a/spec/ruby/library/socket/addrinfo/pfamily_spec.rb +++ b/spec/ruby/library/socket/addrinfo/pfamily_spec.rb @@ -29,15 +29,13 @@ describe "Addrinfo#pfamily" do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end - - it "returns Socket::PF_UNIX" do - @addrinfo.pfamily.should == Socket::PF_UNIX - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end + + it "returns Socket::PF_UNIX" do + @addrinfo.pfamily.should == Socket::PF_UNIX end end end diff --git a/spec/ruby/library/socket/addrinfo/protocol_spec.rb b/spec/ruby/library/socket/addrinfo/protocol_spec.rb index ea143fc4a8..f6ffc9acf9 100644 --- a/spec/ruby/library/socket/addrinfo/protocol_spec.rb +++ b/spec/ruby/library/socket/addrinfo/protocol_spec.rb @@ -10,15 +10,13 @@ describe "Addrinfo#protocol" do Addrinfo.tcp('::1', 80).protocol.should == Socket::IPPROTO_TCP end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "returns 0" do - @addrinfo.protocol.should == 0 - end + it "returns 0" do + @addrinfo.protocol.should == 0 end end end diff --git a/spec/ruby/library/socket/addrinfo/shared/to_sockaddr.rb b/spec/ruby/library/socket/addrinfo/shared/to_sockaddr.rb index c32da5986d..70d6bfbbfe 100644 --- a/spec/ruby/library/socket/addrinfo/shared/to_sockaddr.rb +++ b/spec/ruby/library/socket/addrinfo/shared/to_sockaddr.rb @@ -1,5 +1,4 @@ -describe :socket_addrinfo_to_sockaddr, :shared => true do - +describe :socket_addrinfo_to_sockaddr, shared: true do describe "for an ipv4 socket" do before :each do @addrinfo = Addrinfo.tcp("127.0.0.1", 80) @@ -20,15 +19,13 @@ describe :socket_addrinfo_to_sockaddr, :shared => true do end end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "returns a sockaddr packed structure" do - @addrinfo.send(@method).should == Socket.sockaddr_un('/tmp/sock') - end + it "returns a sockaddr packed structure" do + @addrinfo.send(@method).should == Socket.sockaddr_un('/tmp/sock') end end @@ -47,5 +44,4 @@ describe :socket_addrinfo_to_sockaddr, :shared => true do addr.send(@method).should == Socket.sockaddr_in(0, '') end end - end diff --git a/spec/ruby/library/socket/addrinfo/socktype_spec.rb b/spec/ruby/library/socket/addrinfo/socktype_spec.rb index b994bea140..e5f02cd759 100644 --- a/spec/ruby/library/socket/addrinfo/socktype_spec.rb +++ b/spec/ruby/library/socket/addrinfo/socktype_spec.rb @@ -9,15 +9,13 @@ describe "Addrinfo#socktype" do Addrinfo.tcp('127.0.0.1', 80).socktype.should == Socket::SOCK_STREAM end - with_feature :unix_socket do - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "returns Socket::SOCK_STREAM" do - @addrinfo.socktype.should == Socket::SOCK_STREAM - end + it "returns Socket::SOCK_STREAM" do + @addrinfo.socktype.should == Socket::SOCK_STREAM end end end diff --git a/spec/ruby/library/socket/addrinfo/udp_spec.rb b/spec/ruby/library/socket/addrinfo/udp_spec.rb index b05cbf9b0b..ac02e76ef5 100644 --- a/spec/ruby/library/socket/addrinfo/udp_spec.rb +++ b/spec/ruby/library/socket/addrinfo/udp_spec.rb @@ -27,10 +27,8 @@ describe 'Addrinfo.udp' do Addrinfo.udp(ip_address, 80).socktype.should == Socket::SOCK_DGRAM end - platform_is_not :solaris do - it 'sets the socket protocol' do - Addrinfo.udp(ip_address, 80).protocol.should == Socket::IPPROTO_UDP - end + it 'sets the socket protocol' do + Addrinfo.udp(ip_address, 80).protocol.should == Socket::IPPROTO_UDP end end end diff --git a/spec/ruby/library/socket/addrinfo/unix_path_spec.rb b/spec/ruby/library/socket/addrinfo/unix_path_spec.rb index 0f25881724..2a9076a354 100644 --- a/spec/ruby/library/socket/addrinfo/unix_path_spec.rb +++ b/spec/ruby/library/socket/addrinfo/unix_path_spec.rb @@ -1,37 +1,35 @@ require_relative '../spec_helper' -with_feature :unix_socket do - describe "Addrinfo#unix_path" do - describe "for an ipv4 socket" do +describe "Addrinfo#unix_path" do + describe "for an ipv4 socket" do - before :each do - @addrinfo = Addrinfo.tcp("127.0.0.1", 80) - end - - it "raises an exception" do - lambda { @addrinfo.unix_path }.should raise_error(SocketError) - end + before :each do + @addrinfo = Addrinfo.tcp("127.0.0.1", 80) + end + it "raises an exception" do + -> { @addrinfo.unix_path }.should raise_error(SocketError) end - describe "for an ipv6 socket" do - before :each do - @addrinfo = Addrinfo.tcp("::1", 80) - end + end - it "raises an exception" do - lambda { @addrinfo.unix_path }.should raise_error(SocketError) - end + describe "for an ipv6 socket" do + before :each do + @addrinfo = Addrinfo.tcp("::1", 80) end - describe "for a unix socket" do - before :each do - @addrinfo = Addrinfo.unix("/tmp/sock") - end + it "raises an exception" do + -> { @addrinfo.unix_path }.should raise_error(SocketError) + end + end + + describe "for a unix socket" do + before :each do + @addrinfo = Addrinfo.unix("/tmp/sock") + end - it "returns the socket path" do - @addrinfo.unix_path.should == "/tmp/sock" - end + it "returns the socket path" do + @addrinfo.unix_path.should == "/tmp/sock" end end end diff --git a/spec/ruby/library/socket/addrinfo/unix_spec.rb b/spec/ruby/library/socket/addrinfo/unix_spec.rb index 4596ece17e..7597533a76 100644 --- a/spec/ruby/library/socket/addrinfo/unix_spec.rb +++ b/spec/ruby/library/socket/addrinfo/unix_spec.rb @@ -1,36 +1,34 @@ require_relative '../spec_helper' -with_feature :unix_socket do - describe 'Addrinfo.unix' do - it 'returns an Addrinfo instance' do - Addrinfo.unix('socket').should be_an_instance_of(Addrinfo) - end +describe 'Addrinfo.unix' do + it 'returns an Addrinfo instance' do + Addrinfo.unix('socket').should be_an_instance_of(Addrinfo) + end - it 'sets the IP address' do - Addrinfo.unix('socket').unix_path.should == 'socket' - end + it 'sets the IP address' do + Addrinfo.unix('socket').unix_path.should == 'socket' + end - it 'sets the address family' do - Addrinfo.unix('socket').afamily.should == Socket::AF_UNIX - end + it 'sets the address family' do + Addrinfo.unix('socket').afamily.should == Socket::AF_UNIX + end - it 'sets the protocol family' do - Addrinfo.unix('socket').pfamily.should == Socket::PF_UNIX - end + it 'sets the protocol family' do + Addrinfo.unix('socket').pfamily.should == Socket::PF_UNIX + end - it 'sets the socket type' do - Addrinfo.unix('socket').socktype.should == Socket::SOCK_STREAM - end + it 'sets the socket type' do + Addrinfo.unix('socket').socktype.should == Socket::SOCK_STREAM + end - it 'sets a custom socket type' do - addr = Addrinfo.unix('socket', Socket::SOCK_DGRAM) + it 'sets a custom socket type' do + addr = Addrinfo.unix('socket', Socket::SOCK_DGRAM) - addr.socktype.should == Socket::SOCK_DGRAM - end + addr.socktype.should == Socket::SOCK_DGRAM + end - it 'sets the socket protocol to 0' do - Addrinfo.unix('socket').protocol.should == 0 - end + it 'sets the socket protocol to 0' do + Addrinfo.unix('socket').protocol.should == 0 end end diff --git a/spec/ruby/library/socket/ancillarydata/cmsg_is_spec.rb b/spec/ruby/library/socket/ancillarydata/cmsg_is_spec.rb index 50763e28b1..c54ee29825 100644 --- a/spec/ruby/library/socket/ancillarydata/cmsg_is_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/cmsg_is_spec.rb @@ -26,7 +26,7 @@ with_feature :ancillary_data do end it 'raises SocketError when comparing with :IPV6 and :RIGHTS' do - lambda { @data.cmsg_is?(:IPV6, :RIGHTS) }.should raise_error(SocketError) + -> { @data.cmsg_is?(:IPV6, :RIGHTS) }.should raise_error(SocketError) end end end diff --git a/spec/ruby/library/socket/ancillarydata/initialize_spec.rb b/spec/ruby/library/socket/ancillarydata/initialize_spec.rb index d4788d0f68..eca45599d7 100644 --- a/spec/ruby/library/socket/ancillarydata/initialize_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/initialize_spec.rb @@ -106,32 +106,32 @@ with_feature :ancillary_data do Socket::AncillaryData.new(:INET, :SOCKET, :RIGHTS, '').type.should == Socket::SCM_RIGHTS end - platform_is_not :"solaris2.10", :aix do + platform_is_not :aix do it 'sets the type to SCM_TIMESTAMP when using :TIMESTAMP as the type argument' do Socket::AncillaryData.new(:INET, :SOCKET, :TIMESTAMP, '').type.should == Socket::SCM_TIMESTAMP end end it 'raises TypeError when using a numeric string as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :IGMP, Socket::SCM_RIGHTS.to_s, '') }.should raise_error(TypeError) end it 'raises SocketError when using :RECVTTL as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :SOCKET, :RECVTTL, '') }.should raise_error(SocketError) end it 'raises SocketError when using :MOO as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :SOCKET, :MOO, '') }.should raise_error(SocketError) end it 'raises SocketError when using :IP_RECVTTL as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :SOCKET, :IP_RECVTTL, '') }.should raise_error(SocketError) end @@ -155,13 +155,13 @@ with_feature :ancillary_data do end it 'raises SocketError when using :RIGHTS as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :IP, :RIGHTS, '') }.should raise_error(SocketError) end it 'raises SocketError when using :MOO as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :IP, :MOO, '') }.should raise_error(SocketError) end @@ -179,13 +179,13 @@ with_feature :ancillary_data do end it 'raises SocketError when using :RIGHTS as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :IPV6, :RIGHTS, '') }.should raise_error(SocketError) end it 'raises SocketError when using :MOO as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :IPV6, :MOO, '') }.should raise_error(SocketError) end @@ -205,13 +205,13 @@ with_feature :ancillary_data do end it 'raises SocketError when using :RIGHTS as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :TCP, :RIGHTS, '') }.should raise_error(SocketError) end it 'raises SocketError when using :MOO as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :TCP, :MOO, '') }.should raise_error(SocketError) end @@ -225,13 +225,13 @@ with_feature :ancillary_data do end it 'raises SocketError when using :RIGHTS as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :UDP, :RIGHTS, '') }.should raise_error(SocketError) end it 'raises SocketError when using :MOO as the type argument' do - lambda { + -> { Socket::AncillaryData.new(:INET, :UDP, :MOO, '') }.should raise_error(SocketError) end @@ -243,7 +243,7 @@ with_feature :ancillary_data do end it 'raises SocketError when using :CORK sa the type argument' do - lambda { + -> { Socket::AncillaryData.new(:UNIX, :SOCKET, :CORK, '') }.should raise_error(SocketError) end @@ -251,7 +251,7 @@ with_feature :ancillary_data do describe 'using :AF_UNIX as the family and :IP as the level' do it 'raises SocketError' do - lambda { + -> { Socket::AncillaryData.new(:UNIX, :IP, :RECVTTL, '') }.should raise_error(SocketError) end @@ -259,7 +259,7 @@ with_feature :ancillary_data do describe 'using :AF_UNIX as the family and :IPV6 as the level' do it 'raises SocketError' do - lambda { + -> { Socket::AncillaryData.new(:UNIX, :IPV6, :NEXTHOP, '') }.should raise_error(SocketError) end @@ -267,7 +267,7 @@ with_feature :ancillary_data do describe 'using :AF_UNIX as the family and :TCP as the level' do it 'raises SocketError' do - lambda { + -> { Socket::AncillaryData.new(:UNIX, :TCP, :CORK, '') }.should raise_error(SocketError) end @@ -275,7 +275,7 @@ with_feature :ancillary_data do describe 'using :AF_UNIX as the family and :UDP as the level' do it 'raises SocketError' do - lambda { + -> { Socket::AncillaryData.new(:UNIX, :UDP, :CORK, '') }.should raise_error(SocketError) end diff --git a/spec/ruby/library/socket/ancillarydata/int_spec.rb b/spec/ruby/library/socket/ancillarydata/int_spec.rb index 0d7c5e3652..fe41a30a1a 100644 --- a/spec/ruby/library/socket/ancillarydata/int_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/int_spec.rb @@ -37,7 +37,7 @@ with_feature :ancillary_data do it 'raises when the data is not an Integer' do data = Socket::AncillaryData.new(:UNIX, :SOCKET, :RIGHTS, 'ugh') - lambda { data.int }.should raise_error(TypeError) + -> { data.int }.should raise_error(TypeError) end end end diff --git a/spec/ruby/library/socket/ancillarydata/ip_pktinfo_spec.rb b/spec/ruby/library/socket/ancillarydata/ip_pktinfo_spec.rb index 9fc4e8c201..84910a038a 100644 --- a/spec/ruby/library/socket/ancillarydata/ip_pktinfo_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/ip_pktinfo_spec.rb @@ -89,7 +89,7 @@ with_feature :ancillary_data, :pktinfo do end it 'is not the same object as the input Addrinfo' do - @addr.should_not.equal? @source + @addr.should_not equal @source end end @@ -109,7 +109,7 @@ with_feature :ancillary_data, :pktinfo do end it 'is not the same object as the input Addrinfo' do - @addr.should_not.equal? @dest + @addr.should_not equal @dest end end end diff --git a/spec/ruby/library/socket/ancillarydata/ipv6_pktinfo_spec.rb b/spec/ruby/library/socket/ancillarydata/ipv6_pktinfo_spec.rb index 6f576f5169..0fffc720dc 100644 --- a/spec/ruby/library/socket/ancillarydata/ipv6_pktinfo_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/ipv6_pktinfo_spec.rb @@ -58,7 +58,7 @@ with_feature :ancillary_data, :ipv6_pktinfo do end it 'is not the same object as the input Addrinfo' do - @addr.should_not.equal? @source + @addr.should_not equal @source end end diff --git a/spec/ruby/library/socket/ancillarydata/unix_rights_spec.rb b/spec/ruby/library/socket/ancillarydata/unix_rights_spec.rb index 34d954af81..95052fd91c 100644 --- a/spec/ruby/library/socket/ancillarydata/unix_rights_spec.rb +++ b/spec/ruby/library/socket/ancillarydata/unix_rights_spec.rb @@ -26,7 +26,7 @@ with_feature :ancillary_data do describe 'using non IO objects' do it 'raises TypeError' do - lambda { Socket::AncillaryData.unix_rights(10) }.should raise_error(TypeError) + -> { Socket::AncillaryData.unix_rights(10) }.should raise_error(TypeError) end end end @@ -47,14 +47,14 @@ with_feature :ancillary_data do it 'raises TypeError when the level is not SOL_SOCKET' do data = Socket::AncillaryData.new(:INET, :IP, :RECVTTL, '') - lambda { data.unix_rights }.should raise_error(TypeError) + -> { data.unix_rights }.should raise_error(TypeError) end - platform_is_not :"solaris2.10", :aix do + platform_is_not :aix do it 'raises TypeError when the type is not SCM_RIGHTS' do data = Socket::AncillaryData.new(:INET, :SOCKET, :TIMESTAMP, '') - lambda { data.unix_rights }.should raise_error(TypeError) + -> { data.unix_rights }.should raise_error(TypeError) end end end diff --git a/spec/ruby/library/socket/basicsocket/close_read_spec.rb b/spec/ruby/library/socket/basicsocket/close_read_spec.rb index c989fcaf72..f317b34955 100644 --- a/spec/ruby/library/socket/basicsocket/close_read_spec.rb +++ b/spec/ruby/library/socket/basicsocket/close_read_spec.rb @@ -12,13 +12,13 @@ describe "Socket::BasicSocket#close_read" do it "closes the reading end of the socket" do @server.close_read - lambda { @server.read }.should raise_error(IOError) + -> { @server.read }.should raise_error(IOError) end it 'does not raise when called on a socket already closed for reading' do @server.close_read @server.close_read - lambda { @server.read }.should raise_error(IOError) + -> { @server.read }.should raise_error(IOError) end it 'does not fully close the socket' do @@ -34,7 +34,7 @@ describe "Socket::BasicSocket#close_read" do it 'raises IOError when called on a fully closed socket' do @server.close - lambda { @server.close_read }.should raise_error(IOError) + -> { @server.close_read }.should raise_error(IOError) end it "returns nil" do diff --git a/spec/ruby/library/socket/basicsocket/close_write_spec.rb b/spec/ruby/library/socket/basicsocket/close_write_spec.rb index f37e0e5074..232cfbb7c6 100644 --- a/spec/ruby/library/socket/basicsocket/close_write_spec.rb +++ b/spec/ruby/library/socket/basicsocket/close_write_spec.rb @@ -12,13 +12,13 @@ describe "Socket::BasicSocket#close_write" do it "closes the writing end of the socket" do @server.close_write - lambda { @server.write("foo") }.should raise_error(IOError) + -> { @server.write("foo") }.should raise_error(IOError) end it 'does not raise when called on a socket already closed for writing' do @server.close_write @server.close_write - lambda { @server.write("foo") }.should raise_error(IOError) + -> { @server.write("foo") }.should raise_error(IOError) end it 'does not fully close the socket' do @@ -39,7 +39,7 @@ describe "Socket::BasicSocket#close_write" do it 'raises IOError when called on a fully closed socket' do @server.close - lambda { @server.close_write }.should raise_error(IOError) + -> { @server.close_write }.should raise_error(IOError) end it "returns nil" do diff --git a/spec/ruby/library/socket/basicsocket/connect_address_spec.rb b/spec/ruby/library/socket/basicsocket/connect_address_spec.rb index 18baac7f17..2e318fcb85 100644 --- a/spec/ruby/library/socket/basicsocket/connect_address_spec.rb +++ b/spec/ruby/library/socket/basicsocket/connect_address_spec.rb @@ -10,7 +10,7 @@ describe 'Socket#connect_address' do it 'raises SocketError' do @sock = Socket.new(:INET, :STREAM) - lambda { @sock.connect_address }.should raise_error(SocketError) + -> { @sock.connect_address }.should raise_error(SocketError) end end @@ -94,61 +94,59 @@ describe 'Socket#connect_address' do end end - with_feature :unix_socket do - platform_is_not :aix do - describe 'using an unbound UNIX socket' do - before do - @path = SocketSpecs.socket_path - @server = UNIXServer.new(@path) - @client = UNIXSocket.new(@path) - end - - after do - @client.close - @server.close - rm_r(@path) - end - - it 'raises SocketError' do - lambda { @client.connect_address }.should raise_error(SocketError) - end - end - end - - describe 'using a bound UNIX socket' do + platform_is_not :aix do + describe 'using an unbound UNIX socket' do before do @path = SocketSpecs.socket_path - @sock = UNIXServer.new(@path) + @server = UNIXServer.new(@path) + @client = UNIXSocket.new(@path) end after do - @sock.close + @client.close + @server.close rm_r(@path) end - it 'returns an Addrinfo' do - @sock.connect_address.should be_an_instance_of(Addrinfo) + it 'raises SocketError' do + -> { @client.connect_address }.should raise_error(SocketError) end + end + end - it 'uses the correct socket path' do - @sock.connect_address.unix_path.should == @path - end + describe 'using a bound UNIX socket' do + before do + @path = SocketSpecs.socket_path + @sock = UNIXServer.new(@path) + end - it 'uses AF_UNIX as the address family' do - @sock.connect_address.afamily.should == Socket::AF_UNIX - end + after do + @sock.close + rm_r(@path) + end - it 'uses PF_UNIX as the protocol family' do - @sock.connect_address.pfamily.should == Socket::PF_UNIX - end + it 'returns an Addrinfo' do + @sock.connect_address.should be_an_instance_of(Addrinfo) + end - it 'uses SOCK_STREAM as the socket type' do - @sock.connect_address.socktype.should == Socket::SOCK_STREAM - end + it 'uses the correct socket path' do + @sock.connect_address.unix_path.should == @path + end - it 'uses 0 as the protocol' do - @sock.connect_address.protocol.should == 0 - end + it 'uses AF_UNIX as the address family' do + @sock.connect_address.afamily.should == Socket::AF_UNIX + end + + it 'uses PF_UNIX as the protocol family' do + @sock.connect_address.pfamily.should == Socket::PF_UNIX + end + + it 'uses SOCK_STREAM as the socket type' do + @sock.connect_address.socktype.should == Socket::SOCK_STREAM + end + + it 'uses 0 as the protocol' do + @sock.connect_address.protocol.should == 0 end end end diff --git a/spec/ruby/library/socket/basicsocket/getpeereid_spec.rb b/spec/ruby/library/socket/basicsocket/getpeereid_spec.rb index 9eeb6d0e0b..2e03cd3684 100644 --- a/spec/ruby/library/socket/basicsocket/getpeereid_spec.rb +++ b/spec/ruby/library/socket/basicsocket/getpeereid_spec.rb @@ -2,7 +2,7 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' describe 'BasicSocket#getpeereid' do - with_feature :unix_socket do + platform_is_not :windows do describe 'using a UNIXSocket' do before do @path = SocketSpecs.socket_path @@ -30,7 +30,7 @@ describe 'BasicSocket#getpeereid' do it 'raises NoMethodError' do @sock = TCPServer.new('127.0.0.1', 0) - lambda { @sock.getpeereid }.should raise_error(NoMethodError) + -> { @sock.getpeereid }.should raise_error(NoMethodError) end end end diff --git a/spec/ruby/library/socket/basicsocket/getpeername_spec.rb b/spec/ruby/library/socket/basicsocket/getpeername_spec.rb index 23c73056cd..0b93f02eef 100644 --- a/spec/ruby/library/socket/basicsocket/getpeername_spec.rb +++ b/spec/ruby/library/socket/basicsocket/getpeername_spec.rb @@ -20,6 +20,6 @@ describe "Socket::BasicSocket#getpeername" do end it 'raises Errno::ENOTCONN for a disconnected socket' do - lambda { @server.getpeername }.should raise_error(Errno::ENOTCONN) + -> { @server.getpeername }.should raise_error(Errno::ENOTCONN) end end diff --git a/spec/ruby/library/socket/basicsocket/getsockopt_spec.rb b/spec/ruby/library/socket/basicsocket/getsockopt_spec.rb index aad21ca535..ce65d6c92b 100644 --- a/spec/ruby/library/socket/basicsocket/getsockopt_spec.rb +++ b/spec/ruby/library/socket/basicsocket/getsockopt_spec.rb @@ -41,7 +41,7 @@ describe "BasicSocket#getsockopt" do end it "raises a SystemCallError with an invalid socket option" do - lambda { @sock.getsockopt Socket::SOL_SOCKET, -1 }.should raise_error(Errno::ENOPROTOOPT) + -> { @sock.getsockopt Socket::SOL_SOCKET, -1 }.should raise_error(Errno::ENOPROTOOPT) end it 'returns a Socket::Option using a constant' do @@ -69,7 +69,7 @@ describe "BasicSocket#getsockopt" do end it 'raises Errno::ENOPROTOOPT when requesting an invalid option' do - lambda { @sock.getsockopt(Socket::SOL_SOCKET, -1) }.should raise_error(Errno::ENOPROTOOPT) + -> { @sock.getsockopt(Socket::SOL_SOCKET, -1) }.should raise_error(Errno::ENOPROTOOPT) end describe 'using Symbols as arguments' do diff --git a/spec/ruby/library/socket/basicsocket/local_address_spec.rb b/spec/ruby/library/socket/basicsocket/local_address_spec.rb new file mode 100644 index 0000000000..0bd60a44cd --- /dev/null +++ b/spec/ruby/library/socket/basicsocket/local_address_spec.rb @@ -0,0 +1,10 @@ +require_relative '../spec_helper' +require_relative '../shared/address' + +describe 'BasicSocket#local_address' do + it_behaves_like :socket_local_remote_address, :local_address, -> socket { + a2 = BasicSocket.for_fd(socket.fileno) + a2.autoclose = false + a2.local_address + } +end diff --git a/spec/ruby/library/socket/basicsocket/read_nonblock_spec.rb b/spec/ruby/library/socket/basicsocket/read_nonblock_spec.rb new file mode 100644 index 0000000000..ea5e65da5c --- /dev/null +++ b/spec/ruby/library/socket/basicsocket/read_nonblock_spec.rb @@ -0,0 +1,74 @@ +require_relative '../spec_helper' +require_relative '../fixtures/classes' + +describe "BasicSocket#read_nonblock" do + SocketSpecs.each_ip_protocol do |family, ip_address| + before :each do + @r = Socket.new(family, :DGRAM) + @w = Socket.new(family, :DGRAM) + + @r.bind(Socket.pack_sockaddr_in(0, ip_address)) + @w.send("aaa", 0, @r.getsockname) + end + + after :each do + @r.close unless @r.closed? + @w.close unless @w.closed? + end + + it "receives data after it's ready" do + IO.select([@r], nil, nil, 2) + @r.read_nonblock(5).should == "aaa" + end + + platform_is_not :windows do + it 'returned data is binary encoded regardless of the external encoding' do + IO.select([@r], nil, nil, 2) + @r.read_nonblock(1).encoding.should == Encoding::BINARY + + @w.send("bbb", 0, @r.getsockname) + @r.set_encoding(Encoding::ISO_8859_1) + IO.select([@r], nil, nil, 2) + buffer = @r.read_nonblock(3) + buffer.should == "bbb" + buffer.encoding.should == Encoding::BINARY + end + end + + it 'replaces the content of the provided buffer without changing its encoding' do + buffer = "initial data".dup.force_encoding(Encoding::UTF_8) + + IO.select([@r], nil, nil, 2) + @r.read_nonblock(3, buffer) + buffer.should == "aaa" + buffer.encoding.should == Encoding::UTF_8 + + @w.send("bbb", 0, @r.getsockname) + @r.set_encoding(Encoding::ISO_8859_1) + IO.select([@r], nil, nil, 2) + @r.read_nonblock(3, buffer) + buffer.should == "bbb" + buffer.encoding.should == Encoding::UTF_8 + end + + platform_is :linux do + it 'does not set the IO in nonblock mode' do + require 'io/nonblock' + @r.nonblock = false + IO.select([@r], nil, nil, 2) + @r.read_nonblock(3).should == "aaa" + @r.should_not.nonblock? + end + end + + platform_is_not :linux, :windows do + it 'sets the IO in nonblock mode' do + require 'io/nonblock' + @r.nonblock = false + IO.select([@r], nil, nil, 2) + @r.read_nonblock(3).should == "aaa" + @r.should.nonblock? + end + end + end +end diff --git a/spec/ruby/library/socket/basicsocket/read_spec.rb b/spec/ruby/library/socket/basicsocket/read_spec.rb new file mode 100644 index 0000000000..ba9de7d5cf --- /dev/null +++ b/spec/ruby/library/socket/basicsocket/read_spec.rb @@ -0,0 +1,47 @@ +require_relative '../spec_helper' +require_relative '../fixtures/classes' + +describe "BasicSocket#read" do + SocketSpecs.each_ip_protocol do |family, ip_address| + before :each do + @r = Socket.new(family, :DGRAM) + @w = Socket.new(family, :DGRAM) + + @r.bind(Socket.pack_sockaddr_in(0, ip_address)) + @w.send("aaa", 0, @r.getsockname) + end + + after :each do + @r.close unless @r.closed? + @w.close unless @w.closed? + end + + it "receives data after it's ready" do + @r.read(3).should == "aaa" + end + + it 'returned data is binary encoded regardless of the external encoding' do + @r.read(3).encoding.should == Encoding::BINARY + + @w.send("bbb", 0, @r.getsockname) + @r.set_encoding(Encoding::UTF_8) + buffer = @r.read(3) + buffer.should == "bbb" + buffer.encoding.should == Encoding::BINARY + end + + it 'replaces the content of the provided buffer without changing its encoding' do + buffer = "initial data".dup.force_encoding(Encoding::UTF_8) + + @r.read(3, buffer) + buffer.should == "aaa" + buffer.encoding.should == Encoding::UTF_8 + + @w.send("bbb", 0, @r.getsockname) + @r.set_encoding(Encoding::ISO_8859_1) + @r.read(3, buffer) + buffer.should == "bbb" + buffer.encoding.should == Encoding::UTF_8 + end + end +end diff --git a/spec/ruby/library/socket/basicsocket/recv_nonblock_spec.rb b/spec/ruby/library/socket/basicsocket/recv_nonblock_spec.rb index 26683b3d23..f2a6682f12 100644 --- a/spec/ruby/library/socket/basicsocket/recv_nonblock_spec.rb +++ b/spec/ruby/library/socket/basicsocket/recv_nonblock_spec.rb @@ -16,14 +16,14 @@ describe "Socket::BasicSocket#recv_nonblock" do platform_is_not :windows do describe 'using an unbound socket' do it 'raises an exception extending IO::WaitReadable' do - lambda { @s1.recv_nonblock(1) }.should raise_error(IO::WaitReadable) + -> { @s1.recv_nonblock(1) }.should raise_error(IO::WaitReadable) end end end it "raises an exception extending IO::WaitReadable if there's no data available" do @s1.bind(Socket.pack_sockaddr_in(0, ip_address)) - lambda { + -> { @s1.recv_nonblock(5) }.should raise_error(IO::WaitReadable) { |e| platform_is_not :windows do @@ -52,9 +52,19 @@ describe "Socket::BasicSocket#recv_nonblock" do @s2.send("data", 0, @s1.getsockname) IO.select([@s1], nil, nil, 2) - buf = "foo" - @s1.recv_nonblock(5, 0, buf) - buf.should == "data" + buffer = +"foo" + @s1.recv_nonblock(5, 0, buffer).should.equal?(buffer) + buffer.should == "data" + end + + it "preserves the encoding of the given buffer" do + @s1.bind(Socket.pack_sockaddr_in(0, ip_address)) + @s2.send("data", 0, @s1.getsockname) + IO.select([@s1], nil, nil, 2) + + buffer = ''.encode(Encoding::ISO_8859_1) + @s1.recv_nonblock(5, 0, buffer) + buffer.encoding.should == Encoding::ISO_8859_1 end it "does not block if there's no data available" do @@ -62,9 +72,101 @@ describe "Socket::BasicSocket#recv_nonblock" do @s2.send("a", 0, @s1.getsockname) IO.select([@s1], nil, nil, 2) @s1.recv_nonblock(1).should == "a" - lambda { + -> { @s1.recv_nonblock(5) }.should raise_error(IO::WaitReadable) end end + + SocketSpecs.each_ip_protocol do |family, ip_address| + describe 'using a connected but not bound socket' do + before do + @server = Socket.new(family, :STREAM) + end + + after do + @server.close + end + + it "raises Errno::ENOTCONN" do + -> { @server.recv_nonblock(1) }.should raise_error { |e| + [Errno::ENOTCONN, Errno::EINVAL].should.include?(e.class) + } + -> { @server.recv_nonblock(1, exception: false) }.should raise_error { |e| + [Errno::ENOTCONN, Errno::EINVAL].should.include?(e.class) + } + end + end + end +end + +describe "Socket::BasicSocket#recv_nonblock" do + context "when recvfrom(2) returns 0 (if no messages are available to be received and the peer has performed an orderly shutdown)" do + describe "stream socket" do + before :each do + @server = TCPServer.new('127.0.0.1', 0) + @port = @server.addr[1] + end + + after :each do + @server.close unless @server.closed? + end + + ruby_version_is ""..."3.3" do + it "returns an empty String on a closed stream socket" do + ready = false + + t = Thread.new do + client = @server.accept + + Thread.pass while !ready + begin + client.recv_nonblock(10) + rescue IO::EAGAINWaitReadable + retry + end + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + socket = TCPSocket.new('127.0.0.1', @port) + socket.close + ready = true + + t.value.should == "" + end + end + + ruby_version_is "3.3" do + it "returns nil on a closed stream socket" do + ready = false + + t = Thread.new do + client = @server.accept + + Thread.pass while !ready + begin + client.recv_nonblock(10) + rescue IO::EAGAINWaitReadable + retry + end + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + socket = TCPSocket.new('127.0.0.1', @port) + socket.close + ready = true + + t.value.should be_nil + end + end + end + end end diff --git a/spec/ruby/library/socket/basicsocket/recv_spec.rb b/spec/ruby/library/socket/basicsocket/recv_spec.rb index a277dc2d97..a51920f52a 100644 --- a/spec/ruby/library/socket/basicsocket/recv_spec.rb +++ b/spec/ruby/library/socket/basicsocket/recv_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../spec_helper' require_relative '../fixtures/classes' @@ -32,28 +32,26 @@ describe "BasicSocket#recv" do ScratchPad.recorded.should == 'hello' end - platform_is_not :solaris do - it "accepts flags to specify unusual receiving behaviour" do - t = Thread.new do - client = @server.accept + it "accepts flags to specify unusual receiving behaviour" do + t = Thread.new do + client = @server.accept - # in-band data (TCP), doesn't receive the flag. - ScratchPad.record client.recv(10) + # in-band data (TCP), doesn't receive the flag. + ScratchPad.record client.recv(10) - # this recv is important (TODO: explain) - client.recv(10) - client.close - end - Thread.pass while t.status and t.status != "sleep" - t.status.should_not be_nil - - socket = TCPSocket.new('127.0.0.1', @port) - socket.send('helloU', Socket::MSG_OOB) - socket.shutdown(1) - t.join - socket.close - ScratchPad.recorded.should == 'hello' + # this recv is important (TODO: explain) + client.recv(10) + client.close end + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + socket = TCPSocket.new('127.0.0.1', @port) + socket.send('helloU', Socket::MSG_OOB) + socket.shutdown(1) + t.join + socket.close + ScratchPad.recorded.should == 'hello' end it "gets lines delimited with a custom separator" do @@ -81,13 +79,29 @@ describe "BasicSocket#recv" do socket.write("data") client = @server.accept - buf = "foo" + buffer = +"foo" begin - client.recv(4, 0, buf) + client.recv(4, 0, buffer).should.equal?(buffer) ensure client.close end - buf.should == "data" + buffer.should == "data" + + socket.close + end + + it "preserves the encoding of the given buffer" do + socket = TCPSocket.new('127.0.0.1', @port) + socket.write("data") + + client = @server.accept + buffer = ''.encode(Encoding::ISO_8859_1) + begin + client.recv(4, 0, buffer) + ensure + client.close + end + buffer.encoding.should == Encoding::ISO_8859_1 socket.close end @@ -107,7 +121,7 @@ describe 'BasicSocket#recv' do describe 'using an unbound socket' do it 'blocks the caller' do - lambda { @server.recv(4) }.should block_caller + -> { @server.recv(4) }.should block_caller end end @@ -118,7 +132,7 @@ describe 'BasicSocket#recv' do describe 'without any data available' do it 'blocks the caller' do - lambda { @server.recv(4) }.should block_caller + -> { @server.recv(4) }.should block_caller end end @@ -144,7 +158,7 @@ describe 'BasicSocket#recv' do @server.recv(2).should == 'he' - lambda { @server.recv(4) }.should block_caller + -> { @server.recv(4) }.should block_caller end it 'takes a peek at the data when using the MSG_PEEK flag' do @@ -157,3 +171,80 @@ describe 'BasicSocket#recv' do end end end + +describe "BasicSocket#recv" do + context "when recvfrom(2) returns 0 (if no messages are available to be received and the peer has performed an orderly shutdown)" do + describe "stream socket" do + before :each do + @server = TCPServer.new('127.0.0.1', 0) + @port = @server.addr[1] + end + + after :each do + @server.close unless @server.closed? + end + + ruby_version_is ""..."3.3" do + it "returns an empty String on a closed stream socket" do + t = Thread.new do + client = @server.accept + client.recv(10) + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + socket = TCPSocket.new('127.0.0.1', @port) + socket.close + + t.value.should == "" + end + end + + ruby_version_is "3.3" do + it "returns nil on a closed stream socket" do + t = Thread.new do + client = @server.accept + client.recv(10) + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + socket = TCPSocket.new('127.0.0.1', @port) + socket.close + + t.value.should be_nil + end + end + end + + describe "datagram socket" do + SocketSpecs.each_ip_protocol do |family, ip_address| + before :each do + @server = UDPSocket.new(family) + @client = UDPSocket.new(family) + end + + after :each do + @server.close unless @server.closed? + @client.close unless @client.closed? + end + + it "returns empty String" do + @server.bind(ip_address, 0) + addr = @server.connect_address + @client.connect(addr.ip_address, addr.ip_port) + + @client.send('', 0) + + @server.recv(1).should == "" + end + end + end + end +end diff --git a/spec/ruby/library/socket/basicsocket/recvmsg_nonblock_spec.rb b/spec/ruby/library/socket/basicsocket/recvmsg_nonblock_spec.rb index c440bac8bf..b5fdd7c93b 100644 --- a/spec/ruby/library/socket/basicsocket/recvmsg_nonblock_spec.rb +++ b/spec/ruby/library/socket/basicsocket/recvmsg_nonblock_spec.rb @@ -17,7 +17,7 @@ describe 'BasicSocket#recvmsg_nonblock' do platform_is_not :windows do describe 'using an unbound socket' do it 'raises an exception extending IO::WaitReadable' do - lambda { @server.recvmsg_nonblock }.should raise_error(IO::WaitReadable) + -> { @server.recvmsg_nonblock }.should raise_error(IO::WaitReadable) end end end @@ -29,7 +29,7 @@ describe 'BasicSocket#recvmsg_nonblock' do describe 'without any data available' do it 'raises an exception extending IO::WaitReadable' do - lambda { @server.recvmsg_nonblock }.should raise_error(IO::WaitReadable) + -> { @server.recvmsg_nonblock }.should raise_error(IO::WaitReadable) end it 'returns :wait_readable with exception: false' do @@ -114,6 +114,21 @@ describe 'BasicSocket#recvmsg_nonblock' do end platform_is_not :windows do + describe 'using a connected but not bound socket' do + before do + @server = Socket.new(family, :STREAM) + end + + after do + @server.close + end + + it "raises Errno::ENOTCONN" do + -> { @server.recvmsg_nonblock }.should raise_error(Errno::ENOTCONN) + -> { @server.recvmsg_nonblock(exception: false) }.should raise_error(Errno::ENOTCONN) + end + end + describe 'using a connected socket' do before do @client = Socket.new(family, :STREAM) @@ -132,7 +147,7 @@ describe 'BasicSocket#recvmsg_nonblock' do describe 'without any data available' do it 'raises IO::WaitReadable' do - lambda { + -> { socket, _ = @server.accept begin socket.recvmsg_nonblock @@ -182,7 +197,7 @@ describe 'BasicSocket#recvmsg_nonblock' do end it 'raises when receiving the ip_address message' do - lambda { @addr.ip_address }.should raise_error(SocketError) + -> { @addr.ip_address }.should raise_error(SocketError) end it 'uses the correct address family' do @@ -198,7 +213,7 @@ describe 'BasicSocket#recvmsg_nonblock' do end it 'raises when receiving the ip_port message' do - lambda { @addr.ip_port }.should raise_error(SocketError) + -> { @addr.ip_port }.should raise_error(SocketError) end end end @@ -207,3 +222,79 @@ describe 'BasicSocket#recvmsg_nonblock' do end end end + +describe 'BasicSocket#recvmsg_nonblock' do + context "when recvfrom(2) returns 0 (if no messages are available to be received and the peer has performed an orderly shutdown)" do + describe "stream socket" do + before :each do + @server = TCPServer.new('127.0.0.1', 0) + @port = @server.addr[1] + end + + after :each do + @server.close unless @server.closed? + end + + ruby_version_is ""..."3.3" do + platform_is_not :windows do # #recvmsg_nonblock() raises 'Errno::EINVAL: Invalid argument - recvmsg(2)' + it "returns an empty String as received data on a closed stream socket" do + ready = false + + t = Thread.new do + client = @server.accept + + Thread.pass while !ready + begin + client.recvmsg_nonblock(10) + rescue IO::EAGAINWaitReadable + retry + end + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + socket = TCPSocket.new('127.0.0.1', @port) + socket.close + ready = true + + t.value.should.is_a? Array + t.value[0].should == "" + end + end + end + + ruby_version_is "3.3" do + platform_is_not :windows do + it "returns nil on a closed stream socket" do + ready = false + + t = Thread.new do + client = @server.accept + + Thread.pass while !ready + begin + client.recvmsg_nonblock(10) + rescue IO::EAGAINWaitReadable + retry + end + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + socket = TCPSocket.new('127.0.0.1', @port) + socket.close + ready = true + + t.value.should be_nil + end + end + end + end + end +end diff --git a/spec/ruby/library/socket/basicsocket/recvmsg_spec.rb b/spec/ruby/library/socket/basicsocket/recvmsg_spec.rb index 58eb8d0360..04ba1d74c7 100644 --- a/spec/ruby/library/socket/basicsocket/recvmsg_spec.rb +++ b/spec/ruby/library/socket/basicsocket/recvmsg_spec.rb @@ -17,7 +17,7 @@ describe 'BasicSocket#recvmsg' do platform_is_not :windows do describe 'using an unbound socket' do it 'blocks the caller' do - lambda { @server.recvmsg }.should block_caller + -> { @server.recvmsg }.should block_caller end end end @@ -29,7 +29,7 @@ describe 'BasicSocket#recvmsg' do describe 'without any data available' do it 'blocks the caller' do - lambda { @server.recvmsg }.should block_caller + -> { @server.recvmsg }.should block_caller end end @@ -126,7 +126,7 @@ describe 'BasicSocket#recvmsg' do it 'blocks the caller' do socket, _ = @server.accept begin - lambda { socket.recvmsg }.should block_caller + -> { socket.recvmsg }.should block_caller ensure socket.close end @@ -170,7 +170,7 @@ describe 'BasicSocket#recvmsg' do end it 'raises when receiving the ip_address message' do - lambda { @addr.ip_address }.should raise_error(SocketError) + -> { @addr.ip_address }.should raise_error(SocketError) end it 'uses the correct address family' do @@ -186,7 +186,7 @@ describe 'BasicSocket#recvmsg' do end it 'raises when receiving the ip_port message' do - lambda { @addr.ip_port }.should raise_error(SocketError) + -> { @addr.ip_port }.should raise_error(SocketError) end end end @@ -195,3 +195,87 @@ describe 'BasicSocket#recvmsg' do end end end + +describe 'BasicSocket#recvmsg' do + context "when recvfrom(2) returns 0 (if no messages are available to be received and the peer has performed an orderly shutdown)" do + describe "stream socket" do + before :each do + @server = TCPServer.new('127.0.0.1', 0) + @port = @server.addr[1] + end + + after :each do + @server.close unless @server.closed? + end + + ruby_version_is ""..."3.3" do + platform_is_not :windows do + it "returns an empty String as received data on a closed stream socket" do + t = Thread.new do + client = @server.accept + client.recvmsg(10) + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + socket = TCPSocket.new('127.0.0.1', @port) + socket.close + + t.value.should.is_a? Array + t.value[0].should == "" + end + end + end + + ruby_version_is "3.3" do + platform_is_not :windows do + it "returns nil on a closed stream socket" do + t = Thread.new do + client = @server.accept + client.recvmsg(10) + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + socket = TCPSocket.new('127.0.0.1', @port) + socket.close + + t.value.should be_nil + end + end + end + end + + describe "datagram socket" do + SocketSpecs.each_ip_protocol do |family, ip_address| + before :each do + @server = UDPSocket.new(family) + @client = UDPSocket.new(family) + end + + after :each do + @server.close unless @server.closed? + @client.close unless @client.closed? + end + + it "returns an empty String as received data" do + @server.bind(ip_address, 0) + addr = @server.connect_address + @client.connect(addr.ip_address, addr.ip_port) + + @client.send('', 0) + message = @server.recvmsg(1) + + message.should.is_a? Array + message[0].should == "" + end + end + end + end +end diff --git a/spec/ruby/library/socket/basicsocket/remote_address_spec.rb b/spec/ruby/library/socket/basicsocket/remote_address_spec.rb new file mode 100644 index 0000000000..439bf31592 --- /dev/null +++ b/spec/ruby/library/socket/basicsocket/remote_address_spec.rb @@ -0,0 +1,10 @@ +require_relative '../spec_helper' +require_relative '../shared/address' + +describe 'BasicSocket#remote_address' do + it_behaves_like :socket_local_remote_address, :remote_address, -> socket { + a2 = BasicSocket.for_fd(socket.fileno) + a2.autoclose = false + a2.remote_address + } +end diff --git a/spec/ruby/library/socket/basicsocket/send_spec.rb b/spec/ruby/library/socket/basicsocket/send_spec.rb index c4845fc09e..25ba3f5655 100644 --- a/spec/ruby/library/socket/basicsocket/send_spec.rb +++ b/spec/ruby/library/socket/basicsocket/send_spec.rb @@ -16,29 +16,29 @@ describe "BasicSocket#send" do @socket.close end - it "sends a message to another socket and returns the number of bytes sent" do - data = "" - t = Thread.new do - client = @server.accept - loop do - got = client.recv(5) - break if got.empty? - data << got - end - client.close - end - Thread.pass while t.status and t.status != "sleep" - t.status.should_not be_nil - - @socket.send('hello', 0).should == 5 - @socket.shutdown(1) # indicate, that we are done sending - @socket.recv(10) - - t.join - data.should == 'hello' - end - - platform_is_not :solaris, :windows do + it "sends a message to another socket and returns the number of bytes sent" do + data = +"" + t = Thread.new do + client = @server.accept + loop do + got = client.recv(5) + break if got.nil? || got.empty? + data << got + end + client.close + end + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + @socket.send('hello', 0).should == 5 + @socket.shutdown(1) # indicate, that we are done sending + @socket.recv(10) + + t.join + data.should == 'hello' + end + + platform_is_not :windows do it "accepts flags to specify unusual sending behaviour" do data = nil peek_data = nil @@ -62,25 +62,25 @@ describe "BasicSocket#send" do end it "accepts a sockaddr as recipient address" do - data = "" - t = Thread.new do - client = @server.accept - loop do - got = client.recv(5) - break if got.empty? - data << got - end - client.close - end - Thread.pass while t.status and t.status != "sleep" - t.status.should_not be_nil - - sockaddr = Socket.pack_sockaddr_in(@port, "127.0.0.1") - @socket.send('hello', 0, sockaddr).should == 5 - @socket.shutdown # indicate, that we are done sending - - t.join - data.should == 'hello' + data = +"" + t = Thread.new do + client = @server.accept + loop do + got = client.recv(5) + break if got.nil? || got.empty? + data << got + end + client.close + end + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + sockaddr = Socket.pack_sockaddr_in(@port, "127.0.0.1") + @socket.send('hello', 0, sockaddr).should == 5 + @socket.shutdown # indicate, that we are done sending + + t.join + data.should == 'hello' end end @@ -99,9 +99,17 @@ describe 'BasicSocket#send' do @server.close end + describe 'with an object implementing #to_str' do + it 'returns the amount of sent bytes' do + data = mock('message') + data.should_receive(:to_str).and_return('hello') + @client.send(data, 0, @server.getsockname).should == 5 + end + end + describe 'without a destination address' do it "raises #{SocketSpecs.dest_addr_req_error}" do - lambda { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error) + -> { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error) end end @@ -113,7 +121,7 @@ describe 'BasicSocket#send' do it 'does not persist the connection after writing to the socket' do @client.send('hello', 0, @server.getsockname) - lambda { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error) + -> { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error) end end @@ -161,7 +169,7 @@ describe 'BasicSocket#send' do it 'sends the message to the given address instead' do @client.send('hello', 0, @alt_server.getsockname).should == 5 - lambda { @server.recv(5) }.should block_caller + -> { @server.recv(5) }.should block_caller @alt_server.recv(5).should == 'hello' end diff --git a/spec/ruby/library/socket/basicsocket/sendmsg_nonblock_spec.rb b/spec/ruby/library/socket/basicsocket/sendmsg_nonblock_spec.rb index 000971f6af..7acfc659bd 100644 --- a/spec/ruby/library/socket/basicsocket/sendmsg_nonblock_spec.rb +++ b/spec/ruby/library/socket/basicsocket/sendmsg_nonblock_spec.rb @@ -18,7 +18,12 @@ describe 'BasicSocket#sendmsg_nonblock' do describe 'without a destination address' do it "raises #{SocketSpecs.dest_addr_req_error}" do - lambda { @client.sendmsg_nonblock('hello') }.should raise_error(SocketSpecs.dest_addr_req_error) + -> { + @client.sendmsg_nonblock('hello') + }.should raise_error(SocketSpecs.dest_addr_req_error) + -> { + @client.sendmsg_nonblock('hello', exception: false) + }.should raise_error(SocketSpecs.dest_addr_req_error) end end @@ -70,7 +75,7 @@ describe 'BasicSocket#sendmsg_nonblock' do it 'sends the message to the given address instead' do @client.sendmsg_nonblock('hello', 0, @alt_server.getsockname).should == 5 - lambda { @server.recv(5) }.should block_caller + -> { @server.recv(5) }.should block_caller @alt_server.recv(5).should == 'hello' end end @@ -94,7 +99,7 @@ describe 'BasicSocket#sendmsg_nonblock' do end it 'raises IO::WaitWritable when the underlying buffer is full' do - lambda { + -> { 10.times { @client.sendmsg_nonblock('hello' * 1_000_000) } }.should raise_error(IO::WaitWritable) end diff --git a/spec/ruby/library/socket/basicsocket/sendmsg_spec.rb b/spec/ruby/library/socket/basicsocket/sendmsg_spec.rb index f2c11f443a..7ff336c0b7 100644 --- a/spec/ruby/library/socket/basicsocket/sendmsg_spec.rb +++ b/spec/ruby/library/socket/basicsocket/sendmsg_spec.rb @@ -19,7 +19,7 @@ describe 'BasicSocket#sendmsg' do platform_is_not :windows do describe 'without a destination address' do it "raises #{SocketSpecs.dest_addr_req_error}" do - lambda { @client.sendmsg('hello') }.should raise_error(SocketSpecs.dest_addr_req_error) + -> { @client.sendmsg('hello') }.should raise_error(SocketSpecs.dest_addr_req_error) end end end @@ -74,7 +74,7 @@ describe 'BasicSocket#sendmsg' do it 'sends the message to the given address instead' do @client.sendmsg('hello', 0, @alt_server.getsockname).should == 5 - lambda { @server.recv(5) }.should block_caller + -> { @server.recv(5) }.should block_caller @alt_server.recv(5).should == 'hello' end @@ -101,7 +101,7 @@ describe 'BasicSocket#sendmsg' do it 'blocks when the underlying buffer is full' do # Buffer sizes may differ per platform, so sadly this is the only # reliable way of testing blocking behaviour. - lambda do + -> do 10.times { @client.sendmsg('hello' * 1_000_000) } end.should block_caller end diff --git a/spec/ruby/library/socket/basicsocket/setsockopt_spec.rb b/spec/ruby/library/socket/basicsocket/setsockopt_spec.rb index 1471b03798..f686e67326 100644 --- a/spec/ruby/library/socket/basicsocket/setsockopt_spec.rb +++ b/spec/ruby/library/socket/basicsocket/setsockopt_spec.rb @@ -38,7 +38,7 @@ describe "BasicSocket#setsockopt" do platform_is_not :windows do it "raises EINVAL if passed wrong linger value" do - lambda do + -> do @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, 0) end.should raise_error(Errno::EINVAL) end @@ -70,7 +70,7 @@ describe "BasicSocket#setsockopt" do n.should_not == [0].pack("i") platform_is_not :windows do - lambda { + -> { @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_OOBINLINE, "") }.should raise_error(SystemCallError) end @@ -80,7 +80,7 @@ describe "BasicSocket#setsockopt" do n.should_not == [0].pack("i") platform_is_not :windows do - lambda { + -> { @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_OOBINLINE, "0") }.should raise_error(SystemCallError) end @@ -90,13 +90,13 @@ describe "BasicSocket#setsockopt" do n.should == [0].pack("i") platform_is_not :windows do - lambda { + -> { @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_OOBINLINE, "1") }.should raise_error(SystemCallError) end platform_is_not :windows do - lambda { + -> { @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_OOBINLINE, "\x00\x00\x00") }.should raise_error(SystemCallError) end @@ -125,7 +125,7 @@ describe "BasicSocket#setsockopt" do n = @sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF).to_s n.unpack('i')[0].should >= 1 - lambda { + -> { @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, nil).should == 0 }.should raise_error(TypeError) @@ -137,23 +137,23 @@ describe "BasicSocket#setsockopt" do n = @sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF).to_s n.unpack('i')[0].should >= 2 - lambda { + -> { @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, "") }.should raise_error(SystemCallError) - lambda { + -> { @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, "bla") }.should raise_error(SystemCallError) - lambda { + -> { @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, "0") }.should raise_error(SystemCallError) - lambda { + -> { @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, "1") }.should raise_error(SystemCallError) - lambda { + -> { @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, "\x00\x00\x00") }.should raise_error(SystemCallError) @@ -224,7 +224,7 @@ describe 'BasicSocket#setsockopt' do describe 'using separate arguments with Symbols' do it 'raises TypeError when the first argument is nil' do - lambda { @socket.setsockopt(nil, :REUSEADDR, true) }.should raise_error(TypeError) + -> { @socket.setsockopt(nil, :REUSEADDR, true) }.should raise_error(TypeError) end it 'sets a boolean option' do @@ -251,7 +251,7 @@ describe 'BasicSocket#setsockopt' do platform_is_not :windows do it 'raises Errno::EINVAL when setting an invalid option value' do - lambda { @socket.setsockopt(:SOCKET, :OOBINLINE, 'bla') }.should raise_error(Errno::EINVAL) + -> { @socket.setsockopt(:SOCKET, :OOBINLINE, 'bla') }.should raise_error(Errno::EINVAL) end end end @@ -305,32 +305,30 @@ describe 'BasicSocket#setsockopt' do it 'raises ArgumentError when passing 2 arguments' do option = Socket::Option.bool(:INET, :SOCKET, :REUSEADDR, true) - lambda { @socket.setsockopt(option, :REUSEADDR) }.should raise_error(ArgumentError) + -> { @socket.setsockopt(option, :REUSEADDR) }.should raise_error(ArgumentError) end it 'raises TypeError when passing 3 arguments' do option = Socket::Option.bool(:INET, :SOCKET, :REUSEADDR, true) - lambda { @socket.setsockopt(option, :REUSEADDR, true) }.should raise_error(TypeError) + -> { @socket.setsockopt(option, :REUSEADDR, true) }.should raise_error(TypeError) end end end - with_feature :unix_socket do - describe 'using a UNIX socket' do - before do - @path = SocketSpecs.socket_path - @server = UNIXServer.new(@path) - end + describe 'using a UNIX socket' do + before do + @path = SocketSpecs.socket_path + @server = UNIXServer.new(@path) + end - after do - @server.close - rm_r @path - end + after do + @server.close + rm_r @path + end - it 'sets a boolean option' do - @server.setsockopt(:SOCKET, :REUSEADDR, true) - @server.getsockopt(:SOCKET, :REUSEADDR).bool.should == true - end + it 'sets a boolean option' do + @server.setsockopt(:SOCKET, :REUSEADDR, true) + @server.getsockopt(:SOCKET, :REUSEADDR).bool.should == true end end end diff --git a/spec/ruby/library/socket/basicsocket/shutdown_spec.rb b/spec/ruby/library/socket/basicsocket/shutdown_spec.rb index 87a32ae1dc..c78b32de38 100644 --- a/spec/ruby/library/socket/basicsocket/shutdown_spec.rb +++ b/spec/ruby/library/socket/basicsocket/shutdown_spec.rb @@ -23,25 +23,25 @@ platform_is_not :windows do # hangs it 'shuts down a socket for reading' do @client.shutdown(Socket::SHUT_RD) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for writing' do @client.shutdown(Socket::SHUT_WR) - lambda { @client.write('hello') }.should raise_error(Errno::EPIPE) + -> { @client.write('hello') }.should raise_error(Errno::EPIPE) end it 'shuts down a socket for reading and writing' do @client.shutdown(Socket::SHUT_RDWR) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty - lambda { @client.write('hello') }.should raise_error(Errno::EPIPE) + -> { @client.write('hello') }.should raise_error(Errno::EPIPE) end it 'raises ArgumentError when using an invalid option' do - lambda { @server.shutdown(666) }.should raise_error(ArgumentError) + -> { @server.shutdown(666) }.should raise_error(ArgumentError) end end @@ -49,37 +49,37 @@ platform_is_not :windows do # hangs it 'shuts down a socket for reading using :RD' do @client.shutdown(:RD) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for reading using :SHUT_RD' do @client.shutdown(:SHUT_RD) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for writing using :WR' do @client.shutdown(:WR) - lambda { @client.write('hello') }.should raise_error(Errno::EPIPE) + -> { @client.write('hello') }.should raise_error(Errno::EPIPE) end it 'shuts down a socket for writing using :SHUT_WR' do @client.shutdown(:SHUT_WR) - lambda { @client.write('hello') }.should raise_error(Errno::EPIPE) + -> { @client.write('hello') }.should raise_error(Errno::EPIPE) end it 'shuts down a socket for reading and writing' do @client.shutdown(:RDWR) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty - lambda { @client.write('hello') }.should raise_error(Errno::EPIPE) + -> { @client.write('hello') }.should raise_error(Errno::EPIPE) end it 'raises ArgumentError when using an invalid option' do - lambda { @server.shutdown(:Nope) }.should raise_error(SocketError) + -> { @server.shutdown(:Nope) }.should raise_error(SocketError) end end @@ -87,29 +87,29 @@ platform_is_not :windows do # hangs it 'shuts down a socket for reading using "RD"' do @client.shutdown('RD') - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for reading using "SHUT_RD"' do @client.shutdown('SHUT_RD') - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for writing using "WR"' do @client.shutdown('WR') - lambda { @client.write('hello') }.should raise_error(Errno::EPIPE) + -> { @client.write('hello') }.should raise_error(Errno::EPIPE) end it 'shuts down a socket for writing using "SHUT_WR"' do @client.shutdown('SHUT_WR') - lambda { @client.write('hello') }.should raise_error(Errno::EPIPE) + -> { @client.write('hello') }.should raise_error(Errno::EPIPE) end it 'raises ArgumentError when using an invalid option' do - lambda { @server.shutdown('Nope') }.should raise_error(SocketError) + -> { @server.shutdown('Nope') }.should raise_error(SocketError) end end @@ -123,7 +123,7 @@ platform_is_not :windows do # hangs @client.shutdown(@dummy) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for reading using "SHUT_RD"' do @@ -131,7 +131,7 @@ platform_is_not :windows do # hangs @client.shutdown(@dummy) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty end it 'shuts down a socket for reading and writing' do @@ -139,15 +139,15 @@ platform_is_not :windows do # hangs @client.shutdown(@dummy) - @client.recv(1).should be_empty + @client.recv(1).to_s.should be_empty - lambda { @client.write('hello') }.should raise_error(Errno::EPIPE) + -> { @client.write('hello') }.should raise_error(Errno::EPIPE) end end describe 'using an object that does not respond to #to_str' do it 'raises TypeError' do - lambda { @server.shutdown(mock(:dummy)) }.should raise_error(TypeError) + -> { @server.shutdown(mock(:dummy)) }.should raise_error(TypeError) end end end diff --git a/spec/ruby/library/socket/basicsocket/write_nonblock_spec.rb b/spec/ruby/library/socket/basicsocket/write_nonblock_spec.rb new file mode 100644 index 0000000000..523e732959 --- /dev/null +++ b/spec/ruby/library/socket/basicsocket/write_nonblock_spec.rb @@ -0,0 +1,43 @@ +require_relative '../spec_helper' +require_relative '../fixtures/classes' + +describe "BasicSocket#write_nonblock" do + SocketSpecs.each_ip_protocol do |family, ip_address| + before :each do + @r = Socket.new(family, :DGRAM) + @w = Socket.new(family, :DGRAM) + + @r.bind(Socket.pack_sockaddr_in(0, ip_address)) + @w.connect(@r.getsockname) + end + + after :each do + @r.close unless @r.closed? + @w.close unless @w.closed? + end + + it "sends data" do + @w.write_nonblock("aaa").should == 3 + IO.select([@r], nil, nil, 2) + @r.recv_nonblock(5).should == "aaa" + end + + platform_is :linux do + it 'does not set the IO in nonblock mode' do + require 'io/nonblock' + @w.nonblock = false + @w.write_nonblock("aaa").should == 3 + @w.should_not.nonblock? + end + end + + platform_is_not :linux, :windows do + it 'sets the IO in nonblock mode' do + require 'io/nonblock' + @w.nonblock = false + @w.write_nonblock("aaa").should == 3 + @w.should.nonblock? + end + end + end +end diff --git a/spec/ruby/library/socket/constants/constants_spec.rb b/spec/ruby/library/socket/constants/constants_spec.rb index 710af12828..b9a9d42725 100644 --- a/spec/ruby/library/socket/constants/constants_spec.rb +++ b/spec/ruby/library/socket/constants/constants_spec.rb @@ -68,7 +68,7 @@ describe "Socket::Constants" do end end - platform_is_not :solaris, :windows, :aix do + platform_is_not :windows, :aix, :android do it "defines multicast options" do consts = ["IP_MAX_MEMBERSHIPS"] consts.each do |c| diff --git a/spec/ruby/library/socket/fixtures/classes.rb b/spec/ruby/library/socket/fixtures/classes.rb index 4a590502ca..786629d2ef 100644 --- a/spec/ruby/library/socket/fixtures/classes.rb +++ b/spec/ruby/library/socket/fixtures/classes.rb @@ -37,7 +37,9 @@ module SocketSpecs # Check for too long unix socket path (max 104 bytes on macOS) # Note that Linux accepts not null-terminated paths but the man page advises against it. if path.bytesize > 104 - path = "/tmp/unix_server_spec.socket" + # rm_r in spec/mspec/lib/mspec/helpers/fs.rb fails against + # "/tmp/unix_server_spec.socket" + skip "too long unix socket path: #{path}" end rm_socket(path) path @@ -111,7 +113,7 @@ module SocketSpecs begin data = socket.recv(1024) - return if data.empty? + return if data.nil? || data.empty? log "SpecTCPServer received: #{data.inspect}" return if data == "QUIT" diff --git a/spec/ruby/library/socket/ipsocket/addr_spec.rb b/spec/ruby/library/socket/ipsocket/addr_spec.rb index 07f4cb20c9..199eb85ab7 100644 --- a/spec/ruby/library/socket/ipsocket/addr_spec.rb +++ b/spec/ruby/library/socket/ipsocket/addr_spec.rb @@ -81,7 +81,7 @@ describe 'Socket::IPSocket#addr' do describe 'using :cats as the argument' do it 'raises ArgumentError' do - lambda { @server.addr(:cats) }.should raise_error(ArgumentError) + -> { @server.addr(:cats) }.should raise_error(ArgumentError) end end end diff --git a/spec/ruby/library/socket/ipsocket/getaddress_spec.rb b/spec/ruby/library/socket/ipsocket/getaddress_spec.rb index 8e14707d4f..329f8267d3 100644 --- a/spec/ruby/library/socket/ipsocket/getaddress_spec.rb +++ b/spec/ruby/library/socket/ipsocket/getaddress_spec.rb @@ -2,7 +2,6 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' describe "Socket::IPSocket#getaddress" do - it "returns the IP address of hostname" do addr_local = IPSocket.getaddress(SocketSpecs.hostname) ["127.0.0.1", "::1"].include?(addr_local).should == true @@ -14,12 +13,16 @@ describe "Socket::IPSocket#getaddress" do IPSocket.getaddress('::1').should == '::1' end + it 'returns IPv4 compatible IPv6 addresses' do + IPSocket.getaddress('::ffff:192.168.1.1').should == '::ffff:192.168.1.1' + end + # There is no way to make this fail-proof on all machines, because # DNS servers like opendns return A records for ANY host, including # traditionally invalidly named ones. it "raises an error on unknown hostnames" do - lambda { - IPSocket.getaddress("rubyspecdoesntexist.fallingsnow.net") + -> { + IPSocket.getaddress("rubyspecdoesntexist.ruby-lang.org") }.should raise_error(SocketError) end end diff --git a/spec/ruby/library/socket/ipsocket/peeraddr_spec.rb b/spec/ruby/library/socket/ipsocket/peeraddr_spec.rb index 26aa61d1c4..702650940b 100644 --- a/spec/ruby/library/socket/ipsocket/peeraddr_spec.rb +++ b/spec/ruby/library/socket/ipsocket/peeraddr_spec.rb @@ -16,7 +16,7 @@ describe "Socket::IPSocket#peeraddr" do end it "raises error if socket is not connected" do - lambda { + -> { @server.peeraddr }.should raise_error(Errno::ENOTCONN) end @@ -92,7 +92,7 @@ describe 'Socket::IPSocket#peeraddr' do describe 'using :cats as the argument' do it 'raises ArgumentError' do - lambda { @client.peeraddr(:cats) }.should raise_error(ArgumentError) + -> { @client.peeraddr(:cats) }.should raise_error(ArgumentError) end end end diff --git a/spec/ruby/library/socket/ipsocket/recvfrom_spec.rb b/spec/ruby/library/socket/ipsocket/recvfrom_spec.rb index 3bcb7b8f02..b58903df23 100644 --- a/spec/ruby/library/socket/ipsocket/recvfrom_spec.rb +++ b/spec/ruby/library/socket/ipsocket/recvfrom_spec.rb @@ -64,11 +64,93 @@ describe "Socket::IPSocket#recvfrom" do data.size.should == 2 data.first.should == "hel" - # This does not apply to every platform, dependant on recvfrom(2) + # This does not apply to every platform, dependent on recvfrom(2) # data.last.should == nil end end +describe "Socket::IPSocket#recvfrom" do + context "when recvfrom(2) returns 0 (if no messages are available to be received and the peer has performed an orderly shutdown)" do + describe "stream socket" do + before :each do + @server = TCPServer.new("127.0.0.1", 0) + port = @server.addr[1] + @client = TCPSocket.new("127.0.0.1", port) + end + + after :each do + @server.close unless @server.closed? + @client.close unless @client.closed? + end + + ruby_version_is ""..."3.3" do + it "returns an empty String as received data on a closed stream socket" do + t = Thread.new do + client = @server.accept + message = client.recvfrom(10) + message + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + @client.close + + t.value.should.is_a? Array + t.value[0].should == "" + end + end + + ruby_version_is "3.3" do + it "returns nil on a closed stream socket" do + t = Thread.new do + client = @server.accept + message = client.recvfrom(10) + message + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + @client.close + + t.value.should be_nil + end + end + end + + describe "datagram socket" do + SocketSpecs.each_ip_protocol do |family, ip_address| + before :each do + @server = UDPSocket.new(family) + @client = UDPSocket.new(family) + end + + after :each do + @server.close unless @server.closed? + @client.close unless @client.closed? + end + + it "returns an empty String as received data" do + @server.bind(ip_address, 0) + addr = @server.connect_address + @client.connect(addr.ip_address, addr.ip_port) + + @client.send('', 0) + message = @server.recvfrom(1) + + message.should.is_a? Array + message[0].should == "" + end + end + end + end +end + describe 'Socket::IPSocket#recvfrom' do SocketSpecs.each_ip_protocol do |family, ip_address, family_name| before do diff --git a/spec/ruby/library/socket/option/bool_spec.rb b/spec/ruby/library/socket/option/bool_spec.rb index c4f8a277ba..144a78043d 100644 --- a/spec/ruby/library/socket/option/bool_spec.rb +++ b/spec/ruby/library/socket/option/bool_spec.rb @@ -21,7 +21,7 @@ describe "Socket::Option#bool" do platform_is_not :windows do it 'raises TypeError when called on a non boolean option' do opt = Socket::Option.linger(1, 4) - lambda { opt.bool }.should raise_error(TypeError) + -> { opt.bool }.should raise_error(TypeError) end end end diff --git a/spec/ruby/library/socket/option/initialize_spec.rb b/spec/ruby/library/socket/option/initialize_spec.rb index 0d4621b71e..8071ad7ef0 100644 --- a/spec/ruby/library/socket/option/initialize_spec.rb +++ b/spec/ruby/library/socket/option/initialize_spec.rb @@ -32,19 +32,19 @@ describe 'Socket::Option#initialize' do end it 'raises when using an invalid address family' do - lambda { + -> { Socket::Option.new(:INET2, :SOCKET, :KEEPALIVE, @bool) }.should raise_error(SocketError) end it 'raises when using an invalid level' do - lambda { + -> { Socket::Option.new(:INET, :CATS, :KEEPALIVE, @bool) }.should raise_error(SocketError) end it 'raises when using an invalid option name' do - lambda { + -> { Socket::Option.new(:INET, :SOCKET, :CATS, @bool) }.should raise_error(SocketError) end @@ -63,19 +63,19 @@ describe 'Socket::Option#initialize' do end it 'raises when using an invalid address family' do - lambda { + -> { Socket::Option.new('INET2', 'SOCKET', 'KEEPALIVE', @bool) }.should raise_error(SocketError) end it 'raises when using an invalid level' do - lambda { + -> { Socket::Option.new('INET', 'CATS', 'KEEPALIVE', @bool) }.should raise_error(SocketError) end it 'raises when using an invalid option name' do - lambda { + -> { Socket::Option.new('INET', 'SOCKET', 'CATS', @bool) }.should raise_error(SocketError) end diff --git a/spec/ruby/library/socket/option/int_spec.rb b/spec/ruby/library/socket/option/int_spec.rb index 5c67ec26d8..8c69ef6cbd 100644 --- a/spec/ruby/library/socket/option/int_spec.rb +++ b/spec/ruby/library/socket/option/int_spec.rb @@ -37,7 +37,7 @@ describe "Socket::Option#int" do platform_is_not :windows do it 'raises TypeError when called on a non integer option' do opt = Socket::Option.linger(1, 4) - lambda { opt.int }.should raise_error(TypeError) + -> { opt.int }.should raise_error(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 94467ebf71..ee987db85b 100644 --- a/spec/ruby/library/socket/option/linger_spec.rb +++ b/spec/ruby/library/socket/option/linger_spec.rb @@ -52,25 +52,25 @@ describe "Socket::Option#linger" do 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_error(TypeError) end it 'raises TypeError when called on a non SOL_SOCKET/SO_LINGER option' do opt = Socket::Option.int(:INET, :IP, :TTL, 4) - lambda { opt.linger }.should raise_error(TypeError) + -> { opt.linger }.should raise_error(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_error(TypeError) end end it 'raises TypeError when called on a non linger option' do opt = Socket::Option.new(:INET, :SOCKET, :LINGER, '') - lambda { opt.linger }.should raise_error(TypeError) + -> { opt.linger }.should raise_error(TypeError) end end diff --git a/spec/ruby/library/socket/option/new_spec.rb b/spec/ruby/library/socket/option/new_spec.rb index f3b7b31c91..a9e6f09097 100644 --- a/spec/ruby/library/socket/option/new_spec.rb +++ b/spec/ruby/library/socket/option/new_spec.rb @@ -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_error(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_error(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_error(SocketError) end end diff --git a/spec/ruby/library/socket/shared/address.rb b/spec/ruby/library/socket/shared/address.rb new file mode 100644 index 0000000000..49ba17c400 --- /dev/null +++ b/spec/ruby/library/socket/shared/address.rb @@ -0,0 +1,259 @@ +require_relative '../fixtures/classes' + +describe :socket_local_remote_address, shared: true do + describe 'using TCPSocket' do + before :each do + @s = TCPServer.new('127.0.0.1', 0) + @a = TCPSocket.new('127.0.0.1', @s.addr[1]) + @b = @s.accept + @addr = @object.call(@a) + end + + after :each do + [@b, @a, @s].each(&:close) + end + + it 'uses AF_INET as the address family' do + @addr.afamily.should == Socket::AF_INET + end + + it 'uses PF_INET as the protocol family' do + @addr.pfamily.should == Socket::PF_INET + end + + it 'uses SOCK_STREAM as the socket type' do + @addr.socktype.should == Socket::SOCK_STREAM + end + + it 'uses the correct IP address' do + @addr.ip_address.should == '127.0.0.1' + end + + it 'uses the correct port' do + if @method == :local_address + @addr.ip_port.should != @s.addr[1] + else + @addr.ip_port.should == @s.addr[1] + end + end + + it 'equals address of peer socket' do + if @method == :local_address + @addr.to_s.should == @b.remote_address.to_s + else + @addr.to_s.should == @b.local_address.to_s + end + end + + it 'returns an Addrinfo' do + @addr.should be_an_instance_of(Addrinfo) + end + + it 'uses 0 as the protocol' do + @addr.protocol.should == 0 + end + + it 'can be used to connect to the server' do + skip if @method == :local_address + b = @addr.connect + begin + b.remote_address.to_s.should == @addr.to_s + ensure + b.close + end + end + end + + guard -> { SocketSpecs.ipv6_available? } do + describe 'using IPv6' do + before :each do + @s = TCPServer.new('::1', 0) + @a = TCPSocket.new('::1', @s.addr[1]) + @b = @s.accept + @addr = @object.call(@a) + end + + after :each do + [@b, @a, @s].each(&:close) + end + + it 'uses AF_INET6 as the address family' do + @addr.afamily.should == Socket::AF_INET6 + end + + it 'uses PF_INET6 as the protocol family' do + @addr.pfamily.should == Socket::PF_INET6 + end + + it 'uses SOCK_STREAM as the socket type' do + @addr.socktype.should == Socket::SOCK_STREAM + end + + it 'uses the correct IP address' do + @addr.ip_address.should == '::1' + end + + it 'uses the correct port' do + if @method == :local_address + @addr.ip_port.should != @s.addr[1] + else + @addr.ip_port.should == @s.addr[1] + end + end + + it 'equals address of peer socket' do + if @method == :local_address + @addr.to_s.should == @b.remote_address.to_s + else + @addr.to_s.should == @b.local_address.to_s + end + end + + it 'returns an Addrinfo' do + @addr.should be_an_instance_of(Addrinfo) + end + + it 'uses 0 as the protocol' do + @addr.protocol.should == 0 + end + + it 'can be used to connect to the server' do + skip if @method == :local_address + b = @addr.connect + begin + b.remote_address.to_s.should == @addr.to_s + ensure + b.close + end + end + end + end + + describe 'using UNIXSocket' do + before :each do + @path = SocketSpecs.socket_path + @s = UNIXServer.new(@path) + @a = UNIXSocket.new(@path) + @b = @s.accept + @addr = @object.call(@a) + end + + after :each do + [@b, @a, @s].each(&:close) + rm_r(@path) + end + + it 'uses AF_UNIX as the address family' do + @addr.afamily.should == Socket::AF_UNIX + end + + it 'uses PF_UNIX as the protocol family' do + @addr.pfamily.should == Socket::PF_UNIX + end + + it 'uses SOCK_STREAM as the socket type' do + @addr.socktype.should == Socket::SOCK_STREAM + end + + it 'uses the correct socket path' do + if @method == :local_address + @addr.unix_path.should == "" + else + @addr.unix_path.should == @path + end + end + + platform_is_not :windows do + it 'equals address of peer socket' do + if @method == :local_address + @addr.to_s.should == @b.remote_address.to_s + else + @addr.to_s.should == @b.local_address.to_s + end + end + end + + guard -> { platform_is :windows and ruby_bug "#21702", ""..."4.2" } do + it 'equals address of peer socket' do + if @method == :local_address + @addr.to_s.should == @b.remote_address.to_s + else + @addr.to_s.should == @b.local_address.to_s + end + end + end + + it 'returns an Addrinfo' do + @addr.should be_an_instance_of(Addrinfo) + end + + it 'uses 0 as the protocol' do + @addr.protocol.should == 0 + end + + it 'can be used to connect to the server' do + skip if @method == :local_address + b = @addr.connect + begin + b.remote_address.to_s.should == @addr.to_s + ensure + b.close + end + end + end + + describe 'using UDPSocket' do + before :each do + @s = UDPSocket.new + @s.bind("127.0.0.1", 0) + @a = UDPSocket.new + @a.connect("127.0.0.1", @s.addr[1]) + @addr = @object.call(@a) + end + + after :each do + [@a, @s].each(&:close) + end + + it 'uses the correct address family' do + @addr.afamily.should == Socket::AF_INET + end + + it 'uses the correct protocol family' do + @addr.pfamily.should == Socket::PF_INET + end + + it 'uses SOCK_DGRAM as the socket type' do + @addr.socktype.should == Socket::SOCK_DGRAM + end + + it 'uses the correct IP address' do + @addr.ip_address.should == '127.0.0.1' + end + + it 'uses the correct port' do + if @method == :local_address + @addr.ip_port.should != @s.addr[1] + else + @addr.ip_port.should == @s.addr[1] + end + end + + it 'returns an Addrinfo' do + @addr.should be_an_instance_of(Addrinfo) + end + + it 'uses 0 as the protocol' do + @addr.protocol.should == 0 + end + + it 'can be used to connect to the peer' do + b = @addr.connect + begin + b.remote_address.to_s.should == @addr.to_s + ensure + b.close + end + end + end +end diff --git a/spec/ruby/library/socket/shared/pack_sockaddr.rb b/spec/ruby/library/socket/shared/pack_sockaddr.rb index 40b0d8d59e..4bfcf4edb9 100644 --- a/spec/ruby/library/socket/shared/pack_sockaddr.rb +++ b/spec/ruby/library/socket/shared/pack_sockaddr.rb @@ -17,6 +17,14 @@ describe :socket_pack_sockaddr_in, shared: true do sockaddr_in = Socket.public_send(@method, nil, '127.0.0.1') Socket.unpack_sockaddr_in(sockaddr_in).should == [0, '127.0.0.1'] + + sockaddr_in = Socket.public_send(@method, 80, Socket::INADDR_ANY) + Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '0.0.0.0'] + end + + it 'resolves the service name to a port' do + sockaddr_in = Socket.public_send(@method, 'http', '127.0.0.1') + Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '127.0.0.1'] end describe 'using an IPv4 address' do @@ -28,47 +36,32 @@ describe :socket_pack_sockaddr_in, shared: true do end end - platform_is_not :solaris do - describe 'using an IPv6 address' do - it 'returns a String of 28 bytes' do - str = Socket.public_send(@method, 80, '::1') - - str.should be_an_instance_of(String) - str.bytesize.should == 28 - end - end - end - - platform_is :solaris do - describe 'using an IPv6 address' do - it 'returns a String of 32 bytes' do - str = Socket.public_send(@method, 80, '::1') + describe 'using an IPv6 address' do + it 'returns a String of 28 bytes' do + str = Socket.public_send(@method, 80, '::1') - str.should be_an_instance_of(String) - str.bytesize.should == 32 - end + str.should be_an_instance_of(String) + str.bytesize.should == 28 end end end describe :socket_pack_sockaddr_un, shared: true do - with_feature :unix_socket do - it 'should be idempotent' do - bytes = Socket.public_send(@method, '/tmp/foo').bytes - bytes[2..9].should == [47, 116, 109, 112, 47, 102, 111, 111] - bytes[10..-1].all?(&:zero?).should == true - end + it 'should be idempotent' do + bytes = Socket.public_send(@method, '/tmp/foo').bytes + bytes[2..9].should == [47, 116, 109, 112, 47, 102, 111, 111] + bytes[10..-1].all?(&:zero?).should == true + end - it "packs and unpacks" do - sockaddr_un = Socket.public_send(@method, '/tmp/s') - Socket.unpack_sockaddr_un(sockaddr_un).should == '/tmp/s' - end + it "packs and unpacks" do + sockaddr_un = Socket.public_send(@method, '/tmp/s') + Socket.unpack_sockaddr_un(sockaddr_un).should == '/tmp/s' + end - it "handles correctly paths with multibyte chars" do - sockaddr_un = Socket.public_send(@method, '/home/вася/sock') - path = Socket.unpack_sockaddr_un(sockaddr_un).encode('UTF-8', 'UTF-8') - path.should == '/home/вася/sock' - end + it "handles correctly paths with multibyte chars" do + sockaddr_un = Socket.public_send(@method, '/home/вася/sock') + path = Socket.unpack_sockaddr_un(sockaddr_un).encode('UTF-8', 'UTF-8') + path.should == '/home/вася/sock' end platform_is :linux do @@ -89,11 +82,11 @@ describe :socket_pack_sockaddr_un, shared: true do end end - platform_is_not :windows, :aix do + platform_is_not :aix do it "raises ArgumentError for paths that are too long" do # AIX doesn't raise error long_path = 'a' * 110 - lambda { Socket.public_send(@method, long_path) }.should raise_error(ArgumentError) + -> { Socket.public_send(@method, long_path) }.should raise_error(ArgumentError) end end end diff --git a/spec/ruby/library/socket/shared/partially_closable_sockets.rb b/spec/ruby/library/socket/shared/partially_closable_sockets.rb index 1bdff08bf6..b1c2ebabe1 100644 --- a/spec/ruby/library/socket/shared/partially_closable_sockets.rb +++ b/spec/ruby/library/socket/shared/partially_closable_sockets.rb @@ -1,4 +1,4 @@ -describe "partially closable sockets", shared: true do +describe :partially_closable_sockets, shared: true do it "if the write end is closed then the other side can read past EOF without blocking" do @s1.write("foo") @s1.close_write diff --git a/spec/ruby/library/socket/shared/socketpair.rb b/spec/ruby/library/socket/shared/socketpair.rb index 08db2e59b8..25146cfff6 100644 --- a/spec/ruby/library/socket/shared/socketpair.rb +++ b/spec/ruby/library/socket/shared/socketpair.rb @@ -42,11 +42,11 @@ describe :socket_socketpair, shared: true do end it 'raises SocketError for an unknown address family' do - lambda { Socket.public_send(@method, :CATS, :STREAM) }.should raise_error(SocketError) + -> { Socket.public_send(@method, :CATS, :STREAM) }.should raise_error(SocketError) end it 'raises SocketError for an unknown socket type' do - lambda { Socket.public_send(@method, :UNIX, :CATS) }.should raise_error(SocketError) + -> { Socket.public_send(@method, :UNIX, :CATS) }.should raise_error(SocketError) end end @@ -61,11 +61,11 @@ describe :socket_socketpair, shared: true do end it 'raises SocketError for an unknown address family' do - lambda { Socket.public_send(@method, 'CATS', 'STREAM') }.should raise_error(SocketError) + -> { Socket.public_send(@method, 'CATS', 'STREAM') }.should raise_error(SocketError) end it 'raises SocketError for an unknown socket type' do - lambda { Socket.public_send(@method, 'UNIX', 'CATS') }.should raise_error(SocketError) + -> { Socket.public_send(@method, 'UNIX', 'CATS') }.should raise_error(SocketError) end end @@ -92,7 +92,7 @@ describe :socket_socketpair, shared: true do family.stub!(:to_str).and_return(Socket::AF_UNIX) type.stub!(:to_str).and_return(Socket::SOCK_STREAM) - lambda { Socket.public_send(@method, family, type) }.should raise_error(TypeError) + -> { Socket.public_send(@method, family, type) }.should raise_error(TypeError) end it 'raises SocketError for an unknown address family' do @@ -102,7 +102,7 @@ describe :socket_socketpair, shared: true do family.stub!(:to_str).and_return('CATS') type.stub!(:to_str).and_return('STREAM') - lambda { Socket.public_send(@method, family, type) }.should raise_error(SocketError) + -> { Socket.public_send(@method, family, type) }.should raise_error(SocketError) end it 'raises SocketError for an unknown socket type' do @@ -112,7 +112,7 @@ describe :socket_socketpair, shared: true do family.stub!(:to_str).and_return('UNIX') type.stub!(:to_str).and_return('CATS') - lambda { Socket.public_send(@method, family, type) }.should raise_error(SocketError) + -> { Socket.public_send(@method, family, type) }.should raise_error(SocketError) end end diff --git a/spec/ruby/library/socket/socket/accept_loop_spec.rb b/spec/ruby/library/socket/socket/accept_loop_spec.rb index ee11569da3..78e8c3fa4a 100644 --- a/spec/ruby/library/socket/socket/accept_loop_spec.rb +++ b/spec/ruby/library/socket/socket/accept_loop_spec.rb @@ -20,7 +20,7 @@ describe 'Socket.accept_loop' do # https://ci.appveyor.com/project/ruby/ruby/builds/20817932/job/dor2ipny7ru4erpa platform_is_not :windows do it 'blocks the caller' do - lambda { Socket.accept_loop([@server]) }.should block_caller + -> { Socket.accept_loop([@server]) }.should block_caller end end end @@ -53,7 +53,7 @@ describe 'Socket.accept_loop' do describe 'using separate Socket arguments' do describe 'without any available connections' do it 'blocks the caller' do - lambda { Socket.accept_loop(@server) }.should block_caller + -> { Socket.accept_loop(@server) }.should block_caller end end diff --git a/spec/ruby/library/socket/socket/accept_nonblock_spec.rb b/spec/ruby/library/socket/socket/accept_nonblock_spec.rb index cde9e6a4b6..011622988c 100644 --- a/spec/ruby/library/socket/socket/accept_nonblock_spec.rb +++ b/spec/ruby/library/socket/socket/accept_nonblock_spec.rb @@ -15,7 +15,7 @@ describe "Socket#accept_nonblock" do end it "raises IO::WaitReadable if the connection is not accepted yet" do - lambda { + -> { @socket.accept_nonblock }.should raise_error(IO::WaitReadable) { |e| platform_is_not :windows do @@ -45,7 +45,8 @@ describe 'Socket#accept_nonblock' do describe 'using an unbound socket' do it 'raises Errno::EINVAL' do - lambda { @server.accept_nonblock }.should raise_error(Errno::EINVAL) + -> { @server.accept_nonblock }.should raise_error(Errno::EINVAL) + -> { @server.accept_nonblock(exception: false) }.should raise_error(Errno::EINVAL) end end @@ -55,7 +56,8 @@ describe 'Socket#accept_nonblock' do end it 'raises Errno::EINVAL' do - lambda { @server.accept_nonblock }.should raise_error(Errno::EINVAL) + -> { @server.accept_nonblock }.should raise_error(Errno::EINVAL) + -> { @server.accept_nonblock(exception: false) }.should raise_error(Errno::EINVAL) end end @@ -63,7 +65,8 @@ describe 'Socket#accept_nonblock' do it 'raises IOError' do @server.close - lambda { @server.accept_nonblock }.should raise_error(IOError) + -> { @server.accept_nonblock }.should raise_error(IOError) + -> { @server.accept_nonblock(exception: false) }.should raise_error(IOError) end end @@ -75,7 +78,7 @@ describe 'Socket#accept_nonblock' do describe 'without a connected client' do it 'raises IO::WaitReadable' do - lambda { @server.accept_nonblock }.should raise_error(IO::WaitReadable) + -> { @server.accept_nonblock }.should raise_error(IO::WaitReadable) end end diff --git a/spec/ruby/library/socket/socket/accept_spec.rb b/spec/ruby/library/socket/socket/accept_spec.rb index a0778caa23..417f996c55 100644 --- a/spec/ruby/library/socket/socket/accept_spec.rb +++ b/spec/ruby/library/socket/socket/accept_spec.rb @@ -15,7 +15,7 @@ describe 'Socket#accept' do platform_is :linux do # hangs on other platforms describe 'using an unbound socket' do it 'raises Errno::EINVAL' do - lambda { @server.accept }.should raise_error(Errno::EINVAL) + -> { @server.accept }.should raise_error(Errno::EINVAL) end end @@ -25,7 +25,7 @@ describe 'Socket#accept' do end it 'raises Errno::EINVAL' do - lambda { @server.accept }.should raise_error(Errno::EINVAL) + -> { @server.accept }.should raise_error(Errno::EINVAL) end end end @@ -34,7 +34,7 @@ describe 'Socket#accept' do it 'raises IOError' do @server.close - lambda { @server.accept }.should raise_error(IOError) + -> { @server.accept }.should raise_error(IOError) end end @@ -56,7 +56,6 @@ describe 'Socket#accept' do client.connect(@server_addr) - thread.join(5) value = thread.value begin value.should be_an_instance_of(Array) diff --git a/spec/ruby/library/socket/socket/bind_spec.rb b/spec/ruby/library/socket/socket/bind_spec.rb index 3a7df6ed71..e76336eafa 100644 --- a/spec/ruby/library/socket/socket/bind_spec.rb +++ b/spec/ruby/library/socket/socket/bind_spec.rb @@ -13,7 +13,7 @@ describe "Socket#bind on SOCK_DGRAM socket" do end it "binds to a port" do - lambda { @sock.bind(@sockaddr) }.should_not raise_error + -> { @sock.bind(@sockaddr) }.should_not raise_error end it "returns 0 if successful" do @@ -23,19 +23,20 @@ describe "Socket#bind on SOCK_DGRAM socket" do it "raises Errno::EINVAL when already bound" do @sock.bind(@sockaddr) - lambda { @sock.bind(@sockaddr) }.should raise_error(Errno::EINVAL) + -> { @sock.bind(@sockaddr) }.should raise_error(Errno::EINVAL) end it "raises Errno::EADDRNOTAVAIL when the specified sockaddr is not available from the local machine" do sockaddr1 = Socket.pack_sockaddr_in(0, "4.3.2.1") - lambda { @sock.bind(sockaddr1) }.should raise_error(Errno::EADDRNOTAVAIL) + -> { @sock.bind(sockaddr1) }.should raise_error(Errno::EADDRNOTAVAIL) end platform_is_not :windows, :cygwin do as_user do + break if File.read('/proc/sys/net/ipv4/ip_unprivileged_port_start').to_i <= 1 rescue nil it "raises Errno::EACCES when the current user does not have permission to bind" do sockaddr1 = Socket.pack_sockaddr_in(1, "127.0.0.1") - lambda { @sock.bind(sockaddr1) }.should raise_error(Errno::EACCES) + -> { @sock.bind(sockaddr1) }.should raise_error(Errno::EACCES) end end end @@ -54,7 +55,7 @@ describe "Socket#bind on SOCK_STREAM socket" do end it "binds to a port" do - lambda { @sock.bind(@sockaddr) }.should_not raise_error + -> { @sock.bind(@sockaddr) }.should_not raise_error end it "returns 0 if successful" do @@ -64,19 +65,20 @@ describe "Socket#bind on SOCK_STREAM socket" do it "raises Errno::EINVAL when already bound" do @sock.bind(@sockaddr) - lambda { @sock.bind(@sockaddr) }.should raise_error(Errno::EINVAL) + -> { @sock.bind(@sockaddr) }.should raise_error(Errno::EINVAL) end it "raises Errno::EADDRNOTAVAIL when the specified sockaddr is not available from the local machine" do sockaddr1 = Socket.pack_sockaddr_in(0, "4.3.2.1") - lambda { @sock.bind(sockaddr1) }.should raise_error(Errno::EADDRNOTAVAIL) + -> { @sock.bind(sockaddr1) }.should raise_error(Errno::EADDRNOTAVAIL) end platform_is_not :windows, :cygwin do as_user do + break if File.read('/proc/sys/net/ipv4/ip_unprivileged_port_start').to_i <= 1 rescue nil it "raises Errno::EACCES when the current user does not have permission to bind" do sockaddr1 = Socket.pack_sockaddr_in(1, "127.0.0.1") - lambda { @sock.bind(sockaddr1) }.should raise_error(Errno::EACCES) + -> { @sock.bind(sockaddr1) }.should raise_error(Errno::EACCES) end end end @@ -101,24 +103,26 @@ describe 'Socket#bind' do it 'raises Errno::EINVAL when binding to an already bound port' do @socket.bind(@sockaddr) - lambda { @socket.bind(@sockaddr) }.should raise_error(Errno::EINVAL) + -> { @socket.bind(@sockaddr) }.should raise_error(Errno::EINVAL) end it 'raises Errno::EADDRNOTAVAIL when the specified sockaddr is not available' do ip = family == Socket::AF_INET ? '4.3.2.1' : '::2' sockaddr1 = Socket.sockaddr_in(0, ip) - lambda { @socket.bind(sockaddr1) }.should raise_error(Errno::EADDRNOTAVAIL) + -> { @socket.bind(sockaddr1) }.should raise_error(Errno::EADDRNOTAVAIL) end platform_is_not :windows do as_user do + break if File.read('/proc/sys/net/ipv4/ip_unprivileged_port_start').to_i <= 1 rescue nil + it 'raises Errno::EACCES when the user is not allowed to bind to the port' do sockaddr1 = Socket.pack_sockaddr_in(1, ip_address) - lambda { @socket.bind(sockaddr1) }.should raise_error(Errno::EACCES) + -> { @socket.bind(sockaddr1) }.should raise_error(Errno::EACCES) end - end + end end end diff --git a/spec/ruby/library/socket/socket/connect_nonblock_spec.rb b/spec/ruby/library/socket/socket/connect_nonblock_spec.rb index b5f416d96f..359b8719fb 100644 --- a/spec/ruby/library/socket/socket/connect_nonblock_spec.rb +++ b/spec/ruby/library/socket/socket/connect_nonblock_spec.rb @@ -16,50 +16,48 @@ describe "Socket#connect_nonblock" do @thread.join if @thread end - platform_is_not :solaris do - it "connects the socket to the remote side" do - port = nil - accept = false - @thread = Thread.new do - server = TCPServer.new(@hostname, 0) - port = server.addr[1] - Thread.pass until accept - conn = server.accept - conn << "hello!" - conn.close - server.close - end - - Thread.pass until port + it "connects the socket to the remote side" do + port = nil + accept = false + @thread = Thread.new do + server = TCPServer.new(@hostname, 0) + port = server.addr[1] + Thread.pass until accept + conn = server.accept + conn << "hello!" + conn.close + server.close + end - addr = Socket.sockaddr_in(port, @hostname) - begin - @socket.connect_nonblock(addr) - rescue Errno::EINPROGRESS - end + Thread.pass until port - accept = true - IO.select nil, [@socket] + addr = Socket.sockaddr_in(port, @hostname) + begin + @socket.connect_nonblock(addr) + rescue Errno::EINPROGRESS + end - begin - @socket.connect_nonblock(addr) - rescue Errno::EISCONN - # Not all OS's use this errno, so we trap and ignore it - end + accept = true + IO.select nil, [@socket] - @socket.read(6).should == "hello!" + begin + @socket.connect_nonblock(addr) + rescue Errno::EISCONN + # Not all OS's use this errno, so we trap and ignore it end + + @socket.read(6).should == "hello!" end - platform_is_not :freebsd, :solaris, :aix do + platform_is_not :freebsd, :aix do it "raises Errno::EINPROGRESS when the connect would block" do - lambda do + -> do @socket.connect_nonblock(@addr) end.should raise_error(Errno::EINPROGRESS) end it "raises Errno::EINPROGRESS with IO::WaitWritable mixed in when the connect would block" do - lambda do + -> do @socket.connect_nonblock(@addr) end.should raise_error(IO::WaitWritable) end @@ -95,7 +93,7 @@ describe 'Socket#connect_nonblock' do end it 'raises TypeError when passed an Integer' do - lambda { @client.connect_nonblock(666) }.should raise_error(TypeError) + -> { @client.connect_nonblock(666) }.should raise_error(TypeError) end end @@ -116,7 +114,7 @@ describe 'Socket#connect_nonblock' do @server.listen(1) @client.connect(@server.connect_address).should == 0 - lambda { + -> { @client.connect_nonblock(@server.connect_address) # A second call needed if non-blocking sockets become default @@ -135,11 +133,11 @@ describe 'Socket#connect_nonblock' do end end - platform_is_not :freebsd, :solaris do + platform_is_not :freebsd do it 'raises IO:EINPROGRESSWaitWritable when the connection would block' do @server.bind(@sockaddr) - lambda { + -> { @client.connect_nonblock(@server.connect_address) }.should raise_error(IO::EINPROGRESSWaitWritable) end diff --git a/spec/ruby/library/socket/socket/connect_spec.rb b/spec/ruby/library/socket/socket/connect_spec.rb index df5cc5bf34..130379ce2b 100644 --- a/spec/ruby/library/socket/socket/connect_spec.rb +++ b/spec/ruby/library/socket/socket/connect_spec.rb @@ -32,7 +32,7 @@ describe 'Socket#connect' do @client.connect(@server.getsockname).should == 0 - lambda { + -> { @client.connect(@server.getsockname) # A second call needed if non-blocking sockets become default @@ -53,4 +53,26 @@ describe 'Socket#connect' do end end end + + ruby_version_is "3.4" do + it "fails with timeout" do + # TEST-NET-1 IP address are reserved for documentation and example purposes. + address = Socket.pack_sockaddr_in(1, "192.0.2.1") + + client = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM) + client.timeout = 0 + + -> { + begin + client.connect(address) + rescue Errno::ECONNREFUSED + skip "Outgoing packets may be filtered" + rescue Errno::ENETUNREACH + skip "Off line" + end + }.should raise_error(IO::TimeoutError) + ensure + client.close + end + end end diff --git a/spec/ruby/library/socket/socket/getaddrinfo_spec.rb b/spec/ruby/library/socket/socket/getaddrinfo_spec.rb index e0eff3cef4..6576af52ee 100644 --- a/spec/ruby/library/socket/socket/getaddrinfo_spec.rb +++ b/spec/ruby/library/socket/socket/getaddrinfo_spec.rb @@ -11,7 +11,7 @@ describe "Socket.getaddrinfo" do BasicSocket.do_not_reverse_lookup = @do_not_reverse_lookup end - platform_is_not :solaris, :windows do + platform_is_not :windows do it "gets the address information" do expected = [] # The check for AP_INET6's class is needed because ipaddr.rb adds @@ -106,6 +106,24 @@ describe "Socket.getaddrinfo" do ] res.each { |a| expected.should include(a) } end + + ruby_version_is ""..."3.3" do + it "raises SocketError when fails to resolve address" do + -> { + Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX") + }.should raise_error(SocketError) + end + end + + ruby_version_is "3.3" do + it "raises ResolutionError when fails to resolve address" do + -> { + Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX") + }.should raise_error(Socket::ResolutionError) { |e| + [Socket::EAI_FAMILY, Socket::EAI_FAIL].should.include?(e.error_code) + } + end + end end end diff --git a/spec/ruby/library/socket/socket/gethostbyaddr_spec.rb b/spec/ruby/library/socket/socket/gethostbyaddr_spec.rb index 639a318132..5d936046f5 100644 --- a/spec/ruby/library/socket/socket/gethostbyaddr_spec.rb +++ b/spec/ruby/library/socket/socket/gethostbyaddr_spec.rb @@ -10,19 +10,16 @@ describe 'Socket.gethostbyaddr' do describe 'without an explicit address family' do it 'returns an Array' do - Socket.gethostbyaddr(@addr).should be_an_instance_of(Array) + suppress_warning { Socket.gethostbyaddr(@addr) }.should be_an_instance_of(Array) end describe 'the returned Array' do before do - @array = Socket.gethostbyaddr(@addr) + @array = suppress_warning { Socket.gethostbyaddr(@addr) } end - # RubyCI Solaris 11x defines 127.0.0.1 as unstable11x - platform_is_not :"solaris2.11" do - it 'includes the hostname as the first value' do - @array[0].should == SocketSpecs.hostname_reverse_lookup - end + it 'includes the hostname as the first value' do + @array[0].should == SocketSpecs.hostname_reverse_lookup end it 'includes the aliases as the 2nd value' do @@ -49,15 +46,15 @@ describe 'Socket.gethostbyaddr' do describe 'with an explicit address family' do it 'returns an Array when using an Integer as the address family' do - Socket.gethostbyaddr(@addr, Socket::AF_INET).should be_an_instance_of(Array) + suppress_warning { Socket.gethostbyaddr(@addr, Socket::AF_INET) }.should be_an_instance_of(Array) end it 'returns an Array when using a Symbol as the address family' do - Socket.gethostbyaddr(@addr, :INET).should be_an_instance_of(Array) + suppress_warning { Socket.gethostbyaddr(@addr, :INET) }.should be_an_instance_of(Array) end it 'raises SocketError when the address is not supported by the family' do - lambda { Socket.gethostbyaddr(@addr, :INET6) }.should raise_error(SocketError) + -> { suppress_warning { Socket.gethostbyaddr(@addr, :INET6) } }.should raise_error(SocketError) end end end @@ -70,12 +67,12 @@ describe 'Socket.gethostbyaddr' do describe 'without an explicit address family' do it 'returns an Array' do - Socket.gethostbyaddr(@addr).should be_an_instance_of(Array) + suppress_warning { Socket.gethostbyaddr(@addr) }.should be_an_instance_of(Array) end describe 'the returned Array' do before do - @array = Socket.gethostbyaddr(@addr) + @array = suppress_warning { Socket.gethostbyaddr(@addr) } end it 'includes the hostname as the first value' do @@ -106,16 +103,16 @@ describe 'Socket.gethostbyaddr' do describe 'with an explicit address family' do it 'returns an Array when using an Integer as the address family' do - Socket.gethostbyaddr(@addr, Socket::AF_INET6).should be_an_instance_of(Array) + suppress_warning { Socket.gethostbyaddr(@addr, Socket::AF_INET6) }.should be_an_instance_of(Array) end it 'returns an Array when using a Symbol as the address family' do - Socket.gethostbyaddr(@addr, :INET6).should be_an_instance_of(Array) + suppress_warning { Socket.gethostbyaddr(@addr, :INET6) }.should be_an_instance_of(Array) end - platform_is_not :windows do + platform_is_not :windows, :wsl do it 'raises SocketError when the address is not supported by the family' do - lambda { Socket.gethostbyaddr(@addr, :INET) }.should raise_error(SocketError) + -> { suppress_warning { Socket.gethostbyaddr(@addr, :INET) } }.should raise_error(SocketError) end end end diff --git a/spec/ruby/library/socket/socket/gethostbyname_spec.rb b/spec/ruby/library/socket/socket/gethostbyname_spec.rb index 2696f44566..618ef85387 100644 --- a/spec/ruby/library/socket/socket/gethostbyname_spec.rb +++ b/spec/ruby/library/socket/socket/gethostbyname_spec.rb @@ -1,27 +1,27 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../spec_helper' require_relative '../fixtures/classes' describe "Socket.gethostbyname" do it "returns broadcast address info for '<broadcast>'" do - addr = Socket.gethostbyname('<broadcast>'); + addr = suppress_warning { Socket.gethostbyname('<broadcast>') } addr.should == ["255.255.255.255", [], 2, "\xFF\xFF\xFF\xFF"] end it "returns broadcast address info for '<any>'" do - addr = Socket.gethostbyname('<any>'); + addr = suppress_warning { Socket.gethostbyname('<any>') } addr.should == ["0.0.0.0", [], 2, "\x00\x00\x00\x00"] end end describe 'Socket.gethostbyname' do it 'returns an Array' do - Socket.gethostbyname('127.0.0.1').should be_an_instance_of(Array) + suppress_warning { Socket.gethostbyname('127.0.0.1') }.should be_an_instance_of(Array) end describe 'the returned Array' do before do - @array = Socket.gethostbyname('127.0.0.1') + @array = suppress_warning { Socket.gethostbyname('127.0.0.1') } end it 'includes the hostname as the first value' do @@ -54,7 +54,7 @@ describe 'Socket.gethostbyname' do describe 'using <broadcast> as the input address' do describe 'the returned Array' do before do - @addr = Socket.gethostbyname('<broadcast>') + @addr = suppress_warning { Socket.gethostbyname('<broadcast>') } end it 'includes the broadcast address as the first value' do @@ -74,7 +74,7 @@ describe 'Socket.gethostbyname' do describe 'using <any> as the input address' do describe 'the returned Array' do before do - @addr = Socket.gethostbyname('<any>') + @addr = suppress_warning { Socket.gethostbyname('<any>') } end it 'includes the wildcard address as the first value' do @@ -94,7 +94,7 @@ describe 'Socket.gethostbyname' do describe 'using an IPv4 address' do describe 'the returned Array' do before do - @addr = Socket.gethostbyname('127.0.0.1') + @addr = suppress_warning { Socket.gethostbyname('127.0.0.1') } end it 'includes the IP address as the first value' do @@ -115,7 +115,7 @@ describe 'Socket.gethostbyname' do describe 'using an IPv6 address' do describe 'the returned Array' do before do - @addr = Socket.gethostbyname('::1') + @addr = suppress_warning { Socket.gethostbyname('::1') } end it 'includes the IP address as the first value' do diff --git a/spec/ruby/library/socket/socket/gethostname_spec.rb b/spec/ruby/library/socket/socket/gethostname_spec.rb index 4b79747b27..dfca7cf5cd 100644 --- a/spec/ruby/library/socket/socket/gethostname_spec.rb +++ b/spec/ruby/library/socket/socket/gethostname_spec.rb @@ -2,7 +2,17 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' describe "Socket.gethostname" do + def system_hostname + if platform_is_not :windows + # `uname -n` is the most portable way to get the hostname, as it is a POSIX standard: + `uname -n`.strip + else + # Windows does not have uname, so we use hostname instead: + `hostname`.strip + end + end + it "returns the host name" do - Socket.gethostname.should == `hostname`.strip + Socket.gethostname.should == system_hostname end end diff --git a/spec/ruby/library/socket/socket/getifaddrs_spec.rb b/spec/ruby/library/socket/socket/getifaddrs_spec.rb index 7df542abe6..839854ea27 100644 --- a/spec/ruby/library/socket/socket/getifaddrs_spec.rb +++ b/spec/ruby/library/socket/socket/getifaddrs_spec.rb @@ -1,6 +1,6 @@ require_relative '../spec_helper' -platform_is_not :aix, :"solaris2.10" do +platform_is_not :aix do describe 'Socket.getifaddrs' do before do @ifaddrs = Socket.getifaddrs diff --git a/spec/ruby/library/socket/socket/getnameinfo_spec.rb b/spec/ruby/library/socket/socket/getnameinfo_spec.rb index fbbbcb53c5..af4a10c9c2 100644 --- a/spec/ruby/library/socket/socket/getnameinfo_spec.rb +++ b/spec/ruby/library/socket/socket/getnameinfo_spec.rb @@ -60,6 +60,24 @@ describe "Socket.getnameinfo" do name_info = Socket.getnameinfo ["AF_INET", 9, 'foo', '127.0.0.1'] name_info[1].should == 'discard' end + + ruby_version_is ""..."3.3" do + it "raises SocketError when fails to resolve address" do + -> { + Socket.getnameinfo(["AF_UNIX", 80, "0.0.0.0"]) + }.should raise_error(SocketError) + end + end + + ruby_version_is "3.3" do + it "raises ResolutionError when fails to resolve address" do + -> { + Socket.getnameinfo(["AF_UNIX", 80, "0.0.0.0"]) + }.should raise_error(Socket::ResolutionError) { |e| + [Socket::EAI_FAMILY, Socket::EAI_FAIL].should.include?(e.error_code) + } + end + end end describe 'Socket.getnameinfo' do @@ -69,8 +87,8 @@ describe 'Socket.getnameinfo' do end it 'raises SocketError or TypeError when using an invalid String' do - lambda { Socket.getnameinfo('cats') }.should raise_error(Exception) { |e| - [SocketError, TypeError].should include(e.class) + -> { Socket.getnameinfo('cats') }.should raise_error(Exception) { |e| + (e.is_a?(SocketError) || e.is_a?(TypeError)).should == true } end @@ -102,7 +120,7 @@ describe 'Socket.getnameinfo' do end it 'raises ArgumentError when using an invalid Array' do - lambda { Socket.getnameinfo([family_name]) }.should raise_error(ArgumentError) + -> { Socket.getnameinfo([family_name]) }.should raise_error(ArgumentError) end platform_is_not :windows do diff --git a/spec/ruby/library/socket/socket/getservbyname_spec.rb b/spec/ruby/library/socket/socket/getservbyname_spec.rb index 9479b26228..d361e619f2 100644 --- a/spec/ruby/library/socket/socket/getservbyname_spec.rb +++ b/spec/ruby/library/socket/socket/getservbyname_spec.rb @@ -27,6 +27,6 @@ describe "Socket#getservbyname" do end it "raises a SocketError when the service or port is invalid" do - lambda { Socket.getservbyname('invalid') }.should raise_error(SocketError) + -> { Socket.getservbyname('invalid') }.should raise_error(SocketError) end end diff --git a/spec/ruby/library/socket/socket/getservbyport_spec.rb b/spec/ruby/library/socket/socket/getservbyport_spec.rb index 9be2ac527e..563c592b54 100644 --- a/spec/ruby/library/socket/socket/getservbyport_spec.rb +++ b/spec/ruby/library/socket/socket/getservbyport_spec.rb @@ -18,6 +18,6 @@ describe 'Socket.getservbyport' do end it 'raises SocketError for an unknown port number' do - lambda { Socket.getservbyport(0) }.should raise_error(SocketError) + -> { Socket.getservbyport(0) }.should raise_error(SocketError) end end diff --git a/spec/ruby/library/socket/socket/initialize_spec.rb b/spec/ruby/library/socket/socket/initialize_spec.rb index 2343c6e289..f8337bcaa5 100644 --- a/spec/ruby/library/socket/socket/initialize_spec.rb +++ b/spec/ruby/library/socket/socket/initialize_spec.rb @@ -53,7 +53,7 @@ describe 'Socket#initialize' do family.stub!(:to_str).and_return(Socket::AF_INET) type.stub!(:to_str).and_return(Socket::SOCK_STREAM) - lambda { Socket.new(family, type) }.should raise_error(TypeError) + -> { Socket.new(family, type) }.should raise_error(TypeError) end end @@ -65,7 +65,7 @@ describe 'Socket#initialize' do end it 'raises TypeError when using a Symbol' do - lambda { Socket.new(:INET, :STREAM, :TCP) }.should raise_error(TypeError) + -> { Socket.new(:INET, :STREAM, :TCP) }.should raise_error(TypeError) end end diff --git a/spec/ruby/library/socket/socket/listen_spec.rb b/spec/ruby/library/socket/socket/listen_spec.rb index d0f9c70c4e..4d2aedab19 100644 --- a/spec/ruby/library/socket/socket/listen_spec.rb +++ b/spec/ruby/library/socket/socket/listen_spec.rb @@ -34,8 +34,10 @@ describe 'Socket#listen' do @server.close end - it 'raises Errno::EOPNOTSUPP' do - lambda { @server.listen(1) }.should raise_error(Errno::EOPNOTSUPP) + it 'raises Errno::EOPNOTSUPP or Errno::EACCES' do + -> { @server.listen(1) }.should raise_error { |e| + [Errno::EOPNOTSUPP, Errno::EACCES].should.include?(e.class) + } end end @@ -57,7 +59,7 @@ describe 'Socket#listen' do end it "raises when the given argument can't be coerced to an Integer" do - lambda { @server.listen('cats') }.should raise_error(TypeError) + -> { @server.listen('cats') }.should raise_error(TypeError) end end end diff --git a/spec/ruby/library/socket/socket/new_spec.rb b/spec/ruby/library/socket/socket/new_spec.rb deleted file mode 100644 index b2ec607f6a..0000000000 --- a/spec/ruby/library/socket/socket/new_spec.rb +++ /dev/null @@ -1,2 +0,0 @@ -require_relative '../spec_helper' -require_relative '../fixtures/classes' diff --git a/spec/ruby/library/socket/socket/pack_sockaddr_in_spec.rb b/spec/ruby/library/socket/socket/pack_sockaddr_in_spec.rb index 63d4724453..ef2a2d4ba9 100644 --- a/spec/ruby/library/socket/socket/pack_sockaddr_in_spec.rb +++ b/spec/ruby/library/socket/socket/pack_sockaddr_in_spec.rb @@ -2,6 +2,6 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' require_relative '../shared/pack_sockaddr' -describe "Socket#pack_sockaddr_in" do +describe "Socket.pack_sockaddr_in" do it_behaves_like :socket_pack_sockaddr_in, :pack_sockaddr_in end diff --git a/spec/ruby/library/socket/socket/pair_spec.rb b/spec/ruby/library/socket/socket/pair_spec.rb index 292eacd38d..8dd470a95e 100644 --- a/spec/ruby/library/socket/socket/pair_spec.rb +++ b/spec/ruby/library/socket/socket/pair_spec.rb @@ -2,6 +2,6 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' require_relative '../shared/socketpair' -describe "Socket#pair" do +describe "Socket.pair" do it_behaves_like :socket_socketpair, :pair end diff --git a/spec/ruby/library/socket/socket/recvfrom_nonblock_spec.rb b/spec/ruby/library/socket/socket/recvfrom_nonblock_spec.rb index 5f717d4b54..01b42bcc52 100644 --- a/spec/ruby/library/socket/socket/recvfrom_nonblock_spec.rb +++ b/spec/ruby/library/socket/socket/recvfrom_nonblock_spec.rb @@ -16,7 +16,7 @@ describe 'Socket#recvfrom_nonblock' do platform_is_not :windows do describe 'using an unbound socket' do it 'raises IO::WaitReadable' do - lambda { @server.recvfrom_nonblock(1) }.should raise_error(IO::WaitReadable) + -> { @server.recvfrom_nonblock(1) }.should raise_error(IO::WaitReadable) end end end @@ -29,7 +29,7 @@ describe 'Socket#recvfrom_nonblock' do describe 'without any data available' do it 'raises IO::WaitReadable' do - lambda { @server.recvfrom_nonblock(1) }.should raise_error(IO::WaitReadable) + -> { @server.recvfrom_nonblock(1) }.should raise_error(IO::WaitReadable) end it 'returns :wait_readable with exception: false' do @@ -52,6 +52,27 @@ describe 'Socket#recvfrom_nonblock' do end end + it "allows an output buffer as third argument" do + @client.write('hello') + + IO.select([@server]) + buffer = +'' + message, = @server.recvfrom_nonblock(5, 0, buffer) + + message.should.equal?(buffer) + buffer.should == 'hello' + end + + it "preserves the encoding of the given buffer" do + @client.write('hello') + + IO.select([@server]) + buffer = ''.encode(Encoding::ISO_8859_1) + @server.recvfrom_nonblock(5, 0, buffer) + + buffer.encoding.should == Encoding::ISO_8859_1 + end + describe 'the returned data' do it 'is the same as the sent data' do 5.times do @@ -116,3 +137,83 @@ describe 'Socket#recvfrom_nonblock' do end end end + +describe 'Socket#recvfrom_nonblock' do + context "when recvfrom(2) returns 0 (if no messages are available to be received and the peer has performed an orderly shutdown)" do + describe "stream socket" do + before :each do + @server = Socket.new Socket::AF_INET, :STREAM, 0 + @sockaddr = Socket.sockaddr_in(0, "127.0.0.1") + @server.bind(@sockaddr) + @server.listen(1) + + server_ip = @server.local_address.ip_port + @server_addr = Socket.sockaddr_in(server_ip, "127.0.0.1") + + @client = Socket.new(Socket::AF_INET, :STREAM, 0) + end + + after :each do + @server.close unless @server.closed? + @client.close unless @client.closed? + end + + ruby_version_is ""..."3.3" do + it "returns an empty String as received data on a closed stream socket" do + ready = false + + t = Thread.new do + client, _ = @server.accept + + Thread.pass while !ready + begin + client.recvfrom_nonblock(10) + rescue IO::EAGAINWaitReadable + retry + end + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + @client.connect(@server_addr) + @client.close + ready = true + + t.value.should.is_a? Array + t.value[0].should == "" + end + end + + ruby_version_is "3.3" do + it "returns nil on a closed stream socket" do + ready = false + + t = Thread.new do + client, _ = @server.accept + + Thread.pass while !ready + begin + client.recvfrom_nonblock(10) + rescue IO::EAGAINWaitReadable + retry + end + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + @client.connect(@server_addr) + @client.close + ready = true + + t.value.should be_nil + end + end + end + end +end diff --git a/spec/ruby/library/socket/socket/recvfrom_spec.rb b/spec/ruby/library/socket/socket/recvfrom_spec.rb index 7f0714511c..6ba39ffcaf 100644 --- a/spec/ruby/library/socket/socket/recvfrom_spec.rb +++ b/spec/ruby/library/socket/socket/recvfrom_spec.rb @@ -15,7 +15,7 @@ describe 'Socket#recvfrom' do describe 'using an unbound socket' do it 'blocks the caller' do - lambda { @server.recvfrom(1) }.should block_caller + -> { @server.recvfrom(1) }.should block_caller end end @@ -27,7 +27,7 @@ describe 'Socket#recvfrom' do describe 'without any data available' do it 'blocks the caller' do - lambda { @server.recvfrom(1) }.should block_caller + -> { @server.recvfrom(1) }.should block_caller end end @@ -90,3 +90,90 @@ describe 'Socket#recvfrom' do end end end + +describe 'Socket#recvfrom' do + context "when recvfrom(2) returns 0 (if no messages are available to be received and the peer has performed an orderly shutdown)" do + describe "stream socket" do + before :each do + @server = Socket.new Socket::AF_INET, :STREAM, 0 + sockaddr = Socket.sockaddr_in(0, "127.0.0.1") + @server.bind(sockaddr) + @server.listen(1) + + server_ip = @server.local_address.ip_port + @server_addr = Socket.sockaddr_in(server_ip, "127.0.0.1") + + @client = Socket.new(Socket::AF_INET, :STREAM, 0) + end + + after :each do + @server.close unless @server.closed? + @client.close unless @client.closed? + end + + ruby_version_is ""..."3.3" do + it "returns an empty String as received data on a closed stream socket" do + t = Thread.new do + client, _ = @server.accept + client.recvfrom(10) + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + @client.connect(@server_addr) + @client.close + + t.value.should.is_a? Array + t.value[0].should == "" + end + end + + ruby_version_is "3.3" do + it "returns nil on a closed stream socket" do + t = Thread.new do + client, _ = @server.accept + client.recvfrom(10) + ensure + client.close if client + end + + Thread.pass while t.status and t.status != "sleep" + t.status.should_not be_nil + + @client.connect(@server_addr) + @client.close + + t.value.should be_nil + end + end + end + + describe "datagram socket" do + SocketSpecs.each_ip_protocol do |family, ip_address| + before :each do + @server = Socket.new(family, :DGRAM) + @client = Socket.new(family, :DGRAM) + end + + after :each do + @server.close unless @server.closed? + @client.close unless @client.closed? + end + + it "returns an empty String as received data" do + @server.bind(Socket.sockaddr_in(0, ip_address)) + @client.connect(@server.getsockname) + + @client.send('', 0) + message = @server.recvfrom(1) + + message.should.is_a? Array + message[0].should == "" + end + end + end + end +end diff --git a/spec/ruby/library/socket/socket/socketpair_spec.rb b/spec/ruby/library/socket/socket/socketpair_spec.rb index 5b8311124e..551c376d49 100644 --- a/spec/ruby/library/socket/socket/socketpair_spec.rb +++ b/spec/ruby/library/socket/socket/socketpair_spec.rb @@ -2,6 +2,6 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' require_relative '../shared/socketpair' -describe "Socket#socketpair" do +describe "Socket.socketpair" do it_behaves_like :socket_socketpair, :socketpair end diff --git a/spec/ruby/library/socket/socket/sysaccept_spec.rb b/spec/ruby/library/socket/socket/sysaccept_spec.rb index f039096707..92ac21124e 100644 --- a/spec/ruby/library/socket/socket/sysaccept_spec.rb +++ b/spec/ruby/library/socket/socket/sysaccept_spec.rb @@ -15,7 +15,7 @@ describe 'Socket#sysaccept' do platform_is :linux do # hangs on other platforms describe 'using an unbound socket' do it 'raises Errno::EINVAL' do - lambda { @server.sysaccept }.should raise_error(Errno::EINVAL) + -> { @server.sysaccept }.should raise_error(Errno::EINVAL) end end @@ -25,7 +25,7 @@ describe 'Socket#sysaccept' do end it 'raises Errno::EINVAL' do - lambda { @server.sysaccept }.should raise_error(Errno::EINVAL) + -> { @server.sysaccept }.should raise_error(Errno::EINVAL) end end end @@ -59,8 +59,6 @@ describe 'Socket#sysaccept' do @client.connect(@server_addr) - thread.join(5) - thread.value.should be_an_instance_of(Array) end end diff --git a/spec/ruby/library/socket/socket/tcp_server_loop_spec.rb b/spec/ruby/library/socket/socket/tcp_server_loop_spec.rb index 05ecad67d1..a46c6df5c6 100644 --- a/spec/ruby/library/socket/socket/tcp_server_loop_spec.rb +++ b/spec/ruby/library/socket/socket/tcp_server_loop_spec.rb @@ -4,7 +4,7 @@ require_relative '../fixtures/classes' describe 'Socket.tcp_server_loop' do describe 'when no connections are available' do it 'blocks the caller' do - lambda { Socket.tcp_server_loop('127.0.0.1', 0) }.should block_caller + -> { Socket.tcp_server_loop('127.0.0.1', 0) }.should block_caller end end @@ -45,7 +45,7 @@ describe 'Socket.tcp_server_loop' do # At this point the connection has been set up but the thread may not yet # have returned, thus we'll need to wait a little longer for it to # complete. - thread.join(2) + thread.join @sock.should be_an_instance_of(Socket) addr.should be_an_instance_of(Addrinfo) diff --git a/spec/ruby/library/socket/socket/tcp_server_sockets_spec.rb b/spec/ruby/library/socket/socket/tcp_server_sockets_spec.rb index 10c030a8ce..bd496d3015 100644 --- a/spec/ruby/library/socket/socket/tcp_server_sockets_spec.rb +++ b/spec/ruby/library/socket/socket/tcp_server_sockets_spec.rb @@ -32,7 +32,7 @@ describe 'Socket.tcp_server_sockets' do Socket.tcp_server_sockets(0) { |socks| sockets = socks } sockets.each do |socket| - socket.closed?.should == true + socket.should.closed? end end end diff --git a/spec/ruby/library/socket/socket/tcp_spec.rb b/spec/ruby/library/socket/socket/tcp_spec.rb index 29f166ffc5..faf020b1ea 100644 --- a/spec/ruby/library/socket/socket/tcp_spec.rb +++ b/spec/ruby/library/socket/socket/tcp_spec.rb @@ -36,7 +36,7 @@ describe 'Socket.tcp' do @socket = socket end - @socket.closed?.should == true + @socket.should.closed? end it 'binds to a local address and port when specified' do @@ -49,7 +49,7 @@ describe 'Socket.tcp' do end it 'raises ArgumentError when 6 arguments are provided' do - lambda { + -> { Socket.tcp(@host, @port, @host, 0, {:connect_timeout => 1}, 10) }.should raise_error(ArgumentError) end diff --git a/spec/ruby/library/socket/socket/udp_server_loop_on_spec.rb b/spec/ruby/library/socket/socket/udp_server_loop_on_spec.rb index 1cb82d72be..cb8c5c5587 100644 --- a/spec/ruby/library/socket/socket/udp_server_loop_on_spec.rb +++ b/spec/ruby/library/socket/socket/udp_server_loop_on_spec.rb @@ -13,7 +13,7 @@ describe 'Socket.udp_server_loop_on' do describe 'when no connections are available' do it 'blocks the caller' do - lambda { Socket.udp_server_loop_on([@server]) }.should block_caller + -> { Socket.udp_server_loop_on([@server]) }.should block_caller end end diff --git a/spec/ruby/library/socket/socket/udp_server_loop_spec.rb b/spec/ruby/library/socket/socket/udp_server_loop_spec.rb index 168c17545f..cd22ea56cf 100644 --- a/spec/ruby/library/socket/socket/udp_server_loop_spec.rb +++ b/spec/ruby/library/socket/socket/udp_server_loop_spec.rb @@ -4,7 +4,7 @@ require_relative '../fixtures/classes' describe 'Socket.udp_server_loop' do describe 'when no connections are available' do it 'blocks the caller' do - lambda { Socket.udp_server_loop('127.0.0.1', 0) }.should block_caller + -> { Socket.udp_server_loop('127.0.0.1', 0) }.should block_caller end end @@ -21,7 +21,7 @@ describe 'Socket.udp_server_loop' do it 'yields the message and a Socket::UDPSource' do msg, src = nil - Thread.new do + thread = Thread.new do SocketSpecs::ServerLoopPortFinder.udp_server_loop('127.0.0.1', 0) do |message, source| msg = message src = source @@ -50,6 +50,8 @@ describe 'Socket.udp_server_loop' do end end + thread.join + msg.should == 'hello' src.should be_an_instance_of(Socket::UDPSource) end diff --git a/spec/ruby/library/socket/socket/udp_server_sockets_spec.rb b/spec/ruby/library/socket/socket/udp_server_sockets_spec.rb index 3aeb472dda..f8be672612 100644 --- a/spec/ruby/library/socket/socket/udp_server_sockets_spec.rb +++ b/spec/ruby/library/socket/socket/udp_server_sockets_spec.rb @@ -32,7 +32,7 @@ describe 'Socket.udp_server_sockets' do Socket.udp_server_sockets(0) { |socks| sockets = socks } sockets.each do |socket| - socket.closed?.should == true + socket.should.closed? end end end diff --git a/spec/ruby/library/socket/socket/unix_server_loop_spec.rb b/spec/ruby/library/socket/socket/unix_server_loop_spec.rb index a2d2ad9fdb..6192bc8bf6 100644 --- a/spec/ruby/library/socket/socket/unix_server_loop_spec.rb +++ b/spec/ruby/library/socket/socket/unix_server_loop_spec.rb @@ -1,58 +1,56 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -with_feature :unix_socket do - describe 'Socket.unix_server_loop' do - before do - @path = SocketSpecs.socket_path - end +describe 'Socket.unix_server_loop' do + before do + @path = SocketSpecs.socket_path + end - after do - rm_r(@path) if File.file?(@path) - end + after do + rm_r(@path) if File.file?(@path) + end - describe 'when no connections are available' do - it 'blocks the caller' do - lambda { Socket.unix_server_loop(@path) }.should block_caller - end + describe 'when no connections are available' do + it 'blocks the caller' do + -> { Socket.unix_server_loop(@path) }.should block_caller end + end - describe 'when a connection is available' do - before do - @client = nil - end + describe 'when a connection is available' do + before do + @client = nil + end - after do - @sock.close if @sock - @client.close if @client - end + after do + @sock.close if @sock + @client.close if @client + end - it 'yields a Socket and an Addrinfo' do - @sock, addr = nil + it 'yields a Socket and an Addrinfo' do + @sock, addr = nil - thread = Thread.new do - Socket.unix_server_loop(@path) do |socket, addrinfo| - @sock = socket - addr = addrinfo + thread = Thread.new do + Socket.unix_server_loop(@path) do |socket, addrinfo| + @sock = socket + addr = addrinfo - break - end + break end + end - SocketSpecs.loop_with_timeout do - begin - @client = Socket.unix(@path) - rescue SystemCallError - sleep 0.01 - :retry - end + SocketSpecs.loop_with_timeout do + begin + @client = Socket.unix(@path) + rescue SystemCallError + sleep 0.01 + :retry end + end - thread.join(2) + thread.join - @sock.should be_an_instance_of(Socket) - addr.should be_an_instance_of(Addrinfo) - end + @sock.should be_an_instance_of(Socket) + addr.should be_an_instance_of(Addrinfo) end end end diff --git a/spec/ruby/library/socket/socket/unix_server_socket_spec.rb b/spec/ruby/library/socket/socket/unix_server_socket_spec.rb index fc357740fa..34c3b96d07 100644 --- a/spec/ruby/library/socket/socket/unix_server_socket_spec.rb +++ b/spec/ruby/library/socket/socket/unix_server_socket_spec.rb @@ -1,48 +1,46 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -with_feature :unix_socket do - describe 'Socket.unix_server_socket' do +describe 'Socket.unix_server_socket' do + before do + @path = SocketSpecs.socket_path + end + + after do + rm_r(@path) + end + + describe 'when no block is given' do before do - @path = SocketSpecs.socket_path + @socket = nil end after do - rm_r(@path) + @socket.close end - describe 'when no block is given' do - before do - @socket = nil - end - - after do - @socket.close - end + it 'returns a Socket' do + @socket = Socket.unix_server_socket(@path) - it 'returns a Socket' do - @socket = Socket.unix_server_socket(@path) - - @socket.should be_an_instance_of(Socket) - end + @socket.should be_an_instance_of(Socket) end + end - describe 'when a block is given' do - it 'yields a Socket' do - Socket.unix_server_socket(@path) do |sock| - sock.should be_an_instance_of(Socket) - end + describe 'when a block is given' do + it 'yields a Socket' do + Socket.unix_server_socket(@path) do |sock| + sock.should be_an_instance_of(Socket) end + end - it 'closes the Socket when the block returns' do - socket = nil - - Socket.unix_server_socket(@path) do |sock| - socket = sock - end + it 'closes the Socket when the block returns' do + socket = nil - socket.should be_an_instance_of(Socket) + Socket.unix_server_socket(@path) do |sock| + socket = sock end + + socket.should be_an_instance_of(Socket) end end end diff --git a/spec/ruby/library/socket/socket/unix_spec.rb b/spec/ruby/library/socket/socket/unix_spec.rb index add54a097d..2a5d77f96f 100644 --- a/spec/ruby/library/socket/socket/unix_spec.rb +++ b/spec/ruby/library/socket/socket/unix_spec.rb @@ -1,45 +1,43 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -with_feature :unix_socket do - describe 'Socket.unix' do - before do - @path = SocketSpecs.socket_path - @server = UNIXServer.new(@path) - @socket = nil - end +describe 'Socket.unix' do + before do + @path = SocketSpecs.socket_path + @server = UNIXServer.new(@path) + @socket = nil + end - after do - @server.close - @socket.close if @socket + after do + @server.close + @socket.close if @socket - rm_r(@path) - end + rm_r(@path) + end - describe 'when no block is given' do - it 'returns a Socket' do - @socket = Socket.unix(@path) + describe 'when no block is given' do + it 'returns a Socket' do + @socket = Socket.unix(@path) - @socket.should be_an_instance_of(Socket) - end + @socket.should be_an_instance_of(Socket) end + end - describe 'when a block is given' do - it 'yields a Socket' do - Socket.unix(@path) do |sock| - sock.should be_an_instance_of(Socket) - end + describe 'when a block is given' do + it 'yields a Socket' do + Socket.unix(@path) do |sock| + sock.should be_an_instance_of(Socket) end + end - it 'closes the Socket when the block returns' do - socket = nil - - Socket.unix(@path) do |sock| - socket = sock - end + it 'closes the Socket when the block returns' do + socket = nil - socket.closed?.should == true + Socket.unix(@path) do |sock| + socket = sock end + + socket.should.closed? end end end diff --git a/spec/ruby/library/socket/socket/unpack_sockaddr_in_spec.rb b/spec/ruby/library/socket/socket/unpack_sockaddr_in_spec.rb index 579ae307df..935b5cb543 100644 --- a/spec/ruby/library/socket/socket/unpack_sockaddr_in_spec.rb +++ b/spec/ruby/library/socket/socket/unpack_sockaddr_in_spec.rb @@ -32,15 +32,13 @@ describe "Socket.unpack_sockaddr_in" do end end - with_feature :unix_socket do - it "raises an ArgumentError when the sin_family is not AF_INET" do - sockaddr = Socket.sockaddr_un '/tmp/x' - lambda { Socket.unpack_sockaddr_in sockaddr }.should raise_error(ArgumentError) - end + it "raises an ArgumentError when the sin_family is not AF_INET" do + sockaddr = Socket.sockaddr_un '/tmp/x' + -> { Socket.unpack_sockaddr_in sockaddr }.should raise_error(ArgumentError) + end - it "raises an ArgumentError when passed addrinfo is not AF_INET/AF_INET6" do - addrinfo = Addrinfo.unix('/tmp/sock') - lambda { Socket.unpack_sockaddr_in(addrinfo) }.should raise_error(ArgumentError) - end + it "raises an ArgumentError when passed addrinfo is not AF_INET/AF_INET6" do + addrinfo = Addrinfo.unix('/tmp/sock') + -> { Socket.unpack_sockaddr_in(addrinfo) }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/library/socket/socket/unpack_sockaddr_un_spec.rb b/spec/ruby/library/socket/socket/unpack_sockaddr_un_spec.rb index 39e33c90e9..6e0f11de3d 100644 --- a/spec/ruby/library/socket/socket/unpack_sockaddr_un_spec.rb +++ b/spec/ruby/library/socket/socket/unpack_sockaddr_un_spec.rb @@ -1,26 +1,24 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -with_feature :unix_socket do - describe 'Socket.unpack_sockaddr_un' do - it 'decodes sockaddr to unix path' do - sockaddr = Socket.sockaddr_un('/tmp/sock') - Socket.unpack_sockaddr_un(sockaddr).should == '/tmp/sock' - end +describe 'Socket.unpack_sockaddr_un' do + it 'decodes sockaddr to unix path' do + sockaddr = Socket.sockaddr_un('/tmp/sock') + Socket.unpack_sockaddr_un(sockaddr).should == '/tmp/sock' + end - it 'returns unix path from a passed Addrinfo' do - addrinfo = Addrinfo.unix('/tmp/sock') - Socket.unpack_sockaddr_un(addrinfo).should == '/tmp/sock' - end + it 'returns unix path from a passed Addrinfo' do + addrinfo = Addrinfo.unix('/tmp/sock') + Socket.unpack_sockaddr_un(addrinfo).should == '/tmp/sock' + end - it 'raises an ArgumentError when the sin_family is not AF_UNIX' do - sockaddr = Socket.sockaddr_in(0, '127.0.0.1') - lambda { Socket.unpack_sockaddr_un(sockaddr) }.should raise_error(ArgumentError) - end + it 'raises an ArgumentError when the sa_family is not AF_UNIX' do + sockaddr = Socket.sockaddr_in(0, '127.0.0.1') + -> { Socket.unpack_sockaddr_un(sockaddr) }.should raise_error(ArgumentError) + end - it 'raises an ArgumentError when passed addrinfo is not AF_UNIX' do - addrinfo = Addrinfo.tcp('127.0.0.1', 0) - lambda { Socket.unpack_sockaddr_un(addrinfo) }.should raise_error(ArgumentError) - end + it 'raises an ArgumentError when passed addrinfo is not AF_UNIX' do + addrinfo = Addrinfo.tcp('127.0.0.1', 0) + -> { Socket.unpack_sockaddr_un(addrinfo) }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/library/socket/spec_helper.rb b/spec/ruby/library/socket/spec_helper.rb index 1121542dd5..b33663e02d 100644 --- a/spec/ruby/library/socket/spec_helper.rb +++ b/spec/ruby/library/socket/spec_helper.rb @@ -2,7 +2,6 @@ require_relative '../../spec_helper' require 'socket' MSpec.enable_feature :sock_packet if Socket.const_defined?(:SOCK_PACKET) -MSpec.enable_feature :unix_socket unless PlatformGuard.windows? MSpec.enable_feature :udp_cork if Socket.const_defined?(:UDP_CORK) MSpec.enable_feature :tcp_cork if Socket.const_defined?(:TCP_CORK) MSpec.enable_feature :pktinfo if Socket.const_defined?(:IP_PKTINFO) diff --git a/spec/ruby/library/socket/tcpserver/accept_nonblock_spec.rb b/spec/ruby/library/socket/tcpserver/accept_nonblock_spec.rb index 6f118026ae..91f6a327f0 100644 --- a/spec/ruby/library/socket/tcpserver/accept_nonblock_spec.rb +++ b/spec/ruby/library/socket/tcpserver/accept_nonblock_spec.rb @@ -13,7 +13,7 @@ describe "Socket::TCPServer.accept_nonblock" do it "accepts non blocking connections" do @server.listen(5) - lambda { + -> { @server.accept_nonblock }.should raise_error(IO::WaitReadable) @@ -33,12 +33,12 @@ describe "Socket::TCPServer.accept_nonblock" do it "raises an IOError if the socket is closed" do @server.close - lambda { @server.accept }.should raise_error(IOError) + -> { @server.accept }.should raise_error(IOError) end describe 'without a connected client' do it 'raises error' do - lambda { @server.accept_nonblock }.should raise_error(IO::WaitReadable) + -> { @server.accept_nonblock }.should raise_error(IO::WaitReadable) end it 'returns :wait_readable in exceptionless mode' do @@ -59,7 +59,7 @@ describe 'TCPServer#accept_nonblock' do describe 'without a connected client' do it 'raises IO::WaitReadable' do - lambda { @server.accept_nonblock }.should raise_error(IO::WaitReadable) + -> { @server.accept_nonblock }.should raise_error(IO::WaitReadable) end end diff --git a/spec/ruby/library/socket/tcpserver/accept_spec.rb b/spec/ruby/library/socket/tcpserver/accept_spec.rb index f0ef9cc727..d8892cd5f0 100644 --- a/spec/ruby/library/socket/tcpserver/accept_spec.rb +++ b/spec/ruby/library/socket/tcpserver/accept_spec.rb @@ -58,9 +58,29 @@ describe "TCPServer#accept" do t.join end + it "is automatically retried when interrupted by SIGVTALRM" do + t = Thread.new do + client = @server.accept + value = client.read(2) + client.close + value + end + + Thread.pass while t.status and t.status != "sleep" + # Thread#backtrace uses SIGVTALRM on TruffleRuby and potentially other implementations. + # Sending a signal to a thread is not possible with Ruby APIs. + t.backtrace.join("\n").should =~ /in [`'](?:TCPServer#)?accept'/ + + socket = TCPSocket.new('127.0.0.1', @port) + socket.write("OK") + socket.close + + t.value.should == "OK" + end + it "raises an IOError if the socket is closed" do @server.close - lambda { @server.accept }.should raise_error(IOError) + -> { @server.accept }.should raise_error(IOError) end end @@ -76,7 +96,7 @@ describe 'TCPServer#accept' do describe 'without a connected client' do it 'blocks the caller' do - lambda { @server.accept }.should block_caller + -> { @server.accept }.should block_caller end end @@ -94,6 +114,19 @@ describe 'TCPServer#accept' do @socket = @server.accept @socket.should be_an_instance_of(TCPSocket) end + + platform_is_not :windows do + it "returns a TCPSocket which is set to nonblocking" do + require 'io/nonblock' + @socket = @server.accept + @socket.should.nonblock? + end + end + + it "returns a TCPSocket which is set to close on exec" do + @socket = @server.accept + @socket.should.close_on_exec? + end end end end diff --git a/spec/ruby/library/socket/tcpserver/gets_spec.rb b/spec/ruby/library/socket/tcpserver/gets_spec.rb index 936295dce2..417976d737 100644 --- a/spec/ruby/library/socket/tcpserver/gets_spec.rb +++ b/spec/ruby/library/socket/tcpserver/gets_spec.rb @@ -11,6 +11,6 @@ describe "TCPServer#gets" do end it "raises Errno::ENOTCONN on gets" do - lambda { @server.gets }.should raise_error(Errno::ENOTCONN) + -> { @server.gets }.should raise_error(Errno::ENOTCONN) end end diff --git a/spec/ruby/library/socket/tcpserver/initialize_spec.rb b/spec/ruby/library/socket/tcpserver/initialize_spec.rb index 412bdbfb9d..4ddd1f465f 100644 --- a/spec/ruby/library/socket/tcpserver/initialize_spec.rb +++ b/spec/ruby/library/socket/tcpserver/initialize_spec.rb @@ -52,7 +52,7 @@ describe 'TCPServer#initialize' do describe 'with a single String argument containing a non numeric value' do it 'raises SocketError' do - lambda { TCPServer.new('cats') }.should raise_error(SocketError) + -> { TCPServer.new('cats') }.should raise_error(SocketError) end end diff --git a/spec/ruby/library/socket/tcpserver/listen_spec.rb b/spec/ruby/library/socket/tcpserver/listen_spec.rb index e266decd61..c877fdced6 100644 --- a/spec/ruby/library/socket/tcpserver/listen_spec.rb +++ b/spec/ruby/library/socket/tcpserver/listen_spec.rb @@ -16,7 +16,7 @@ describe 'TCPServer#listen' do end it "raises when the given argument can't be coerced to an Integer" do - lambda { @server.listen('cats') }.should raise_error(TypeError) + -> { @server.listen('cats') }.should raise_error(TypeError) end end end diff --git a/spec/ruby/library/socket/tcpserver/new_spec.rb b/spec/ruby/library/socket/tcpserver/new_spec.rb index 4717b95a2c..dd1ba676bd 100644 --- a/spec/ruby/library/socket/tcpserver/new_spec.rb +++ b/spec/ruby/library/socket/tcpserver/new_spec.rb @@ -40,7 +40,25 @@ describe "TCPServer.new" do end it "binds to INADDR_ANY if the hostname is empty and the port is a string" do - @server = TCPServer.new('', 0) + @server = TCPServer.new('', '0') + addr = @server.addr + addr[0].should == 'AF_INET' + addr[1].should be_kind_of(Integer) + addr[2].should == '0.0.0.0' + addr[3].should == '0.0.0.0' + end + + it "binds to a port if the port is explicitly nil" do + @server = TCPServer.new('', nil) + addr = @server.addr + addr[0].should == 'AF_INET' + addr[1].should be_kind_of(Integer) + addr[2].should == '0.0.0.0' + addr[3].should == '0.0.0.0' + end + + it "binds to a port if the port is an empty string" do + @server = TCPServer.new('', '') addr = @server.addr addr[0].should == 'AF_INET' addr[1].should be_kind_of(Integer) @@ -49,7 +67,7 @@ describe "TCPServer.new" do end it "coerces port to string, then determines port from that number or service name" do - lambda { TCPServer.new(SocketSpecs.hostname, Object.new) }.should raise_error(TypeError) + -> { TCPServer.new(SocketSpecs.hostname, Object.new) }.should raise_error(TypeError) port = Object.new port.should_receive(:to_str).and_return("0") @@ -62,8 +80,31 @@ describe "TCPServer.new" do # pick such a service port that will be able to reliably bind... end + it "has a single argument form and treats it as a port number" do + @server = TCPServer.new(0) + addr = @server.addr + addr[1].should be_kind_of(Integer) + end + + it "coerces port to a string when it is the only argument" do + -> { TCPServer.new(Object.new) }.should raise_error(TypeError) + + port = Object.new + port.should_receive(:to_str).and_return("0") + + @server = TCPServer.new(port) + addr = @server.addr + addr[1].should be_kind_of(Integer) + end + + it "does not use the given block and warns to use TCPServer::open" do + -> { + @server = TCPServer.new(0) { raise } + }.should complain(/warning: TCPServer::new\(\) does not take block; use TCPServer::open\(\) instead/) + end + it "raises Errno::EADDRNOTAVAIL when the address is unknown" do - lambda { TCPServer.new("1.2.3.4", 0) }.should raise_error(Errno::EADDRNOTAVAIL) + -> { TCPServer.new("1.2.3.4", 0) }.should raise_error(Errno::EADDRNOTAVAIL) end # There is no way to make this fail-proof on all machines, because @@ -71,7 +112,7 @@ describe "TCPServer.new" do # traditionally invalidly named ones. quarantine! do it "raises a SocketError when the host is unknown" do - lambda { + -> { TCPServer.new("--notavalidname", 0) }.should raise_error(SocketError) end @@ -79,7 +120,7 @@ describe "TCPServer.new" do it "raises Errno::EADDRINUSE when address is already in use" do @server = TCPServer.new('127.0.0.1', 0) - lambda { + -> { @server = TCPServer.new('127.0.0.1', @server.addr[1]) }.should raise_error(Errno::EADDRINUSE) end diff --git a/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb b/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb index 5543b67755..bd7d33faf4 100644 --- a/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb +++ b/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb @@ -12,7 +12,7 @@ describe "TCPServer#sysaccept" do end it 'blocks if no connections' do - lambda { @server.sysaccept }.should block_caller + -> { @server.sysaccept }.should block_caller end it 'returns file descriptor of an accepted connection' do @@ -41,7 +41,7 @@ describe 'TCPServer#sysaccept' do describe 'without a connected client' do it 'blocks the caller' do - lambda { @server.sysaccept }.should block_caller + -> { @server.sysaccept }.should block_caller end end diff --git a/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb b/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb index 703abff81c..5a2c704f35 100644 --- a/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb @@ -2,9 +2,11 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' # TODO: verify these for windows -describe "TCPSocket#gethostbyname" do +describe "TCPSocket.gethostbyname" do before :each do - @host_info = TCPSocket.gethostbyname(SocketSpecs.hostname) + suppress_warning do + @host_info = TCPSocket.gethostbyname(SocketSpecs.hostname) + end end it "returns an array elements of information on the hostname" do @@ -50,15 +52,19 @@ describe "TCPSocket#gethostbyname" do end end -describe 'TCPSocket#gethostbyname' do +describe 'TCPSocket.gethostbyname' do it 'returns an Array' do - TCPSocket.gethostbyname('127.0.0.1').should be_an_instance_of(Array) + suppress_warning do + TCPSocket.gethostbyname('127.0.0.1').should be_an_instance_of(Array) + end end describe 'using a hostname' do describe 'the returned Array' do before do - @array = TCPSocket.gethostbyname('127.0.0.1') + suppress_warning do + @array = TCPSocket.gethostbyname('127.0.0.1') + end end it 'includes the canonical name as the 1st value' do @@ -88,7 +94,9 @@ describe 'TCPSocket#gethostbyname' do SocketSpecs.each_ip_protocol do |family, ip_address| describe 'the returned Array' do before do - @array = TCPSocket.gethostbyname(ip_address) + suppress_warning do + @array = TCPSocket.gethostbyname(ip_address) + end end it 'includes the IP address as the 1st value' do diff --git a/spec/ruby/library/socket/tcpsocket/initialize_spec.rb b/spec/ruby/library/socket/tcpsocket/initialize_spec.rb index a3593f0d5f..d7feb9751b 100644 --- a/spec/ruby/library/socket/tcpsocket/initialize_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/initialize_spec.rb @@ -1,11 +1,37 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' +require_relative 'shared/new' + +describe 'TCPSocket#initialize' do + it_behaves_like :tcpsocket_new, :new + + describe "with a running server" do + before :each do + @server = SocketSpecs::SpecTCPServer.new + @hostname = @server.hostname + end + + after :each do + if @socket + @socket.write "QUIT" + @socket.close + end + @server.shutdown + end + + it "does not use the given block and warns to use TCPSocket::open" do + -> { + @socket = TCPSocket.new(@hostname, @server.port, nil) { raise } + }.should complain(/warning: TCPSocket::new\(\) does not take block; use TCPSocket::open\(\) instead/) + end + end +end describe 'TCPSocket#initialize' do SocketSpecs.each_ip_protocol do |family, ip_address| describe 'when no server is listening on the given address' do it 'raises Errno::ECONNREFUSED' do - lambda { TCPSocket.new(ip_address, 666) }.should raise_error(Errno::ECONNREFUSED) + -> { TCPSocket.new(ip_address, 666) }.should raise_error(Errno::ECONNREFUSED) end end @@ -31,7 +57,7 @@ describe 'TCPSocket#initialize' do end it 'raises SocketError when the port number is a non numeric String' do - lambda { TCPSocket.new(ip_address, 'cats') }.should raise_error(SocketError) + -> { TCPSocket.new(ip_address, 'cats') }.should raise_error(SocketError) end it 'set the socket to binmode' do @@ -46,6 +72,19 @@ describe 'TCPSocket#initialize' do @client.remote_address.ip_port.should == @server.local_address.ip_port end + platform_is_not :windows do + it "creates a socket which is set to nonblocking" do + require 'io/nonblock' + @client = TCPSocket.new(ip_address, @port) + @client.should.nonblock? + end + end + + it "creates a socket which is set to close on exec" do + @client = TCPSocket.new(ip_address, @port) + @client.should.close_on_exec? + end + describe 'using a local address and service' do it 'binds the client socket to the local address and service' do @client = TCPSocket.new(ip_address, @port, ip_address, 0) diff --git a/spec/ruby/library/socket/tcpsocket/new_spec.rb b/spec/ruby/library/socket/tcpsocket/new_spec.rb deleted file mode 100644 index 4924468be7..0000000000 --- a/spec/ruby/library/socket/tcpsocket/new_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require_relative 'shared/new' - -describe "TCPSocket.new" do - it_behaves_like :tcpsocket_new, :new -end diff --git a/spec/ruby/library/socket/tcpsocket/open_spec.rb b/spec/ruby/library/socket/tcpsocket/open_spec.rb index 31b630a23b..0c0b579064 100644 --- a/spec/ruby/library/socket/tcpsocket/open_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/open_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../../spec_helper" require_relative 'shared/new' describe "TCPSocket.open" do diff --git a/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb b/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb index a381627a39..d365ecd335 100644 --- a/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb @@ -16,6 +16,6 @@ describe "TCPSocket partial closability" do @s2.close end - it_should_behave_like "partially closable sockets" + it_should_behave_like :partially_closable_sockets end diff --git a/spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb b/spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb index bfd815c658..6ce5a41b58 100644 --- a/spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb @@ -27,6 +27,20 @@ describe "TCPSocket#recv_nonblock" do @socket.recv_nonblock(50).should == "TCPSocket#recv_nonblock" end + it 'writes the read to a buffer from the socket' do + @socket = TCPSocket.new @hostname, @server.port + @socket.write "TCPSocket#recv_nonblock" + + # Wait for the server to echo. This spec is testing the return + # value, not the non-blocking behavior. + # + # TODO: Figure out a good way to test non-blocking. + IO.select([@socket]) + buffer = "".b + @socket.recv_nonblock(50, 0, buffer) + buffer.should == 'TCPSocket#recv_nonblock' + end + it 'returns :wait_readable in exceptionless mode' do @socket = TCPSocket.new @hostname, @server.port @socket.recv_nonblock(50, exception: false).should == :wait_readable diff --git a/spec/ruby/library/socket/tcpsocket/shared/new.rb b/spec/ruby/library/socket/tcpsocket/shared/new.rb index d0358923c9..0e405253c8 100644 --- a/spec/ruby/library/socket/tcpsocket/shared/new.rb +++ b/spec/ruby/library/socket/tcpsocket/shared/new.rb @@ -3,17 +3,26 @@ require_relative '../../fixtures/classes' describe :tcpsocket_new, shared: true do it "requires a hostname and a port as arguments" do - lambda { TCPSocket.send(@method) }.should raise_error(ArgumentError) + -> { TCPSocket.send(@method) }.should raise_error(ArgumentError) end it "refuses the connection when there is no server to connect to" do - lambda do + -> do TCPSocket.send(@method, SocketSpecs.hostname, SocketSpecs.reserved_unused_port) end.should raise_error(SystemCallError) {|e| [Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL].should include(e.class) } end + it 'raises IO::TimeoutError with :connect_timeout when no server is listening on the given address' do + -> { + TCPSocket.send(@method, "192.0.2.1", 80, connect_timeout: 0) + }.should raise_error(IO::TimeoutError) + rescue Errno::ENETUNREACH + # In the case all network interfaces down. + # raise_error cannot deal with multiple expected exceptions + end + describe "with a running server" do before :each do @server = SocketSpecs::SpecTCPServer.new @@ -44,14 +53,23 @@ describe :tcpsocket_new, shared: true do end it "connects to a server when passed local_host and local_port arguments" do - server = TCPServer.new(SocketSpecs.hostname, 0) + retries = 0 + max_retries = 3 + begin - available_port = server.addr[1] - ensure - server.close + retries += 1 + server = TCPServer.new(SocketSpecs.hostname, 0) + begin + available_port = server.addr[1] + ensure + server.close + end + @socket = TCPSocket.send(@method, @hostname, @server.port, + @hostname, available_port) + rescue Errno::EADDRINUSE + raise if retries >= max_retries + retry end - @socket = TCPSocket.send(@method, @hostname, @server.port, - @hostname, available_port) @socket.should be_an_instance_of(TCPSocket) end @@ -75,5 +93,10 @@ describe :tcpsocket_new, shared: true do @socket.addr[1].should be_kind_of(Integer) @socket.addr[2].should =~ /^#{@hostname}/ end + + it "connects to a server when passed connect_timeout argument" do + @socket = TCPSocket.send(@method, @hostname, @server.port, connect_timeout: 1) + @socket.should be_an_instance_of(TCPSocket) + end end end diff --git a/spec/ruby/library/socket/udpsocket/bind_spec.rb b/spec/ruby/library/socket/udpsocket/bind_spec.rb index 4dbdb285f6..08b386e941 100644 --- a/spec/ruby/library/socket/udpsocket/bind_spec.rb +++ b/spec/ruby/library/socket/udpsocket/bind_spec.rb @@ -18,7 +18,7 @@ describe "UDPSocket#bind" do it "raises Errno::EINVAL when already bound" do @socket.bind(SocketSpecs.hostname, 0) - lambda { + -> { @socket.bind(SocketSpecs.hostname, @socket.addr[1]) }.should raise_error(Errno::EINVAL) end diff --git a/spec/ruby/library/socket/udpsocket/initialize_spec.rb b/spec/ruby/library/socket/udpsocket/initialize_spec.rb index 1d635149f7..ecf0043c10 100644 --- a/spec/ruby/library/socket/udpsocket/initialize_spec.rb +++ b/spec/ruby/library/socket/udpsocket/initialize_spec.rb @@ -30,6 +30,19 @@ describe 'UDPSocket#initialize' do @socket.binmode?.should be_true end + platform_is_not :windows do + it 'sets the socket to nonblock' do + require 'io/nonblock' + @socket = UDPSocket.new(:INET) + @socket.should.nonblock? + end + end + + it 'sets the socket to close on exec' do + @socket = UDPSocket.new(:INET) + @socket.should.close_on_exec? + end + it 'raises Errno::EAFNOSUPPORT or Errno::EPROTONOSUPPORT when given an invalid address family' do -> { UDPSocket.new(666) diff --git a/spec/ruby/library/socket/udpsocket/inspect_spec.rb b/spec/ruby/library/socket/udpsocket/inspect_spec.rb index 201e8b3fc6..e212120b14 100644 --- a/spec/ruby/library/socket/udpsocket/inspect_spec.rb +++ b/spec/ruby/library/socket/udpsocket/inspect_spec.rb @@ -10,16 +10,8 @@ describe 'UDPSocket#inspect' do @socket.close end - ruby_version_is ""..."2.5" do - it 'returns a String with the fd' do - @socket.inspect.should == "#<UDPSocket:fd #{@socket.fileno}>" - end - end - - ruby_version_is "2.5" do - it 'returns a String with the fd, family, address and port' do - port = @socket.addr[1] - @socket.inspect.should == "#<UDPSocket:fd #{@socket.fileno}, AF_INET, 127.0.0.1, #{port}>" - end + it 'returns a String with the fd, family, address and port' do + port = @socket.addr[1] + @socket.inspect.should == "#<UDPSocket:fd #{@socket.fileno}, AF_INET, 127.0.0.1, #{port}>" end end diff --git a/spec/ruby/library/socket/udpsocket/new_spec.rb b/spec/ruby/library/socket/udpsocket/new_spec.rb index 157ff138be..79bfcb624d 100644 --- a/spec/ruby/library/socket/udpsocket/new_spec.rb +++ b/spec/ruby/library/socket/udpsocket/new_spec.rb @@ -26,8 +26,14 @@ describe 'UDPSocket.new' do @socket.should be_an_instance_of(UDPSocket) end + it "does not use the given block and warns to use UDPSocket::open" do + -> { + @socket = UDPSocket.new { raise } + }.should complain(/warning: UDPSocket::new\(\) does not take block; use UDPSocket::open\(\) instead/) + end + it 'raises Errno::EAFNOSUPPORT or Errno::EPROTONOSUPPORT if unsupported family passed' do - lambda { UDPSocket.new(-1) }.should raise_error(SystemCallError) { |e| + -> { UDPSocket.new(-1) }.should raise_error(SystemCallError) { |e| [Errno::EAFNOSUPPORT, Errno::EPROTONOSUPPORT].should include(e.class) } end diff --git a/spec/ruby/library/socket/udpsocket/recvfrom_nonblock_spec.rb b/spec/ruby/library/socket/udpsocket/recvfrom_nonblock_spec.rb index fb88a2dc13..b804099589 100644 --- a/spec/ruby/library/socket/udpsocket/recvfrom_nonblock_spec.rb +++ b/spec/ruby/library/socket/udpsocket/recvfrom_nonblock_spec.rb @@ -16,7 +16,7 @@ describe 'UDPSocket#recvfrom_nonblock' do platform_is_not :windows do describe 'using an unbound socket' do it 'raises IO::WaitReadable' do - lambda { @server.recvfrom_nonblock(1) }.should raise_error(IO::WaitReadable) + -> { @server.recvfrom_nonblock(1) }.should raise_error(IO::WaitReadable) end end end @@ -32,7 +32,7 @@ describe 'UDPSocket#recvfrom_nonblock' do describe 'without any data available' do it 'raises IO::WaitReadable' do - lambda { @server.recvfrom_nonblock(1) }.should raise_error(IO::WaitReadable) + -> { @server.recvfrom_nonblock(1) }.should raise_error(IO::WaitReadable) end it 'returns :wait_readable with exception: false' do @@ -51,6 +51,22 @@ describe 'UDPSocket#recvfrom_nonblock' do @server.recvfrom_nonblock(1).should be_an_instance_of(Array) end + it 'writes the data to the buffer when one is present' do + buffer = "".b + IO.select([@server]) + @server.recvfrom_nonblock(1, 0, buffer) + buffer.should == 'h' + end + + it "preserves the encoding of the given buffer" do + buffer = ''.encode(Encoding::ISO_8859_1) + IO.select([@server]) + message, = @server.recvfrom_nonblock(1, 0, buffer) + + message.should.equal?(buffer) + buffer.encoding.should == Encoding::ISO_8859_1 + end + describe 'the returned Array' do before do IO.select([@server]) diff --git a/spec/ruby/library/socket/udpsocket/send_spec.rb b/spec/ruby/library/socket/udpsocket/send_spec.rb index 431129723e..6dd5f67bea 100644 --- a/spec/ruby/library/socket/udpsocket/send_spec.rb +++ b/spec/ruby/library/socket/udpsocket/send_spec.rb @@ -63,10 +63,10 @@ describe "UDPSocket#send" do @msg[1][3].should == "127.0.0.1" end - it "raises EMSGSIZE if data is too too big" do + it "raises EMSGSIZE if data is too big" do @socket = UDPSocket.open begin - lambda do + -> do @socket.send('1' * 100_000, 0, SocketSpecs.hostname, @port.to_s) end.should raise_error(Errno::EMSGSIZE) ensure @@ -96,7 +96,7 @@ describe 'UDPSocket#send' do describe 'using a disconnected socket' do describe 'without a destination address' do it "raises #{SocketSpecs.dest_addr_req_error}" do - lambda { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error) + -> { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error) end end @@ -108,7 +108,7 @@ describe 'UDPSocket#send' do it 'does not persist the connection after sending data' do @client.send('hello', 0, @addr.ip_address, @addr.ip_port) - lambda { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error) + -> { @client.send('hello', 0) }.should raise_error(SocketSpecs.dest_addr_req_error) end end @@ -144,7 +144,7 @@ describe 'UDPSocket#send' do it 'sends the data to the given address instead' do @client.send('hello', 0, @alt_server.getsockname).should == 5 - lambda { @server.recv(5) }.should block_caller + -> { @server.recv(5) }.should block_caller @alt_server.recv(5).should == 'hello' end diff --git a/spec/ruby/library/socket/udpsocket/write_spec.rb b/spec/ruby/library/socket/udpsocket/write_spec.rb index e960de1baf..c971f29b62 100644 --- a/spec/ruby/library/socket/udpsocket/write_spec.rb +++ b/spec/ruby/library/socket/udpsocket/write_spec.rb @@ -10,7 +10,7 @@ describe "UDPSocket#write" do s2 = UDPSocket.new s2.connect(host, s1.addr[1]) - lambda do + -> do s2.write('1' * 100_000) end.should raise_error(Errno::EMSGSIZE) ensure diff --git a/spec/ruby/library/socket/unixserver/accept_nonblock_spec.rb b/spec/ruby/library/socket/unixserver/accept_nonblock_spec.rb index 3ebe38a090..f67941b296 100644 --- a/spec/ruby/library/socket/unixserver/accept_nonblock_spec.rb +++ b/spec/ruby/library/socket/unixserver/accept_nonblock_spec.rb @@ -2,89 +2,84 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' describe "UNIXServer#accept_nonblock" do + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) - platform_is_not :windows do - before :each do - @path = SocketSpecs.socket_path - @server = UNIXServer.open(@path) - @client = UNIXSocket.open(@path) + @socket = @server.accept_nonblock + @client.send("foobar", 0) + end - @socket = @server.accept_nonblock - @client.send("foobar", 0) - end + after :each do + @socket.close + @client.close + @server.close + SocketSpecs.rm_socket @path + end - after :each do - @socket.close - @client.close - @server.close - SocketSpecs.rm_socket @path - end + it "accepts a connection in a non-blocking way" do + data = @socket.recvfrom(6).first + data.should == "foobar" + end - it "accepts a connection in a non-blocking way" do - data = @socket.recvfrom(6).first - data.should == "foobar" - end + it "returns a UNIXSocket" do + @socket.should be_kind_of(UNIXSocket) + end - it "returns a UNIXSocket" do - @socket.should be_kind_of(UNIXSocket) - end + it 'returns :wait_readable in exceptionless mode' do + @server.accept_nonblock(exception: false).should == :wait_readable + end +end + +describe 'UNIXServer#accept_nonblock' do + before do + @path = SocketSpecs.socket_path + @server = UNIXServer.new(@path) + end - it 'returns :wait_readable in exceptionless mode' do - @server.accept_nonblock(exception: false).should == :wait_readable + after do + @server.close + rm_r(@path) + end + + describe 'without a client' do + it 'raises IO::WaitReadable' do + -> { @server.accept_nonblock }.should raise_error(IO::WaitReadable) end end -end -with_feature :unix_socket do - describe 'UNIXServer#accept_nonblock' do + describe 'with a client' do before do - @path = SocketSpecs.socket_path - @server = UNIXServer.new(@path) + @client = UNIXSocket.new(@path) end after do - @server.close - rm_r(@path) + @client.close + @socket.close if @socket end - describe 'without a client' do - it 'raises IO::WaitReadable' do - lambda { @server.accept_nonblock }.should raise_error(IO::WaitReadable) + describe 'without any data' do + it 'returns a UNIXSocket' do + @socket = @server.accept_nonblock + @socket.should be_an_instance_of(UNIXSocket) end end - describe 'with a client' do + describe 'with data available' do before do - @client = UNIXSocket.new(@path) - end - - after do - @client.close - @socket.close if @socket + @client.write('hello') end - describe 'without any data' do - it 'returns a UNIXSocket' do - @socket = @server.accept_nonblock - @socket.should be_an_instance_of(UNIXSocket) - end + it 'returns a UNIXSocket' do + @socket = @server.accept_nonblock + @socket.should be_an_instance_of(UNIXSocket) end - describe 'with data available' do - before do - @client.write('hello') - end - - it 'returns a UNIXSocket' do + describe 'the returned UNIXSocket' do + it 'can read the data written' do @socket = @server.accept_nonblock - @socket.should be_an_instance_of(UNIXSocket) - end - - describe 'the returned UNIXSocket' do - it 'can read the data written' do - @socket = @server.accept_nonblock - @socket.recv(5).should == 'hello' - end + @socket.recv(5).should == 'hello' end end end diff --git a/spec/ruby/library/socket/unixserver/accept_spec.rb b/spec/ruby/library/socket/unixserver/accept_spec.rb index a1570f7013..cc2c922b6f 100644 --- a/spec/ruby/library/socket/unixserver/accept_spec.rb +++ b/spec/ruby/library/socket/unixserver/accept_spec.rb @@ -1,115 +1,124 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -platform_is_not :windows do - describe "UNIXServer#accept" do - before :each do - @path = SocketSpecs.socket_path - @server = UNIXServer.open(@path) - end +describe "UNIXServer#accept" do + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + end - after :each do - @server.close if @server - SocketSpecs.rm_socket @path - end + after :each do + @server.close if @server + SocketSpecs.rm_socket @path + end - it "accepts what is written by the client" do - client = UNIXSocket.open(@path) + it "accepts what is written by the client" do + client = UNIXSocket.open(@path) - client.send('hello', 0) + client.send('hello', 0) - sock = @server.accept - begin - data, info = sock.recvfrom(5) + sock = @server.accept + begin + data, info = sock.recvfrom(5) - data.should == 'hello' - info.should_not be_empty - ensure - sock.close - client.close - end + data.should == 'hello' + info.should_not be_empty + ensure + sock.close + client.close end + end - it "can be interrupted by Thread#kill" do - t = Thread.new { - @server.accept - } - Thread.pass while t.status and t.status != "sleep" - - # kill thread, ensure it dies in a reasonable amount of time - t.kill - a = 0 - while t.alive? and a < 5000 - sleep 0.001 - a += 1 - end - a.should < 5000 + it "can be interrupted by Thread#kill" do + t = Thread.new { + @server.accept + } + Thread.pass while t.status and t.status != "sleep" + + # kill thread, ensure it dies in a reasonable amount of time + t.kill + a = 0 + while t.alive? and a < 5000 + sleep 0.001 + a += 1 end + a.should < 5000 + end - it "can be interrupted by Thread#raise" do - t = Thread.new { - -> { - @server.accept - }.should raise_error(Exception, "interrupted") - } + it "can be interrupted by Thread#raise" do + t = Thread.new { + -> { + @server.accept + }.should raise_error(Exception, "interrupted") + } - Thread.pass while t.status and t.status != "sleep" - t.raise Exception, "interrupted" - t.join - end + Thread.pass while t.status and t.status != "sleep" + t.raise Exception, "interrupted" + t.join end end -with_feature :unix_socket do - describe 'UNIXServer#accept' do +describe 'UNIXServer#accept' do + before do + @path = SocketSpecs.socket_path + @server = UNIXServer.new(@path) + end + + after do + @server.close + rm_r(@path) + end + + describe 'without a client' do + it 'blocks the calling thread' do + -> { @server.accept }.should block_caller + end + end + + describe 'with a client' do before do - @path = SocketSpecs.socket_path - @server = UNIXServer.new(@path) + @client = UNIXSocket.new(@path) end after do - @server.close - rm_r(@path) + @client.close + @socket.close if @socket end - describe 'without a client' do - it 'blocks the calling thread' do - lambda { @server.accept }.should block_caller + describe 'without any data' do + it 'returns a UNIXSocket' do + @socket = @server.accept + @socket.should be_an_instance_of(UNIXSocket) end end - describe 'with a client' do + describe 'with data available' do before do - @client = UNIXSocket.new(@path) + @client.write('hello') end - after do - @client.close - @socket.close if @socket + it 'returns a UNIXSocket' do + @socket = @server.accept + @socket.should be_an_instance_of(UNIXSocket) end - describe 'without any data' do - it 'returns a UNIXSocket' do + describe 'the returned UNIXSocket' do + it 'can read the data written' do @socket = @server.accept - @socket.should be_an_instance_of(UNIXSocket) + @socket.recv(5).should == 'hello' end - end - describe 'with data available' do - before do - @client.write('hello') + platform_is_not :windows do + it "is set to nonblocking" do + require 'io/nonblock' + @socket = @server.accept + @socket.should.nonblock? + end end - it 'returns a UNIXSocket' do + it "is set to close on exec" do @socket = @server.accept - @socket.should be_an_instance_of(UNIXSocket) - end - - describe 'the returned UNIXSocket' do - it 'can read the data written' do - @socket = @server.accept - @socket.recv(5).should == 'hello' - end + @socket.should.close_on_exec? end end end diff --git a/spec/ruby/library/socket/unixserver/for_fd_spec.rb b/spec/ruby/library/socket/unixserver/for_fd_spec.rb index 4f3816ad37..be1c2df4d7 100644 --- a/spec/ruby/library/socket/unixserver/for_fd_spec.rb +++ b/spec/ruby/library/socket/unixserver/for_fd_spec.rb @@ -1,23 +1,21 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -platform_is_not :windows do - describe "UNIXServer#for_fd" do - before :each do - @unix_path = SocketSpecs.socket_path - @unix = UNIXServer.new(@unix_path) - end +describe "UNIXServer.for_fd" do + before :each do + @unix_path = SocketSpecs.socket_path + @unix = UNIXServer.new(@unix_path) + end - after :each do - @unix.close if @unix - SocketSpecs.rm_socket @unix_path - end + after :each do + @unix.close if @unix + SocketSpecs.rm_socket @unix_path + end - it "can calculate the path" do - b = UNIXServer.for_fd(@unix.fileno) - b.autoclose = false + it "can calculate the path" do + b = UNIXServer.for_fd(@unix.fileno) + b.autoclose = false - b.path.should == @unix_path - end + b.path.should == @unix_path end end diff --git a/spec/ruby/library/socket/unixserver/initialize_spec.rb b/spec/ruby/library/socket/unixserver/initialize_spec.rb index cb186ceb76..3728a307b0 100644 --- a/spec/ruby/library/socket/unixserver/initialize_spec.rb +++ b/spec/ruby/library/socket/unixserver/initialize_spec.rb @@ -1,28 +1,26 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -with_feature :unix_socket do - describe 'UNIXServer#initialize' do - before do - @path = SocketSpecs.socket_path - @server = UNIXServer.new(@path) - end +describe 'UNIXServer#initialize' do + before do + @path = SocketSpecs.socket_path + @server = UNIXServer.new(@path) + end - after do - @server.close if @server - rm_r @path - end + after do + @server.close if @server + rm_r @path + end - it 'returns a new UNIXServer' do - @server.should be_an_instance_of(UNIXServer) - end + it 'returns a new UNIXServer' do + @server.should be_an_instance_of(UNIXServer) + end - it 'sets the socket to binmode' do - @server.binmode?.should be_true - end + it 'sets the socket to binmode' do + @server.binmode?.should be_true + end - it 'raises Errno::EADDRINUSE when the socket is already in use' do - lambda { UNIXServer.new(@path) }.should raise_error(Errno::EADDRINUSE) - end + it 'raises Errno::EADDRINUSE when the socket is already in use' do + -> { UNIXServer.new(@path) }.should raise_error(Errno::EADDRINUSE) end end diff --git a/spec/ruby/library/socket/unixserver/listen_spec.rb b/spec/ruby/library/socket/unixserver/listen_spec.rb index b90b3bbb09..7938d648c4 100644 --- a/spec/ruby/library/socket/unixserver/listen_spec.rb +++ b/spec/ruby/library/socket/unixserver/listen_spec.rb @@ -1,21 +1,19 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -with_feature :unix_socket do - describe 'UNIXServer#listen' do - before do - @path = SocketSpecs.socket_path - @server = UNIXServer.new(@path) - end +describe 'UNIXServer#listen' do + before do + @path = SocketSpecs.socket_path + @server = UNIXServer.new(@path) + end - after do - @server.close + after do + @server.close - rm_r(@path) - end + rm_r(@path) + end - it 'returns 0' do - @server.listen(1).should == 0 - end + it 'returns 0' do + @server.listen(1).should == 0 end end diff --git a/spec/ruby/library/socket/unixserver/new_spec.rb b/spec/ruby/library/socket/unixserver/new_spec.rb index f831f40bc6..7d0c7bf76e 100644 --- a/spec/ruby/library/socket/unixserver/new_spec.rb +++ b/spec/ruby/library/socket/unixserver/new_spec.rb @@ -3,4 +3,10 @@ require_relative 'shared/new' describe "UNIXServer.new" do it_behaves_like :unixserver_new, :new + + it "does not use the given block and warns to use UNIXServer::open" do + -> { + @server = UNIXServer.new(@path) { raise } + }.should complain(/warning: UNIXServer::new\(\) does not take block; use UNIXServer::open\(\) instead/) + end end diff --git a/spec/ruby/library/socket/unixserver/open_spec.rb b/spec/ruby/library/socket/unixserver/open_spec.rb index f2506d9f6f..c49df802d0 100644 --- a/spec/ruby/library/socket/unixserver/open_spec.rb +++ b/spec/ruby/library/socket/unixserver/open_spec.rb @@ -5,22 +5,20 @@ require_relative 'shared/new' describe "UNIXServer.open" do it_behaves_like :unixserver_new, :open - platform_is_not :windows do - before :each do - @path = SocketSpecs.socket_path - end + before :each do + @path = SocketSpecs.socket_path + end - after :each do - @server.close if @server - @server = nil - SocketSpecs.rm_socket @path - end + after :each do + @server.close if @server + @server = nil + SocketSpecs.rm_socket @path + end - it "yields the new UNIXServer object to the block, if given" do - UNIXServer.open(@path) do |unix| - unix.path.should == @path - unix.addr.should == ["AF_UNIX", @path] - end + it "yields the new UNIXServer object to the block, if given" do + UNIXServer.open(@path) do |unix| + unix.path.should == @path + unix.addr.should == ["AF_UNIX", @path] end end end diff --git a/spec/ruby/library/socket/unixserver/shared/new.rb b/spec/ruby/library/socket/unixserver/shared/new.rb index 35395826c9..b537f2a871 100644 --- a/spec/ruby/library/socket/unixserver/shared/new.rb +++ b/spec/ruby/library/socket/unixserver/shared/new.rb @@ -2,21 +2,19 @@ require_relative '../../spec_helper' require_relative '../../fixtures/classes' describe :unixserver_new, shared: true do - platform_is_not :windows do - before :each do - @path = SocketSpecs.socket_path - end + before :each do + @path = SocketSpecs.socket_path + end - after :each do - @server.close if @server - @server = nil - SocketSpecs.rm_socket @path - end + after :each do + @server.close if @server + @server = nil + SocketSpecs.rm_socket @path + end - it "creates a new UNIXServer" do - @server = UNIXServer.send(@method, @path) - @server.path.should == @path - @server.addr.should == ["AF_UNIX", @path] - end + it "creates a new UNIXServer" do + @server = UNIXServer.send(@method, @path) + @server.path.should == @path + @server.addr.should == ["AF_UNIX", @path] end end diff --git a/spec/ruby/library/socket/unixserver/sysaccept_spec.rb b/spec/ruby/library/socket/unixserver/sysaccept_spec.rb index 65a35c91a9..c4a4ecc824 100644 --- a/spec/ruby/library/socket/unixserver/sysaccept_spec.rb +++ b/spec/ruby/library/socket/unixserver/sysaccept_spec.rb @@ -1,51 +1,49 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -with_feature :unix_socket do - describe 'UNIXServer#sysaccept' do +describe 'UNIXServer#sysaccept' do + before do + @path = SocketSpecs.socket_path + @server = UNIXServer.new(@path) + end + + after do + @server.close + + rm_r(@path) + end + + describe 'without a client' do + it 'blocks the calling thread' do + -> { @server.sysaccept }.should block_caller + end + end + + describe 'with a client' do before do - @path = SocketSpecs.socket_path - @server = UNIXServer.new(@path) + @client = UNIXSocket.new(@path) end after do - @server.close - - rm_r(@path) + Socket.for_fd(@fd).close if @fd + @client.close end - describe 'without a client' do - it 'blocks the calling thread' do - lambda { @server.sysaccept }.should block_caller + describe 'without any data' do + it 'returns an Integer' do + @fd = @server.sysaccept + @fd.should be_kind_of(Integer) end end - describe 'with a client' do + describe 'with data available' do before do - @client = UNIXSocket.new(@path) - end - - after do - Socket.for_fd(@fd).close if @fd - @client.close + @client.write('hello') end - describe 'without any data' do - it 'returns an Integer' do - @fd = @server.sysaccept - @fd.should be_kind_of(Integer) - end - end - - describe 'with data available' do - before do - @client.write('hello') - end - - it 'returns an Integer' do - @fd = @server.sysaccept - @fd.should be_kind_of(Integer) - end + it 'returns an Integer' do + @fd = @server.sysaccept + @fd.should be_kind_of(Integer) end end end diff --git a/spec/ruby/library/socket/unixsocket/addr_spec.rb b/spec/ruby/library/socket/unixsocket/addr_spec.rb index e8431bea16..1afe9b12dc 100644 --- a/spec/ruby/library/socket/unixsocket/addr_spec.rb +++ b/spec/ruby/library/socket/unixsocket/addr_spec.rb @@ -2,35 +2,32 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' describe "UNIXSocket#addr" do + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end - platform_is_not :windows do - before :each do - @path = SocketSpecs.socket_path - @server = UNIXServer.open(@path) - @client = UNIXSocket.open(@path) - end - - after :each do - @client.close - @server.close - SocketSpecs.rm_socket @path - end + after :each do + @client.close + @server.close + SocketSpecs.rm_socket @path + end - it "returns an array" do - @client.addr.should be_kind_of(Array) - end + it "returns an array" do + @client.addr.should be_kind_of(Array) + end - it "returns the address family of this socket in an array" do - @client.addr[0].should == "AF_UNIX" - @server.addr[0].should == "AF_UNIX" - end + it "returns the address family of this socket in an array" do + @client.addr[0].should == "AF_UNIX" + @server.addr[0].should == "AF_UNIX" + end - it "returns the path of the socket in an array if it's a server" do - @server.addr[1].should == @path - end + it "returns the path of the socket in an array if it's a server" do + @server.addr[1].should == @path + end - it "returns an empty string for path if it's a client" do - @client.addr[1].should == "" - end + it "returns an empty string for path if it's a client" do + @client.addr[1].should == "" end end diff --git a/spec/ruby/library/socket/unixsocket/initialize_spec.rb b/spec/ruby/library/socket/unixsocket/initialize_spec.rb index a6217d404f..5dccfcc745 100644 --- a/spec/ruby/library/socket/unixsocket/initialize_spec.rb +++ b/spec/ruby/library/socket/unixsocket/initialize_spec.rb @@ -1,38 +1,56 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -with_feature :unix_socket do - describe 'UNIXSocket#initialize' do - describe 'using a non existing path' do +describe 'UNIXSocket#initialize' do + describe 'using a non existing path' do + platform_is_not :windows do it 'raises Errno::ENOENT' do - lambda { UNIXSocket.new(SocketSpecs.socket_path) }.should raise_error(Errno::ENOENT) + -> { UNIXSocket.new(SocketSpecs.socket_path) }.should raise_error(Errno::ENOENT) end end - describe 'using an existing socket path' do - before do - @path = SocketSpecs.socket_path - @server = UNIXServer.new(@path) - @socket = UNIXSocket.new(@path) + platform_is :windows do + # Why, Windows, why? + it 'raises Errno::ECONNREFUSED' do + -> { UNIXSocket.new(SocketSpecs.socket_path) }.should raise_error(Errno::ECONNREFUSED) end + end + end - after do - @socket.close - @server.close - rm_r(@path) - end + describe 'using an existing socket path' do + before do + @path = SocketSpecs.socket_path + @server = UNIXServer.new(@path) + @socket = UNIXSocket.new(@path) + end - it 'returns a new UNIXSocket' do - @socket.should be_an_instance_of(UNIXSocket) - end + after do + @socket.close + @server.close + rm_r(@path) + end - it 'sets the socket path to an empty String' do - @socket.path.should == '' - end + it 'returns a new UNIXSocket' do + @socket.should be_an_instance_of(UNIXSocket) + end + + it 'sets the socket path to an empty String' do + @socket.path.should == '' + end - it 'sets the socket to binmode' do - @socket.binmode?.should be_true + it 'sets the socket to binmode' do + @socket.binmode?.should be_true + end + + platform_is_not :windows do + it 'sets the socket to nonblock' do + require 'io/nonblock' + @socket.should.nonblock? end end + + it 'sets the socket to close on exec' do + @socket.should.close_on_exec? + end end end diff --git a/spec/ruby/library/socket/unixsocket/inspect_spec.rb b/spec/ruby/library/socket/unixsocket/inspect_spec.rb index d2e3cabbd3..77bb521069 100644 --- a/spec/ruby/library/socket/unixsocket/inspect_spec.rb +++ b/spec/ruby/library/socket/unixsocket/inspect_spec.rb @@ -2,16 +2,14 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' describe "UNIXSocket#inspect" do - platform_is_not :windows do - it "returns sockets fd for unnamed sockets" do - begin - s1, s2 = UNIXSocket.socketpair - s1.inspect.should == "#<UNIXSocket:fd #{s1.fileno}>" - s2.inspect.should == "#<UNIXSocket:fd #{s2.fileno}>" - ensure - s1.close - s2.close - end + it "returns sockets fd for unnamed sockets" do + begin + s1, s2 = UNIXSocket.socketpair + s1.inspect.should == "#<UNIXSocket:fd #{s1.fileno}>" + s2.inspect.should == "#<UNIXSocket:fd #{s2.fileno}>" + ensure + s1.close + s2.close end end end diff --git a/spec/ruby/library/socket/unixsocket/local_address_spec.rb b/spec/ruby/library/socket/unixsocket/local_address_spec.rb index cbf315f9f4..0fdec38293 100644 --- a/spec/ruby/library/socket/unixsocket/local_address_spec.rb +++ b/spec/ruby/library/socket/unixsocket/local_address_spec.rb @@ -1,96 +1,92 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -with_feature :unix_socket do - describe 'UNIXSocket#local_address' do - before do - @path = SocketSpecs.socket_path - @server = UNIXServer.new(@path) - @client = UNIXSocket.new(@path) - end +describe 'UNIXSocket#local_address' do + before do + @path = SocketSpecs.socket_path + @server = UNIXServer.new(@path) + @client = UNIXSocket.new(@path) + end - after do - @client.close - @server.close + after do + @client.close + @server.close - rm_r(@path) - end - - it 'returns an Addrinfo' do - @client.local_address.should be_an_instance_of(Addrinfo) - end + rm_r(@path) + end - describe 'the returned Addrinfo' do - platform_is_not :aix do - it 'uses AF_UNIX as the address family' do - @client.local_address.afamily.should == Socket::AF_UNIX - end + it 'returns an Addrinfo' do + @client.local_address.should be_an_instance_of(Addrinfo) + end - it 'uses PF_UNIX as the protocol family' do - @client.local_address.pfamily.should == Socket::PF_UNIX - end + describe 'the returned Addrinfo' do + platform_is_not :aix do + it 'uses AF_UNIX as the address family' do + @client.local_address.afamily.should == Socket::AF_UNIX end - it 'uses SOCK_STREAM as the socket type' do - @client.local_address.socktype.should == Socket::SOCK_STREAM + it 'uses PF_UNIX as the protocol family' do + @client.local_address.pfamily.should == Socket::PF_UNIX end + end - platform_is_not :aix do - it 'uses an empty socket path' do - @client.local_address.unix_path.should == '' - end - end + it 'uses SOCK_STREAM as the socket type' do + @client.local_address.socktype.should == Socket::SOCK_STREAM + end - it 'uses 0 as the protocol' do - @client.local_address.protocol.should == 0 + platform_is_not :aix do + it 'uses an empty socket path' do + @client.local_address.unix_path.should == '' end end + + it 'uses 0 as the protocol' do + @client.local_address.protocol.should == 0 + end end end -with_feature :unix_socket do - describe 'UNIXSocket#local_address with a UNIX socket pair' do - before :each do - @sock, @sock2 = Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM) - end +describe 'UNIXSocket#local_address with a UNIX socket pair' do + before :each do + @sock, @sock2 = Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM) + end - after :each do - @sock.close - @sock2.close - end + after :each do + @sock.close + @sock2.close + end - it 'returns an Addrinfo' do - @sock.local_address.should be_an_instance_of(Addrinfo) - end + it 'returns an Addrinfo' do + @sock.local_address.should be_an_instance_of(Addrinfo) + end - describe 'the returned Addrinfo' do - it 'uses AF_UNIX as the address family' do - @sock.local_address.afamily.should == Socket::AF_UNIX - end + describe 'the returned Addrinfo' do + it 'uses AF_UNIX as the address family' do + @sock.local_address.afamily.should == Socket::AF_UNIX + end - it 'uses PF_UNIX as the protocol family' do - @sock.local_address.pfamily.should == Socket::PF_UNIX - end + it 'uses PF_UNIX as the protocol family' do + @sock.local_address.pfamily.should == Socket::PF_UNIX + end - it 'uses SOCK_STREAM as the socket type' do - @sock.local_address.socktype.should == Socket::SOCK_STREAM - end + it 'uses SOCK_STREAM as the socket type' do + @sock.local_address.socktype.should == Socket::SOCK_STREAM + end - it 'raises SocketError for #ip_address' do - -> { - @sock.local_address.ip_address - }.should raise_error(SocketError, "need IPv4 or IPv6 address") - end + it 'raises SocketError for #ip_address' do + -> { + @sock.local_address.ip_address + }.should raise_error(SocketError, "need IPv4 or IPv6 address") + end - it 'raises SocketError for #ip_port' do - -> { - @sock.local_address.ip_port - }.should raise_error(SocketError, "need IPv4 or IPv6 address") - end + it 'raises SocketError for #ip_port' do + -> { + @sock.local_address.ip_port + }.should raise_error(SocketError, "need IPv4 or IPv6 address") + end - it 'uses 0 as the protocol' do - @sock.local_address.protocol.should == 0 - end + it 'uses 0 as the protocol' do + @sock.local_address.protocol.should == 0 end end end diff --git a/spec/ruby/library/socket/unixsocket/new_spec.rb b/spec/ruby/library/socket/unixsocket/new_spec.rb index 05a6b3eda2..fea2c1e2b7 100644 --- a/spec/ruby/library/socket/unixsocket/new_spec.rb +++ b/spec/ruby/library/socket/unixsocket/new_spec.rb @@ -3,4 +3,10 @@ require_relative 'shared/new' describe "UNIXSocket.new" do it_behaves_like :unixsocket_new, :new + + it "does not use the given block and warns to use UNIXSocket::open" do + -> { + @client = UNIXSocket.new(@path) { raise } + }.should complain(/warning: UNIXSocket::new\(\) does not take block; use UNIXSocket::open\(\) instead/) + end end diff --git a/spec/ruby/library/socket/unixsocket/open_spec.rb b/spec/ruby/library/socket/unixsocket/open_spec.rb index ad5048fedd..b5e8c6c23a 100644 --- a/spec/ruby/library/socket/unixsocket/open_spec.rb +++ b/spec/ruby/library/socket/unixsocket/open_spec.rb @@ -7,22 +7,20 @@ describe "UNIXSocket.open" do end describe "UNIXSocket.open" do - platform_is_not :windows do - before :each do - @path = SocketSpecs.socket_path - @server = UNIXServer.open(@path) - end + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + end - after :each do - @server.close - SocketSpecs.rm_socket @path - end + after :each do + @server.close + SocketSpecs.rm_socket @path + end - it "opens a unix socket on the specified file and yields it to the block" do - UNIXSocket.open(@path) do |client| - client.addr[0].should == "AF_UNIX" - client.closed?.should == false - end + it "opens a unix socket on the specified file and yields it to the block" do + UNIXSocket.open(@path) do |client| + client.addr[0].should == "AF_UNIX" + client.should_not.closed? end end end diff --git a/spec/ruby/library/socket/unixsocket/pair_spec.rb b/spec/ruby/library/socket/unixsocket/pair_spec.rb index 845ff76ecc..9690142668 100644 --- a/spec/ruby/library/socket/unixsocket/pair_spec.rb +++ b/spec/ruby/library/socket/unixsocket/pair_spec.rb @@ -1,39 +1,18 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' require_relative '../shared/partially_closable_sockets' +require_relative 'shared/pair' -describe "UNIXSocket#pair" do - platform_is_not :windows do +describe "UNIXSocket.pair" do + it_should_behave_like :unixsocket_pair + it_should_behave_like :partially_closable_sockets - it_should_behave_like "partially closable sockets" - - before :each do - @s1, @s2 = UNIXSocket.pair - end - - after :each do - @s1.close - @s2.close - end - - it "returns a pair of connected sockets" do - @s1.puts "foo" - @s2.gets.should == "foo\n" - end - - it "returns sockets with no name" do - @s1.path.should == @s2.path - @s1.path.should == "" - end - - it "returns sockets with no address" do - @s1.addr.should == ["AF_UNIX", ""] - @s2.addr.should == ["AF_UNIX", ""] - end + before :each do + @s1, @s2 = UNIXSocket.pair + end - it "returns sockets with no peeraddr" do - @s1.peeraddr.should == ["AF_UNIX", ""] - @s2.peeraddr.should == ["AF_UNIX", ""] - end + after :each do + @s1.close + @s2.close end end diff --git a/spec/ruby/library/socket/unixsocket/partially_closable_spec.rb b/spec/ruby/library/socket/unixsocket/partially_closable_spec.rb index 78a64fe6be..108a6c3063 100644 --- a/spec/ruby/library/socket/unixsocket/partially_closable_spec.rb +++ b/spec/ruby/library/socket/unixsocket/partially_closable_spec.rb @@ -2,24 +2,20 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' require_relative '../shared/partially_closable_sockets' -platform_is_not :windows do - describe "UNIXSocket partial closability" do - - before :each do - @path = SocketSpecs.socket_path - @server = UNIXServer.open(@path) - @s1 = UNIXSocket.new(@path) - @s2 = @server.accept - end - - after :each do - @server.close - @s1.close - @s2.close - SocketSpecs.rm_socket @path - end - - it_should_behave_like "partially closable sockets" +describe "UNIXSocket partial closability" do + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + @s1 = UNIXSocket.new(@path) + @s2 = @server.accept + end + after :each do + @server.close + @s1.close + @s2.close + SocketSpecs.rm_socket @path end + + it_should_behave_like :partially_closable_sockets end diff --git a/spec/ruby/library/socket/unixsocket/path_spec.rb b/spec/ruby/library/socket/unixsocket/path_spec.rb index 317ffc0975..ffe7e4bea2 100644 --- a/spec/ruby/library/socket/unixsocket/path_spec.rb +++ b/spec/ruby/library/socket/unixsocket/path_spec.rb @@ -2,27 +2,23 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' describe "UNIXSocket#path" do + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end - platform_is_not :windows do - before :each do - @path = SocketSpecs.socket_path - @server = UNIXServer.open(@path) - @client = UNIXSocket.open(@path) - end - - after :each do - @client.close - @server.close - SocketSpecs.rm_socket @path - end - - it "returns the path of the socket if it's a server" do - @server.path.should == @path - end + after :each do + @client.close + @server.close + SocketSpecs.rm_socket @path + end - it "returns an empty string for path if it's a client" do - @client.path.should == "" - end + it "returns the path of the socket if it's a server" do + @server.path.should == @path end + it "returns an empty string for path if it's a client" do + @client.path.should == "" + end end diff --git a/spec/ruby/library/socket/unixsocket/peeraddr_spec.rb b/spec/ruby/library/socket/unixsocket/peeraddr_spec.rb index e39c8a0570..10cab13b42 100644 --- a/spec/ruby/library/socket/unixsocket/peeraddr_spec.rb +++ b/spec/ruby/library/socket/unixsocket/peeraddr_spec.rb @@ -2,29 +2,25 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' describe "UNIXSocket#peeraddr" do + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end - platform_is_not :windows do - before :each do - @path = SocketSpecs.socket_path - @server = UNIXServer.open(@path) - @client = UNIXSocket.open(@path) - end - - after :each do - @client.close - @server.close - SocketSpecs.rm_socket @path - end - - it "returns the address family and path of the server end of the connection" do - @client.peeraddr.should == ["AF_UNIX", @path] - end + after :each do + @client.close + @server.close + SocketSpecs.rm_socket @path + end - it "raises an error in server sockets" do - lambda { - @server.peeraddr - }.should raise_error(Errno::ENOTCONN) - end + it "returns the address family and path of the server end of the connection" do + @client.peeraddr.should == ["AF_UNIX", @path] end + it "raises an error in server sockets" do + -> { + @server.peeraddr + }.should raise_error(Errno::ENOTCONN) + end end diff --git a/spec/ruby/library/socket/unixsocket/recv_io_spec.rb b/spec/ruby/library/socket/unixsocket/recv_io_spec.rb index 533f02a0fa..f0b408f309 100644 --- a/spec/ruby/library/socket/unixsocket/recv_io_spec.rb +++ b/spec/ruby/library/socket/unixsocket/recv_io_spec.rb @@ -1,9 +1,8 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -describe "UNIXSocket#recv_io" do - - platform_is_not :windows do +platform_is_not :windows do + describe "UNIXSocket#recv_io" do before :each do @path = SocketSpecs.socket_path @server = UNIXServer.open(@path) @@ -41,9 +40,7 @@ describe "UNIXSocket#recv_io" do @io.should be_an_instance_of(File) end end -end -with_feature :unix_socket do describe 'UNIXSocket#recv_io' do before do @file = File.open('/dev/null', 'w') diff --git a/spec/ruby/library/socket/unixsocket/recvfrom_spec.rb b/spec/ruby/library/socket/unixsocket/recvfrom_spec.rb index c0e1cf670b..9ae3777961 100644 --- a/spec/ruby/library/socket/unixsocket/recvfrom_spec.rb +++ b/spec/ruby/library/socket/unixsocket/recvfrom_spec.rb @@ -2,35 +2,104 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' describe "UNIXSocket#recvfrom" do - platform_is_not :windows do - before :each do - @path = SocketSpecs.socket_path - @server = UNIXServer.open(@path) - @client = UNIXSocket.open(@path) + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + @client = UNIXSocket.open(@path) + end + + after :each do + @client.close + @server.close + SocketSpecs.rm_socket @path + end + + it "receives len bytes from sock, returning an array containing sent data as first element" do + @client.send("foobar", 0) + sock = @server.accept + sock.recvfrom(6).first.should == "foobar" + sock.close + end + + context "when called on a server's socket" do + platform_is_not :windows do + it "returns an array containing basic information on the client as second element" do + @client.send("foobar", 0) + sock = @server.accept + data = sock.recvfrom(6) + data.last.should == ["AF_UNIX", ""] + sock.close + end end - after :each do - @client.close - @server.close - SocketSpecs.rm_socket @path + guard -> { platform_is :windows and ruby_bug "#21702", ""..."4.2" } do + it "returns an array containing basic information on the client as second element" do + @client.send("foobar", 0) + sock = @server.accept + data = sock.recvfrom(6) + data.last.should == ["AF_UNIX", ""] + sock.close + end end + end - it "receives len bytes from sock" do - @client.send("foobar", 0) - sock = @server.accept - sock.recvfrom(6).first.should == "foobar" - sock.close + context "when called on a client's socket" do + platform_is :linux do + it "returns an array containing server's address as second element" do + @client.send("", 0) + sock = @server.accept + sock.send("barfoo", 0) + @client.recvfrom(6).last.should == ["AF_UNIX", @server.local_address.unix_path] + sock.close + end end - it "returns an array with data and information on the sender" do - @client.send("foobar", 0) - sock = @server.accept - data = sock.recvfrom(6) - data.first.should == "foobar" - data.last.should == ["AF_UNIX", ""] - sock.close + guard -> { platform_is :windows and ruby_bug "#21702", ""..."4.2" } do + it "returns an array containing server's address as second element" do + @client.send("", 0) + sock = @server.accept + sock.send("barfoo", 0) + # This may not be correct, depends on what underlying recvfrom actually returns. + @client.recvfrom(6).last.should == ["AF_UNIX", @server.local_address.unix_path] + sock.close + end end + platform_is :darwin do + it "returns an array containing basic information on the server as second element" do + @client.send("", 0) + sock = @server.accept + sock.send("barfoo", 0) + @client.recvfrom(6).last.should == ["AF_UNIX", ""] + sock.close + end + end + end + + it "allows an output buffer as third argument" do + buffer = +'' + + @client.send("foobar", 0) + sock = @server.accept + message, = sock.recvfrom(6, 0, buffer) + sock.close + + message.should.equal?(buffer) + buffer.should == "foobar" + end + + it "preserves the encoding of the given buffer" do + buffer = ''.encode(Encoding::ISO_8859_1) + + @client.send("foobar", 0) + sock = @server.accept + sock.recvfrom(6, 0, buffer) + sock.close + + buffer.encoding.should == Encoding::ISO_8859_1 + end + + platform_is_not :windows do it "uses different message options" do @client.send("foobar", Socket::MSG_PEEK) sock = @server.accept @@ -44,25 +113,32 @@ describe "UNIXSocket#recvfrom" do end end +describe 'UNIXSocket#recvfrom' do + describe 'using a socket pair' do + before do + @client, @server = UNIXSocket.socketpair + @client.write('hello') + end -with_feature :unix_socket do - describe 'UNIXSocket#recvfrom' do - describe 'using a socket pair' do - before do - @client, @server = UNIXSocket.socketpair - @client.write('hello') - end + after do + @client.close + @server.close + end - after do - @client.close - @server.close + platform_is_not :windows do + it 'returns an Array containing the data and address information' do + @server.recvfrom(5).should == ['hello', ['AF_UNIX', '']] end + end + guard -> { platform_is :windows and ruby_bug "#21702", ""..."4.2" } do it 'returns an Array containing the data and address information' do @server.recvfrom(5).should == ['hello', ['AF_UNIX', '']] end end + end + platform_is_not :windows do # These specs are taken from the rdoc examples on UNIXSocket#recvfrom. describe 'using a UNIX socket constructed using UNIXSocket.for_fd' do before do diff --git a/spec/ruby/library/socket/unixsocket/remote_address_spec.rb b/spec/ruby/library/socket/unixsocket/remote_address_spec.rb index 0b416254d0..84bdff0a6a 100644 --- a/spec/ruby/library/socket/unixsocket/remote_address_spec.rb +++ b/spec/ruby/library/socket/unixsocket/remote_address_spec.rb @@ -1,45 +1,43 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -with_feature :unix_socket do - describe 'UNIXSocket#remote_address' do - before do - @path = SocketSpecs.socket_path - @server = UNIXServer.new(@path) - @client = UNIXSocket.new(@path) - end +describe 'UNIXSocket#remote_address' do + before do + @path = SocketSpecs.socket_path + @server = UNIXServer.new(@path) + @client = UNIXSocket.new(@path) + end - after do - @client.close - @server.close + after do + @client.close + @server.close - rm_r(@path) - end + rm_r(@path) + end - it 'returns an Addrinfo' do - @client.remote_address.should be_an_instance_of(Addrinfo) - end + it 'returns an Addrinfo' do + @client.remote_address.should be_an_instance_of(Addrinfo) + end - describe 'the returned Addrinfo' do - it 'uses AF_UNIX as the address family' do - @client.remote_address.afamily.should == Socket::AF_UNIX - end + describe 'the returned Addrinfo' do + it 'uses AF_UNIX as the address family' do + @client.remote_address.afamily.should == Socket::AF_UNIX + end - it 'uses PF_UNIX as the protocol family' do - @client.remote_address.pfamily.should == Socket::PF_UNIX - end + it 'uses PF_UNIX as the protocol family' do + @client.remote_address.pfamily.should == Socket::PF_UNIX + end - it 'uses SOCK_STREAM as the socket type' do - @client.remote_address.socktype.should == Socket::SOCK_STREAM - end + it 'uses SOCK_STREAM as the socket type' do + @client.remote_address.socktype.should == Socket::SOCK_STREAM + end - it 'uses the correct socket path' do - @client.remote_address.unix_path.should == @path - end + it 'uses the correct socket path' do + @client.remote_address.unix_path.should == @path + end - it 'uses 0 as the protocol' do - @client.remote_address.protocol.should == 0 - end + it 'uses 0 as the protocol' do + @client.remote_address.protocol.should == 0 end end end diff --git a/spec/ruby/library/socket/unixsocket/send_io_spec.rb b/spec/ruby/library/socket/unixsocket/send_io_spec.rb index a2a7d26539..52186ec3cf 100644 --- a/spec/ruby/library/socket/unixsocket/send_io_spec.rb +++ b/spec/ruby/library/socket/unixsocket/send_io_spec.rb @@ -1,9 +1,8 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' -describe "UNIXSocket#send_io" do - - platform_is_not :windows do +platform_is_not :windows do + describe "UNIXSocket#send_io" do before :each do @path = SocketSpecs.socket_path @server = UNIXServer.open(@path) @@ -32,9 +31,7 @@ describe "UNIXSocket#send_io" do @io.read.should == File.read(@send_io_path) end end -end -with_feature :unix_socket do describe 'UNIXSocket#send_io' do before do @file = File.open('/dev/null', 'w') diff --git a/spec/ruby/library/socket/unixsocket/shared/new.rb b/spec/ruby/library/socket/unixsocket/shared/new.rb index 76a4e1701e..f075b03c5e 100644 --- a/spec/ruby/library/socket/unixsocket/shared/new.rb +++ b/spec/ruby/library/socket/unixsocket/shared/new.rb @@ -2,23 +2,21 @@ require_relative '../../spec_helper' require_relative '../../fixtures/classes' describe :unixsocket_new, shared: true do - platform_is_not :windows do - before :each do - @path = SocketSpecs.socket_path - @server = UNIXServer.open(@path) - end + before :each do + @path = SocketSpecs.socket_path + @server = UNIXServer.open(@path) + end - after :each do - @client.close if @client - @server.close - SocketSpecs.rm_socket @path - end + after :each do + @client.close if @client + @server.close + SocketSpecs.rm_socket @path + end - it "opens a unix socket on the specified file" do - @client = UNIXSocket.send(@method, @path) + it "opens a unix socket on the specified file" do + @client = UNIXSocket.send(@method, @path) - @client.addr[0].should == "AF_UNIX" - @client.closed?.should == false - end + @client.addr[0].should == "AF_UNIX" + @client.should_not.closed? end end diff --git a/spec/ruby/library/socket/unixsocket/shared/pair.rb b/spec/ruby/library/socket/unixsocket/shared/pair.rb new file mode 100644 index 0000000000..d68ee466bf --- /dev/null +++ b/spec/ruby/library/socket/unixsocket/shared/pair.rb @@ -0,0 +1,47 @@ +require_relative '../../spec_helper' +require_relative '../../fixtures/classes' + +describe :unixsocket_pair, shared: true do + it "returns two UNIXSockets" do + @s1.should be_an_instance_of(UNIXSocket) + @s2.should be_an_instance_of(UNIXSocket) + end + + it "returns a pair of connected sockets" do + @s1.puts "foo" + @s2.gets.should == "foo\n" + end + + platform_is_not :windows do + it "sets the socket paths to empty Strings" do + @s1.path.should == "" + @s2.path.should == "" + end + + it "sets the socket addresses to empty Strings" do + @s1.addr.should == ["AF_UNIX", ""] + @s2.addr.should == ["AF_UNIX", ""] + end + + it "sets the socket peer addresses to empty Strings" do + @s1.peeraddr.should == ["AF_UNIX", ""] + @s2.peeraddr.should == ["AF_UNIX", ""] + end + end + + platform_is :windows do + it "emulates unnamed sockets with a temporary file with a path" do + @s1.addr.should == ["AF_UNIX", @s1.path] + @s2.peeraddr.should == ["AF_UNIX", @s1.path] + end + + it "sets the peer address of first socket to an empty string" do + @s1.peeraddr.should == ["AF_UNIX", ""] + end + + it "sets the address and path of second socket to an empty string" do + @s2.addr.should == ["AF_UNIX", ""] + @s2.path.should == "" + end + end +end diff --git a/spec/ruby/library/socket/unixsocket/socketpair_spec.rb b/spec/ruby/library/socket/unixsocket/socketpair_spec.rb index 3e9646f76b..c61fc00be4 100644 --- a/spec/ruby/library/socket/unixsocket/socketpair_spec.rb +++ b/spec/ruby/library/socket/unixsocket/socketpair_spec.rb @@ -1,40 +1,18 @@ require_relative '../spec_helper' +require_relative '../fixtures/classes' +require_relative '../shared/partially_closable_sockets' +require_relative 'shared/pair' -with_feature :unix_socket do - describe 'UNIXSocket.socketpair' do - before do - @s1, @s2 = UNIXSocket.socketpair - end +describe "UNIXSocket.socketpair" do + it_should_behave_like :unixsocket_pair + it_should_behave_like :partially_closable_sockets - after do - @s1.close - @s2.close - end - - it 'returns two UNIXSockets' do - @s1.should be_an_instance_of(UNIXSocket) - @s2.should be_an_instance_of(UNIXSocket) - end - - it 'connects the sockets to each other' do - @s1.write('hello') - - @s2.recv(5).should == 'hello' - end - - it 'sets the socket paths to empty Strings' do - @s1.path.should == '' - @s2.path.should == '' - end - - it 'sets the socket addresses to empty Strings' do - @s1.addr.should == ['AF_UNIX', ''] - @s2.addr.should == ['AF_UNIX', ''] - end + before :each do + @s1, @s2 = UNIXSocket.socketpair + end - it 'sets the socket peer addresses to empty Strings' do - @s1.peeraddr.should == ['AF_UNIX', ''] - @s2.peeraddr.should == ['AF_UNIX', ''] - end + after :each do + @s1.close + @s2.close end end |
