2018-08-03 12:19:40 -04:00
|
|
|
require_relative '../../spec_helper'
|
2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../fixtures/classes'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
|
|
|
describe :tcpsocket_new, shared: true do
|
|
|
|
it "requires a hostname and a port as arguments" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { TCPSocket.send(@method) }.should raise_error(ArgumentError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "refuses the connection when there is no server to connect to" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> do
|
2017-05-16 08:29:30 -04:00
|
|
|
TCPSocket.send(@method, SocketSpecs.hostname, SocketSpecs.reserved_unused_port)
|
2017-05-16 09:57:18 -04:00
|
|
|
end.should raise_error(SystemCallError) {|e|
|
|
|
|
[Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL].should include(e.class)
|
|
|
|
}
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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 "silently ignores 'nil' as the third parameter" do
|
2017-05-27 17:55:02 -04:00
|
|
|
@socket = TCPSocket.send(@method, @hostname, @server.port, nil)
|
2017-05-07 08:04:49 -04:00
|
|
|
@socket.should be_an_instance_of(TCPSocket)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "connects to a listening server with host and port" do
|
2017-05-27 17:55:02 -04:00
|
|
|
@socket = TCPSocket.send(@method, @hostname, @server.port)
|
2017-05-07 08:04:49 -04:00
|
|
|
@socket.should be_an_instance_of(TCPSocket)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "connects to a server when passed local_host argument" do
|
2017-05-27 17:55:02 -04:00
|
|
|
@socket = TCPSocket.send(@method, @hostname, @server.port, @hostname)
|
2017-05-07 08:04:49 -04:00
|
|
|
@socket.should be_an_instance_of(TCPSocket)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "connects to a server when passed local_host and local_port arguments" do
|
2017-06-15 08:48:52 -04:00
|
|
|
server = TCPServer.new(SocketSpecs.hostname, 0)
|
|
|
|
begin
|
|
|
|
available_port = server.addr[1]
|
|
|
|
ensure
|
|
|
|
server.close
|
|
|
|
end
|
2017-05-27 17:55:02 -04:00
|
|
|
@socket = TCPSocket.send(@method, @hostname, @server.port,
|
2017-06-15 08:48:52 -04:00
|
|
|
@hostname, available_port)
|
2017-05-07 08:04:49 -04:00
|
|
|
@socket.should be_an_instance_of(TCPSocket)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has an address once it has connected to a listening server" do
|
2017-05-27 17:55:02 -04:00
|
|
|
@socket = TCPSocket.send(@method, @hostname, @server.port)
|
2017-05-07 08:04:49 -04:00
|
|
|
@socket.should be_an_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
|
|
|
|
# will return AF_INET6. At least this check will weed out clearly
|
|
|
|
# erroneous values.
|
|
|
|
@socket.addr[0].should =~ /^AF_INET6?/
|
|
|
|
|
|
|
|
case @socket.addr[0]
|
|
|
|
when 'AF_INET'
|
|
|
|
@socket.addr[3].should == SocketSpecs.addr(:ipv4)
|
|
|
|
when 'AF_INET6'
|
|
|
|
@socket.addr[3].should == SocketSpecs.addr(:ipv6)
|
|
|
|
end
|
|
|
|
|
2018-09-25 06:41:16 -04:00
|
|
|
@socket.addr[1].should be_kind_of(Integer)
|
2017-05-07 08:04:49 -04:00
|
|
|
@socket.addr[2].should =~ /^#{@hostname}/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|