Skip to content

Commit 724a8e6

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 9e32ea0 commit 724a8e6

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
@@ -1057,36 +1057,44 @@ def test_tlsext_hostname
10571057
end
10581058
end
10591059

1060-
def test_servername_cb_raises_an_exception_on_unknown_objects
1061-
hostname = 'example.org'
1062-
1063-
ctx2 = OpenSSL::SSL::SSLContext.new
1064-
ctx2.cert = @svr_cert
1065-
ctx2.key = @svr_key
1066-
ctx2.servername_cb = lambda { |args| Object.new }
1067-
1060+
def test_servername_cb_exception
10681061
sock1, sock2 = socketpair
10691062

1063+
t = Thread.new {
1064+
s1 = OpenSSL::SSL::SSLSocket.new(sock1)
1065+
s1.hostname = "localhost"
1066+
assert_raise_with_message(OpenSSL::SSL::SSLError, /unrecognized.name/i) {
1067+
s1.connect
1068+
}
1069+
}
1070+
1071+
ctx2 = OpenSSL::SSL::SSLContext.new
1072+
ctx2.servername_cb = lambda { |args| raise RuntimeError, "foo" }
10701073
s2 = OpenSSL::SSL::SSLSocket.new(sock2, ctx2)
1074+
assert_raise_with_message(RuntimeError, "foo") { s2.accept }
1075+
assert t.join
1076+
ensure
1077+
sock1.close
1078+
sock2.close
1079+
end
10711080

1072-
ctx1 = OpenSSL::SSL::SSLContext.new
1081+
def test_servername_cb_raises_an_exception_on_unknown_objects
1082+
sock1, sock2 = socketpair
10731083

1074-
s1 = OpenSSL::SSL::SSLSocket.new(sock1, ctx1)
1075-
s1.hostname = hostname
10761084
t = Thread.new {
1077-
assert_raise(OpenSSL::SSL::SSLError) do
1078-
s1.connect
1079-
end
1085+
s1 = OpenSSL::SSL::SSLSocket.new(sock1)
1086+
s1.hostname = "localhost"
1087+
assert_raise(OpenSSL::SSL::SSLError) { s1.connect }
10801088
}
10811089

1082-
assert_raise(ArgumentError) do
1083-
s2.accept
1084-
end
1085-
1090+
ctx2 = OpenSSL::SSL::SSLContext.new
1091+
ctx2.servername_cb = lambda { |args| Object.new }
1092+
s2 = OpenSSL::SSL::SSLSocket.new(sock2, ctx2)
1093+
assert_raise(ArgumentError) { s2.accept }
10861094
assert t.join
10871095
ensure
1088-
sock1.close if sock1
1089-
sock2.close if sock2
1096+
sock1.close
1097+
sock2.close
10901098
end
10911099

10921100
def test_accept_errors_include_peeraddr

0 commit comments

Comments
 (0)