2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2007-12-20 11:21:22 -05:00
|
|
|
require "test/unit"
|
|
|
|
begin
|
|
|
|
require 'net/https'
|
2007-12-23 14:05:39 -05:00
|
|
|
require 'stringio'
|
2011-01-12 02:26:18 -05:00
|
|
|
require 'timeout'
|
2007-12-23 14:05:39 -05:00
|
|
|
require File.expand_path("utils", File.dirname(__FILE__))
|
2007-12-20 11:21:22 -05:00
|
|
|
rescue LoadError
|
|
|
|
# should skip this test
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestNetHTTPS < Test::Unit::TestCase
|
|
|
|
include TestNetHTTPUtils
|
|
|
|
|
2016-11-29 10:48:45 -05:00
|
|
|
def self.fixture(key)
|
|
|
|
File.read(File.expand_path("../fixtures/#{key}", __dir__))
|
|
|
|
end
|
|
|
|
|
|
|
|
CA_CERT = OpenSSL::X509::Certificate.new(fixture("cacert.pem"))
|
|
|
|
SERVER_KEY = OpenSSL::PKey.read(fixture("server.key"))
|
|
|
|
SERVER_CERT = OpenSSL::X509::Certificate.new(fixture("server.crt"))
|
|
|
|
DHPARAMS = OpenSSL::PKey::DH.new(fixture("dhparams.pem"))
|
|
|
|
TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }
|
2007-12-20 11:21:22 -05:00
|
|
|
|
|
|
|
CONFIG = {
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
'proxy_host' => nil,
|
|
|
|
'proxy_port' => nil,
|
|
|
|
'ssl_enable' => true,
|
2016-11-29 10:48:45 -05:00
|
|
|
'ssl_certificate' => SERVER_CERT,
|
|
|
|
'ssl_private_key' => SERVER_KEY,
|
|
|
|
'ssl_tmp_dh_callback' => proc { DHPARAMS },
|
2007-12-20 11:21:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
def test_get
|
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
http.use_ssl = true
|
2016-11-29 10:48:45 -05:00
|
|
|
http.cert_store = TEST_STORE
|
|
|
|
certs = []
|
2007-12-20 11:21:22 -05:00
|
|
|
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
|
2016-11-29 10:48:45 -05:00
|
|
|
certs << store_ctx.current_cert
|
|
|
|
preverify_ok
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
|
|
|
http.request_get("/") {|res|
|
|
|
|
assert_equal($test_net_http_data, res.body)
|
|
|
|
}
|
2016-11-29 10:48:45 -05:00
|
|
|
assert_equal(CA_CERT.to_der, certs[0].to_der)
|
|
|
|
assert_equal(SERVER_CERT.to_der, certs[1].to_der)
|
2010-09-27 02:57:39 -04:00
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_post
|
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
http.use_ssl = true
|
2016-11-29 10:48:45 -05:00
|
|
|
http.cert_store = TEST_STORE
|
2007-12-20 11:21:22 -05:00
|
|
|
data = config('ssl_private_key').to_der
|
2014-08-05 15:08:26 -04:00
|
|
|
http.request_post("/", data, {'content-type' => 'application/x-www-form-urlencoded'}) {|res|
|
2007-12-20 11:21:22 -05:00
|
|
|
assert_equal(data, res.body)
|
|
|
|
}
|
2010-09-27 02:57:39 -04:00
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
|
|
|
|
2012-07-24 20:05:59 -04:00
|
|
|
def test_session_reuse
|
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
http.use_ssl = true
|
2016-11-29 10:48:45 -05:00
|
|
|
http.cert_store = TEST_STORE
|
2012-07-24 20:05:59 -04:00
|
|
|
|
|
|
|
http.start
|
|
|
|
http.get("/")
|
|
|
|
http.finish
|
|
|
|
|
|
|
|
http.start
|
|
|
|
http.get("/")
|
|
|
|
http.finish # three times due to possible bug in OpenSSL 0.9.8
|
|
|
|
|
2014-11-26 03:22:27 -05:00
|
|
|
sid = http.instance_variable_get(:@ssl_session).id
|
|
|
|
|
2012-07-24 20:05:59 -04:00
|
|
|
http.start
|
|
|
|
http.get("/")
|
|
|
|
|
|
|
|
socket = http.instance_variable_get(:@socket).io
|
|
|
|
|
|
|
|
assert socket.session_reused?
|
2014-11-26 03:22:27 -05:00
|
|
|
|
|
|
|
assert_equal sid, http.instance_variable_get(:@ssl_session).id
|
|
|
|
|
|
|
|
http.finish
|
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_session_reuse_but_expire
|
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
http.use_ssl = true
|
2016-11-29 10:48:45 -05:00
|
|
|
http.cert_store = TEST_STORE
|
2014-11-26 03:22:27 -05:00
|
|
|
|
|
|
|
http.ssl_timeout = -1
|
|
|
|
http.start
|
|
|
|
http.get("/")
|
|
|
|
http.finish
|
|
|
|
|
|
|
|
sid = http.instance_variable_get(:@ssl_session).id
|
|
|
|
|
|
|
|
http.start
|
|
|
|
http.get("/")
|
|
|
|
|
|
|
|
socket = http.instance_variable_get(:@socket).io
|
|
|
|
assert_equal false, socket.session_reused?
|
|
|
|
|
|
|
|
assert_not_equal sid, http.instance_variable_get(:@ssl_session).id
|
|
|
|
|
2014-05-30 11:03:34 -04:00
|
|
|
http.finish
|
2012-07-26 01:54:29 -04:00
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
2012-07-24 20:05:59 -04:00
|
|
|
end
|
|
|
|
|
2007-12-20 11:21:22 -05:00
|
|
|
if ENV["RUBY_OPENSSL_TEST_ALL"]
|
|
|
|
def test_verify
|
|
|
|
http = Net::HTTP.new("ssl.netlab.jp", 443)
|
|
|
|
http.use_ssl = true
|
|
|
|
assert(
|
2007-12-22 03:31:53 -05:00
|
|
|
(http.request_head("/"){|res| } rescue false),
|
2007-12-20 11:21:22 -05:00
|
|
|
"The system may not have default CA certificate store."
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_none
|
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
http.use_ssl = true
|
|
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
|
|
http.request_get("/") {|res|
|
|
|
|
assert_equal($test_net_http_data, res.body)
|
|
|
|
}
|
2010-09-27 02:57:39 -04:00
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_certificate_verify_failure
|
|
|
|
http = Net::HTTP.new("localhost", config("port"))
|
|
|
|
http.use_ssl = true
|
|
|
|
ex = assert_raise(OpenSSL::SSL::SSLError){
|
2010-09-27 02:57:39 -04:00
|
|
|
begin
|
|
|
|
http.request_get("/") {|res| }
|
|
|
|
rescue SystemCallError
|
|
|
|
skip $!
|
|
|
|
end
|
2007-12-20 11:21:22 -05:00
|
|
|
}
|
|
|
|
assert_match(/certificate verify failed/, ex.message)
|
2014-12-09 12:51:43 -05:00
|
|
|
unless /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
# on Windows, Errno::ECONNRESET will be raised, and it'll be eaten by
|
|
|
|
# WEBrick
|
|
|
|
@log_tester = lambda {|log|
|
|
|
|
assert_equal(1, log.length)
|
|
|
|
assert_match(/ERROR OpenSSL::SSL::SSLError:/, log[0])
|
|
|
|
}
|
|
|
|
end
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_identity_verify_failure
|
|
|
|
http = Net::HTTP.new("127.0.0.1", config("port"))
|
|
|
|
http.use_ssl = true
|
|
|
|
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
|
2016-11-29 10:48:45 -05:00
|
|
|
true
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
|
|
|
ex = assert_raise(OpenSSL::SSL::SSLError){
|
|
|
|
http.request_get("/") {|res| }
|
|
|
|
}
|
2012-04-29 23:51:03 -04:00
|
|
|
assert_match(/hostname \"127.0.0.1\" does not match/, ex.message)
|
2007-12-20 11:21:22 -05:00
|
|
|
end
|
2011-01-12 02:26:18 -05:00
|
|
|
|
|
|
|
def test_timeout_during_SSL_handshake
|
|
|
|
bug4246 = "expected the SSL connection to have timed out but have not. [ruby-core:34203]"
|
|
|
|
|
|
|
|
# listen for connections... but deliberately do not complete SSL handshake
|
2011-08-01 18:35:05 -04:00
|
|
|
TCPServer.open('localhost', 0) {|server|
|
2011-01-12 02:26:18 -05:00
|
|
|
port = server.addr[1]
|
|
|
|
|
|
|
|
conn = Net::HTTP.new('localhost', port)
|
|
|
|
conn.use_ssl = true
|
2012-02-27 23:51:37 -05:00
|
|
|
conn.read_timeout = 0.01
|
|
|
|
conn.open_timeout = 0.01
|
2011-01-12 02:26:18 -05:00
|
|
|
|
|
|
|
th = Thread.new do
|
2012-02-28 00:15:54 -05:00
|
|
|
assert_raise(Net::OpenTimeout) {
|
2011-01-12 02:26:18 -05:00
|
|
|
conn.get('/')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
assert th.join(10), bug4246
|
|
|
|
}
|
|
|
|
end
|
2016-11-29 10:48:45 -05:00
|
|
|
end if defined?(OpenSSL::SSL)
|