[前][次][番号順一覧][スレッド一覧]

ruby-changes:44550

From: shugo <ko1@a...>
Date: Sun, 6 Nov 2016 11:50:53 +0900 (JST)
Subject: [ruby-changes:44550] shugo:r56623 (trunk): * test/net/smtp/test_smtp.rb (test_tls_connect, test_tls_connect):

shugo	2016-11-06 11:50:51 +0900 (Sun, 06 Nov 2016)

  New Revision: 56623

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56623

  Log:
    * test/net/smtp/test_smtp.rb (test_tls_connect, test_tls_connect):
      use Socket.tcp_server_sockets in case localhost is resolved to ::1.

  Modified files:
    trunk/ChangeLog
    trunk/test/net/smtp/test_smtp.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56622)
+++ ChangeLog	(revision 56623)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Nov  6 11:48:55 2016  Shugo Maeda  <shugo@r...>
+
+	* test/net/smtp/test_smtp.rb (test_tls_connect, test_tls_connect):
+	  use Socket.tcp_server_sockets in case localhost is resolved to ::1.
+
 Sun Nov  6 11:49:47 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* lib/irb.rb (IRB::Irb#run): split from IRB.start.
Index: test/net/smtp/test_smtp.rb
===================================================================
--- test/net/smtp/test_smtp.rb	(revision 56622)
+++ test/net/smtp/test_smtp.rb	(revision 56623)
@@ -104,7 +104,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/test/net/smtp/test_smtp.rb#L104
     end
 
     def test_tls_connect
-      server = TCPServer.new("127.0.0.1", 0)
+      servers = Socket.tcp_server_sockets("localhost", 0)
       ctx = OpenSSL::SSL::SSLContext.new
       ctx.ca_file = CA_FILE
       ctx.key = File.open(SERVER_KEY) { |f|
@@ -113,36 +113,38 @@ module Net https://github.com/ruby/ruby/blob/trunk/test/net/smtp/test_smtp.rb#L113
       ctx.cert = File.open(SERVER_CERT) { |f|
         OpenSSL::X509::Certificate.new(f)
       }
-      ssl_server = OpenSSL::SSL::SSLServer.new(server, ctx)
       begin
         sock = nil
         Thread.start do
-          sock = ssl_server.accept
+          s = accept(servers)
+          sock = OpenSSL::SSL::SSLSocket.new(s, ctx)
+          sock.sync_close = true
+          sock.accept
           sock.write("220 localhost Service ready\r\n")
           sock.gets
           sock.write("250 localhost\r\n")
           sock.gets
           sock.write("221 localhost Service closing transmission channel\r\n")
         end
-        smtp = Net::SMTP.new("localhost", server.addr[1])
+        smtp = Net::SMTP.new("localhost", servers[0].local_address.ip_port)
         smtp.enable_tls
         smtp.open_timeout = 0.1
         smtp.start do
         end
       ensure
         sock.close if sock
-        ssl_server.close
+        servers.each(&:close)
       end
     end
 
     def test_tls_connect_timeout
-      server = TCPServer.new("127.0.0.1", 0)
+      servers = Socket.tcp_server_sockets("localhost", 0)
       begin
         sock = nil
         Thread.start do
-          sock = server.accept
+          sock = accept(servers)
         end
-        smtp = Net::SMTP.new("127.0.0.1", server.addr[1])
+        smtp = Net::SMTP.new("localhost", servers[0].local_address.ip_port)
         smtp.enable_tls
         smtp.open_timeout = 0.1
         assert_raise(Net::OpenTimeout) do
@@ -151,7 +153,20 @@ module Net https://github.com/ruby/ruby/blob/trunk/test/net/smtp/test_smtp.rb#L153
         end
       ensure
         sock.close if sock
-        server.close
+        servers.each(&:close)
+      end
+    end
+
+    private
+
+    def accept(servers)
+      loop do
+        readable, = IO.select(servers.map(&:to_io))
+        readable.each do |r|
+          sock, addr = r.accept_nonblock(exception: false)
+          next if sock == :wait_readable
+          return sock
+        end
       end
     end
   end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]