Skip to content

Commit 35c1745

Browse files
committed
Refactor HTTPS tests
This contains various improvements in tests for openssl integration: - Remove DHE parameters from test servers. OpenSSL is almost always compiled with ECC support nowadays and will prefer ECDHE over DHE. - Remove an outdated omission for a bug in OpenSSL 1.1.0h released in 2018. None of our CI systems use this specific OpenSSL version. - Use top-level return to skip tests if openssl is unavailable. - Refactor tests for Net::HTTP#verify_callback.
1 parent ef50f97 commit 35c1745

File tree

4 files changed

+26
-88
lines changed

4 files changed

+26
-88
lines changed

test/net/fixtures/dhparams.pem

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/net/http/test_https.rb

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# should skip this test
88
end
99

10+
return unless defined?(OpenSSL::SSL)
11+
1012
class TestNetHTTPS < Test::Unit::TestCase
1113
include TestNetHTTPUtils
1214

@@ -19,7 +21,6 @@ def self.read_fixture(key)
1921
CA_CERT = OpenSSL::X509::Certificate.new(read_fixture("cacert.pem"))
2022
SERVER_KEY = OpenSSL::PKey.read(read_fixture("server.key"))
2123
SERVER_CERT = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
22-
DHPARAMS = OpenSSL::PKey::DH.new(read_fixture("dhparams.pem"))
2324
TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }
2425

2526
CONFIG = {
@@ -29,44 +30,27 @@ def self.read_fixture(key)
2930
'ssl_enable' => true,
3031
'ssl_certificate' => SERVER_CERT,
3132
'ssl_private_key' => SERVER_KEY,
32-
'ssl_tmp_dh_callback' => proc { DHPARAMS },
3333
}
3434

3535
def test_get
3636
http = Net::HTTP.new(HOST, config("port"))
3737
http.use_ssl = true
3838
http.cert_store = TEST_STORE
39-
certs = []
40-
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
41-
certs << store_ctx.current_cert
42-
preverify_ok
43-
end
4439
http.request_get("/") {|res|
4540
assert_equal($test_net_http_data, res.body)
41+
assert_equal(SERVER_CERT.to_der, http.peer_cert.to_der)
4642
}
47-
# TODO: OpenSSL 1.1.1h seems to yield only SERVER_CERT; need to check the incompatibility
48-
certs.zip([CA_CERT, SERVER_CERT][-certs.size..-1]) do |actual, expected|
49-
assert_equal(expected.to_der, actual.to_der)
50-
end
5143
end
5244

5345
def test_get_SNI
5446
http = Net::HTTP.new(HOST, config("port"))
5547
http.ipaddr = config('host')
5648
http.use_ssl = true
5749
http.cert_store = TEST_STORE
58-
certs = []
59-
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
60-
certs << store_ctx.current_cert
61-
preverify_ok
62-
end
6350
http.request_get("/") {|res|
6451
assert_equal($test_net_http_data, res.body)
52+
assert_equal(SERVER_CERT.to_der, http.peer_cert.to_der)
6553
}
66-
# TODO: OpenSSL 1.1.1h seems to yield only SERVER_CERT; need to check the incompatibility
67-
certs.zip([CA_CERT, SERVER_CERT][-certs.size..-1]) do |actual, expected|
68-
assert_equal(expected.to_der, actual.to_der)
69-
end
7054
end
7155

7256
def test_get_SNI_proxy
@@ -78,11 +62,6 @@ def test_get_SNI_proxy
7862
http.ipaddr = "192.0.2.1"
7963
http.use_ssl = true
8064
http.cert_store = TEST_STORE
81-
certs = []
82-
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
83-
certs << store_ctx.current_cert
84-
preverify_ok
85-
end
8665
begin
8766
http.start
8867
rescue EOFError
@@ -114,11 +93,6 @@ def test_get_SNI_failure
11493
http.ipaddr = config('host')
11594
http.use_ssl = true
11695
http.cert_store = TEST_STORE
117-
certs = []
118-
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
119-
certs << store_ctx.current_cert
120-
preverify_ok
121-
end
12296
@log_tester = lambda {|_| }
12397
assert_raise(OpenSSL::SSL::SSLError){ http.start }
12498
end
@@ -135,10 +109,6 @@ def test_post
135109
end
136110

137111
def test_session_reuse
138-
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
139-
# See https://github.com/openssl/openssl/pull/5967 for details.
140-
omit if OpenSSL::OPENSSL_LIBRARY_VERSION.include?('OpenSSL 1.1.0h')
141-
142112
http = Net::HTTP.new(HOST, config("port"))
143113
http.use_ssl = true
144114
http.cert_store = TEST_STORE
@@ -165,9 +135,6 @@ def test_session_reuse
165135
end
166136

167137
def test_session_reuse_but_expire
168-
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
169-
omit if OpenSSL::OPENSSL_LIBRARY_VERSION.include?('OpenSSL 1.1.0h')
170-
171138
http = Net::HTTP.new(HOST, config("port"))
172139
http.use_ssl = true
173140
http.cert_store = TEST_STORE
@@ -240,6 +207,21 @@ def test_certificate_verify_failure
240207
assert_match(/certificate verify failed/, ex.message)
241208
end
242209

210+
def test_verify_callback
211+
http = Net::HTTP.new(HOST, config("port"))
212+
http.use_ssl = true
213+
http.cert_store = TEST_STORE
214+
certs = []
215+
http.verify_callback = Proc.new {|preverify_ok, store_ctx|
216+
certs << store_ctx.current_cert
217+
preverify_ok
218+
}
219+
http.request_get("/") {|res|
220+
assert_equal($test_net_http_data, res.body)
221+
}
222+
assert_equal(SERVER_CERT.to_der, certs.last.to_der)
223+
end
224+
243225
def test_timeout_during_SSL_handshake
244226
bug4246 = "expected the SSL connection to have timed out but have not. [ruby-core:34203]"
245227

@@ -275,9 +257,7 @@ def test_max_version
275257
http = Net::HTTP.new(HOST, config("port"))
276258
http.use_ssl = true
277259
http.max_version = :SSL2
278-
http.verify_callback = Proc.new do |preverify_ok, store_ctx|
279-
true
280-
end
260+
http.cert_store = TEST_STORE
281261
@log_tester = lambda {|_| }
282262
ex = assert_raise(OpenSSL::SSL::SSLError){
283263
http.request_get("/") {|res| }
@@ -286,7 +266,7 @@ def test_max_version
286266
assert_match(re_msg, ex.message)
287267
end
288268

289-
end if defined?(OpenSSL::SSL)
269+
end
290270

291271
class TestNetHTTPSIdentityVerifyFailure < Test::Unit::TestCase
292272
include TestNetHTTPUtils
@@ -300,7 +280,6 @@ def self.read_fixture(key)
300280
CA_CERT = OpenSSL::X509::Certificate.new(read_fixture("cacert.pem"))
301281
SERVER_KEY = OpenSSL::PKey.read(read_fixture("server.key"))
302282
SERVER_CERT = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
303-
DHPARAMS = OpenSSL::PKey::DH.new(read_fixture("dhparams.pem"))
304283
TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }
305284

306285
CONFIG = {
@@ -310,7 +289,6 @@ def self.read_fixture(key)
310289
'ssl_enable' => true,
311290
'ssl_certificate' => SERVER_CERT,
312291
'ssl_private_key' => SERVER_KEY,
313-
'ssl_tmp_dh_callback' => proc { DHPARAMS },
314292
}
315293

316294
def test_identity_verify_failure
@@ -326,4 +304,4 @@ def test_identity_verify_failure
326304
re_msg = /certificate verify failed|hostname \"#{HOST_IP}\" does not match/
327305
assert_match(re_msg, ex.message)
328306
end
329-
end if defined?(OpenSSL::SSL)
307+
end

test/net/http/test_https_proxy.rb

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55
end
66
require 'test/unit'
77

8+
return unless defined?(OpenSSL::SSL)
9+
810
class HTTPSProxyTest < Test::Unit::TestCase
911
def test_https_proxy_authentication
10-
begin
11-
OpenSSL
12-
rescue LoadError
13-
omit 'autoload problem. see [ruby-dev:45021][Bug #5786]'
14-
end
15-
1612
TCPServer.open("127.0.0.1", 0) {|serv|
1713
_, port, _, _ = serv.addr
1814
client_thread = Thread.new {
@@ -50,12 +46,6 @@ def read_fixture(key)
5046
end
5147

5248
def test_https_proxy_ssl_connection
53-
begin
54-
OpenSSL
55-
rescue LoadError
56-
omit 'autoload problem. see [ruby-dev:45021][Bug #5786]'
57-
end
58-
5949
TCPServer.open("127.0.0.1", 0) {|tcpserver|
6050
ctx = OpenSSL::SSL::SSLContext.new
6151
ctx.key = OpenSSL::PKey.read(read_fixture("server.key"))
@@ -91,4 +81,4 @@ def test_https_proxy_ssl_connection
9181
assert_join_threads([client_thread, server_thread])
9282
}
9383
end
94-
end if defined?(OpenSSL)
84+
end

test/net/http/utils.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: false
22
require 'socket'
3-
require 'openssl'
43

54
module TestNetHTTPUtils
65

@@ -14,10 +13,10 @@ def initialize(config, &block)
1413
@procs = {}
1514

1615
if @config['ssl_enable']
16+
require 'openssl'
1717
context = OpenSSL::SSL::SSLContext.new
1818
context.cert = @config['ssl_certificate']
1919
context.key = @config['ssl_private_key']
20-
context.tmp_dh_callback = @config['ssl_tmp_dh_callback']
2120
@ssl_server = OpenSSL::SSL::SSLServer.new(@server, context)
2221
end
2322

0 commit comments

Comments
 (0)