Skip to content

Commit

Permalink
Return Option from handshake_kind()
Browse files Browse the repository at this point in the history
  • Loading branch information
ctz committed Apr 17, 2024
1 parent d2e1e66 commit 5ea02ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
7 changes: 3 additions & 4 deletions rustls/src/common_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,10 @@ impl CommonState {
///
/// This tells you whether the handshake was a resumption or not.
///
/// This will return `Err(Error::HandshakeNotComplete)` before it is
/// known which sort of handshake occurred.
pub fn handshake_kind(&self) -> Result<HandshakeKind, Error> {
/// This will return `None` before it is known which sort of
/// handshake occurred.
pub fn handshake_kind(&self) -> Option<HandshakeKind> {
self.handshake_kind
.ok_or(Error::HandshakeNotComplete)
}

pub(crate) fn is_tls13(&self) -> bool {
Expand Down
48 changes: 24 additions & 24 deletions rustls/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,15 +516,15 @@ fn resumption_combinations() {
make_pair_for_configs(client_config.clone(), server_config.clone());
do_handshake(&mut client, &mut server);

assert_eq!(client.handshake_kind(), Ok(HandshakeKind::Full));
assert_eq!(server.handshake_kind(), Ok(HandshakeKind::Full));
assert_eq!(client.handshake_kind(), Some(HandshakeKind::Full));
assert_eq!(server.handshake_kind(), Some(HandshakeKind::Full));

let (mut client, mut server) =
make_pair_for_configs(client_config.clone(), server_config.clone());
do_handshake(&mut client, &mut server);

assert_eq!(client.handshake_kind(), Ok(HandshakeKind::Resumed));
assert_eq!(server.handshake_kind(), Ok(HandshakeKind::Resumed));
assert_eq!(client.handshake_kind(), Some(HandshakeKind::Resumed));
assert_eq!(server.handshake_kind(), Some(HandshakeKind::Resumed));
}
}
}
Expand Down Expand Up @@ -3782,8 +3782,8 @@ fn tls13_stateful_resumption() {
.map(|certs| certs.len()),
Some(3)
);
assert_eq!(client.handshake_kind(), Ok(HandshakeKind::Full));
assert_eq!(server.handshake_kind(), Ok(HandshakeKind::Full));
assert_eq!(client.handshake_kind(), Some(HandshakeKind::Full));
assert_eq!(server.handshake_kind(), Some(HandshakeKind::Full));

// resumed
let (mut client, mut server) = make_pair_for_arc_configs(&client_config, &server_config);
Expand All @@ -3799,8 +3799,8 @@ fn tls13_stateful_resumption() {
.map(|certs| certs.len()),
Some(3)
);
assert_eq!(client.handshake_kind(), Ok(HandshakeKind::Resumed));
assert_eq!(server.handshake_kind(), Ok(HandshakeKind::Resumed));
assert_eq!(client.handshake_kind(), Some(HandshakeKind::Resumed));
assert_eq!(server.handshake_kind(), Some(HandshakeKind::Resumed));

// resumed again
let (mut client, mut server) = make_pair_for_arc_configs(&client_config, &server_config);
Expand All @@ -3816,8 +3816,8 @@ fn tls13_stateful_resumption() {
.map(|certs| certs.len()),
Some(3)
);
assert_eq!(client.handshake_kind(), Ok(HandshakeKind::Resumed));
assert_eq!(server.handshake_kind(), Ok(HandshakeKind::Resumed));
assert_eq!(client.handshake_kind(), Some(HandshakeKind::Resumed));
assert_eq!(server.handshake_kind(), Some(HandshakeKind::Resumed));
}

#[test]
Expand All @@ -3844,8 +3844,8 @@ fn tls13_stateless_resumption() {
.map(|certs| certs.len()),
Some(3)
);
assert_eq!(client.handshake_kind(), Ok(HandshakeKind::Full));
assert_eq!(server.handshake_kind(), Ok(HandshakeKind::Full));
assert_eq!(client.handshake_kind(), Some(HandshakeKind::Full));
assert_eq!(server.handshake_kind(), Some(HandshakeKind::Full));

// resumed
let (mut client, mut server) = make_pair_for_arc_configs(&client_config, &server_config);
Expand All @@ -3861,8 +3861,8 @@ fn tls13_stateless_resumption() {
.map(|certs| certs.len()),
Some(3)
);
assert_eq!(client.handshake_kind(), Ok(HandshakeKind::Resumed));
assert_eq!(server.handshake_kind(), Ok(HandshakeKind::Resumed));
assert_eq!(client.handshake_kind(), Some(HandshakeKind::Resumed));
assert_eq!(server.handshake_kind(), Some(HandshakeKind::Resumed));

// resumed again
let (mut client, mut server) = make_pair_for_arc_configs(&client_config, &server_config);
Expand All @@ -3878,8 +3878,8 @@ fn tls13_stateless_resumption() {
.map(|certs| certs.len()),
Some(3)
);
assert_eq!(client.handshake_kind(), Ok(HandshakeKind::Resumed));
assert_eq!(server.handshake_kind(), Ok(HandshakeKind::Resumed));
assert_eq!(client.handshake_kind(), Some(HandshakeKind::Resumed));
assert_eq!(server.handshake_kind(), Some(HandshakeKind::Resumed));
}

#[test]
Expand Down Expand Up @@ -4775,8 +4775,8 @@ fn test_client_sends_helloretryrequest() {

let (mut client, mut server) = make_pair_for_configs(client_config, server_config);

assert_eq!(client.handshake_kind(), Err(Error::HandshakeNotComplete));
assert_eq!(server.handshake_kind(), Err(Error::HandshakeNotComplete));
assert_eq!(client.handshake_kind(), None);
assert_eq!(server.handshake_kind(), None);

// client sends hello
{
Expand All @@ -4787,10 +4787,10 @@ fn test_client_sends_helloretryrequest() {
assert!(pipe.writevs[0].len() == 1);
}

assert_eq!(client.handshake_kind(), Err(Error::HandshakeNotComplete));
assert_eq!(client.handshake_kind(), None);
assert_eq!(
server.handshake_kind(),
Ok(HandshakeKind::FullWithHelloRetryRequest)
Some(HandshakeKind::FullWithHelloRetryRequest)
);

// server sends HRR
Expand All @@ -4804,11 +4804,11 @@ fn test_client_sends_helloretryrequest() {

assert_eq!(
client.handshake_kind(),
Ok(HandshakeKind::FullWithHelloRetryRequest)
Some(HandshakeKind::FullWithHelloRetryRequest)
);
assert_eq!(
server.handshake_kind(),
Ok(HandshakeKind::FullWithHelloRetryRequest)
Some(HandshakeKind::FullWithHelloRetryRequest)
);

// client sends fixed hello
Expand All @@ -4831,11 +4831,11 @@ fn test_client_sends_helloretryrequest() {

assert_eq!(
client.handshake_kind(),
Ok(HandshakeKind::FullWithHelloRetryRequest)
Some(HandshakeKind::FullWithHelloRetryRequest)
);
assert_eq!(
server.handshake_kind(),
Ok(HandshakeKind::FullWithHelloRetryRequest)
Some(HandshakeKind::FullWithHelloRetryRequest)
);

do_handshake_until_error(&mut client, &mut server).unwrap();
Expand Down

0 comments on commit 5ea02ed

Please sign in to comment.