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

tests: misc coverage for calendar.rs, der.rs #71

Merged
merged 2 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
57 changes: 53 additions & 4 deletions src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ 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);
debug_assert!(days_before_year_ad >= DAYS_BEFORE_UNIX_EPOCH_AD);
Ok(days_before_year_ad - DAYS_BEFORE_UNIX_EPOCH_AD)
}

const UNIX_EPOCH_YEAR: u64 = 1970;

fn days_before_year_ad(year: u64) -> u64 {
((year - 1) * 365)
+ ((year - 1) / 4) // leap years are every 4 years,
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,37 @@ 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};

// 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!(
Expand Down
66 changes: 66 additions & 0 deletions src/der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,69 @@ macro_rules! oid {
[(40 * $first) + $second, $( $tail ),*]
)
}

#[cfg(test)]
mod tests {
#[test]
fn test_optional_boolean() {
use super::{optional_boolean, Error};

// Empty input results in false
assert!(!optional_boolean(&mut bytes_reader(&[])).unwrap());

// Optional, so another data type results in false
assert!(!optional_boolean(&mut bytes_reader(&[0x05, 0x00])).unwrap());

// Only 0x00 and 0xff are accepted values
assert_eq!(
Err(Error::BadDer),
optional_boolean(&mut bytes_reader(&[0x01, 0x01, 0x42]))
);

// True
assert!(optional_boolean(&mut bytes_reader(&[0x01, 0x01, 0xff])).unwrap());

// False
assert!(!optional_boolean(&mut bytes_reader(&[0x01, 0x01, 0x00])).unwrap());
}

#[test]
fn test_bit_string_with_no_unused_bits() {
use super::{bit_string_with_no_unused_bits, Error};

// Unexpected type
assert_eq!(
Err(Error::BadDer),
bit_string_with_no_unused_bits(&mut bytes_reader(&[0x01, 0x01, 0xff]))
);

// Unexpected nonexistent type
assert_eq!(
Err(Error::BadDer),
bit_string_with_no_unused_bits(&mut bytes_reader(&[0x42, 0xff, 0xff]))
);

// Unexpected empty input
assert_eq!(
Err(Error::BadDer),
bit_string_with_no_unused_bits(&mut bytes_reader(&[]))
);

// Valid input with non-zero unused bits
assert_eq!(
Err(Error::BadDer),
bit_string_with_no_unused_bits(&mut bytes_reader(&[0x03, 0x03, 0x04, 0x12, 0x34]))
);

// Valid input
assert_eq!(
untrusted::Input::from(&[0x12, 0x34]),
bit_string_with_no_unused_bits(&mut bytes_reader(&[0x03, 0x03, 0x00, 0x12, 0x34]))
.unwrap()
);
}

fn bytes_reader(bytes: &[u8]) -> untrusted::Reader {
return untrusted::Reader::new(untrusted::Input::from(bytes));
}
}