@@ -16,14 +16,16 @@ def self.read_fixture(key)
16
16
File . read ( File . expand_path ( "../fixtures/#{ key } " , __dir__ ) )
17
17
end
18
18
19
+ HOST = 'localhost'
20
+ HOST_IP = '127.0.0.1'
19
21
CA_CERT = OpenSSL ::X509 ::Certificate . new ( read_fixture ( "cacert.pem" ) )
20
22
SERVER_KEY = OpenSSL ::PKey . read ( read_fixture ( "server.key" ) )
21
23
SERVER_CERT = OpenSSL ::X509 ::Certificate . new ( read_fixture ( "server.crt" ) )
22
24
DHPARAMS = OpenSSL ::PKey ::DH . new ( read_fixture ( "dhparams.pem" ) )
23
25
TEST_STORE = OpenSSL ::X509 ::Store . new . tap { |s | s . add_cert ( CA_CERT ) }
24
26
25
27
CONFIG = {
26
- 'host' => '127.0.0.1' ,
28
+ 'host' => HOST ,
27
29
'proxy_host' => nil ,
28
30
'proxy_port' => nil ,
29
31
'ssl_enable' => true ,
@@ -33,7 +35,7 @@ def self.read_fixture(key)
33
35
}
34
36
35
37
def test_get
36
- http = Net ::HTTP . new ( "localhost" , config ( "port" ) )
38
+ http = Net ::HTTP . new ( HOST , config ( "port" ) )
37
39
http . use_ssl = true
38
40
http . cert_store = TEST_STORE
39
41
certs = [ ]
@@ -48,12 +50,10 @@ def test_get
48
50
certs . zip ( [ CA_CERT , SERVER_CERT ] [ -certs . size ..] ) do |actual , expected |
49
51
assert_equal ( expected . to_der , actual . to_der )
50
52
end
51
- rescue SystemCallError
52
- skip $!
53
53
end
54
54
55
55
def test_get_SNI
56
- http = Net ::HTTP . new ( "localhost" , config ( "port" ) )
56
+ http = Net ::HTTP . new ( HOST , config ( "port" ) )
57
57
http . ipaddr = config ( 'host' )
58
58
http . use_ssl = true
59
59
http . cert_store = TEST_STORE
@@ -72,10 +72,10 @@ def test_get_SNI
72
72
end
73
73
74
74
def test_get_SNI_proxy
75
- TCPServer . open ( "127.0.0.1" , 0 ) { |serv |
75
+ TCPServer . open ( HOST_IP , 0 ) { |serv |
76
76
_ , port , _ , _ = serv . addr
77
77
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' )
79
79
http = proxy . new ( "foo.example.org" , 8000 )
80
80
http . ipaddr = "192.0.2.1"
81
81
http . use_ssl = true
@@ -127,23 +127,21 @@ def test_get_SNI_failure
127
127
end
128
128
129
129
def test_post
130
- http = Net ::HTTP . new ( "localhost" , config ( "port" ) )
130
+ http = Net ::HTTP . new ( HOST , config ( "port" ) )
131
131
http . use_ssl = true
132
132
http . cert_store = TEST_STORE
133
133
data = config ( 'ssl_private_key' ) . to_der
134
134
http . request_post ( "/" , data , { 'content-type' => 'application/x-www-form-urlencoded' } ) { |res |
135
135
assert_equal ( data , res . body )
136
136
}
137
- rescue SystemCallError
138
- skip $!
139
137
end
140
138
141
139
def test_session_reuse
142
140
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
143
141
# See https://github.com/openssl/openssl/pull/5967 for details.
144
142
skip if OpenSSL ::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
145
143
146
- http = Net ::HTTP . new ( "localhost" , config ( "port" ) )
144
+ http = Net ::HTTP . new ( HOST , config ( "port" ) )
147
145
http . use_ssl = true
148
146
http . cert_store = TEST_STORE
149
147
@@ -156,25 +154,21 @@ def test_session_reuse
156
154
end
157
155
158
156
http . start
157
+ assert_equal false , http . instance_variable_get ( :@socket ) . io . session_reused?
159
158
http . get ( "/" )
160
159
http . finish
161
160
162
161
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
168
164
http . finish
169
- rescue SystemCallError
170
- skip $!
171
165
end
172
166
173
167
def test_session_reuse_but_expire
174
168
# FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
175
169
skip if OpenSSL ::OPENSSL_LIBRARY_VERSION =~ /OpenSSL 1.1.0h/
176
170
177
- http = Net ::HTTP . new ( "localhost" , config ( "port" ) )
171
+ http = Net ::HTTP . new ( HOST , config ( "port" ) )
178
172
http . use_ssl = true
179
173
http . cert_store = TEST_STORE
180
174
@@ -190,8 +184,6 @@ def test_session_reuse_but_expire
190
184
assert_equal false , socket . session_reused?
191
185
192
186
http . finish
193
- rescue SystemCallError
194
- skip $!
195
187
end
196
188
197
189
if ENV [ "RUBY_OPENSSL_TEST_ALL" ]
@@ -206,14 +198,12 @@ def test_verify
206
198
end
207
199
208
200
def test_verify_none
209
- http = Net ::HTTP . new ( "localhost" , config ( "port" ) )
201
+ http = Net ::HTTP . new ( HOST , config ( "port" ) )
210
202
http . use_ssl = true
211
203
http . verify_mode = OpenSSL ::SSL ::VERIFY_NONE
212
204
http . request_get ( "/" ) { |res |
213
205
assert_equal ( $test_net_http_data, res . body )
214
206
}
215
- rescue SystemCallError
216
- skip $!
217
207
end
218
208
219
209
def test_skip_hostname_verification
@@ -242,14 +232,10 @@ def test_fail_if_verify_hostname_is_true
242
232
end
243
233
244
234
def test_certificate_verify_failure
245
- http = Net ::HTTP . new ( "localhost" , config ( "port" ) )
235
+ http = Net ::HTTP . new ( HOST , config ( "port" ) )
246
236
http . use_ssl = true
247
237
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 | }
253
239
}
254
240
assert_match ( /certificate verify failed/ , ex . message )
255
241
unless /mswin|mingw/ =~ RUBY_PLATFORM
@@ -264,25 +250,25 @@ def test_certificate_verify_failure
264
250
265
251
def test_identity_verify_failure
266
252
# 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" ) )
268
254
http . use_ssl = true
269
255
http . cert_store = TEST_STORE
270
256
@log_tester = lambda { |_ | }
271
257
ex = assert_raise ( OpenSSL ::SSL ::SSLError ) {
272
258
http . request_get ( "/" ) { |res | }
273
259
}
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/
275
261
assert_match ( re_msg , ex . message )
276
262
end
277
263
278
264
def test_timeout_during_SSL_handshake
279
265
bug4246 = "expected the SSL connection to have timed out but have not. [ruby-core:34203]"
280
266
281
267
# listen for connections... but deliberately do not complete SSL handshake
282
- TCPServer . open ( 'localhost' , 0 ) { |server |
268
+ TCPServer . open ( HOST , 0 ) { |server |
283
269
port = server . addr [ 1 ]
284
270
285
- conn = Net ::HTTP . new ( 'localhost' , port )
271
+ conn = Net ::HTTP . new ( HOST , port )
286
272
conn . use_ssl = true
287
273
conn . read_timeout = 0.01
288
274
conn . open_timeout = 0.01
@@ -297,7 +283,7 @@ def test_timeout_during_SSL_handshake
297
283
end
298
284
299
285
def test_min_version
300
- http = Net ::HTTP . new ( "localhost" , config ( "port" ) )
286
+ http = Net ::HTTP . new ( HOST , config ( "port" ) )
301
287
http . use_ssl = true
302
288
http . min_version = :TLS1
303
289
http . cert_store = TEST_STORE
@@ -307,7 +293,7 @@ def test_min_version
307
293
end
308
294
309
295
def test_max_version
310
- http = Net ::HTTP . new ( "127.0.0.1" , config ( "port" ) )
296
+ http = Net ::HTTP . new ( HOST_IP , config ( "port" ) )
311
297
http . use_ssl = true
312
298
http . max_version = :SSL2
313
299
http . verify_callback = Proc . new do |preverify_ok , store_ctx |
0 commit comments