Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage for pre-1970 dates #215

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ pub fn time_from_ymdhms_utc(
))
}

const UNIX_EPOCH_YEAR: u64 = 1970;

fn days_before_year_since_unix_epoch(year: u64) -> Result<u64, Error> {
// 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 < UNIX_EPOCH_YEAR {
return Err(Error::BadDerTime);
}
let days_before_year_ad = days_before_year_ad(year);
Expand Down Expand Up @@ -105,8 +107,25 @@ 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, UNIX_EPOCH_YEAR};
assert_eq!(
DAYS_BEFORE_UNIX_EPOCH_AD,
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, 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(UNIX_EPOCH_YEAR + 1)
);
assert_eq!(
Err(Error::BadDerTime),
days_before_year_since_unix_epoch(UNIX_EPOCH_YEAR - 1)
);
}

#[test]
Expand Down Expand Up @@ -135,7 +154,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, Error, Time, UNIX_EPOCH_YEAR};

// Before 1970
assert_eq!(
Err(Error::BadDerTime),
time_from_ymdhms_utc(UNIX_EPOCH_YEAR - 1, 1, 1, 0, 0, 0)
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems OK to me, but I'd also like to see test cases for the very edge cases: the moment before the first of 1970 and exactly 1970.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the tests to cover:

  • 1969-12-31 00:00:00
  • 1969-12-31 23:59:59
  • 1970-01-01 00:00:00
  • 1970-01-01 00:00:01
  • 1971-01-01 00:00:00

This would catch off by one errors in date calculations.


// year boundary
assert_eq!(
Expand Down