ruby-changes:52245
From: eregon <ko1@a...>
Date: Sun, 19 Aug 2018 04:35:09 +0900 (JST)
Subject: [ruby-changes:52245] eregon:r64452 (trunk): Re-add socket specs
eregon 2018-08-19 04:35:02 +0900 (Sun, 19 Aug 2018) New Revision: 64452 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64452 Log: Re-add socket specs * This reverts commit df9521fd043df1fb862e46f9b1af83223f16eb2d: "Remove failing spec files" * Platform guards follow in the next commits. Added files: trunk/spec/ruby/library/socket/addrinfo/foreach_spec.rb trunk/spec/ruby/library/socket/addrinfo/getnameinfo_spec.rb trunk/spec/ruby/library/socket/addrinfo/initialize_spec.rb trunk/spec/ruby/library/socket/constants/constants_spec.rb trunk/spec/ruby/library/socket/socket/getaddrinfo_spec.rb trunk/spec/ruby/library/socket/socket/gethostbyaddr_spec.rb trunk/spec/ruby/library/socket/socket/getnameinfo_spec.rb trunk/spec/ruby/library/socket/socket/getservbyname_spec.rb trunk/spec/ruby/library/socket/socket/pack_sockaddr_in_spec.rb trunk/spec/ruby/library/socket/socket/sockaddr_in_spec.rb Index: spec/ruby/library/socket/socket/gethostbyaddr_spec.rb =================================================================== --- spec/ruby/library/socket/socket/gethostbyaddr_spec.rb (nonexistent) +++ spec/ruby/library/socket/socket/gethostbyaddr_spec.rb (revision 64452) @@ -0,0 +1,121 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/socket/socket/gethostbyaddr_spec.rb#L1 +require_relative '../spec_helper' +require_relative '../fixtures/classes' +require 'ipaddr' + +describe 'Socket.gethostbyaddr' do + describe 'using an IPv4 address' do + before do + @addr = IPAddr.new('127.0.0.1').hton + end + + describe 'without an explicit address family' do + it 'returns an Array' do + Socket.gethostbyaddr(@addr).should be_an_instance_of(Array) + end + + describe 'the returned Array' do + before do + @array = Socket.gethostbyaddr(@addr) + 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 + @array[1].should be_an_instance_of(Array) + + @array[1].each do |val| + val.should be_an_instance_of(String) + end + end + + it 'includes the address type as the 3rd value' do + @array[2].should == Socket::AF_INET + end + + it 'includes all address strings as the remaining values' do + @array[3].should == @addr + + @array[4..-1].each do |val| + val.should be_an_instance_of(String) + end + end + end + end + + describe 'with an explicit address family' do + it 'returns an Array when using a Fixnum as the address family' do + 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) + end + + it 'raises SocketError when the address is not supported by the family' do + lambda { Socket.gethostbyaddr(@addr, :INET6) }.should raise_error(SocketError) + end + end + end + + guard -> { SocketSpecs.ipv6_available? } do + describe 'using an IPv6 address' do + before do + @addr = IPAddr.new('::1').hton + end + + describe 'without an explicit address family' do + it 'returns an Array' do + Socket.gethostbyaddr(@addr).should be_an_instance_of(Array) + end + + describe 'the returned Array' do + before do + @array = Socket.gethostbyaddr(@addr) + end + + it 'includes the hostname as the first value' do + @array[0].should == SocketSpecs.hostname_reverse_lookup("::1") + end + + it 'includes the aliases as the 2nd value' do + @array[1].should be_an_instance_of(Array) + + @array[1].each do |val| + val.should be_an_instance_of(String) + end + end + + it 'includes the address type as the 3rd value' do + @array[2].should == Socket::AF_INET6 + end + + it 'includes all address strings as the remaining values' do + @array[3].should be_an_instance_of(String) + + @array[4..-1].each do |val| + val.should be_an_instance_of(String) + end + end + end + end + + describe 'with an explicit address family' do + it 'returns an Array when using a Fixnum as the address family' do + 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) + end + + platform_is_not :windows do + it 'raises SocketError when the address is not supported by the family' do + lambda { Socket.gethostbyaddr(@addr, :INET) }.should raise_error(SocketError) + end + end + end + end + end +end Index: spec/ruby/library/socket/socket/getservbyname_spec.rb =================================================================== --- spec/ruby/library/socket/socket/getservbyname_spec.rb (nonexistent) +++ spec/ruby/library/socket/socket/getservbyname_spec.rb (revision 64452) @@ -0,0 +1,32 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/socket/socket/getservbyname_spec.rb#L1 +require_relative '../spec_helper' +require_relative '../fixtures/classes' + +describe "Socket#getservbyname" do + it "returns the port for service 'discard'" do + Socket.getservbyname('discard').should == 9 + end + + it "returns the port for service 'discard' with protocol 'tcp'" do + Socket.getservbyname('discard', 'tcp').should == 9 + end + + it 'returns the port for service "http"' do + Socket.getservbyname('http').should == 80 + end + + it 'returns the port for service "http" with protocol "tcp"' do + Socket.getservbyname('http', 'tcp').should == 80 + end + + it "returns the port for service 'domain' with protocol 'udp'" do + Socket.getservbyname('domain', 'udp').should == 53 + end + + it "returns the port for service 'daytime'" do + Socket.getservbyname('daytime').should == 13 + end + + it "raises a SocketError when the service or port is invalid" do + lambda { Socket.getservbyname('invalid') }.should raise_error(SocketError) + end +end Index: spec/ruby/library/socket/socket/sockaddr_in_spec.rb =================================================================== --- spec/ruby/library/socket/socket/sockaddr_in_spec.rb (nonexistent) +++ spec/ruby/library/socket/socket/sockaddr_in_spec.rb (revision 64452) @@ -0,0 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/socket/socket/sockaddr_in_spec.rb#L1 +require_relative '../spec_helper' +require_relative '../fixtures/classes' +require_relative '../shared/pack_sockaddr' + +describe "Socket#sockaddr_in" do + it_behaves_like :socket_pack_sockaddr_in, :sockaddr_in +end Index: spec/ruby/library/socket/socket/getaddrinfo_spec.rb =================================================================== --- spec/ruby/library/socket/socket/getaddrinfo_spec.rb (nonexistent) +++ spec/ruby/library/socket/socket/getaddrinfo_spec.rb (revision 64452) @@ -0,0 +1,377 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/socket/socket/getaddrinfo_spec.rb#L1 +require_relative '../spec_helper' +require_relative '../fixtures/classes' + +describe "Socket.getaddrinfo" do + before :each do + @do_not_reverse_lookup = BasicSocket.do_not_reverse_lookup + BasicSocket.do_not_reverse_lookup = true + end + + after :each do + BasicSocket.do_not_reverse_lookup = @do_not_reverse_lookup + end + + platform_is_not :solaris, :windows do + it "gets the address information" do + expected = [] + # The check for AP_INET6's class is needed because ipaddr.rb adds + # fake AP_INET6 even in case when IPv6 is not really supported. + # Without such check, this test might fail when ipaddr was required + # by some other specs. + if (Socket.constants.include? 'AF_INET6') && + (Socket::AF_INET6.class != Object) then + expected.concat [ + ['AF_INET6', 9, SocketSpecs.hostname, '::1', Socket::AF_INET6, + Socket::SOCK_DGRAM, Socket::IPPROTO_UDP], + ['AF_INET6', 9, SocketSpecs.hostname, '::1', Socket::AF_INET6, + Socket::SOCK_STREAM, Socket::IPPROTO_TCP], + ['AF_INET6', 9, SocketSpecs.hostname, 'fe80::1%lo0', Socket::AF_INET6, + Socket::SOCK_DGRAM, Socket::IPPROTO_UDP], + ['AF_INET6', 9, SocketSpecs.hostname, 'fe80::1%lo0', Socket::AF_INET6, + Socket::SOCK_STREAM, Socket::IPPROTO_TCP], + ] + end + + expected.concat [ + ['AF_INET', 9, SocketSpecs.hostname, '127.0.0.1', Socket::AF_INET, + Socket::SOCK_DGRAM, Socket::IPPROTO_UDP], + ['AF_INET', 9, SocketSpecs.hostname, '127.0.0.1', Socket::AF_INET, + Socket::SOCK_STREAM, Socket::IPPROTO_TCP], + ] + + addrinfo = Socket.getaddrinfo SocketSpecs.hostname, 'discard' + addrinfo.each do |a| + case a.last + when Socket::IPPROTO_UDP, Socket::IPPROTO_TCP + expected.should include(a) + else + # don't check this. It's some weird protocol we don't know about + # so we can't spec it. + end + end + end + + # #getaddrinfo will return a INADDR_ANY address (0.0.0.0 or "::") + # if it's a passive socket. In the case of non-passive + # sockets (AI_PASSIVE not set) it should return the loopback + # address (127.0.0.1 or "::1"). + + it "accepts empty addresses for IPv4 passive sockets" do + res = Socket.getaddrinfo(nil, "discard", + Socket::AF_INET, + Socket::SOCK_STREAM, + Socket::IPPROTO_TCP, + Socket::AI_PASSIVE) + + expected = [["AF_INET", 9, "0.0.0.0", "0.0.0.0", Socket::AF_INET, Socket::SOCK_STREAM, Socket::IPPROTO_TCP]] + res.should == expected + end + + it "accepts empty addresses for IPv4 non-passive sockets" do + res = Socket.getaddrinfo(nil, "discard", + Socket::AF_INET, + Socket::SOCK_STREAM, + Socket::IPPROTO_TCP, + 0) + + expected = [["AF_INET", 9, "127.0.0.1", "127.0.0.1", Socket::AF_INET, Socket::SOCK_STREAM, Socket::IPPROTO_TCP]] + res.should == expected + end + + + it "accepts empty addresses for IPv6 passive sockets" do + res = Socket.getaddrinfo(nil, "discard", + Socket::AF_INET6, + Socket::SOCK_STREAM, + Socket::IPPROTO_TCP, + Socket::AI_PASSIVE) + + expected = [ + ["AF_INET6", 9, "::", "::", Socket::AF_INET6, Socket::SOCK_STREAM, Socket::IPPROTO_TCP], + ["AF_INET6", 9, "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0:0", Socket::AF_INET6, Socket::SOCK_STREAM, Socket::IPPROTO_TCP] + ] + res.each { |a| expected.should include(a) } + end + + it "accepts empty addresses for IPv6 non-passive sockets" do + res = Socket.getaddrinfo(nil, "discard", + Socket::AF_INET6, + Socket::SOCK_STREAM, + Socket::IPPROTO_TCP, + 0) + + expected = [ + ["AF_INET6", 9, "::1", "::1", Socket::AF_INET6, Socket::SOCK_STREAM, Socket::IPPROTO_TCP], + ["AF_INET6", 9, "0:0:0:0:0:0:0:1", "0:0:0:0:0:0:0:1", Socket::AF_INET6, Socket::SOCK_STREAM, Socket::IPPROTO_TCP] + ] + res.each { |a| expected.should include(a) } + end + end +end + +describe 'Socket.getaddrinfo' do + describe 'without global reverse lookups' do + it 'returns an Array' do + Socket.getaddrinfo(nil, 'http').should be_an_instance_of(Array) + end + + it 'accepts a Fixnum as the address family' do + array = Socket.getaddrinfo(nil, 'http', Socket::AF_INET)[0] + + array[0].should == 'AF_INET' + array[1].should == 80 + array[2].should == '127.0.0.1' + array[3].should == '127.0.0.1' + array[4].should == Socket::AF_INET + array[5].should be_an_instance_of(Fixnum) + array[6].should be_an_instance_of(Fixnum) + end + + it 'accepts a Fixnum as the address family using IPv6' do + array = Socket.getaddrinfo(nil, 'http', Socket::AF_INET6)[0] + + array[0].should == 'AF_INET6' + array[1].should == 80 + array[2].should == '::1' + array[3].should == '::1' + array[4].should == Socket::AF_INET6 + array[5].should be_an_instance_of(Fixnum) + array[6].should be_an_instance_of(Fixnum) + end + + it 'accepts a Symbol as the address family' do + array = Socket.getaddrinfo(nil, 'http', :INET)[0] + + array[0].should == 'AF_INET' + array[1].should == 80 + array[2].should == '127.0.0.1' + array[3].should == '127.0.0.1' + array[4].should == Socket::AF_INET + array[5].should be_an_instance_of(Fixnum) + array[6].should be_an_instance_of(Fixnum) + end + + it 'accepts a Symbol as the address family using IPv6' do + array = Socket.getaddrinfo(nil, 'http', :INET6)[0] + + array[0].should == 'AF_INET6' + array[1].should == 80 + array[2].should == '::1' + array[3].should == '::1' + array[4].should == Socket::AF_INET6 + array[5].should be_an_instance_of(Fixnum) + array[6].should be_an_instance_of(Fixnum) + end + + it 'accepts a String as the address family' do + array = Socket.getaddrinfo(nil, 'http', 'INET')[0] + + array[0].should == 'AF_INET' + array[1].should == 80 + array[2].should == '127.0.0.1' + array[3].should == '127.0.0.1' + array[4].should == Socket::AF_INET + array[5].should be_an_instance_of(Fixnum) + array[6].should be_an_instance_of(Fixnum) + end + + it 'accepts a String as the address family using IPv6' do + array = Socket.getaddrinfo(nil, 'http', 'INET6')[0] + + array[0].should == 'AF_INET6' + array[1].should == 80 + array[2].should == '::1' + array[3].should == '::1' + array[4].should == Socket::AF_INET6 + array[5].should be_an_instance_of(Fixnum) + array[6].should be_an_instance_of(Fixnum) + end + + it 'accepts an object responding to #to_str as the host' do + dummy = mock(:dummy) + + dummy.stub!(:to_str).and_return('127.0.0.1') + + array = Socket.getaddrinfo(dummy, 'http')[0] + + array[0].should == 'AF_INET' + array[1].should == 80 + array[2].should == '127.0.0.1' + array[3].should == '127.0.0.1' + array[4].should == Socket::AF_INET + array[5].should be_an_instance_of(Fixnum) + array[6].should be_an_instance_of(Fixnum) + end + + it 'accepts an object responding to #to_str as the address family' do + dummy = mock(:dummy) + + dummy.stub!(:to_str).and_return('INET') + + array = Socket.getaddrinfo(nil, 'http', dummy)[0] + + array[0].should == 'AF_INET' + array[1].should == 80 + array[2].should == '127.0.0.1' + array[3].should == '127.0.0.1' + array[4].should == Socket::AF_INET + array[5].should be_an_instance_of(Fixnum) + array[6].should be_an_instance_of(Fixnum) + end + + ipproto_tcp = Socket::IPPROTO_TCP + platform_is :windows do + ipproto_tcp = 0 + end + + it 'accepts a Fixnum as the socket type' do + Socket.getaddrinfo(nil, 'http', :INET, Socket::SOCK_STREAM)[0].should == [ + 'AF_INET', + 80, + '127.0.0.1', + '127.0.0.1', + Socket::AF_INET, + Socket::SOCK_STREAM, + ipproto_tcp + ] + end + + it 'accepts a Symbol as the socket type' do + Socket.getaddrinfo(nil, 'http', :INET, :STREAM)[0].should == [ + 'AF_INET', + 80, + '127.0.0.1', + '127.0.0.1', + Socket::AF_INET, + Socket::SOCK_STREAM, + ipproto_tcp + ] + end + + it 'accepts a String as the socket type' do + Socket.getaddrinfo(nil, 'http', :INET, 'STREAM')[0].should == [ + 'AF_INET', + 80, + '127.0.0.1', + '127.0.0.1', + Socket::AF_INET, + Socket::SOCK_STREAM, + ipproto_tcp + ] + end + + it 'accepts an object responding to #to_str as the socket type' do + dummy = mock(:dummy) + + dummy.stub!(:to_str).and_return('STREAM') + + Socket.getaddrinfo(nil, 'http', :INET, dummy)[0].should == [ + 'AF_INET', + 80, + '127.0.0.1', + '127.0.0.1', + Socket::AF_INET, + Socket::SOCK_STREAM, + ipproto_tcp + ] + end + + platform_is_not :windows do + it 'accepts a Fixnum as the protocol family' do + addr = Socket.getaddrinfo(nil, 'discard', :INET, :DGRAM, Socket::IPPROTO_UDP) + + addr[0].should == [ + 'AF_INET', + 9, + '127.0.0.1', + '127.0.0.1', + Socket::AF_INET, + Socket::SOCK_DGRAM, + Socket::IPPROTO_UDP + ] + end + end + + it 'accepts a Fixnum as the flags' do + addr = Socket.getaddrinfo(nil, 'http', :INET, :STREAM, + Socket::IPPROTO_TCP, Socket::AI_PASSIVE) + + addr[0].should == [ + 'AF_INET', + 80, + '0.0.0.0', + '0.0.0.0', + Socket::AF_INET, + Socket::SOCK_STREAM, + Socket::IPPROTO_TCP + ] + end + + it 'performs a reverse lookup when the reverse_lookup argument is true' do + addr = Socket.getaddrinfo(nil, 'http', :INET, :STREAM, + Socket::IPPROTO_TCP, 0, true)[0] + + addr[0].should == 'AF_INET' + addr[1].should == 80 + + addr[2].should be_an_instance_of(String) + addr[2].should_not == addr[3] + + addr[3].should == '127.0.0.1' + end + + it 'performs a reverse lookup when the reverse_lookup argument is :hostname' do + addr = Socket.getaddrinfo(nil, 'http', :INET, :STREAM, + Socket::IPPROTO_TCP, 0, :hostname)[0] + + addr[0].should == 'AF_INET' + addr[1].should == 80 + + addr[2].should be_an_instance_of(String) + addr[2].should_not == addr[3] + + addr[3].should == '127.0.0.1' + end + + it 'performs a reverse lookup when the reverse_lookup argument is :numeric' do + addr = Socket.getaddrinfo(nil, 'http', :INET, :STREAM, + Socket::IPPROTO_TCP, 0, :numeric)[0] + + addr.should == [ + 'AF_INET', + 80, + '127.0.0.1', + '127.0.0.1', + Socket::AF_INET, + Socket::SOCK_STREAM, + Socket::IPPROTO_TCP + ] + end + end + + describe 'with global reverse lookups' do + before do + @do_not_reverse_lookup = BasicSocket.do_not_reverse_lookup + BasicSocket.do_not_reverse_lookup = false + end + + after do + BasicSocket.do_not_reverse_lookup = @do_not_reverse_lookup + end + + it 'returns an address honoring the global lookup option' do + addr = Socket.getaddrinfo(nil, 'http', :INET)[0] + + addr[0].should == 'AF_INET' + addr[1].should == 80 + + # We don't have control over this value and there's no way to test this + # without relying on Socket.getaddrinfo()'s own behaviour (meaning this + # test would faily any way of the method was not implemented correctly). + addr[2].should be_an_instance_of(String) + addr[2].should_not == addr[3] + + addr[3].should == '127.0.0.1' + end + end +end Index: spec/ruby/library/socket/socket/getnameinfo_spec.rb =================================================================== --- spec/ruby/library/socket/socket/getnameinfo_spec.rb (nonexistent) +++ spec/ruby/library/socket/socket/getnameinfo_spec.rb (revision 64452) @@ -0,0 +1,154 @@ https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/socket/socket/getnameinfo_spec.rb#L1 +require_relative '../spec_helper' +require_relative '../fixtures/classes' + +describe "Socket.getnameinfo" do + before :each do + @reverse_lookup = BasicSocket.do_not_reverse_lookup + BasicSocket.do_not_reverse_lookup = true + end + + after :each do + BasicSocket.do_not_reverse_lookup = @reverse_lookup + end + + it "gets the name information and don't resolve it" do + sockaddr = Socket.sockaddr_in 3333, '127.0.0.1' + name_info = Socket.getnameinfo(sockaddr, Socket::NI_NUMERICHOST | Socket::NI_NUMERICSERV) + name_info.should == ['127.0.0.1', "3333"] + end + + def should_be_valid_dns (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/