Skip to content

Commit 1ccdc05

Browse files
committed
test/openssl/test_ssl: revise verify_mode test cases
Add explicit test cases for the behaviors with different verify_mode. If we made a bug in verify_mode, we would notice it by failures of other test cases, but there were no dedicated test cases for verify_mode.
1 parent 785b556 commit 1ccdc05

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

test/openssl/test_ssl.rb

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,51 @@ def test_copy_stream
246246
end
247247
end
248248

249-
def test_client_auth_failure
249+
def test_verify_mode_server_cert
250+
start_server(ignore_listener_error: true) { |port|
251+
populated_store = OpenSSL::X509::Store.new
252+
populated_store.add_cert(@ca_cert)
253+
empty_store = OpenSSL::X509::Store.new
254+
255+
# Valid certificate, SSL_VERIFY_PEER
256+
assert_nothing_raised {
257+
ctx = OpenSSL::SSL::SSLContext.new
258+
ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
259+
ctx.cert_store = populated_store
260+
server_connect(port, ctx) { |ssl| ssl.puts("abc"); ssl.gets }
261+
}
262+
263+
# Invalid certificate, SSL_VERIFY_NONE
264+
assert_nothing_raised {
265+
ctx = OpenSSL::SSL::SSLContext.new
266+
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
267+
ctx.cert_store = empty_store
268+
server_connect(port, ctx) { |ssl| ssl.puts("abc"); ssl.gets }
269+
}
270+
271+
# Invalid certificate, SSL_VERIFY_PEER
272+
assert_handshake_error {
273+
ctx = OpenSSL::SSL::SSLContext.new
274+
ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
275+
ctx.cert_store = empty_store
276+
server_connect(port, ctx) { |ssl| ssl.puts("abc"); ssl.gets }
277+
}
278+
}
279+
end
280+
281+
def test_verify_mode_client_cert_required
282+
# Optional, client certificate not supplied
283+
vflag = OpenSSL::SSL::VERIFY_PEER
284+
accept_proc = -> ssl {
285+
assert_equal nil, ssl.peer_cert
286+
}
287+
start_server(verify_mode: vflag, accept_proc: accept_proc) { |port|
288+
assert_nothing_raised {
289+
server_connect(port) { |ssl| ssl.puts("abc"); ssl.gets }
290+
}
291+
}
292+
293+
# Required, client certificate not supplied
250294
vflag = OpenSSL::SSL::VERIFY_PEER|OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
251295
start_server(verify_mode: vflag, ignore_listener_error: true) { |port|
252296
assert_handshake_error {

0 commit comments

Comments
 (0)