Skip to content

Commit d1b02d3

Browse files
committed
Fix test_https.rb host, Rakefile
1 parent aea55e9 commit d1b02d3

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require "bundler/gem_tasks"
1+
require "bundler/gem_tasks" if defined?(Bundler)
22
require "rake/testtask"
33

44
Rake::TestTask.new(:test) do |t|

test/net/http/test_https.rb

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

19+
HOST = 'localhost'
20+
HOST_IP = '127.0.0.1'
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"))
2224
DHPARAMS = OpenSSL::PKey::DH.new(read_fixture("dhparams.pem"))
2325
TEST_STORE = OpenSSL::X509::Store.new.tap {|s| s.add_cert(CA_CERT) }
2426

2527
CONFIG = {
26-
'host' => '127.0.0.1',
28+
'host' => HOST,
2729
'proxy_host' => nil,
2830
'proxy_port' => nil,
2931
'ssl_enable' => true,
@@ -33,7 +35,7 @@ def self.read_fixture(key)
3335
}
3436

3537
def test_get
36-
http = Net::HTTP.new("localhost", config("port"))
38+
http = Net::HTTP.new(HOST, config("port"))
3739
http.use_ssl = true
3840
http.cert_store = TEST_STORE
3941
certs = []
@@ -48,12 +50,10 @@ def test_get
4850
certs.zip([CA_CERT, SERVER_CERT][-certs.size..]) do |actual, expected|
4951
assert_equal(expected.to_der, actual.to_der)
5052
end
51-
rescue SystemCallError
52-
skip $!
5353
end
5454

5555
def test_get_SNI
56-
http = Net::HTTP.new("localhost", config("port"))
56+
http = Net::HTTP.new(HOST, config("port"))
5757
http.ipaddr = config('host')
5858
http.use_ssl = true
5959
http.cert_store = TEST_STORE
@@ -72,10 +72,10 @@ def test_get_SNI
7272
end
7373

7474
def test_get_SNI_proxy
75-
TCPServer.open("127.0.0.1", 0) {|serv|
75+
TCPServer.open(HOST_IP, 0) {|serv|
7676
_, port, _, _ = serv.addr
7777
client_thread = Thread.new {
78-
proxy = Net::HTTP.Proxy("127.0.0.1", port, 'user', 'password')
78+
proxy = Net::HTTP.Proxy(HOST_IP, port, 'user', 'password')
7979
http = proxy.new("foo.example.org", 8000)
8080
http.ipaddr = "192.0.2.1"
8181
http.use_ssl = true
@@ -127,23 +127,21 @@ def test_get_SNI_failure
127127
end
128128

129129
def test_post
130-
http = Net::HTTP.new("localhost", config("port"))
130+
http = Net::HTTP.new(HOST, config("port"))
131131
http.use_ssl = true
132132
http.cert_store = TEST_STORE
133133
data = config('ssl_private_key').to_der
134134
http.request_post("/", data, {'content-type' => 'application/x-www-form-urlencoded'}) {|res|
135135
assert_equal(data, res.body)
136136
}
137-
rescue SystemCallError
138-
skip $!
139137
end
140138

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

146-
http = Net::HTTP.new("localhost", config("port"))
144+
http = Net::HTTP.new(HOST, config("port"))
147145
http.use_ssl = true
148146
http.cert_store = TEST_STORE
149147

@@ -156,25 +154,21 @@ def test_session_reuse
156154
end
157155

158156
http.start
157+
assert_equal false, http.instance_variable_get(:@socket).io.session_reused?
159158
http.get("/")
160159
http.finish
161160

162161
http.start
163-
http.get("/")
164-
165-
socket = http.instance_variable_get(:@socket).io
166-
assert_equal true, socket.session_reused?
167-
162+
assert_equal true, http.instance_variable_get(:@socket).io.session_reused?
163+
assert_equal $test_net_http_data, http.get("/").body
168164
http.finish
169-
rescue SystemCallError
170-
skip $!
171165
end
172166

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

177-
http = Net::HTTP.new("localhost", config("port"))
171+
http = Net::HTTP.new(HOST, config("port"))
178172
http.use_ssl = true
179173
http.cert_store = TEST_STORE
180174

@@ -190,8 +184,6 @@ def test_session_reuse_but_expire
190184
assert_equal false, socket.session_reused?
191185

192186
http.finish
193-
rescue SystemCallError
194-
skip $!
195187
end
196188

197189
if ENV["RUBY_OPENSSL_TEST_ALL"]
@@ -206,14 +198,12 @@ def test_verify
206198
end
207199

208200
def test_verify_none
209-
http = Net::HTTP.new("localhost", config("port"))
201+
http = Net::HTTP.new(HOST, config("port"))
210202
http.use_ssl = true
211203
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
212204
http.request_get("/") {|res|
213205
assert_equal($test_net_http_data, res.body)
214206
}
215-
rescue SystemCallError
216-
skip $!
217207
end
218208

219209
def test_skip_hostname_verification
@@ -242,14 +232,10 @@ def test_fail_if_verify_hostname_is_true
242232
end
243233

244234
def test_certificate_verify_failure
245-
http = Net::HTTP.new("localhost", config("port"))
235+
http = Net::HTTP.new(HOST, config("port"))
246236
http.use_ssl = true
247237
ex = assert_raise(OpenSSL::SSL::SSLError){
248-
begin
249-
http.request_get("/") {|res| }
250-
rescue SystemCallError
251-
skip $!
252-
end
238+
http.request_get("/") {|res| }
253239
}
254240
assert_match(/certificate verify failed/, ex.message)
255241
unless /mswin|mingw/ =~ RUBY_PLATFORM
@@ -264,25 +250,25 @@ def test_certificate_verify_failure
264250

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

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

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

285-
conn = Net::HTTP.new('localhost', port)
271+
conn = Net::HTTP.new(HOST, port)
286272
conn.use_ssl = true
287273
conn.read_timeout = 0.01
288274
conn.open_timeout = 0.01
@@ -297,7 +283,7 @@ def test_timeout_during_SSL_handshake
297283
end
298284

299285
def test_min_version
300-
http = Net::HTTP.new("localhost", config("port"))
286+
http = Net::HTTP.new(HOST, config("port"))
301287
http.use_ssl = true
302288
http.min_version = :TLS1
303289
http.cert_store = TEST_STORE
@@ -307,7 +293,7 @@ def test_min_version
307293
end
308294

309295
def test_max_version
310-
http = Net::HTTP.new("127.0.0.1", config("port"))
296+
http = Net::HTTP.new(HOST_IP, config("port"))
311297
http.use_ssl = true
312298
http.max_version = :SSL2
313299
http.verify_callback = Proc.new do |preverify_ok, store_ctx|

0 commit comments

Comments
 (0)