From 0277318ea8cf6cab271cbacbd1c5da1a2ce5cf64 Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Sat, 17 Apr 2021 21:58:24 -0400 Subject: [PATCH 1/4] Add test coverage for pre-1970 dates --- src/calendar.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/calendar.rs b/src/calendar.rs index f1d2c444..d872beeb 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -62,11 +62,13 @@ pub fn time_from_ymdhms_utc( )) } +const MIN_SUPPORTED_YEAR: u64 = 1970; + fn days_before_year_since_unix_epoch(year: u64) -> Result { // We don't support dates before January 1, 1970 because that is the // Unix epoch. It is likely that other software won't deal well with // certificates that have dates before the epoch. - if year < 1970 { + if year < MIN_SUPPORTED_YEAR { return Err(Error::BadDerTime); } let days_before_year_ad = days_before_year_ad(year); @@ -105,8 +107,16 @@ const DAYS_BEFORE_UNIX_EPOCH_AD: u64 = 719162; mod tests { #[test] fn test_days_before_unix_epoch() { - use super::{days_before_year_ad, DAYS_BEFORE_UNIX_EPOCH_AD}; - assert_eq!(DAYS_BEFORE_UNIX_EPOCH_AD, days_before_year_ad(1970)); + use super::{days_before_year_ad, DAYS_BEFORE_UNIX_EPOCH_AD, MIN_SUPPORTED_YEAR}; + assert_eq!(DAYS_BEFORE_UNIX_EPOCH_AD, days_before_year_ad(MIN_SUPPORTED_YEAR)); + } + + #[test] + fn test_days_before_year_since_unix_epoch() { + use super::{days_before_year_since_unix_epoch, Error, MIN_SUPPORTED_YEAR}; + assert_eq!(Ok(0), days_before_year_since_unix_epoch(MIN_SUPPORTED_YEAR)); + assert_eq!(Ok(365), days_before_year_since_unix_epoch(MIN_SUPPORTED_YEAR+1)); + assert_eq!(Err(Error::BadDerTime), days_before_year_since_unix_epoch(MIN_SUPPORTED_YEAR-1)); } #[test] @@ -135,7 +145,13 @@ mod tests { #[allow(clippy::unreadable_literal)] // TODO: Make this clear. #[test] fn test_time_from_ymdhms_utc() { - use super::{time_from_ymdhms_utc, Time}; + use super::{time_from_ymdhms_utc, Time, Error, MIN_SUPPORTED_YEAR}; + + // Before 1970 + assert_eq!( + Err(Error::BadDerTime), + time_from_ymdhms_utc(MIN_SUPPORTED_YEAR-1, 1, 1, 0, 0, 0) + ); // year boundary assert_eq!( From 9cbee3f9f82aaa36d120513911f382011d4cc4c9 Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Sat, 17 Apr 2021 22:07:56 -0400 Subject: [PATCH 2/4] Add test coverage for pre-1970 dates --- src/calendar.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/calendar.rs b/src/calendar.rs index d872beeb..804f18ac 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -108,15 +108,24 @@ mod tests { #[test] fn test_days_before_unix_epoch() { use super::{days_before_year_ad, DAYS_BEFORE_UNIX_EPOCH_AD, MIN_SUPPORTED_YEAR}; - assert_eq!(DAYS_BEFORE_UNIX_EPOCH_AD, days_before_year_ad(MIN_SUPPORTED_YEAR)); + assert_eq!( + DAYS_BEFORE_UNIX_EPOCH_AD, + days_before_year_ad(MIN_SUPPORTED_YEAR) + ); } #[test] fn test_days_before_year_since_unix_epoch() { use super::{days_before_year_since_unix_epoch, Error, MIN_SUPPORTED_YEAR}; assert_eq!(Ok(0), days_before_year_since_unix_epoch(MIN_SUPPORTED_YEAR)); - assert_eq!(Ok(365), days_before_year_since_unix_epoch(MIN_SUPPORTED_YEAR+1)); - assert_eq!(Err(Error::BadDerTime), days_before_year_since_unix_epoch(MIN_SUPPORTED_YEAR-1)); + assert_eq!( + Ok(365), + days_before_year_since_unix_epoch(MIN_SUPPORTED_YEAR + 1) + ); + assert_eq!( + Err(Error::BadDerTime), + days_before_year_since_unix_epoch(MIN_SUPPORTED_YEAR - 1) + ); } #[test] @@ -145,12 +154,12 @@ mod tests { #[allow(clippy::unreadable_literal)] // TODO: Make this clear. #[test] fn test_time_from_ymdhms_utc() { - use super::{time_from_ymdhms_utc, Time, Error, MIN_SUPPORTED_YEAR}; + use super::{time_from_ymdhms_utc, Error, Time, MIN_SUPPORTED_YEAR}; // Before 1970 assert_eq!( Err(Error::BadDerTime), - time_from_ymdhms_utc(MIN_SUPPORTED_YEAR-1, 1, 1, 0, 0, 0) + time_from_ymdhms_utc(MIN_SUPPORTED_YEAR - 1, 1, 1, 0, 0, 0) ); // year boundary From cea94e4292f2431ab21e5ee489be6a435b1fd438 Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Sat, 17 Apr 2021 22:17:21 -0400 Subject: [PATCH 3/4] Add test coverage for pre-1970 dates --- src/calendar.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/calendar.rs b/src/calendar.rs index 804f18ac..8037ed27 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -62,13 +62,13 @@ pub fn time_from_ymdhms_utc( )) } -const MIN_SUPPORTED_YEAR: u64 = 1970; +const UNIX_EPOCH_YEAR: u64 = 1970; fn days_before_year_since_unix_epoch(year: u64) -> Result { // We don't support dates before January 1, 1970 because that is the // Unix epoch. It is likely that other software won't deal well with // certificates that have dates before the epoch. - if year < MIN_SUPPORTED_YEAR { + if year < UNIX_EPOCH_YEAR { return Err(Error::BadDerTime); } let days_before_year_ad = days_before_year_ad(year); @@ -107,24 +107,24 @@ const DAYS_BEFORE_UNIX_EPOCH_AD: u64 = 719162; mod tests { #[test] fn test_days_before_unix_epoch() { - use super::{days_before_year_ad, DAYS_BEFORE_UNIX_EPOCH_AD, MIN_SUPPORTED_YEAR}; + use super::{days_before_year_ad, DAYS_BEFORE_UNIX_EPOCH_AD, UNIX_EPOCH_YEAR}; assert_eq!( DAYS_BEFORE_UNIX_EPOCH_AD, - days_before_year_ad(MIN_SUPPORTED_YEAR) + days_before_year_ad(UNIX_EPOCH_YEAR) ); } #[test] fn test_days_before_year_since_unix_epoch() { - use super::{days_before_year_since_unix_epoch, Error, MIN_SUPPORTED_YEAR}; - assert_eq!(Ok(0), days_before_year_since_unix_epoch(MIN_SUPPORTED_YEAR)); + use super::{days_before_year_since_unix_epoch, Error, UNIX_EPOCH_YEAR}; + assert_eq!(Ok(0), days_before_year_since_unix_epoch(UNIX_EPOCH_YEAR)); assert_eq!( Ok(365), - days_before_year_since_unix_epoch(MIN_SUPPORTED_YEAR + 1) + days_before_year_since_unix_epoch(UNIX_EPOCH_YEAR + 1) ); assert_eq!( Err(Error::BadDerTime), - days_before_year_since_unix_epoch(MIN_SUPPORTED_YEAR - 1) + days_before_year_since_unix_epoch(UNIX_EPOCH_YEAR - 1) ); } @@ -154,12 +154,12 @@ mod tests { #[allow(clippy::unreadable_literal)] // TODO: Make this clear. #[test] fn test_time_from_ymdhms_utc() { - use super::{time_from_ymdhms_utc, Error, Time, MIN_SUPPORTED_YEAR}; + use super::{time_from_ymdhms_utc, Error, Time, UNIX_EPOCH_YEAR}; // Before 1970 assert_eq!( Err(Error::BadDerTime), - time_from_ymdhms_utc(MIN_SUPPORTED_YEAR - 1, 1, 1, 0, 0, 0) + time_from_ymdhms_utc(UNIX_EPOCH_YEAR - 1, 1, 1, 0, 0, 0) ); // year boundary From 0571f5cb4e8164a977a419baafda77f0158d9b87 Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Thu, 22 Apr 2021 08:52:23 -0400 Subject: [PATCH 4/4] Add test coverage for pre-1970 dates --- src/calendar.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/calendar.rs b/src/calendar.rs index 8037ed27..2740836f 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -156,12 +156,36 @@ mod tests { fn test_time_from_ymdhms_utc() { use super::{time_from_ymdhms_utc, Error, Time, UNIX_EPOCH_YEAR}; - // Before 1970 + // 1969-12-31 00:00:00 assert_eq!( Err(Error::BadDerTime), time_from_ymdhms_utc(UNIX_EPOCH_YEAR - 1, 1, 1, 0, 0, 0) ); + // 1969-12-31 23:59:59 + assert_eq!( + Err(Error::BadDerTime), + time_from_ymdhms_utc(UNIX_EPOCH_YEAR - 1, 12, 31, 23, 59, 59) + ); + + // 1970-01-01 00:00:00 + assert_eq!( + Time::from_seconds_since_unix_epoch(0), + time_from_ymdhms_utc(UNIX_EPOCH_YEAR, 1, 1, 0, 0, 0).unwrap() + ); + + // 1970-01-01 00:00:01 + assert_eq!( + Time::from_seconds_since_unix_epoch(1), + time_from_ymdhms_utc(UNIX_EPOCH_YEAR, 1, 1, 0, 0, 1).unwrap() + ); + + // 1971-01-01 00:00:00 + assert_eq!( + Time::from_seconds_since_unix_epoch(365 * 86400), + time_from_ymdhms_utc(UNIX_EPOCH_YEAR + 1, 1, 1, 0, 0, 0).unwrap() + ); + // year boundary assert_eq!( Time::from_seconds_since_unix_epoch(1483228799),