Skip to content

Commit 5f02f5d

Browse files
MSP-Greghsbt
authored andcommitted
Fix test/net/http/test_https.rb host naming for Windows
1 parent eeb4f45 commit 5f02f5d

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

test/net/http/test_https.rb

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ def self.read_fixture(key)
1414
File.read(File.expand_path("../fixtures/#{key}", __dir__))
1515
end
1616

17+
HOST = 'localhost'
18+
HOST_IP = '127.0.0.1'
1719
CA_CERT = OpenSSL::X509::Certificate.new(read_fixture("cacert.pem"))
1820
SERVER_KEY = OpenSSL::PKey.read(read_fixture("server.key"))
1921
SERVER_CERT = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
2022
DHPARAMS = OpenSSL::PKey::DH.new(read_fixture("dhparams.pem"))
2123
TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }
2224

2325
CONFIG = {
24-
'host' => '127.0.0.1',
26+
'host' => HOST,
2527
'proxy_host' => nil,
2628
'proxy_port' => nil,
2729
'ssl_enable' => true,
@@ -31,7 +33,7 @@ def self.read_fixture(key)
3133
}
3234

3335
def test_get
34-
http = Net::HTTP.new("localhost", config("port"))
36+
http = Net::HTTP.new(HOST, config("port"))
3537
http.use_ssl = true
3638
http.cert_store = TEST_STORE
3739
certs = []
@@ -43,15 +45,13 @@ def test_get
4345
assert_equal($test_net_http_data, res.body)
4446
}
4547
# TODO: OpenSSL 1.1.1h seems to yield only SERVER_CERT; need to check the incompatibility
46-
certs.zip([CA_CERT, SERVER_CERT][-certs.size..]) do |actual, expected|
48+
certs.zip([CA_CERT, SERVER_CERT][-certs.size..-1]) do |actual, expected|
4749
assert_equal(expected.to_der, actual.to_der)
4850
end
49-
rescue SystemCallError
50-
skip $!
5151
end
5252

5353
def test_get_SNI
54-
http = Net::HTTP.new("localhost", config("port"))
54+
http = Net::HTTP.new(HOST, config("port"))
5555
http.ipaddr = config('host')
5656
http.use_ssl = true
5757
http.cert_store = TEST_STORE
@@ -64,16 +64,16 @@ def test_get_SNI
6464
assert_equal($test_net_http_data, res.body)
6565
}
6666
# 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..]) do |actual, expected|
67+
certs.zip([CA_CERT, SERVER_CERT][-certs.size..-1]) do |actual, expected|
6868
assert_equal(expected.to_der, actual.to_der)
6969
end
7070
end
7171

7272
def test_get_SNI_proxy
73-
TCPServer.open("127.0.0.1", 0) {|serv|
73+
TCPServer.open(HOST_IP, 0) {|serv|
7474
_, port, _, _ = serv.addr
7575
client_thread = Thread.new {
76-
proxy = Net::HTTP.Proxy("127.0.0.1", port, 'user', 'password')
76+
proxy = Net::HTTP.Proxy(HOST_IP, port, 'user', 'password')
7777
http = proxy.new("foo.example.org", 8000)
7878
http.ipaddr = "192.0.2.1"
7979
http.use_ssl = true
@@ -125,23 +125,21 @@ def test_get_SNI_failure
125125
end
126126

127127
def test_post
128-
http = Net::HTTP.new("localhost", config("port"))
128+
http = Net::HTTP.new(HOST, config("port"))
129129
http.use_ssl = true
130130
http.cert_store = TEST_STORE
131131
data = config('ssl_private_key').to_der
132132
http.request_post("/", data, {'content-type' => 'application/x-www-form-urlencoded'}) {|res|
133133
assert_equal(data, res.body)
134134
}
135-
rescue SystemCallError
136-
skip $!
137135
end
138136

139137
def test_session_reuse
140138
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
141139
# See https://github.com/openssl/openssl/pull/5967 for details.
142140
skip if OpenSSL::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
143141

144-
http = Net::HTTP.new("localhost", config("port"))
142+
http = Net::HTTP.new(HOST, config("port"))
145143
http.use_ssl = true
146144
http.cert_store = TEST_STORE
147145

@@ -154,25 +152,21 @@ def test_session_reuse
154152
end
155153

156154
http.start
155+
assert_equal false, http.instance_variable_get(:@socket).io.session_reused?
157156
http.get("/")
158157
http.finish
159158

160159
http.start
161-
http.get("/")
162-
163-
socket = http.instance_variable_get(:@socket).io
164-
assert_equal true, socket.session_reused?
165-
160+
assert_equal true, http.instance_variable_get(:@socket).io.session_reused?
161+
assert_equal $test_net_http_data, http.get("/").body
166162
http.finish
167-
rescue SystemCallError
168-
skip $!
169163
end
170164

171165
def test_session_reuse_but_expire
172166
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
173167
skip if OpenSSL::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
174168

175-
http = Net::HTTP.new("localhost", config("port"))
169+
http = Net::HTTP.new(HOST, config("port"))
176170
http.use_ssl = true
177171
http.cert_store = TEST_STORE
178172

@@ -188,8 +182,6 @@ def test_session_reuse_but_expire
188182
assert_equal false, socket.session_reused?
189183

190184
http.finish
191-
rescue SystemCallError
192-
skip $!
193185
end
194186

195187
if ENV["RUBY_OPENSSL_TEST_ALL"]
@@ -204,14 +196,12 @@ def test_verify
204196
end
205197

206198
def test_verify_none
207-
http = Net::HTTP.new("localhost", config("port"))
199+
http = Net::HTTP.new(HOST, config("port"))
208200
http.use_ssl = true
209201
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
210202
http.request_get("/") {|res|
211203
assert_equal($test_net_http_data, res.body)
212204
}
213-
rescue SystemCallError
214-
skip $!
215205
end
216206

217207
def test_skip_hostname_verification
@@ -240,14 +230,10 @@ def test_fail_if_verify_hostname_is_true
240230
end
241231

242232
def test_certificate_verify_failure
243-
http = Net::HTTP.new("localhost", config("port"))
233+
http = Net::HTTP.new(HOST, config("port"))
244234
http.use_ssl = true
245235
ex = assert_raise(OpenSSL::SSL::SSLError){
246-
begin
247-
http.request_get("/") {|res| }
248-
rescue SystemCallError
249-
skip $!
250-
end
236+
http.request_get("/") {|res| }
251237
}
252238
assert_match(/certificate verify failed/, ex.message)
253239
unless /mswin|mingw/ =~ RUBY_PLATFORM
@@ -262,25 +248,25 @@ def test_certificate_verify_failure
262248

263249
def test_identity_verify_failure
264250
# the certificate's subject has CN=localhost
265-
http = Net::HTTP.new("127.0.0.1", config("port"))
251+
http = Net::HTTP.new(HOST_IP, config("port"))
266252
http.use_ssl = true
267253
http.cert_store = TEST_STORE
268254
@log_tester = lambda {|_| }
269255
ex = assert_raise(OpenSSL::SSL::SSLError){
270256
http.request_get("/") {|res| }
271257
}
272-
re_msg = /certificate verify failed|hostname \"127.0.0.1\" does not match/
258+
re_msg = /certificate verify failed|hostname \"#{HOST_IP}\" does not match/
273259
assert_match(re_msg, ex.message)
274260
end
275261

276262
def test_timeout_during_SSL_handshake
277263
bug4246 = "expected the SSL connection to have timed out but have not. [ruby-core:34203]"
278264

279265
# listen for connections... but deliberately do not complete SSL handshake
280-
TCPServer.open('localhost', 0) {|server|
266+
TCPServer.open(HOST, 0) {|server|
281267
port = server.addr[1]
282268

283-
conn = Net::HTTP.new('localhost', port)
269+
conn = Net::HTTP.new(HOST, port)
284270
conn.use_ssl = true
285271
conn.read_timeout = 0.01
286272
conn.open_timeout = 0.01
@@ -295,7 +281,7 @@ def test_timeout_during_SSL_handshake
295281
end
296282

297283
def test_min_version
298-
http = Net::HTTP.new("localhost", config("port"))
284+
http = Net::HTTP.new(HOST, config("port"))
299285
http.use_ssl = true
300286
http.min_version = :TLS1
301287
http.cert_store = TEST_STORE
@@ -305,7 +291,7 @@ def test_min_version
305291
end
306292

307293
def test_max_version
308-
http = Net::HTTP.new("127.0.0.1", config("port"))
294+
http = Net::HTTP.new(HOST_IP, config("port"))
309295
http.use_ssl = true
310296
http.max_version = :SSL2
311297
http.verify_callback = Proc.new do |preverify_ok, store_ctx|

0 commit comments

Comments
 (0)