summaryrefslogtreecommitdiff
path: root/tool/test/webrick/test_ssl_server.rb
diff options
context:
space:
mode:
authorHiroshi SHIBATA <hsbt@ruby-lang.org>2020-11-02 13:44:28 +0900
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2020-12-10 18:06:25 +0900
commit5dc786bf86bb6e0da2639f88659598ec8b9db30d (patch)
tree87cc4d68088fc7c2616c7c1bef44c36ca10fa01f /tool/test/webrick/test_ssl_server.rb
parent46d3ea2c2569e2e5a9ee3e7e206f07f0f8b693f5 (diff)
Move webrick library into internal test toolchain
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3729
Diffstat (limited to 'tool/test/webrick/test_ssl_server.rb')
-rw-r--r--tool/test/webrick/test_ssl_server.rb67
1 files changed, 67 insertions, 0 deletions
diff --git a/tool/test/webrick/test_ssl_server.rb b/tool/test/webrick/test_ssl_server.rb
new file mode 100644
index 0000000000..4e52598bf5
--- /dev/null
+++ b/tool/test/webrick/test_ssl_server.rb
@@ -0,0 +1,67 @@
+require "test/unit"
+require "webrick"
+require "webrick/ssl"
+require_relative "utils"
+require 'timeout'
+
+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
+ assert_self_signed_cert(
+ :SSLEnable => true,
+ :SSLCertName => [["C", "JP"], ["O", "www.ruby-lang.org"], ["CN", "Ruby"]],
+ )
+ end
+
+ def test_self_signed_cert_server_with_string
+ assert_self_signed_cert(
+ :SSLEnable => true,
+ :SSLCertName => "/C=JP/O=www.ruby-lang.org/CN=Ruby",
+ )
+ end
+
+ def assert_self_signed_cert(config)
+ TestWEBrick.start_server(Echo, config){|server, addr, port, log|
+ io = TCPSocket.new(addr, port)
+ sock = OpenSSL::SSL::SSLSocket.new(io)
+ 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
+ io.close
+ }
+ end
+
+ 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",
+ }
+ EnvUtil.timeout(10) do
+ 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
+end