diff --git a/src/client.rs b/src/client.rs index c1312188..b8faa72d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -571,6 +571,8 @@ impl rustls_client_config { #[cfg(test)] mod tests { + use std::ptr::{null, null_mut}; + use super::*; #[test] @@ -594,4 +596,54 @@ mod tests { } rustls_client_config::rustls_client_config_free(config) } + + // Build a client connection and test the getters and initial values. + #[test] + #[cfg_attr(miri, ignore)] + fn test_client_connection_new() { + let builder: *mut rustls_client_config_builder = + rustls_client_config_builder::rustls_client_config_builder_new(); + let config = rustls_client_config_builder::rustls_client_config_builder_build(builder); + let mut conn: *mut rustls_connection = null_mut(); + let result = rustls_client_config::rustls_client_connection_new( + config, + "example.com\0".as_ptr() as *const c_char, + &mut conn, + ); + if !matches!(result, rustls_result::Ok) { + panic!("expected RUSTLS_RESULT_OK, got {:?}", result); + } + assert_eq!(rustls_connection::rustls_connection_wants_read(conn), false); + assert_eq!(rustls_connection::rustls_connection_wants_write(conn), true); + assert_eq!( + rustls_connection::rustls_connection_is_handshaking(conn), + true + ); + + let some_byte = 42u8; + let mut alpn_protocol: *const u8 = &some_byte; + let mut alpn_protocol_len: usize = 1; + rustls_connection::rustls_connection_get_alpn_protocol( + conn, + &mut alpn_protocol, + &mut alpn_protocol_len, + ); + assert_eq!(alpn_protocol, null()); + assert_eq!(alpn_protocol_len, 0); + + assert_eq!( + rustls_connection::rustls_connection_get_negotiated_ciphersuite(conn), + null() + ); + assert_eq!( + rustls_connection::rustls_connection_get_peer_certificate(conn, 0), + null() + ); + + assert_eq!( + rustls_connection::rustls_connection_get_protocol_version(conn), + 0 + ); + rustls_connection::rustls_connection_free(conn); + } } diff --git a/src/error.rs b/src/error.rs index a6a49f21..083098af 100644 --- a/src/error.rs +++ b/src/error.rs @@ -81,7 +81,7 @@ fn test_rustls_error() { #[allow(dead_code)] #[repr(u32)] -#[derive(TryFromPrimitive)] +#[derive(Debug, TryFromPrimitive)] pub enum rustls_result { Ok = 7000, Io = 7001, diff --git a/src/server.rs b/src/server.rs index 5e114e1a..96266156 100644 --- a/src/server.rs +++ b/src/server.rs @@ -676,6 +676,8 @@ impl rustls_server_config_builder { #[cfg(test)] mod tests { + use std::ptr::null_mut; + use super::*; #[test] @@ -697,4 +699,88 @@ mod tests { } rustls_server_config::rustls_server_config_free(config); } + + // Build a server connection and test the getters and initial values. + #[test] + fn test_server_config_builder_new_empty() { + let builder: *mut rustls_server_config_builder = + rustls_server_config_builder::rustls_server_config_builder_new(); + // Building a config with no certificate and key configured results in null. + assert_eq!( + rustls_server_config_builder::rustls_server_config_builder_build(builder), + null() + ); + } + + #[test] + #[cfg_attr(miri, ignore)] + fn test_server_connection_new() { + let builder: *mut rustls_server_config_builder = + rustls_server_config_builder::rustls_server_config_builder_new(); + let cert_pem = include_str!("../localhost/cert.pem").as_bytes(); + let key_pem = include_str!("../localhost/key.pem").as_bytes(); + let mut certified_key: *const rustls_certified_key = null(); + let result = rustls_certified_key::rustls_certified_key_build( + cert_pem.as_ptr(), + cert_pem.len(), + key_pem.as_ptr(), + key_pem.len(), + &mut certified_key, + ); + if !matches!(result, rustls_result::Ok) { + panic!( + "expected RUSTLS_RESULT_OK from rustls_certified_key_build, got {:?}", + result + ); + } + rustls_server_config_builder::rustls_server_config_builder_set_certified_keys( + builder, + &certified_key, + 1, + ); + + let config = rustls_server_config_builder::rustls_server_config_builder_build(builder); + assert_ne!(config, null()); + + let mut conn: *mut rustls_connection = null_mut(); + let result = rustls_server_config::rustls_server_connection_new(config, &mut conn); + if !matches!(result, rustls_result::Ok) { + panic!("expected RUSTLS_RESULT_OK, got {:?}", result); + } + assert_eq!(rustls_connection::rustls_connection_wants_read(conn), true); + assert_eq!( + rustls_connection::rustls_connection_wants_write(conn), + false + ); + assert_eq!( + rustls_connection::rustls_connection_is_handshaking(conn), + true + ); + + let some_byte = 42u8; + let mut alpn_protocol: *const u8 = &some_byte; + let mut alpn_protocol_len: usize = 1; + rustls_connection::rustls_connection_get_alpn_protocol( + conn, + &mut alpn_protocol, + &mut alpn_protocol_len, + ); + assert_eq!(alpn_protocol, null()); + assert_eq!(alpn_protocol_len, 0); + + assert_eq!( + rustls_connection::rustls_connection_get_negotiated_ciphersuite(conn), + null() + ); + assert_eq!( + rustls_connection::rustls_connection_get_peer_certificate(conn, 0), + null() + ); + + assert_eq!( + rustls_connection::rustls_connection_get_protocol_version(conn), + 0 + ); + rustls_connection::rustls_connection_free(conn); + } }