Skip to content

Commit

Permalink
fix: add length tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KolbyML committed Jan 16, 2025
1 parent ef82117 commit 28c8734
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -112,6 +112,10 @@ impl FromStr for ClientInfo {
type Err = anyhow::Error;

fn from_str(string: &str) -> Result<Self, anyhow::Error> {
ensure!(
string.as_bytes().len() <= 200,
"Client info string is too long"
);
let parts: Vec<&str> = string.split('/').collect();

if parts.len() != 4 {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 28c8734

Please sign in to comment.