diff options
author | Zachary Scott <e@zzak.io> | 2015-01-06 16:35:02 -0800 |
---|---|---|
committer | Zachary Scott <e@zzak.io> | 2015-01-06 16:35:02 -0800 |
commit | 8c20f4435f1e09ff797bf6683c47d4a9d1869bca (patch) | |
tree | 44a98a14570834e5ecb89b82ced6f796228fff64 /test/utils.rb | |
parent | 2953dfd4ad925a669110fed1993d6e83b24e420f (diff) | |
download | ruby-openssl-8c20f4435f1e09ff797bf6683c47d4a9d1869bca.tar.gz |
Sync tests with trunk
Diffstat (limited to 'test/utils.rb')
-rw-r--r-- | test/utils.rb | 139 |
1 files changed, 60 insertions, 79 deletions
diff --git a/test/utils.rb b/test/utils.rb index 607daea8..1da3bcf9 100644 --- a/test/utils.rb +++ b/test/utils.rb @@ -12,7 +12,6 @@ require "digest/md5" require 'tempfile' require "rbconfig" require "socket" -require_relative 'envutil' module OpenSSL::TestUtils TEST_KEY_RSA1024 = OpenSSL::PKey::RSA.new <<-_end_of_pem_ @@ -190,8 +189,6 @@ AQjjxMXhwULlmuR/K+WwlaZPiLIBYalLAZQ7ZbOPeVkJ8ePao0eLAgEC class OpenSSL::SSLTestCase < Test::Unit::TestCase RUBY = EnvUtil.rubybin - SSL_SERVER = File.join(File.dirname(__FILE__), "ssl_server.rb") - PORT = 20443 ITERATIONS = ($0 == __FILE__) ? 100 : 10 def setup @@ -240,88 +237,85 @@ AQjjxMXhwULlmuR/K+WwlaZPiLIBYalLAZQ7ZbOPeVkJ8ePao0eLAgEC ssl.close rescue nil end - def server_loop(ctx, ssls, server_proc, threads) + def server_loop(ctx, ssls, stop_pipe_r, ignore_listener_error, server_proc, threads) loop do ssl = nil begin + readable, = IO.select([ssls, stop_pipe_r]) + if readable.include? stop_pipe_r + return + end ssl = ssls.accept rescue OpenSSL::SSL::SSLError - retry + if ignore_listener_error + retry + else + raise + end end th = Thread.start do - Thread.current.abort_on_exception = true server_proc.call(ctx, ssl) end threads << th end rescue Errno::EBADF, IOError, Errno::EINVAL, Errno::ECONNABORTED, Errno::ENOTSOCK, Errno::ECONNRESET - end - - def start_server(port0, verify_mode, start_immediately, args = {}, &block) - ctx_proc = args[:ctx_proc] - server_proc = args[:server_proc] - server_proc ||= method(:readwrite_loop) - threads = [] - - store = OpenSSL::X509::Store.new - store.add_cert(@ca_cert) - store.purpose = OpenSSL::X509::PURPOSE_SSL_CLIENT - ctx = OpenSSL::SSL::SSLContext.new - ctx.cert_store = store - #ctx.extra_chain_cert = [ ca_cert ] - ctx.cert = @svr_cert - ctx.key = @svr_key - ctx.tmp_dh_callback = proc { OpenSSL::TestUtils::TEST_KEY_DH1024 } - ctx.verify_mode = verify_mode - ctx_proc.call(ctx) if ctx_proc - - Socket.do_not_reverse_lookup = true - tcps = nil - port = port0 - begin - tcps = TCPServer.new("127.0.0.1", port) - rescue Errno::EADDRINUSE - port += 1 - retry + if !ignore_listener_error + raise end + end - ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx) - ssls.start_immediately = start_immediately - - begin - server = Thread.new do - Thread.current.abort_on_exception = true - server_loop(ctx, ssls, server_proc, threads) - end + def start_server(verify_mode, start_immediately, args = {}, &block) + IO.pipe {|stop_pipe_r, stop_pipe_w| + ctx_proc = args[:ctx_proc] + server_proc = args[:server_proc] + ignore_listener_error = args.fetch(:ignore_listener_error, false) + server_proc ||= method(:readwrite_loop) + + store = OpenSSL::X509::Store.new + store.add_cert(@ca_cert) + store.purpose = OpenSSL::X509::PURPOSE_SSL_CLIENT + ctx = OpenSSL::SSL::SSLContext.new + ctx.cert_store = store + #ctx.extra_chain_cert = [ ca_cert ] + ctx.cert = @svr_cert + ctx.key = @svr_key + ctx.tmp_dh_callback = proc { OpenSSL::TestUtils::TEST_KEY_DH1024 } + ctx.verify_mode = verify_mode + ctx_proc.call(ctx) if ctx_proc + + Socket.do_not_reverse_lookup = true + tcps = nil + tcps = TCPServer.new("127.0.0.1", 0) + port = tcps.connect_address.ip_port + + ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx) + ssls.start_immediately = start_immediately + + threads = [] + begin + server = Thread.new do + begin + server_loop(ctx, ssls, stop_pipe_r, ignore_listener_error, server_proc, threads) + ensure + tcps.close + end + end + threads.unshift server - $stderr.printf("%s started: pid=%d port=%d\n", SSL_SERVER, $$, port) if $DEBUG + $stderr.printf("SSL server started: pid=%d port=%d\n", $$, port) if $DEBUG - block.call(server, port.to_i) - ensure - begin - begin - tcps.shutdown - rescue Errno::ENOTCONN - # when `Errno::ENOTCONN: Socket is not connected' on some platforms, - # call #close instead of #shutdown. - tcps.close - tcps = nil - end if (tcps) - if (server) - server.join(5) - if server.alive? - server.join - flunk("TCPServer was closed and SSLServer is still alive") unless $! + client = Thread.new do + begin + block.call(server, port.to_i) + ensure + stop_pipe_w.close end end + threads.unshift client ensure - tcps.close if (tcps) + assert_join_threads(threads) end - end - ensure - threads.each {|th| - th.join } end @@ -333,18 +327,5 @@ AQjjxMXhwULlmuR/K+WwlaZPiLIBYalLAZQ7ZbOPeVkJ8ePao0eLAgEC end end -end if defined?(OpenSSL) - -module Test - module Unit - class TestCase - def message msg = nil, ending = ".", &default - proc { - msg = msg.call.chomp(".") if Proc === msg - custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty? - "#{custom_message}#{default.call}#{ending}" - } - end - end - end -end +end if defined?(OpenSSL::OPENSSL_LIBRARY_VERSION) and + /\AOpenSSL +0\./ !~ OpenSSL::OPENSSL_LIBRARY_VERSION |