Fix test/net/http/test_https.rb host naming for Windows
This commit is contained in:
parent
9a6226c61e
commit
5a42ef496a
Notes:
git
2021-05-04 08:54:20 +09:00
@ -1,10 +1,8 @@
|
|||||||
# frozen_string_literal: false
|
# frozen_string_literal: false
|
||||||
require "test/unit"
|
require "test/unit"
|
||||||
|
require_relative "utils"
|
||||||
begin
|
begin
|
||||||
require 'net/https'
|
require 'net/https'
|
||||||
require 'stringio'
|
|
||||||
require 'timeout'
|
|
||||||
require File.expand_path("utils", File.dirname(__FILE__))
|
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
# should skip this test
|
# should skip this test
|
||||||
end
|
end
|
||||||
@ -16,6 +14,8 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
File.read(File.expand_path("../fixtures/#{key}", __dir__))
|
File.read(File.expand_path("../fixtures/#{key}", __dir__))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
HOST = 'localhost'
|
||||||
|
HOST_IP = '127.0.0.1'
|
||||||
CA_CERT = OpenSSL::X509::Certificate.new(read_fixture("cacert.pem"))
|
CA_CERT = OpenSSL::X509::Certificate.new(read_fixture("cacert.pem"))
|
||||||
SERVER_KEY = OpenSSL::PKey.read(read_fixture("server.key"))
|
SERVER_KEY = OpenSSL::PKey.read(read_fixture("server.key"))
|
||||||
SERVER_CERT = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
|
SERVER_CERT = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
|
||||||
@ -23,7 +23,7 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }
|
TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }
|
||||||
|
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
'host' => '127.0.0.1',
|
'host' => HOST,
|
||||||
'proxy_host' => nil,
|
'proxy_host' => nil,
|
||||||
'proxy_port' => nil,
|
'proxy_port' => nil,
|
||||||
'ssl_enable' => true,
|
'ssl_enable' => true,
|
||||||
@ -33,7 +33,7 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
def test_get
|
def test_get
|
||||||
http = Net::HTTP.new("localhost", config("port"))
|
http = Net::HTTP.new(HOST, config("port"))
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
http.cert_store = TEST_STORE
|
http.cert_store = TEST_STORE
|
||||||
certs = []
|
certs = []
|
||||||
@ -45,15 +45,13 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
assert_equal($test_net_http_data, res.body)
|
assert_equal($test_net_http_data, res.body)
|
||||||
}
|
}
|
||||||
# TODO: OpenSSL 1.1.1h seems to yield only SERVER_CERT; need to check the incompatibility
|
# TODO: OpenSSL 1.1.1h seems to yield only SERVER_CERT; need to check the incompatibility
|
||||||
certs.zip([CA_CERT, SERVER_CERT][-certs.size..]) do |actual, expected|
|
certs.zip([CA_CERT, SERVER_CERT][-certs.size..-1]) do |actual, expected|
|
||||||
assert_equal(expected.to_der, actual.to_der)
|
assert_equal(expected.to_der, actual.to_der)
|
||||||
end
|
end
|
||||||
rescue SystemCallError
|
|
||||||
skip $!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_get_SNI
|
def test_get_SNI
|
||||||
http = Net::HTTP.new("localhost", config("port"))
|
http = Net::HTTP.new(HOST, config("port"))
|
||||||
http.ipaddr = config('host')
|
http.ipaddr = config('host')
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
http.cert_store = TEST_STORE
|
http.cert_store = TEST_STORE
|
||||||
@ -66,16 +64,16 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
assert_equal($test_net_http_data, res.body)
|
assert_equal($test_net_http_data, res.body)
|
||||||
}
|
}
|
||||||
# TODO: OpenSSL 1.1.1h seems to yield only SERVER_CERT; need to check the incompatibility
|
# TODO: OpenSSL 1.1.1h seems to yield only SERVER_CERT; need to check the incompatibility
|
||||||
certs.zip([CA_CERT, SERVER_CERT][-certs.size..]) do |actual, expected|
|
certs.zip([CA_CERT, SERVER_CERT][-certs.size..-1]) do |actual, expected|
|
||||||
assert_equal(expected.to_der, actual.to_der)
|
assert_equal(expected.to_der, actual.to_der)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_get_SNI_proxy
|
def test_get_SNI_proxy
|
||||||
TCPServer.open("127.0.0.1", 0) {|serv|
|
TCPServer.open(HOST_IP, 0) {|serv|
|
||||||
_, port, _, _ = serv.addr
|
_, port, _, _ = serv.addr
|
||||||
client_thread = Thread.new {
|
client_thread = Thread.new {
|
||||||
proxy = Net::HTTP.Proxy("127.0.0.1", port, 'user', 'password')
|
proxy = Net::HTTP.Proxy(HOST_IP, port, 'user', 'password')
|
||||||
http = proxy.new("foo.example.org", 8000)
|
http = proxy.new("foo.example.org", 8000)
|
||||||
http.ipaddr = "192.0.2.1"
|
http.ipaddr = "192.0.2.1"
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
@ -127,24 +125,21 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_post
|
def test_post
|
||||||
http = Net::HTTP.new("localhost", config("port"))
|
http = Net::HTTP.new(HOST, config("port"))
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
http.cert_store = TEST_STORE
|
http.cert_store = TEST_STORE
|
||||||
data = config('ssl_private_key').to_der
|
data = config('ssl_private_key').to_der
|
||||||
http.request_post("/", data, {'content-type' => 'application/x-www-form-urlencoded'}) {|res|
|
http.request_post("/", data, {'content-type' => 'application/x-www-form-urlencoded'}) {|res|
|
||||||
assert_equal(data, res.body)
|
assert_equal(data, res.body)
|
||||||
}
|
}
|
||||||
rescue SystemCallError
|
|
||||||
skip $!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_session_reuse
|
def test_session_reuse
|
||||||
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
|
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
|
||||||
# See https://github.com/openssl/openssl/pull/5967 for details.
|
# See https://github.com/openssl/openssl/pull/5967 for details.
|
||||||
skip if OpenSSL::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
|
skip if OpenSSL::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
|
||||||
skip if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
||||||
|
|
||||||
http = Net::HTTP.new("localhost", config("port"))
|
http = Net::HTTP.new(HOST, config("port"))
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
http.cert_store = TEST_STORE
|
http.cert_store = TEST_STORE
|
||||||
|
|
||||||
@ -157,26 +152,21 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
http.start
|
http.start
|
||||||
|
assert_equal false, http.instance_variable_get(:@socket).io.session_reused?
|
||||||
http.get("/")
|
http.get("/")
|
||||||
http.finish
|
http.finish
|
||||||
|
|
||||||
http.start
|
http.start
|
||||||
http.get("/")
|
assert_equal true, http.instance_variable_get(:@socket).io.session_reused?
|
||||||
|
assert_equal $test_net_http_data, http.get("/").body
|
||||||
socket = http.instance_variable_get(:@socket).io
|
|
||||||
assert_equal true, socket.session_reused?
|
|
||||||
|
|
||||||
http.finish
|
http.finish
|
||||||
rescue SystemCallError
|
|
||||||
skip $!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_session_reuse_but_expire
|
def test_session_reuse_but_expire
|
||||||
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
|
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
|
||||||
skip if OpenSSL::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
|
skip if OpenSSL::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
|
||||||
skip if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
||||||
|
|
||||||
http = Net::HTTP.new("localhost", config("port"))
|
http = Net::HTTP.new(HOST, config("port"))
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
http.cert_store = TEST_STORE
|
http.cert_store = TEST_STORE
|
||||||
|
|
||||||
@ -192,8 +182,6 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
assert_equal false, socket.session_reused?
|
assert_equal false, socket.session_reused?
|
||||||
|
|
||||||
http.finish
|
http.finish
|
||||||
rescue SystemCallError
|
|
||||||
skip $!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if ENV["RUBY_OPENSSL_TEST_ALL"]
|
if ENV["RUBY_OPENSSL_TEST_ALL"]
|
||||||
@ -208,14 +196,12 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_verify_none
|
def test_verify_none
|
||||||
http = Net::HTTP.new("localhost", config("port"))
|
http = Net::HTTP.new(HOST, config("port"))
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
||||||
http.request_get("/") {|res|
|
http.request_get("/") {|res|
|
||||||
assert_equal($test_net_http_data, res.body)
|
assert_equal($test_net_http_data, res.body)
|
||||||
}
|
}
|
||||||
rescue SystemCallError
|
|
||||||
skip $!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_skip_hostname_verification
|
def test_skip_hostname_verification
|
||||||
@ -244,14 +230,10 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_certificate_verify_failure
|
def test_certificate_verify_failure
|
||||||
http = Net::HTTP.new("localhost", config("port"))
|
http = Net::HTTP.new(HOST, config("port"))
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
ex = assert_raise(OpenSSL::SSL::SSLError){
|
ex = assert_raise(OpenSSL::SSL::SSLError){
|
||||||
begin
|
http.request_get("/") {|res| }
|
||||||
http.request_get("/") {|res| }
|
|
||||||
rescue SystemCallError
|
|
||||||
skip $!
|
|
||||||
end
|
|
||||||
}
|
}
|
||||||
assert_match(/certificate verify failed/, ex.message)
|
assert_match(/certificate verify failed/, ex.message)
|
||||||
unless /mswin|mingw/ =~ RUBY_PLATFORM
|
unless /mswin|mingw/ =~ RUBY_PLATFORM
|
||||||
@ -266,14 +248,14 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_identity_verify_failure
|
def test_identity_verify_failure
|
||||||
# the certificate's subject has CN=localhost
|
# the certificate's subject has CN=localhost
|
||||||
http = Net::HTTP.new("127.0.0.1", config("port"))
|
http = Net::HTTP.new(HOST_IP, config("port"))
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
http.cert_store = TEST_STORE
|
http.cert_store = TEST_STORE
|
||||||
@log_tester = lambda {|_| }
|
@log_tester = lambda {|_| }
|
||||||
ex = assert_raise(OpenSSL::SSL::SSLError){
|
ex = assert_raise(OpenSSL::SSL::SSLError){
|
||||||
http.request_get("/") {|res| }
|
http.request_get("/") {|res| }
|
||||||
}
|
}
|
||||||
re_msg = /certificate verify failed|hostname \"127.0.0.1\" does not match/
|
re_msg = /certificate verify failed|hostname \"#{HOST_IP}\" does not match/
|
||||||
assert_match(re_msg, ex.message)
|
assert_match(re_msg, ex.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -281,10 +263,10 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
bug4246 = "expected the SSL connection to have timed out but have not. [ruby-core:34203]"
|
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
|
# listen for connections... but deliberately do not complete SSL handshake
|
||||||
TCPServer.open('localhost', 0) {|server|
|
TCPServer.open(HOST, 0) {|server|
|
||||||
port = server.addr[1]
|
port = server.addr[1]
|
||||||
|
|
||||||
conn = Net::HTTP.new('localhost', port)
|
conn = Net::HTTP.new(HOST, port)
|
||||||
conn.use_ssl = true
|
conn.use_ssl = true
|
||||||
conn.read_timeout = 0.01
|
conn.read_timeout = 0.01
|
||||||
conn.open_timeout = 0.01
|
conn.open_timeout = 0.01
|
||||||
@ -299,7 +281,7 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_min_version
|
def test_min_version
|
||||||
http = Net::HTTP.new("localhost", config("port"))
|
http = Net::HTTP.new(HOST, config("port"))
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
http.min_version = :TLS1
|
http.min_version = :TLS1
|
||||||
http.cert_store = TEST_STORE
|
http.cert_store = TEST_STORE
|
||||||
@ -309,7 +291,7 @@ class TestNetHTTPS < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_max_version
|
def test_max_version
|
||||||
http = Net::HTTP.new("127.0.0.1", config("port"))
|
http = Net::HTTP.new(HOST_IP, config("port"))
|
||||||
http.use_ssl = true
|
http.use_ssl = true
|
||||||
http.max_version = :SSL2
|
http.max_version = :SSL2
|
||||||
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
|
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user