Skip to content

Commit b068a89

Browse files
committed
ssl: add a more direct test case for errors in servername_cb
An exception raised in the SSLContext#servername_cb callback aborts the handshake and sends an "unrecognized_name" alert to the client. Add more direct assertions for this scenario.
1 parent eca7ab8 commit b068a89

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

test/openssl/test_ssl.rb

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,36 +1062,44 @@ def test_tlsext_hostname
10621062
end
10631063
end
10641064

1065-
def test_servername_cb_raises_an_exception_on_unknown_objects
1066-
hostname = 'example.org'
1067-
1068-
ctx2 = OpenSSL::SSL::SSLContext.new
1069-
ctx2.cert = @svr_cert
1070-
ctx2.key = @svr_key
1071-
ctx2.servername_cb = lambda { |args| Object.new }
1072-
1065+
def test_servername_cb_exception
10731066
sock1, sock2 = socketpair
10741067

1068+
t = Thread.new {
1069+
s1 = OpenSSL::SSL::SSLSocket.new(sock1)
1070+
s1.hostname = "localhost"
1071+
assert_raise_with_message(OpenSSL::SSL::SSLError, /unrecognized.name/i) {
1072+
s1.connect
1073+
}
1074+
}
1075+
1076+
ctx2 = OpenSSL::SSL::SSLContext.new
1077+
ctx2.servername_cb = lambda { |args| raise RuntimeError, "foo" }
10751078
s2 = OpenSSL::SSL::SSLSocket.new(sock2, ctx2)
1079+
assert_raise_with_message(RuntimeError, "foo") { s2.accept }
1080+
assert t.join
1081+
ensure
1082+
sock1.close
1083+
sock2.close
1084+
end
10761085

1077-
ctx1 = OpenSSL::SSL::SSLContext.new
1086+
def test_servername_cb_raises_an_exception_on_unknown_objects
1087+
sock1, sock2 = socketpair
10781088

1079-
s1 = OpenSSL::SSL::SSLSocket.new(sock1, ctx1)
1080-
s1.hostname = hostname
10811089
t = Thread.new {
1082-
assert_raise(OpenSSL::SSL::SSLError) do
1083-
s1.connect
1084-
end
1090+
s1 = OpenSSL::SSL::SSLSocket.new(sock1)
1091+
s1.hostname = "localhost"
1092+
assert_raise(OpenSSL::SSL::SSLError) { s1.connect }
10851093
}
10861094

1087-
assert_raise(ArgumentError) do
1088-
s2.accept
1089-
end
1090-
1095+
ctx2 = OpenSSL::SSL::SSLContext.new
1096+
ctx2.servername_cb = lambda { |args| Object.new }
1097+
s2 = OpenSSL::SSL::SSLSocket.new(sock2, ctx2)
1098+
assert_raise(ArgumentError) { s2.accept }
10911099
assert t.join
10921100
ensure
1093-
sock1.close if sock1
1094-
sock2.close if sock2
1101+
sock1.close
1102+
sock2.close
10951103
end
10961104

10971105
def test_accept_errors_include_peeraddr

0 commit comments

Comments
 (0)