Debian Codegraph @

crt/s2n/bindings/rust/extended/s2n-tls-tokio/tests/into_parts.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use s2n_tls_tokio::{TlsAcceptor, TlsConnector, TlsStream};
5 use tokio::io::{AsyncReadExt, AsyncWriteExt};
6
7 pub mod common;
8
9 const TEST_DATA: &[u8] = "hello world".as_bytes();
10
11 // Decompose an established stream with into_parts and reassemble it with
12 // from_parts, then confirm the reassembled stream still sends and receives over
13 // the same negotiated session.
14 #[tokio::test]
15 async fn into_parts_from_parts_round_trip() -> Result<(), Box<dyn std::error::Error>> {
16 let (server_stream, client_stream) = common::get_streams().await?;
17 let connector = TlsConnector::new(common::client_config()?.build()?);
18 let acceptor = TlsAcceptor::new(common::server_config()?.build()?);
19 let (mut client, server) =
20 common::run_negotiate(&connector, client_stream, &acceptor, server_stream).await?;
21
22 // Take the server stream apart and put it back together.
23 let (conn, tcp) = server.into_parts();
24 let mut server = TlsStream::from_parts(conn, tcp);
25
26 // The reassembled stream continues to work on the same session.
27 client.write_all(TEST_DATA).await?;
28 let mut received = [0; TEST_DATA.len()];
29 assert_eq!(server.read_exact(&mut received).await?, TEST_DATA.len());
30 assert_eq!(TEST_DATA, received);
31
32 Ok(())
33 }
34
35 // Send data before into_parts and read it after from_parts. Data in flight at
36 // the handoff lives in the Connection's buffer or the moved socket, both of
37 // which are carried across by into_parts/from_parts, so this confirms it
38 // survives the handoff.
39 #[tokio::test]
40 async fn into_parts_preserves_buffered_data() -> Result<(), Box<dyn std::error::Error>> {
41 let (server_stream, client_stream) = common::get_streams().await?;
42 let connector = TlsConnector::new(common::client_config()?.build()?);
43 let acceptor = TlsAcceptor::new(common::server_config()?.build()?);
44 let (mut client, mut server) =
45 common::run_negotiate(&connector, client_stream, &acceptor, server_stream).await?;
46
47 // Client sends before the handoff, and the server reads enough to pull the
48 // record off the socket and into the Connection's buffer.
49 client.write_all(TEST_DATA).await?;
50 client.flush().await?;
51 let mut received = [0; TEST_DATA.len()];
52 server.read_exact(&mut received).await?;
53 assert_eq!(TEST_DATA, received);
54
55 // A second message is sent before the stream is taken apart.
56 client.write_all(TEST_DATA).await?;
57 client.flush().await?;
58
59 // Take the server stream apart and put it back together.
60 let (conn, tcp) = server.into_parts();
61 let mut server = TlsStream::from_parts(conn, tcp);
62
63 // Ensure data sent before the handoff is still readable afterwards.
64 let mut received = [0; TEST_DATA.len()];
65 assert_eq!(server.read_exact(&mut received).await?, TEST_DATA.len());
66 assert_eq!(TEST_DATA, received);
67
68 Ok(())
69 }