Skip to content

Commit

Permalink
Improve reliability of some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Mar 23, 2023
1 parent 319200a commit e5b6d97
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions openssl/src/ssl/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,21 @@ fn verify_trusted_with_set_cert() {

#[test]
fn verify_untrusted_callback_override_ok() {
static CALLED_BACK: AtomicBool = AtomicBool::new(false);

let server = Server::builder().build();

let mut client = server.client();
client
.ctx()
.set_verify_callback(SslVerifyMode::PEER, |_, x509| {
CALLED_BACK.store(true, Ordering::SeqCst);
assert!(x509.current_cert().is_some());
true
});

client.connect();
assert!(CALLED_BACK.load(Ordering::SeqCst));
}

#[test]
Expand All @@ -113,18 +117,22 @@ fn verify_untrusted_callback_override_bad() {

#[test]
fn verify_trusted_callback_override_ok() {
static CALLED_BACK: AtomicBool = AtomicBool::new(false);

let server = Server::builder().build();

let mut client = server.client();
client.ctx().set_ca_file("test/root-ca.pem").unwrap();
client
.ctx()
.set_verify_callback(SslVerifyMode::PEER, |_, x509| {
CALLED_BACK.store(true, Ordering::SeqCst);
assert!(x509.current_cert().is_some());
true
});

client.connect();
assert!(CALLED_BACK.load(Ordering::SeqCst));
}

#[test]
Expand All @@ -144,33 +152,41 @@ fn verify_trusted_callback_override_bad() {

#[test]
fn verify_callback_load_certs() {
static CALLED_BACK: AtomicBool = AtomicBool::new(false);

let server = Server::builder().build();

let mut client = server.client();
client
.ctx()
.set_verify_callback(SslVerifyMode::PEER, |_, x509| {
CALLED_BACK.store(true, Ordering::SeqCst);
assert!(x509.current_cert().is_some());
true
});

client.connect();
assert!(CALLED_BACK.load(Ordering::SeqCst));
}

#[test]
fn verify_trusted_get_error_ok() {
static CALLED_BACK: AtomicBool = AtomicBool::new(false);

let server = Server::builder().build();

let mut client = server.client();
client.ctx().set_ca_file("test/root-ca.pem").unwrap();
client
.ctx()
.set_verify_callback(SslVerifyMode::PEER, |_, x509| {
CALLED_BACK.store(true, Ordering::SeqCst);
assert_eq!(x509.error(), X509VerifyResult::OK);
true
});

client.connect();
assert!(CALLED_BACK.load(Ordering::SeqCst));
}

#[test]
Expand Down Expand Up @@ -469,8 +485,11 @@ fn test_alpn_server_select_none_fatal() {
#[test]
#[cfg(any(ossl102, libressl261))]
fn test_alpn_server_select_none() {
static CALLED_BACK: AtomicBool = AtomicBool::new(false);

let mut server = Server::builder();
server.ctx().set_alpn_select_callback(|_, client| {
CALLED_BACK.store(true, Ordering::SeqCst);
ssl::select_next_proto(b"\x08http/1.1\x08spdy/3.1", client).ok_or(ssl::AlpnError::NOACK)
});
let server = server.build();
Expand All @@ -479,6 +498,7 @@ fn test_alpn_server_select_none() {
client.ctx().set_alpn_protos(b"\x06http/2").unwrap();
let s = client.connect();
assert_eq!(None, s.ssl().selected_alpn_protocol());
assert!(CALLED_BACK.load(Ordering::SeqCst));
}

#[test]
Expand Down Expand Up @@ -595,7 +615,7 @@ fn refcount_ssl_context() {

{
let new_ctx_a = SslContext::builder(SslMethod::tls()).unwrap().build();
let _new_ctx_b = ssl.set_ssl_context(&new_ctx_a);
ssl.set_ssl_context(&new_ctx_a).unwrap();
}
}

Expand Down Expand Up @@ -731,7 +751,7 @@ fn connector_no_hostname_still_verifies() {
}

#[test]
fn connector_no_hostname_can_disable_verify() {
fn connector_can_disable_verify() {
let server = Server::builder().build();

let mut connector = SslConnector::builder(SslMethod::tls()).unwrap();
Expand All @@ -742,8 +762,7 @@ fn connector_no_hostname_can_disable_verify() {
let mut s = connector
.configure()
.unwrap()
.verify_hostname(false)
.connect("foobar.com", s)
.connect("fizzbuzz.com", s)
.unwrap();
s.read_exact(&mut [0]).unwrap();
}
Expand Down

0 comments on commit e5b6d97

Please sign in to comment.