From e93a9be82bfb1773b5c73de06bb91b0bb25813e1 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Mon, 24 Jul 2023 09:51:35 +0200 Subject: [PATCH] Test panic in `to_rfc2822` and `to_rfc3339` --- src/datetime/tests.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index 973c5c0e3d..d416b932a9 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -451,6 +451,7 @@ fn test_datetime_with_timezone() { #[test] #[cfg(any(feature = "alloc", feature = "std"))] +#[allow(deprecated)] fn test_datetime_rfc2822() { let edt = FixedOffset::east_opt(5 * 60 * 60).unwrap(); @@ -548,10 +549,34 @@ fn test_datetime_rfc2822() { assert!(DateTime::parse_from_rfc2822("Wed. 18 Feb 2015 23:16:09 +0000").is_err()); // *trailing* space causes failure assert!(DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 +0000 ").is_err()); + + const RFC_2822_YEAR_MAX: i32 = 9999; + const RFC_2822_YEAR_MIN: i32 = 0; + + let dt = Utc.with_ymd_and_hms(RFC_2822_YEAR_MAX, 1, 2, 3, 4, 5).unwrap(); + assert_eq!(dt.to_rfc2822(), "Sat, 02 Jan 9999 03:04:05 +0000"); + + let dt = Utc.with_ymd_and_hms(RFC_2822_YEAR_MIN, 1, 2, 3, 4, 5).unwrap(); + assert_eq!(dt.to_rfc2822(), "Sun, 02 Jan 0000 03:04:05 +0000"); +} + +#[test] +#[should_panic] +#[allow(deprecated)] +fn test_rfc_2822_year_range_panic_high() { + let _ = Utc.with_ymd_and_hms(10000, 1, 2, 3, 4, 5).unwrap().to_rfc2822(); +} + +#[test] +#[should_panic] +#[allow(deprecated)] +fn test_rfc_2822_year_range_panic_low() { + let _ = Utc.with_ymd_and_hms(-1, 1, 2, 3, 4, 5).unwrap().to_rfc2822(); } #[test] #[cfg(any(feature = "alloc", feature = "std"))] +#[allow(deprecated)] fn test_datetime_rfc3339() { let edt5 = FixedOffset::east_opt(5 * 60 * 60).unwrap(); let edt0 = FixedOffset::east_opt(0).unwrap(); @@ -661,6 +686,29 @@ fn test_datetime_rfc3339() { assert!(DateTime::parse_from_rfc3339(" 2015-02-18T23:59:60.234567+05:00").is_err()); assert!(DateTime::parse_from_rfc3339("2015- 02-18T23:59:60.234567+05:00").is_err()); assert!(DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567A+05:00").is_err()); + + const RFC_3339_YEAR_MAX: i32 = 9999; + const RFC_3339_YEAR_MIN: i32 = 0; + + let dt = Utc.with_ymd_and_hms(RFC_3339_YEAR_MAX, 1, 2, 3, 4, 5).unwrap(); + assert_eq!(dt.to_rfc3339(), "9999-01-02T03:04:05+00:00"); + + let dt = Utc.with_ymd_and_hms(RFC_3339_YEAR_MIN, 1, 2, 3, 4, 5).unwrap(); + assert_eq!(dt.to_rfc3339(), "0000-01-02T03:04:05+00:00"); +} + +#[test] +#[should_panic] +#[allow(deprecated)] +fn test_rfc_3339_year_range_panic_high() { + let _ = Utc.with_ymd_and_hms(10000, 1, 2, 3, 4, 5).unwrap().to_rfc3339(); +} + +#[test] +#[should_panic] +#[allow(deprecated)] +fn test_rfc_3339_year_range_panic_low() { + let _ = Utc.with_ymd_and_hms(-1, 1, 2, 3, 4, 5).unwrap().to_rfc3339(); } #[test]