| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use s2n_tls::{ |
| 5 |
enums::SerializationVersion, |
| 6 |
error::ErrorType, |
| 7 |
security::Policy, |
| 8 |
testing::{self, TestPair}, |
| 9 |
}; |
| 10 |
use std::task::Poll; |
| 11 |
|
| 12 |
|
| 13 |
const TLS_RECORD_HEADER_LEN: usize = 5; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
const TEST_POLICY: &str = "20240501"; |
| 18 |
|
| 19 |
fn build_serializable_config() -> s2n_tls::config::Config { |
| 20 |
let mut builder = testing::config_builder(&Policy::from_version(TEST_POLICY).unwrap()).unwrap(); |
| 21 |
builder |
| 22 |
.set_serialization_version(SerializationVersion::V1) |
| 23 |
.unwrap(); |
| 24 |
builder.build().unwrap() |
| 25 |
} |
| 26 |
|
| 27 |
fn assert_invalid_state_error(err: s2n_tls::error::Error) { |
| 28 |
assert_eq!(err.kind(), ErrorType::UsageError); |
| 29 |
assert_eq!(err.name(), "S2N_ERR_INVALID_STATE"); |
| 30 |
assert_eq!( |
| 31 |
err.message(), |
| 32 |
"Invalid state, this is the result of invalid use of an API. \ |
| 33 |
Check the API documentation for the function that raised this error for more info" |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
fn try_serialize(conn: &s2n_tls::connection::Connection) -> Result<(), s2n_tls::error::Error> { |
| 39 |
let length = conn.serialization_length()?; |
| 40 |
let mut buf = vec![0u8; length]; |
| 41 |
conn.serialize(&mut buf) |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#[test] |
| 48 |
fn serialization_io_edge_cases() { |
| 49 |
let config = build_serializable_config(); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
{ |
| 55 |
let mut pair = TestPair::from_config(&config); |
| 56 |
pair.handshake().unwrap(); |
| 57 |
|
| 58 |
|
| 59 |
assert!(pair.client.poll_send(&[0; 100]).is_ready()); |
| 60 |
|
| 61 |
|
| 62 |
pair.io.client_tx_stream.borrow_mut().truncate(2); |
| 63 |
|
| 64 |
|
| 65 |
assert!(matches!( |
| 66 |
pair.server.poll_recv(&mut [0; 100]), |
| 67 |
Poll::Pending |
| 68 |
)); |
| 69 |
|
| 70 |
assert_eq!(pair.server.peek_len(), 0); |
| 71 |
|
| 72 |
|
| 73 |
assert_invalid_state_error(try_serialize(&pair.server).unwrap_err()); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
{ |
| 78 |
let mut pair = TestPair::from_config(&config); |
| 79 |
pair.handshake().unwrap(); |
| 80 |
|
| 81 |
|
| 82 |
assert!(pair.client.poll_send(&[0; 100]).is_ready()); |
| 83 |
|
| 84 |
|
| 85 |
pair.io |
| 86 |
.client_tx_stream |
| 87 |
.borrow_mut() |
| 88 |
.truncate(TLS_RECORD_HEADER_LEN + 1); |
| 89 |
|
| 90 |
|
| 91 |
assert!(matches!( |
| 92 |
pair.server.poll_recv(&mut [0; 100]), |
| 93 |
Poll::Pending |
| 94 |
)); |
| 95 |
|
| 96 |
assert_eq!(pair.server.peek_len(), 0); |
| 97 |
|
| 98 |
|
| 99 |
assert_invalid_state_error(try_serialize(&pair.server).unwrap_err()); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
{ |
| 104 |
let mut pair = TestPair::from_config(&config); |
| 105 |
pair.handshake().unwrap(); |
| 106 |
|
| 107 |
|
| 108 |
assert!(pair.client.poll_send(&[0; 100]).is_ready()); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
assert!(matches!( |
| 113 |
pair.server.poll_recv(&mut [0; 1]), |
| 114 |
Poll::Ready(Ok(1)) |
| 115 |
)); |
| 116 |
assert_eq!(pair.server.peek_len(), 99); |
| 117 |
|
| 118 |
|
| 119 |
assert_invalid_state_error(try_serialize(&pair.server).unwrap_err()); |
| 120 |
|
| 121 |
|
| 122 |
assert!(pair.server.poll_recv(&mut [0; 100]).is_ready()); |
| 123 |
assert!(pair.server.serialize(&mut [0; 1_024]).is_ok()); |
| 124 |
} |
| 125 |
} |