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.rb62
-rw-r--r--spec/ruby/library/socket/tcpsocket/initialize_spec.rb61
-rw-r--r--spec/ruby/library/socket/tcpsocket/local_address_spec.rb73
-rw-r--r--spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb2
-rw-r--r--spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb2
-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.rb2
-rw-r--r--spec/ruby/library/socket/tcpsocket/shared/new.rb2
9 files changed, 299 insertions, 5 deletions
diff --git a/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb b/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb
index 9f25280a70..230d14bfb0 100644
--- a/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb
+++ b/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb
@@ -1,4 +1,4 @@
-require_relative '../../../spec_helper'
+require_relative '../spec_helper'
require_relative '../fixtures/classes'
# TODO: verify these for windows
@@ -49,3 +49,63 @@ describe "TCPSocket#gethostbyname" do
@host_info[1].should be_kind_of(Array)
end
end
+
+describe 'TCPSocket#gethostbyname' do
+ it 'returns an Array' do
+ TCPSocket.gethostbyname('127.0.0.1').should be_an_instance_of(Array)
+ end
+
+ describe 'using a hostname' do
+ describe 'the returned Array' do
+ before do
+ @array = TCPSocket.gethostbyname('127.0.0.1')
+ 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 be_an_instance_of(Array)
+ end
+
+ it 'includes the address family as the 3rd value' do
+ @array[2].should be_an_instance_of(Fixnum)
+ 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
+ @array = TCPSocket.gethostbyname(ip_address)
+ 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..184f9abb7d
--- /dev/null
+++ b/spec/ruby/library/socket/tcpsocket/initialize_spec.rb
@@ -0,0 +1,61 @@
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
+
+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)
+ 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 a Fixnum as the port' do
+ @client = TCPSocket.new(ip_address, @port)
+ @client.should be_an_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 be_an_instance_of(TCPSocket)
+ end
+
+ it 'raises SocketError when the port number is a non numeric String' do
+ lambda { TCPSocket.new(ip_address, 'cats') }.should raise_error(SocketError)
+ end
+
+ it 'set the socket to binmode' do
+ @client = TCPSocket.new(ip_address, @port)
+ @client.binmode?.should be_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
+
+ 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..ce66d5ff8f
--- /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 be_an_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/partially_closable_spec.rb b/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb
index 9416d5c71d..a381627a39 100644
--- a/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb
+++ b/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb
@@ -1,4 +1,4 @@
-require_relative '../../../spec_helper'
+require_relative '../spec_helper'
require_relative '../fixtures/classes'
require_relative '../shared/partially_closable_sockets'
diff --git a/spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb b/spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb
index 23bba526e8..bfd815c658 100644
--- a/spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb
+++ b/spec/ruby/library/socket/tcpsocket/recv_nonblock_spec.rb
@@ -1,4 +1,4 @@
-require_relative '../../../spec_helper'
+require_relative '../spec_helper'
require_relative '../fixtures/classes'
describe "TCPSocket#recv_nonblock" do
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..eb9dabc075
--- /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 be_an_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 c9b0ff16dc..8b728b7522 100644
--- a/spec/ruby/library/socket/tcpsocket/setsockopt_spec.rb
+++ b/spec/ruby/library/socket/tcpsocket/setsockopt_spec.rb
@@ -1,4 +1,4 @@
-require_relative '../../../spec_helper'
+require_relative '../spec_helper'
require_relative '../fixtures/classes'
describe "TCPSocket#setsockopt" do
diff --git a/spec/ruby/library/socket/tcpsocket/shared/new.rb b/spec/ruby/library/socket/tcpsocket/shared/new.rb
index c66479a2d8..818bd69a91 100644
--- a/spec/ruby/library/socket/tcpsocket/shared/new.rb
+++ b/spec/ruby/library/socket/tcpsocket/shared/new.rb
@@ -1,4 +1,4 @@
-require_relative '../../../../spec_helper'
+require_relative '../../spec_helper'
require_relative '../../fixtures/classes'
describe :tcpsocket_new, shared: true do