feat(client): implement protocol configuration validation

This commit is contained in:
Nils Andreas Svee committed 2026-05-03 16:12:10 +07:00
1 parent 474aefb723
commit 8c9afce30f
1 file changed
+45 -1
+45 -1
View File
@@ -11,7 +11,7 @@ use irtt_proto::{
}; };
use crate::{ use crate::{
config::{ClientConfig, RecvBudget, RunMode}, config::{ClientConfig, RecvBudget, RunMode, MAX_DSCP_CODEPOINT, MAX_SERVER_FILL_BYTES},
error::ClientError, error::ClientError,
event::{ event::{
ClientEvent, OneWayDelaySample, OpenOutcome, PacketMeta, ReceivedStatsSample, RttSample, ClientEvent, OneWayDelaySample, OpenOutcome, PacketMeta, ReceivedStatsSample, RttSample,
@@ -752,6 +752,7 @@ fn build_received_stats(reply: &EchoReply) -> Option<ReceivedStatsSample> {
} }
fn params_from_config(config: &ClientConfig) -> Result<Params, ClientError> { fn params_from_config(config: &ClientConfig) -> Result<Params, ClientError> {
validate_protocol_config(config)?;
Ok(Params { Ok(Params {
protocol_version: PROTOCOL_VERSION, protocol_version: PROTOCOL_VERSION,
duration_ns: duration_to_ns(config.duration.unwrap_or_default())?, duration_ns: duration_to_ns(config.duration.unwrap_or_default())?,
@@ -765,6 +766,25 @@ fn params_from_config(config: &ClientConfig) -> Result<Params, ClientError> {
}) })
} }
fn validate_protocol_config(config: &ClientConfig) -> Result<(), ClientError> {
if config.dscp > MAX_DSCP_CODEPOINT {
return Err(ClientError::InvalidConfig {
reason: format!("dscp must be <= {MAX_DSCP_CODEPOINT}"),
});
}
if let Some(fill) = &config.server_fill {
let len = fill.len();
if len > MAX_SERVER_FILL_BYTES {
return Err(ClientError::InvalidConfig {
reason: format!("server_fill must be <= {MAX_SERVER_FILL_BYTES} bytes, got {len}"),
});
}
}
Ok(())
}
fn duration_to_ns(duration: Duration) -> Result<i64, ClientError> { fn duration_to_ns(duration: Duration) -> Result<i64, ClientError> {
i64::try_from(duration.as_nanos()).map_err(|_| ClientError::DurationOverflow) i64::try_from(duration.as_nanos()).map_err(|_| ClientError::DurationOverflow)
} }
@@ -1106,6 +1126,30 @@ mod tests {
assert_eq!(params_from_config(&config).unwrap().dscp, 63); assert_eq!(params_from_config(&config).unwrap().dscp, 63);
} }
#[test]
fn params_from_config_rejects_invalid_dscp_codepoint() {
let config = ClientConfig {
dscp: 64,
..ClientConfig::default()
};
assert!(matches!(
params_from_config(&config),
Err(ClientError::InvalidConfig { .. })
));
}
#[test]
fn params_from_config_rejects_oversized_server_fill() {
let config = ClientConfig {
server_fill: Some("0123456789abcdef0123456789abcdefx".to_owned()),
..ClientConfig::default()
};
assert!(matches!(
params_from_config(&config),
Err(ClientError::InvalidConfig { .. })
));
}
#[test] #[test]
fn address_resolution_connects_to_local_fake_server() { fn address_resolution_connects_to_local_fake_server() {
let server = start_fake_server(|socket, tx| { let server = start_fake_server(|socket, tx| {