Skip to content

Commit

Permalink
🚧 WIP tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GiyoMoon committed Oct 8, 2023
1 parent ce0c353 commit aa7607d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 60 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ include = [
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
serde = ["dep:serde"]

[dependencies.serde]
version = "1.0"
default-features = false
Expand Down
109 changes: 56 additions & 53 deletions src/local/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,10 @@ mod local_tests {

#[test]
fn footer_julian_day() {
let mut header = b"TZif3".to_vec();
header.append(&mut vec![b'\0'; 15]);
header.append(&mut vec![b'\0'; 4 * 6]);
let bytes =
b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x0aCET-1CEST,J100,J200\x0a";

let footer: Vec<u8> = b"\x0aCET-1CEST,J100,J200\x0a".to_vec();
let bytes: Vec<u8> = [header.clone(), header.clone(), footer].concat();
let time_zone = TimeZone::from_tzif(bytes.as_slice()).unwrap();
let time_zone = TimeZone::from_tzif(bytes).unwrap();
assert_eq!(
Some(TransitionRule::Alternate(AlternateLocalTimeType::new(
LocalTimeType::new(3600, false),
Expand Down Expand Up @@ -316,9 +313,9 @@ mod local_tests {
time_zone.to_local_time_type(1735689599)
);

let footer: Vec<u8> = b"\x0aCET-1CEST,99,199\x0a".to_vec();
let bytes: Vec<u8> = [header.clone(), header, footer].concat();
let time_zone = TimeZone::from_tzif(bytes.as_slice()).unwrap();
let bytes =
b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x0aCET-1CEST,99,199\x0a";
let time_zone = TimeZone::from_tzif(bytes).unwrap();
assert_eq!(
Some(TransitionRule::Alternate(AlternateLocalTimeType::new(
LocalTimeType::new(3600, false),
Expand Down Expand Up @@ -383,13 +380,8 @@ mod local_tests {

#[test]
fn footer_month_week_day() {
let mut header = b"TZif3".to_vec();
header.append(&mut vec![b'\0'; 15]);
header.append(&mut vec![b'\0'; 4 * 6]);

let footer: Vec<u8> = b"\x0aCET-1CEST,M3.5.0,M10.5.0\x0a".to_vec();
let bytes: Vec<u8> = [header.clone(), header.clone(), footer].concat();
let time_zone = TimeZone::from_tzif(bytes.as_slice()).unwrap();
let bytes = b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x0aCET-1CEST,M3.5.0,M10.5.0\x0a";
let time_zone = TimeZone::from_tzif(bytes).unwrap();
assert_eq!(
Some(TransitionRule::Alternate(AlternateLocalTimeType::new(
LocalTimeType::new(3600, false),
Expand Down Expand Up @@ -429,60 +421,71 @@ mod local_tests {

#[test]
fn invalid_tzif_file() {
let header = b"".to_vec();
let result = TimeZone::from_tzif(header.as_slice());
let result = TimeZone::from_tzif(b"");
assert!(result.is_err());
println!("{}", result.unwrap_err());
let header = b"InvalidMagic".to_vec();
let result = TimeZone::from_tzif(header.as_slice());
let result = TimeZone::from_tzif(b"InvalidMagic");
assert!(result.is_err());
println!("{}", result.unwrap_err());
let header = b"TZif4".to_vec();
let result = TimeZone::from_tzif(header.as_slice());
let result = TimeZone::from_tzif(b"TZif4");
assert!(result.is_err());
println!("{}", result.unwrap_err());
let header = b"TZif".to_vec();
assert!(TimeZone::from_tzif(header.as_slice()).is_err());
let header = b"TZif3".to_vec();
assert!(TimeZone::from_tzif(header.as_slice()).is_err());

let mut header = b"TZif3".to_vec();
header.append(&mut vec![b'\0'; 15]);
assert!(TimeZone::from_tzif(header.as_slice()).is_err());
let mut header = b"TZif3".to_vec();
header.append(&mut vec![b'\0'; 15]);
header.append(&mut vec![b'\xFF'; 4]);
assert!(TimeZone::from_tzif(b"TZif").is_err());
assert!(TimeZone::from_tzif(b"TZif3").is_err());

let header = b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
assert!(TimeZone::from_tzif(header).is_err());
let header = b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xFF\xFF\xFF\xFF";
assert!(TimeZone::from_tzif(header).is_err());
let header = b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
assert!(TimeZone::from_tzif(header.as_slice()).is_err());
let mut header = b"TZif3".to_vec();
header.append(&mut vec![b'\0'; 15]);
header.append(&mut vec![b'\xFF'; 4 * 2]);
let header =
b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
assert!(TimeZone::from_tzif(header.as_slice()).is_err());
let mut header = b"TZif3".to_vec();
header.append(&mut vec![b'\0'; 15]);
header.append(&mut vec![b'\xFF'; 4 * 3]);
let header = b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
assert!(TimeZone::from_tzif(header.as_slice()).is_err());
let mut header = b"TZif3".to_vec();
header.append(&mut vec![b'\0'; 15]);
header.append(&mut vec![b'\xFF'; 4 * 4]);
assert!(TimeZone::from_tzif(header.as_slice()).is_err());
let mut header = b"TZif3".to_vec();
header.append(&mut vec![b'\0'; 15]);
header.append(&mut vec![b'\xFF'; 4 * 5]);
let header = b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF";
assert!(TimeZone::from_tzif(header.as_slice()).is_err());

let bytes = b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFFTZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x0a\x0f\x0a";
let result = TimeZone::from_tzif(bytes);
assert!(result.is_err());

let bytes = b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFFTZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x0a\xc0\x0a";
let result = TimeZone::from_tzif(bytes);
assert!(result.is_err());

let mut header = b"TZif3".to_vec();
header.append(&mut vec![b'\0'; 15]);
header.append(&mut vec![b'\0'; 4 * 6]);

let footer: Vec<u8> = b"\x0a\x0f\x0a".to_vec();
let footer: Vec<u8> = b"\x0aCET-1CEST0M3.5.0,M10.5.0\x0a".to_vec();
let bytes: Vec<u8> = [header.clone(), header.clone(), footer].concat();
let result = TimeZone::from_tzif(bytes.as_slice());
assert!(result.is_err());

let footer: Vec<u8> = b"\x0a\xc0\x0a".to_vec();
assert!(TimeZone::from_tzif(bytes.as_slice()).is_err());
let footer: Vec<u8> = b"\x0aCET-1CEST0\x0a".to_vec();
let bytes: Vec<u8> = [header.clone(), header.clone(), footer].concat();
let result = TimeZone::from_tzif(bytes.as_slice());
assert!(result.is_err());
assert!(TimeZone::from_tzif(bytes.as_slice()).is_err());
let footer: Vec<u8> = b"\x0a\x0a".to_vec();
let bytes: Vec<u8> = [header.clone(), header.clone(), footer].concat();
assert!(TimeZone::from_tzif(bytes.as_slice()).is_err());
}

#[test]
fn data_block() {
let bytes = b"TZif\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\0\xC8";
assert!(TimeZone::from_tzif(bytes).is_err());
let bytes = b"TZif\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\0\x65\x22\x9F\xAB";
assert!(TimeZone::from_tzif(bytes).is_err());
let bytes = b"TZif\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\x01\0\0\0\0\x65\x22\x9F\xAB\x01";
assert!(TimeZone::from_tzif(bytes).is_err());
let bytes = b"TZif\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\x01\0\0\0\x01\x65\x22\x9F\xAB\x01\x00\x00\x00\x00\x00\x00";
assert!(TimeZone::from_tzif(bytes).is_err());
let bytes = b"TZif\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\x01\0\0\0\x01\0\0\0\x01\x65\x22\x9F\xAB\x01\x00\x00\x00\x00\x00\x00\x00";
assert!(TimeZone::from_tzif(bytes).is_err());
let bytes = b"TZif\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\x01\0\0\0\x01\0\0\0\x01\0\0\0\x01\x65\x22\x9F\xAB\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
assert!(TimeZone::from_tzif(bytes).is_err());
let bytes = b"TZif\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\x01\0\0\0\x01\0\0\0\x01\0\0\0\x01\0\0\0\x01\x65\x22\x9F\xAB\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
assert!(TimeZone::from_tzif(bytes).is_err());
}

#[test]
Expand Down
4 changes: 0 additions & 4 deletions src/local/transition_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ impl TransitionRule {
None => return Err(TimeZoneError::InvalidTzFile("Invalid footer")),
};

if cursor.get_next()? != b',' {
return Err(TimeZoneError::InvalidTzFile("Invalid footer"));
};

cursor.read_tag(b",")?;

let (std_end, std_end_time) = parse_tz_string_rule(&mut cursor, string_extensions)?;
Expand Down

0 comments on commit aa7607d

Please sign in to comment.