Skip to content

Commit 637ba65

Browse files
committed
ssl: fix misuse of assert_handshake_error in tests
assert_handshake_error is useful for checking handshake failures triggered by the peer, as the underlying socket may be closed prematurely, leading to different exceptions depending on the platform and timing. However, when the local end aborts a handshake, the only possible exception is OpenSSL::SSL::SSLError. Use stricter assertions in such cases.
1 parent 5089b2d commit 637ba65

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

test/openssl/test_ssl.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ def test_verify_hostname_on_connect
11111111
ssl.connect
11121112
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
11131113
else
1114-
assert_handshake_error { ssl.connect }
1114+
assert_raise(OpenSSL::SSL::SSLError) { ssl.connect }
11151115
end
11161116
ensure
11171117
ssl.close if ssl
@@ -1149,7 +1149,7 @@ def test_verify_hostname_failure_error_code
11491149
sock = TCPSocket.new("127.0.0.1", port)
11501150
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
11511151
ssl.hostname = "b.example.com"
1152-
assert_handshake_error { ssl.connect }
1152+
assert_raise(OpenSSL::SSL::SSLError) { ssl.connect }
11531153
assert_equal false, verify_callback_ok
11541154
assert_equal OpenSSL::X509::V_ERR_HOSTNAME_MISMATCH, verify_callback_err
11551155
ensure
@@ -1250,7 +1250,7 @@ def test_set_params_min_version
12501250
start_server(ctx_proc: ctx_proc, ignore_listener_error: true) { |port|
12511251
ctx = OpenSSL::SSL::SSLContext.new
12521252
ctx.set_params(cert_store: store, verify_hostname: false)
1253-
assert_handshake_error { server_connect(port, ctx) { } }
1253+
assert_raise(OpenSSL::SSL::SSLError) { server_connect(port, ctx) }
12541254
}
12551255
end
12561256
end
@@ -1283,7 +1283,7 @@ def test_minmax_version
12831283
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
12841284
}
12851285
else
1286-
assert_handshake_error { server_connect(port, ctx1) { } }
1286+
assert_raise(OpenSSL::SSL::SSLError) { server_connect(port, ctx1) }
12871287
end
12881288

12891289
# There is no version-specific SSL methods for TLS 1.3
@@ -1297,7 +1297,7 @@ def test_minmax_version
12971297
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
12981298
}
12991299
else
1300-
assert_handshake_error { server_connect(port, ctx2) { } }
1300+
assert_raise(OpenSSL::SSL::SSLError) { server_connect(port, ctx2) }
13011301
end
13021302
end
13031303
end
@@ -1338,7 +1338,7 @@ def test_minmax_version
13381338
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
13391339
}
13401340
else
1341-
assert_handshake_error { server_connect(port, ctx2) { } }
1341+
assert_raise(OpenSSL::SSL::SSLError) { server_connect(port, ctx2) }
13421342
end
13431343
end
13441344
}
@@ -1357,7 +1357,7 @@ def test_minmax_version
13571357
ssl.puts "abc"; assert_equal "abc\n", ssl.gets
13581358
}
13591359
else
1360-
assert_handshake_error { server_connect(port, ctx1) { } }
1360+
assert_raise(OpenSSL::SSL::SSLError) { server_connect(port, ctx1) }
13611361
end
13621362

13631363
# Client sets max_version
@@ -1489,7 +1489,7 @@ def test_options_disable_versions
14891489
# Client only supports TLS 1.2
14901490
ctx1 = OpenSSL::SSL::SSLContext.new
14911491
ctx1.min_version = ctx1.max_version = OpenSSL::SSL::TLS1_2_VERSION
1492-
assert_handshake_error { server_connect(port, ctx1) { } }
1492+
assert_raise(OpenSSL::SSL::SSLError) { server_connect(port, ctx1) }
14931493

14941494
# Client only supports TLS 1.3
14951495
ctx2 = OpenSSL::SSL::SSLContext.new
@@ -1505,7 +1505,7 @@ def test_options_disable_versions
15051505
# Client doesn't support TLS 1.2
15061506
ctx1 = OpenSSL::SSL::SSLContext.new
15071507
ctx1.options |= OpenSSL::SSL::OP_NO_TLSv1_2
1508-
assert_handshake_error { server_connect(port, ctx1) { } }
1508+
assert_raise(OpenSSL::SSL::SSLError) { server_connect(port, ctx1) }
15091509

15101510
# Client supports TLS 1.2 by default
15111511
ctx2 = OpenSSL::SSL::SSLContext.new
@@ -1654,7 +1654,7 @@ def test_npn_selected_protocol_too_long
16541654
ctx = OpenSSL::SSL::SSLContext.new
16551655
ctx.max_version = :TLS1_2
16561656
ctx.npn_select_cb = -> (protocols) { "a" * 256 }
1657-
assert_handshake_error { server_connect(port, ctx) }
1657+
assert_raise(OpenSSL::SSL::SSLError) { server_connect(port, ctx) }
16581658
}
16591659
end
16601660

0 commit comments

Comments
 (0)