diff options
Diffstat (limited to 'spec/ruby/library/socket/ipsocket')
| -rw-r--r-- | spec/ruby/library/socket/ipsocket/addr_spec.rb | 73 | ||||
| -rw-r--r-- | spec/ruby/library/socket/ipsocket/getaddress_spec.rb | 21 | ||||
| -rw-r--r-- | spec/ruby/library/socket/ipsocket/inspect_spec.rb | 24 | ||||
| -rw-r--r-- | spec/ruby/library/socket/ipsocket/peeraddr_spec.rb | 74 | ||||
| -rw-r--r-- | spec/ruby/library/socket/ipsocket/recvfrom_spec.rb | 119 |
5 files changed, 288 insertions, 23 deletions
diff --git a/spec/ruby/library/socket/ipsocket/addr_spec.rb b/spec/ruby/library/socket/ipsocket/addr_spec.rb index 2184082c51..eb16a8efdf 100644 --- a/spec/ruby/library/socket/ipsocket/addr_spec.rb +++ b/spec/ruby/library/socket/ipsocket/addr_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) +require_relative '../spec_helper' +require_relative '../fixtures/classes' describe "Socket::IPSocket#addr" do before :each do @@ -17,7 +17,7 @@ describe "Socket::IPSocket#addr" do BasicSocket.do_not_reverse_lookup = false addrinfo = @socket.addr addrinfo[0].should == "AF_INET" - addrinfo[1].should be_kind_of(Integer) + addrinfo[1].should.is_a?(Integer) addrinfo[2].should == SocketSpecs.hostname addrinfo[3].should == "127.0.0.1" end @@ -27,7 +27,7 @@ describe "Socket::IPSocket#addr" do BasicSocket.do_not_reverse_lookup = true addrinfo = @socket.addr addrinfo[0].should == "AF_INET" - addrinfo[1].should be_kind_of(Integer) + addrinfo[1].should.is_a?(Integer) addrinfo[2].should == "127.0.0.1" addrinfo[3].should == "127.0.0.1" end @@ -35,8 +35,71 @@ describe "Socket::IPSocket#addr" do it "returns an address in the array if passed false" do addrinfo = @socket.addr(false) addrinfo[0].should == "AF_INET" - addrinfo[1].should be_kind_of(Integer) + addrinfo[1].should.is_a?(Integer) addrinfo[2].should == "127.0.0.1" addrinfo[3].should == "127.0.0.1" end end + +describe 'Socket::IPSocket#addr' do + SocketSpecs.each_ip_protocol do |family, ip_address, family_name| + before do + @server = TCPServer.new(ip_address, 0) + @port = @server.connect_address.ip_port + end + + after do + @server.close + end + + describe 'without reverse lookups' do + before do + @hostname = Socket.getaddrinfo(ip_address, nil)[0][2] + end + + it 'returns an Array containing address information' do + @server.addr.should == [family_name, @port, @hostname, ip_address] + end + end + + describe 'with reverse lookups' do + before do + @hostname = Socket.getaddrinfo(ip_address, nil, nil, 0, 0, 0, true)[0][2] + end + + describe 'using true as the argument' do + it 'returns an Array containing address information' do + @server.addr(true).should == [family_name, @port, @hostname, ip_address] + end + end + + describe 'using :hostname as the argument' do + it 'returns an Array containing address information' do + @server.addr(:hostname).should == [family_name, @port, @hostname, ip_address] + end + end + + describe 'using :cats as the argument' do + it 'raises ArgumentError' do + -> { @server.addr(:cats) }.should.raise(ArgumentError) + end + end + end + + describe 'with do_not_reverse_lookup disabled on socket level' do + before do + @server.do_not_reverse_lookup = false + + @hostname = Socket.getaddrinfo(ip_address, nil, nil, 0, 0, 0, true)[0][2] + end + + after do + @server.do_not_reverse_lookup = true + end + + it 'returns an Array containing address information' do + @server.addr.should == [family_name, @port, @hostname, ip_address] + end + end + end +end diff --git a/spec/ruby/library/socket/ipsocket/getaddress_spec.rb b/spec/ruby/library/socket/ipsocket/getaddress_spec.rb index c574c7d267..d302cd6a8a 100644 --- a/spec/ruby/library/socket/ipsocket/getaddress_spec.rb +++ b/spec/ruby/library/socket/ipsocket/getaddress_spec.rb @@ -1,8 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) +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 @@ -11,17 +10,19 @@ describe "Socket::IPSocket#getaddress" do it "returns the IP address when passed an IP" do IPSocket.getaddress("127.0.0.1").should == "127.0.0.1" IPSocket.getaddress("0.0.0.0").should == "0.0.0.0" + 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. - quarantine! do - it "raises an error on unknown hostnames" do - lambda { - IPSocket.getaddress("rubyspecdoesntexist.fallingsnow.net") - }.should raise_error(SocketError) - end + it "raises an error on unknown hostnames" do + -> { + IPSocket.getaddress("rubyspecdoesntexist.ruby-lang.org") + }.should.raise(SocketError) end - end diff --git a/spec/ruby/library/socket/ipsocket/inspect_spec.rb b/spec/ruby/library/socket/ipsocket/inspect_spec.rb new file mode 100644 index 0000000000..85780a16f6 --- /dev/null +++ b/spec/ruby/library/socket/ipsocket/inspect_spec.rb @@ -0,0 +1,24 @@ +require_relative '../spec_helper' + +describe 'IPSocket#inspect' do + it "returns a String with the fd, family, address and port for TCPSocket" do + @server = TCPServer.new("127.0.0.1", 0) + @socket = TCPSocket.new("127.0.0.1", @server.addr[1]) + port = @socket.addr[1] + + @socket.inspect.should == "#<TCPSocket:fd #{@socket.fileno}, AF_INET, 127.0.0.1, #{port}>" + ensure + @socket&.close + @server&.close + end + + it 'returns a String with the fd, family, address and port for UDPSocket' do + @socket = UDPSocket.new + @socket.bind('127.0.0.1', 0) + port = @socket.addr[1] + + @socket.inspect.should == "#<UDPSocket:fd #{@socket.fileno}, AF_INET, 127.0.0.1, #{port}>" + ensure + @socket&.close + end +end diff --git a/spec/ruby/library/socket/ipsocket/peeraddr_spec.rb b/spec/ruby/library/socket/ipsocket/peeraddr_spec.rb index 27529c3d1c..b79222000a 100644 --- a/spec/ruby/library/socket/ipsocket/peeraddr_spec.rb +++ b/spec/ruby/library/socket/ipsocket/peeraddr_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) +require_relative '../spec_helper' +require_relative '../fixtures/classes' describe "Socket::IPSocket#peeraddr" do before :each do @@ -16,9 +16,9 @@ describe "Socket::IPSocket#peeraddr" do end it "raises error if socket is not connected" do - lambda { + -> { @server.peeraddr - }.should raise_error(Errno::ENOTCONN) + }.should.raise(Errno::ENOTCONN) end it "returns an array of information on the peer" do @@ -49,3 +49,69 @@ describe "Socket::IPSocket#peeraddr" do addrinfo[3].should == "127.0.0.1" end end + +describe 'Socket::IPSocket#peeraddr' do + SocketSpecs.each_ip_protocol do |family, ip_address, family_name| + before do + @server = TCPServer.new(ip_address, 0) + @port = @server.connect_address.ip_port + @client = TCPSocket.new(ip_address, @port) + end + + after do + @client.close + @server.close + end + + describe 'without reverse lookups' do + before do + @hostname = Socket.getaddrinfo(ip_address, nil)[0][2] + end + + it 'returns an Array containing address information' do + @client.peeraddr.should == [family_name, @port, @hostname, ip_address] + end + end + + describe 'with reverse lookups' do + before do + @hostname = Socket.getaddrinfo(ip_address, nil, nil, 0, 0, 0, true)[0][2] + end + + describe 'using true as the argument' do + it 'returns an Array containing address information' do + @client.peeraddr(true).should == [family_name, @port, @hostname, ip_address] + end + end + + describe 'using :hostname as the argument' do + it 'returns an Array containing address information' do + @client.peeraddr(:hostname).should == [family_name, @port, @hostname, ip_address] + end + end + + describe 'using :cats as the argument' do + it 'raises ArgumentError' do + -> { @client.peeraddr(:cats) }.should.raise(ArgumentError) + end + end + end + + describe 'with do_not_reverse_lookup disabled on socket level' do + before do + @client.do_not_reverse_lookup = false + + @hostname = Socket.getaddrinfo(ip_address, nil, nil, 0, 0, 0, true)[0][2] + @port = @client.local_address.ip_port + end + + after do + @client.do_not_reverse_lookup = true + end + + it 'returns an Array containing address information' do + @client.addr.should == [family_name, @port, @hostname, ip_address] + end + end + end +end diff --git a/spec/ruby/library/socket/ipsocket/recvfrom_spec.rb b/spec/ruby/library/socket/ipsocket/recvfrom_spec.rb index 54f150decf..7af0078be1 100644 --- a/spec/ruby/library/socket/ipsocket/recvfrom_spec.rb +++ b/spec/ruby/library/socket/ipsocket/recvfrom_spec.rb @@ -1,8 +1,7 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/classes', __FILE__) +require_relative '../spec_helper' +require_relative '../fixtures/classes' describe "Socket::IPSocket#recvfrom" do - before :each do @server = TCPServer.new("127.0.0.1", 0) @port = @server.addr[1] @@ -65,8 +64,120 @@ 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 + + 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 == nil + + @client.close + + t.value.should == nil + 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 + @server = UDPSocket.new(family) + @client = UDPSocket.new(family) + + @server.bind(ip_address, 0) + @client.connect(ip_address, @server.connect_address.ip_port) + + @hostname = Socket.getaddrinfo(ip_address, nil)[0][2] + end + + after do + @client.close + @server.close + end + + it 'returns an Array containing up to N bytes and address information' do + @client.write('hello') + + port = @client.local_address.ip_port + ret = @server.recvfrom(2) + + ret.should == ['he', [family_name, port, @hostname, ip_address]] + end + + it 'allows specifying of flags when receiving data' do + @client.write('hello') + + @server.recvfrom(2, Socket::MSG_PEEK)[0].should == 'he' + + @server.recvfrom(2)[0].should == 'he' + end + + describe 'using reverse lookups' do + before do + @server.do_not_reverse_lookup = false + + @hostname = Socket.getaddrinfo(ip_address, nil, 0, 0, 0, 0, true)[0][2] + end + + it 'includes the hostname in the address Array' do + @client.write('hello') + + port = @client.local_address.ip_port + ret = @server.recvfrom(2) + + ret.should == ['he', [family_name, port, @hostname, ip_address]] + end + end + end end |
