2016-05-07 08:55:17 -04:00
|
|
|
require "test/unit"
|
|
|
|
require "webrick"
|
|
|
|
require "webrick/ssl"
|
|
|
|
require_relative "utils"
|
2017-10-12 14:50:07 -04:00
|
|
|
require 'timeout'
|
2016-05-07 08:55:17 -04:00
|
|
|
|
|
|
|
class TestWEBrickSSLServer < Test::Unit::TestCase
|
|
|
|
class Echo < WEBrick::GenericServer
|
|
|
|
def run(sock)
|
|
|
|
while line = sock.gets
|
|
|
|
sock << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_self_signed_cert_server
|
2016-05-08 20:05:30 -04:00
|
|
|
assert_self_signed_cert(
|
2016-05-07 08:55:17 -04:00
|
|
|
:SSLEnable => true,
|
2016-05-08 20:05:30 -04:00
|
|
|
:SSLCertName => [["C", "JP"], ["O", "www.ruby-lang.org"], ["CN", "Ruby"]],
|
|
|
|
)
|
2016-05-07 08:55:17 -04:00
|
|
|
end
|
2016-05-07 09:37:18 -04:00
|
|
|
|
|
|
|
def test_self_signed_cert_server_with_string
|
2016-05-08 20:05:30 -04:00
|
|
|
assert_self_signed_cert(
|
2016-05-07 09:37:18 -04:00
|
|
|
:SSLEnable => true,
|
2016-05-08 20:05:30 -04:00
|
|
|
:SSLCertName => "/C=JP/O=www.ruby-lang.org/CN=Ruby",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_self_signed_cert(config)
|
2016-05-07 09:37:18 -04:00
|
|
|
TestWEBrick.start_server(Echo, config){|server, addr, port, log|
|
2016-08-22 22:05:24 -04:00
|
|
|
io = TCPSocket.new(addr, port)
|
|
|
|
sock = OpenSSL::SSL::SSLSocket.new(io)
|
2016-05-07 09:37:18 -04:00
|
|
|
sock.connect
|
|
|
|
sock.puts(server.ssl_context.cert.subject.to_s)
|
|
|
|
assert_equal("/C=JP/O=www.ruby-lang.org/CN=Ruby\n", sock.gets, log.call)
|
|
|
|
sock.close
|
2016-08-22 22:05:24 -04:00
|
|
|
io.close
|
2016-05-07 09:37:18 -04:00
|
|
|
}
|
|
|
|
end
|
2017-10-12 14:50:07 -04:00
|
|
|
|
|
|
|
def test_slow_connect
|
|
|
|
poke = lambda do |io, msg|
|
|
|
|
begin
|
|
|
|
sock = OpenSSL::SSL::SSLSocket.new(io)
|
|
|
|
sock.connect
|
|
|
|
sock.puts(msg)
|
|
|
|
assert_equal "#{msg}\n", sock.gets, msg
|
|
|
|
ensure
|
|
|
|
sock&.close
|
|
|
|
io.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
config = {
|
|
|
|
:SSLEnable => true,
|
|
|
|
:SSLCertName => "/C=JP/O=www.ruby-lang.org/CN=Ruby",
|
|
|
|
}
|
2019-06-13 10:04:41 -04:00
|
|
|
EnvUtil.timeout(10) do
|
2017-10-12 14:50:07 -04:00
|
|
|
TestWEBrick.start_server(Echo, config) do |server, addr, port, log|
|
|
|
|
outer = TCPSocket.new(addr, port)
|
|
|
|
inner = TCPSocket.new(addr, port)
|
|
|
|
poke.call(inner, 'fast TLS negotiation')
|
|
|
|
poke.call(outer, 'slow TLS negotiation')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-05-07 08:55:17 -04:00
|
|
|
end
|