From 28c8734bb0e5b4fa3a416760ebea4704bf8ae7e5 Mon Sep 17 00:00:00 2001 From: Kolby Moroz Liebl <31669092+KolbyML@users.noreply.github.com> Date: Thu, 16 Jan 2025 12:57:15 -0700 Subject: [PATCH] fix: add length tests --- .../src/types/ping_extensions/extensions/type_0.rs | 8 +++++++- .../types/ping_extensions/extensions/type_65535.rs | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/ethportal-api/src/types/ping_extensions/extensions/type_0.rs b/crates/ethportal-api/src/types/ping_extensions/extensions/type_0.rs index 6497a4337..818a9239f 100644 --- a/crates/ethportal-api/src/types/ping_extensions/extensions/type_0.rs +++ b/crates/ethportal-api/src/types/ping_extensions/extensions/type_0.rs @@ -1,6 +1,6 @@ use std::str::FromStr; -use anyhow::bail; +use anyhow::{bail, ensure}; use itertools::Itertools; use ssz::{Decode, Encode}; use ssz_derive::{Decode, Encode}; @@ -112,6 +112,10 @@ impl FromStr for ClientInfo { type Err = anyhow::Error; fn from_str(string: &str) -> Result { + ensure!( + string.as_bytes().len() <= 200, + "Client info string is too long" + ); let parts: Vec<&str> = string.split('/').collect(); if parts.len() != 4 { @@ -250,6 +254,8 @@ mod tests { #[case("trin/0.1.1/linux-x86_64/rustc1.81.0")] /// Fails because the CPU architecture is missing #[case("trin/0.1.1-2b00d730/linux/rustc1.81.0")] + /// Fails because client string is too long + #[case(&"t".repeat(201))] #[should_panic] fn test_client_info_from_str_invalid(#[case] string: &str) { ClientInfo::from_str(string).unwrap(); diff --git a/crates/ethportal-api/src/types/ping_extensions/extensions/type_65535.rs b/crates/ethportal-api/src/types/ping_extensions/extensions/type_65535.rs index 57b3e821b..eb6e44856 100644 --- a/crates/ethportal-api/src/types/ping_extensions/extensions/type_65535.rs +++ b/crates/ethportal-api/src/types/ping_extensions/extensions/type_65535.rs @@ -101,6 +101,18 @@ mod tests { assert_eq!(ping_error, decoded); } + #[rstest::rstest] + #[case(301, true)] + #[case(300, false)] + fn test_ping_error_message_too_long(#[case] message_length: usize, #[case] expected: bool) { + let error_code = ErrorCodes::FailedToDecodePayload; + let message = vec![0; message_length]; + assert_eq!( + PingError::new_with_message(error_code, message).is_err(), + expected + ); + } + #[test] fn message_encoding_pong_basic_radius() { let error_code = ErrorCodes::FailedToDecodePayload;