summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/tcpsocket
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/socket/tcpsocket')
-rw-r--r--spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb84
-rw-r--r--spec/ruby/library/socket/tcpsocket/initialize_spec.rb100
-rw-r--r--spec/ruby/library/socket/tcpsocket/local_address_spec.rb73
-rw-r--r--spec/ruby/library/socket/tcpsocket/new_spec.rb5
-rw-r--r--spec/ruby/library/socket/tcpsocket/open_spec.rb3
-rw-r--r--spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb8
-rw-r--r--spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb26
-rw-r--r--spec/ruby/library/socket/tcpsocket/recv_spec.rb28
-rw-r--r--spec/ruby/library/socket/tcpsocket/remote_address_spec.rb72
-rw-r--r--spec/ruby/library/socket/tcpsocket/setsockopt_spec.rb4
-rw-r--r--spec/ruby/library/socket/tcpsocket/shared/new.rb75
11 files changed, 433 insertions, 45 deletions
diff --git a/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb b/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb
index 11838aca27..c6fe007827 100644
--- a/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb
+++ b/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb
@@ -1,14 +1,16 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+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
- @host_info.should be_kind_of(Array)
+ @host_info.should.is_a?(Array)
end
platform_is_not :windows do
@@ -18,12 +20,12 @@ describe "TCPSocket#gethostbyname" do
it "returns the address type as the third value" do
address_type = @host_info[2]
- [Socket::AF_INET, Socket::AF_INET6].include?(address_type).should be_true
+ [Socket::AF_INET, Socket::AF_INET6].include?(address_type).should == true
end
it "returns the IP address as the fourth value" do
ip = @host_info[3]
- ["127.0.0.1", "::1"].include?(ip).should be_true
+ ["127.0.0.1", "::1"].include?(ip).should == true
end
end
@@ -46,6 +48,72 @@ describe "TCPSocket#gethostbyname" do
end
it "returns any aliases to the address as second value" do
- @host_info[1].should be_kind_of(Array)
+ @host_info[1].should.is_a?(Array)
+ end
+end
+
+describe 'TCPSocket.gethostbyname' do
+ it 'returns an Array' do
+ suppress_warning do
+ TCPSocket.gethostbyname('127.0.0.1').should.instance_of?(Array)
+ end
+ end
+
+ describe 'using a hostname' do
+ describe 'the returned Array' do
+ before do
+ suppress_warning do
+ @array = TCPSocket.gethostbyname('127.0.0.1')
+ end
+ end
+
+ it 'includes the canonical name as the 1st value' do
+ @array[0].should == '127.0.0.1'
+ end
+
+ it 'includes an array of alternative hostnames as the 2nd value' do
+ @array[1].should.instance_of?(Array)
+ end
+
+ it 'includes the address family as the 3rd value' do
+ @array[2].should.is_a?(Integer)
+ end
+
+ it 'includes the IP addresses as all the remaining values' do
+ ips = %w{::1 127.0.0.1}
+
+ ips.include?(@array[3]).should == true
+
+ # Not all machines might have both IPv4 and IPv6 set up, so this value is
+ # optional.
+ ips.include?(@array[4]).should == true if @array[4]
+ end
+ end
+ end
+
+ SocketSpecs.each_ip_protocol do |family, ip_address|
+ describe 'the returned Array' do
+ before do
+ suppress_warning do
+ @array = TCPSocket.gethostbyname(ip_address)
+ end
+ end
+
+ it 'includes the IP address as the 1st value' do
+ @array[0].should == ip_address
+ end
+
+ it 'includes an empty list of aliases as the 2nd value' do
+ @array[1].should == []
+ end
+
+ it 'includes the address family as the 3rd value' do
+ @array[2].should == family
+ end
+
+ it 'includes the IP address as the 4th value' do
+ @array[3].should == ip_address
+ end
+ end
end
end
diff --git a/spec/ruby/library/socket/tcpsocket/initialize_spec.rb b/spec/ruby/library/socket/tcpsocket/initialize_spec.rb
new file mode 100644
index 0000000000..a33d0b16ba
--- /dev/null
+++ b/spec/ruby/library/socket/tcpsocket/initialize_spec.rb
@@ -0,0 +1,100 @@
+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
+ -> { TCPSocket.new(ip_address, 666) }.should.raise(Errno::ECONNREFUSED)
+ end
+ end
+
+ describe 'when a server is listening on the given address' do
+ before do
+ @server = TCPServer.new(ip_address, 0)
+ @port = @server.connect_address.ip_port
+ end
+
+ after do
+ @client.close if @client
+ @server.close
+ end
+
+ it 'returns a TCPSocket when using an Integer as the port' do
+ @client = TCPSocket.new(ip_address, @port)
+ @client.should.instance_of?(TCPSocket)
+ end
+
+ it 'returns a TCPSocket when using a String as the port' do
+ @client = TCPSocket.new(ip_address, @port.to_s)
+ @client.should.instance_of?(TCPSocket)
+ end
+
+ it 'raises SocketError when the port number is a non numeric String' do
+ -> { TCPSocket.new(ip_address, 'cats') }.should.raise(SocketError)
+ end
+
+ it 'set the socket to binmode' do
+ @client = TCPSocket.new(ip_address, @port)
+ @client.binmode?.should == true
+ end
+
+ it 'connects to the right address' do
+ @client = TCPSocket.new(ip_address, @port)
+
+ @client.remote_address.ip_address.should == @server.local_address.ip_address
+ @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)
+
+ @client.local_address.ip_address.should == ip_address
+
+ @client.local_address.ip_port.should > 0
+ @client.local_address.ip_port.should_not == @port
+ end
+ end
+ end
+ end
+end
diff --git a/spec/ruby/library/socket/tcpsocket/local_address_spec.rb b/spec/ruby/library/socket/tcpsocket/local_address_spec.rb
new file mode 100644
index 0000000000..5dcf741f29
--- /dev/null
+++ b/spec/ruby/library/socket/tcpsocket/local_address_spec.rb
@@ -0,0 +1,73 @@
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
+
+describe 'TCPSocket#local_address' do
+ SocketSpecs.each_ip_protocol do |family, ip_address|
+ before do
+ @server = TCPServer.new(ip_address, 0)
+ @host = @server.connect_address.ip_address
+ @port = @server.connect_address.ip_port
+ end
+
+ after do
+ @server.close
+ end
+
+ describe 'using an explicit hostname' do
+ before do
+ @sock = TCPSocket.new(@host, @port)
+ end
+
+ after do
+ @sock.close
+ end
+
+ it 'returns an Addrinfo' do
+ @sock.local_address.should.instance_of?(Addrinfo)
+ end
+
+ describe 'the returned Addrinfo' do
+ it 'uses AF_INET as the address family' do
+ @sock.local_address.afamily.should == family
+ end
+
+ it 'uses PF_INET as the protocol family' do
+ @sock.local_address.pfamily.should == family
+ end
+
+ it 'uses SOCK_STREAM as the socket type' do
+ @sock.local_address.socktype.should == Socket::SOCK_STREAM
+ end
+
+ it 'uses the correct IP address' do
+ @sock.local_address.ip_address.should == @host
+ end
+
+ it 'uses a randomly assigned local port' do
+ @sock.local_address.ip_port.should > 0
+ @sock.local_address.ip_port.should_not == @port
+ end
+
+ it 'uses 0 as the protocol' do
+ @sock.local_address.protocol.should == 0
+ end
+ end
+ end
+
+ describe 'using an implicit hostname' do
+ before do
+ @sock = TCPSocket.new(nil, @port)
+ end
+
+ after do
+ @sock.close
+ end
+
+ describe 'the returned Addrinfo' do
+ it 'uses the correct IP address' do
+ @sock.local_address.ip_address.should == @host
+ end
+ end
+ end
+ end
+end
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 279576272b..0000000000
--- a/spec/ruby/library/socket/tcpsocket/new_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require File.expand_path('../shared/new', __FILE__)
-
-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 fb4cc4629a..0c0b579064 100644
--- a/spec/ruby/library/socket/tcpsocket/open_spec.rb
+++ b/spec/ruby/library/socket/tcpsocket/open_spec.rb
@@ -1,4 +1,5 @@
-require File.expand_path('../shared/new', __FILE__)
+require_relative "../../../spec_helper"
+require_relative 'shared/new'
describe "TCPSocket.open" do
it_behaves_like :tcpsocket_new, :open
diff --git a/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb b/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb
index 6a43eea625..d365ecd335 100644
--- a/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb
+++ b/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb
@@ -1,6 +1,6 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
-require File.expand_path('../../shared/partially_closable_sockets', __FILE__)
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
+require_relative '../shared/partially_closable_sockets'
describe "TCPSocket partial closability" do
@@ -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 237ff781a3..6ce5a41b58 100644
--- a/spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb
+++ b/spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb
@@ -1,5 +1,5 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
describe "TCPSocket#recv_nonblock" do
before :each do
@@ -27,10 +27,22 @@ describe "TCPSocket#recv_nonblock" do
@socket.recv_nonblock(50).should == "TCPSocket#recv_nonblock"
end
- ruby_version_is '2.3' do
- it 'returns :wait_readable in exceptionless mode' do
- @socket = TCPSocket.new @hostname, @server.port
- @socket.recv_nonblock(50, exception: false).should == :wait_readable
- 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
end
end
diff --git a/spec/ruby/library/socket/tcpsocket/recv_spec.rb b/spec/ruby/library/socket/tcpsocket/recv_spec.rb
new file mode 100644
index 0000000000..f380db670d
--- /dev/null
+++ b/spec/ruby/library/socket/tcpsocket/recv_spec.rb
@@ -0,0 +1,28 @@
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
+
+describe 'TCPSocket#recv' do
+ SocketSpecs.each_ip_protocol do |family, ip_address|
+ before do
+ @server = TCPServer.new(ip_address, 0)
+ @client = TCPSocket.new(ip_address, @server.connect_address.ip_port)
+ end
+
+ after do
+ @client.close
+ @server.close
+ end
+
+ it 'returns the message data' do
+ @client.write('hello')
+
+ socket = @server.accept
+
+ begin
+ socket.recv(5).should == 'hello'
+ ensure
+ socket.close
+ end
+ end
+ end
+end
diff --git a/spec/ruby/library/socket/tcpsocket/remote_address_spec.rb b/spec/ruby/library/socket/tcpsocket/remote_address_spec.rb
new file mode 100644
index 0000000000..085d57b3f9
--- /dev/null
+++ b/spec/ruby/library/socket/tcpsocket/remote_address_spec.rb
@@ -0,0 +1,72 @@
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
+
+describe 'TCPSocket#remote_address' do
+ SocketSpecs.each_ip_protocol do |family, ip_address|
+ before do
+ @server = TCPServer.new(ip_address, 0)
+ @host = @server.connect_address.ip_address
+ @port = @server.connect_address.ip_port
+ end
+
+ after do
+ @server.close
+ end
+
+ describe 'using an explicit hostname' do
+ before do
+ @sock = TCPSocket.new(@host, @port)
+ end
+
+ after do
+ @sock.close
+ end
+
+ it 'returns an Addrinfo' do
+ @sock.remote_address.should.instance_of?(Addrinfo)
+ end
+
+ describe 'the returned Addrinfo' do
+ it 'uses AF_INET as the address family' do
+ @sock.remote_address.afamily.should == family
+ end
+
+ it 'uses PF_INET as the protocol family' do
+ @sock.remote_address.pfamily.should == family
+ end
+
+ it 'uses SOCK_STREAM as the socket type' do
+ @sock.remote_address.socktype.should == Socket::SOCK_STREAM
+ end
+
+ it 'uses the correct IP address' do
+ @sock.remote_address.ip_address.should == @host
+ end
+
+ it 'uses the correct port' do
+ @sock.remote_address.ip_port.should == @port
+ end
+
+ it 'uses 0 as the protocol' do
+ @sock.remote_address.protocol.should == 0
+ end
+ end
+ end
+
+ describe 'using an implicit hostname' do
+ before do
+ @sock = TCPSocket.new(nil, @port)
+ end
+
+ after do
+ @sock.close
+ end
+
+ describe 'the returned Addrinfo' do
+ it 'uses the correct IP address' do
+ @sock.remote_address.ip_address.should == @host
+ end
+ end
+ end
+ end
+end
diff --git a/spec/ruby/library/socket/tcpsocket/setsockopt_spec.rb b/spec/ruby/library/socket/tcpsocket/setsockopt_spec.rb
index 8a0cb443b5..8b728b7522 100644
--- a/spec/ruby/library/socket/tcpsocket/setsockopt_spec.rb
+++ b/spec/ruby/library/socket/tcpsocket/setsockopt_spec.rb
@@ -1,5 +1,5 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
describe "TCPSocket#setsockopt" do
before :each do
diff --git a/spec/ruby/library/socket/tcpsocket/shared/new.rb b/spec/ruby/library/socket/tcpsocket/shared/new.rb
index 912208c86c..9c15dced4f 100644
--- a/spec/ruby/library/socket/tcpsocket/shared/new.rb
+++ b/spec/ruby/library/socket/tcpsocket/shared/new.rb
@@ -1,19 +1,37 @@
-require File.expand_path('../../../../../spec_helper', __FILE__)
-require File.expand_path('../../../fixtures/classes', __FILE__)
+require_relative '../../spec_helper'
+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(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.should.raise(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(IO::TimeoutError)
+ rescue Errno::ENETUNREACH
+ skip "all network interfaces down"
+ end
+
+ ruby_version_is "4.0" do
+ it 'raises IO::TimeoutError with :open_timeout when no server is listening on the given address' do
+ -> {
+ TCPSocket.send(@method, "192.0.2.1", 80, open_timeout: 0)
+ }.should.raise(IO::TimeoutError)
+ rescue Errno::ENETUNREACH
+ skip "all network interfaces down"
+ end
+ end
+
describe "with a running server" do
before :each do
@server = SocketSpecs::SpecTCPServer.new
@@ -30,34 +48,43 @@ describe :tcpsocket_new, shared: true do
it "silently ignores 'nil' as the third parameter" do
@socket = TCPSocket.send(@method, @hostname, @server.port, nil)
- @socket.should be_an_instance_of(TCPSocket)
+ @socket.should.instance_of?(TCPSocket)
end
it "connects to a listening server with host and port" do
@socket = TCPSocket.send(@method, @hostname, @server.port)
- @socket.should be_an_instance_of(TCPSocket)
+ @socket.should.instance_of?(TCPSocket)
end
it "connects to a server when passed local_host argument" do
@socket = TCPSocket.send(@method, @hostname, @server.port, @hostname)
- @socket.should be_an_instance_of(TCPSocket)
+ @socket.should.instance_of?(TCPSocket)
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)
+ @socket.should.instance_of?(TCPSocket)
end
it "has an address once it has connected to a listening server" do
@socket = TCPSocket.send(@method, @hostname, @server.port)
- @socket.should be_an_instance_of(TCPSocket)
+ @socket.should.instance_of?(TCPSocket)
# TODO: Figure out how to abstract this. You can get AF_INET
# from 'Socket.getaddrinfo(hostname, nil)[0][3]' but socket.addr
@@ -72,8 +99,20 @@ describe :tcpsocket_new, shared: true do
@socket.addr[3].should == SocketSpecs.addr(:ipv6)
end
- @socket.addr[1].should be_kind_of(Fixnum)
+ @socket.addr[1].should.is_a?(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.instance_of?(TCPSocket)
+ end
+
+ ruby_version_is "4.0" do
+ it "connects to a server when passed open_timeout argument" do
+ @socket = TCPSocket.send(@method, @hostname, @server.port, open_timeout: 1)
+ @socket.should.instance_of?(TCPSocket)
+ end
+ end
end
end