From 798d38b5e8d74079339625b6a074fa3153f6bdeb Mon Sep 17 00:00:00 2001 From: Andrew Hickman Date: Fri, 20 Jan 2023 23:13:13 +0000 Subject: [PATCH 1/3] Fix panic in MetadataKey::from_bytes The signature of this function suggests it returns an error on failure, and the documentation makes no mention of panicking. --- tonic/src/metadata/key.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tonic/src/metadata/key.rs b/tonic/src/metadata/key.rs index a5a1248be..0915b12cb 100644 --- a/tonic/src/metadata/key.rs +++ b/tonic/src/metadata/key.rs @@ -42,7 +42,7 @@ impl MetadataKey { match HeaderName::from_bytes(src) { Ok(name) => { if !VE::is_valid_key(name.as_str()) { - panic!("invalid metadata key") + return Err(InvalidMetadataKey::new()); } Ok(MetadataKey { @@ -282,3 +282,19 @@ impl Default for InvalidMetadataKey { } impl Error for InvalidMetadataKey {} + +#[test] +fn test_from_bytes_binary() { + assert!(BinaryMetadataKey::from_bytes(b"").is_err()); + assert!(BinaryMetadataKey::from_bytes(b"\xFF").is_err()); + assert!(BinaryMetadataKey::from_bytes(b"abc").is_err()); + assert_eq!(BinaryMetadataKey::from_bytes(b"abc-bin").unwrap().as_str(), "abc-bin"); +} + +#[test] +fn test_from_bytes_ascii() { + assert!(AsciiMetadataKey::from_bytes(b"").is_err()); + assert!(AsciiMetadataKey::from_bytes(b"\xFF").is_err()); + assert_eq!(AsciiMetadataKey::from_bytes(b"abc").unwrap().as_str(), "abc"); + assert!(AsciiMetadataKey::from_bytes(b"abc-bin").is_err()); +} From 46d1787553ee0c22a954bd09e47e17fd55d75126 Mon Sep 17 00:00:00 2001 From: Andrew Hickman Date: Fri, 20 Jan 2023 23:30:57 +0000 Subject: [PATCH 2/3] rustfmt --- tonic/src/metadata/key.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tonic/src/metadata/key.rs b/tonic/src/metadata/key.rs index 0915b12cb..3b26d086e 100644 --- a/tonic/src/metadata/key.rs +++ b/tonic/src/metadata/key.rs @@ -288,13 +288,19 @@ fn test_from_bytes_binary() { assert!(BinaryMetadataKey::from_bytes(b"").is_err()); assert!(BinaryMetadataKey::from_bytes(b"\xFF").is_err()); assert!(BinaryMetadataKey::from_bytes(b"abc").is_err()); - assert_eq!(BinaryMetadataKey::from_bytes(b"abc-bin").unwrap().as_str(), "abc-bin"); + assert_eq!( + BinaryMetadataKey::from_bytes(b"abc-bin").unwrap().as_str(), + "abc-bin" + ); } #[test] fn test_from_bytes_ascii() { assert!(AsciiMetadataKey::from_bytes(b"").is_err()); assert!(AsciiMetadataKey::from_bytes(b"\xFF").is_err()); - assert_eq!(AsciiMetadataKey::from_bytes(b"abc").unwrap().as_str(), "abc"); + assert_eq!( + AsciiMetadataKey::from_bytes(b"abc").unwrap().as_str(), + "abc" + ); assert!(AsciiMetadataKey::from_bytes(b"abc-bin").is_err()); } From eb16083ad1c968d28d536c2dd818117df775e893 Mon Sep 17 00:00:00 2001 From: Andrew Hickman Date: Tue, 7 Feb 2023 20:06:41 +0000 Subject: [PATCH 3/3] Move tests into #[cfg(test)] module --- tonic/src/metadata/key.rs | 43 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/tonic/src/metadata/key.rs b/tonic/src/metadata/key.rs index 3b26d086e..e40660d60 100644 --- a/tonic/src/metadata/key.rs +++ b/tonic/src/metadata/key.rs @@ -283,24 +283,29 @@ impl Default for InvalidMetadataKey { impl Error for InvalidMetadataKey {} -#[test] -fn test_from_bytes_binary() { - assert!(BinaryMetadataKey::from_bytes(b"").is_err()); - assert!(BinaryMetadataKey::from_bytes(b"\xFF").is_err()); - assert!(BinaryMetadataKey::from_bytes(b"abc").is_err()); - assert_eq!( - BinaryMetadataKey::from_bytes(b"abc-bin").unwrap().as_str(), - "abc-bin" - ); -} +#[cfg(test)] +mod tests { + use super::{AsciiMetadataKey, BinaryMetadataKey}; + + #[test] + fn test_from_bytes_binary() { + assert!(BinaryMetadataKey::from_bytes(b"").is_err()); + assert!(BinaryMetadataKey::from_bytes(b"\xFF").is_err()); + assert!(BinaryMetadataKey::from_bytes(b"abc").is_err()); + assert_eq!( + BinaryMetadataKey::from_bytes(b"abc-bin").unwrap().as_str(), + "abc-bin" + ); + } -#[test] -fn test_from_bytes_ascii() { - assert!(AsciiMetadataKey::from_bytes(b"").is_err()); - assert!(AsciiMetadataKey::from_bytes(b"\xFF").is_err()); - assert_eq!( - AsciiMetadataKey::from_bytes(b"abc").unwrap().as_str(), - "abc" - ); - assert!(AsciiMetadataKey::from_bytes(b"abc-bin").is_err()); + #[test] + fn test_from_bytes_ascii() { + assert!(AsciiMetadataKey::from_bytes(b"").is_err()); + assert!(AsciiMetadataKey::from_bytes(b"\xFF").is_err()); + assert_eq!( + AsciiMetadataKey::from_bytes(b"abc").unwrap().as_str(), + "abc" + ); + assert!(AsciiMetadataKey::from_bytes(b"abc-bin").is_err()); + } }