From f7972f8fa5878ade915ae5c1386ce8d02ee0eb29 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 22 Jun 2023 12:10:25 +0200 Subject: [PATCH] move some tests from src/ to tests/ --- src/fragments/datetime.rs | 1 + src/fragments/header.rs | 136 +-------------------------- src/fragments/mod.rs | 4 +- src/multipass/field_eager.rs | 2 +- src/multipass/field_lazy.rs | 2 +- src/multipass/header_section.rs | 6 +- tests/known.rs | 159 ++++++++++++++++++++++++++++++++ 7 files changed, 169 insertions(+), 141 deletions(-) create mode 100644 tests/known.rs diff --git a/src/fragments/datetime.rs b/src/fragments/datetime.rs index 44e2661..feb4020 100644 --- a/src/fragments/datetime.rs +++ b/src/fragments/datetime.rs @@ -282,6 +282,7 @@ fn obs_zone(input: &str) -> IResult<&str, Option> { #[cfg(test)] mod tests { use super::*; + use chrono::TimeZone; #[test] diff --git a/src/fragments/header.rs b/src/fragments/header.rs index 70fb97c..150dc3a 100644 --- a/src/fragments/header.rs +++ b/src/fragments/header.rs @@ -326,7 +326,8 @@ fn rescue_field(input: &str) -> IResult<&str, Field> { #[cfg(test)] mod tests { use super::*; - use crate::fragments::model::{GroupRef, AddrSpec}; + use crate::fragments::model::{GroupRef, AddrSpec, MailboxRef, AddressRef}; + use crate::fragments::model; // 3.6.1. The Origination Date Field /* #[test] @@ -511,137 +512,4 @@ Hello world"#; })) ); } - - #[test] - fn test_section() { - use chrono::{FixedOffset, TimeZone}; - use std::collections::HashMap; - - let fullmail = r#"Return-Path: -Delivered-To: quentin@example.com -Received: from smtp.example.com ([10.83.2.2]) - by doradille with LMTP - id xyzabcd - (envelope-from ) - for ; Tue, 13 Jun 2023 19:01:08 +0000 -Date: Tue, 13 Jun 2023 10:01:10 +0200 -From: Mary Smith - , "A\lan" -Sender: imf@example.com -Reply-To: "Mary Smith: Personal Account" -To: John Doe -Cc: imf2@example.com -Bcc: (hidden) -Subject: Re: Saying Hello -Comments: A simple message -Comments: Not that complicated -comments : not valid header name but should be accepted - by the parser. -Keywords: hello, world -Héron: Raté - Raté raté -Keywords: salut, le, monde -Not a real header but should still recover -Message-ID: <3456@example.net> -In-Reply-To: <1234@local.machine.example> -References: <1234@local.machine.example> -Unknown: unknown - -This is a reply to your hello. -"#; - assert_eq!( - section(fullmail), - Ok(("This is a reply to your hello.\n", HeaderSection { - date: Some(FixedOffset::east_opt(2 * 3600).unwrap().with_ymd_and_hms(2023, 06, 13, 10, 01, 10).unwrap()), - - from: vec![MailboxRef { - name: Some("Mary Smith".into()), - addrspec: AddrSpec { - local_part: "mary".into(), - domain: "example.net".into(), - } - }, MailboxRef { - name: Some("Alan".into()), - addrspec: AddrSpec { - local_part: "alan".into(), - domain: "example".into(), - } - }], - - sender: Some(MailboxRef { - name: None, - addrspec: AddrSpec { - local_part: "imf".into(), - domain: "example.com".into(), - } - }), - - reply_to: vec![AddressRef::Single(MailboxRef { - name: Some("Mary Smith: Personal Account".into()), - addrspec: AddrSpec { - local_part: "smith".into(), - domain: "home.example".into(), - } - })], - - to: vec![AddressRef::Single(MailboxRef { - name: Some("John Doe".into()), - addrspec: AddrSpec { - local_part: "jdoe".into(), - domain: "machine.example".into(), - } - })], - - cc: vec![AddressRef::Single(MailboxRef { - name: None, - addrspec: AddrSpec { - local_part: "imf2".into(), - domain: "example.com".into(), - } - })], - - bcc: vec![], - - msg_id: Some(model::MessageId { left: "3456", right: "example.net" }), - in_reply_to: vec![model::MessageId { left: "1234", right: "local.machine.example" }], - references: vec![model::MessageId { left: "1234", right: "local.machine.example" }], - - subject: Some("Re: Saying Hello".into()), - - comments: vec![ - "A simple message".into(), - "Not that complicated".into(), - "not valid header name but should be accepted by the parser.".into(), - ], - - keywords: vec![ - "hello".into(), "world".into(), "salut".into(), "le".into(), "monde".into() - ], - - received: vec![ - "from smtp.example.com ([10.83.2.2])\n\tby doradille with LMTP\n\tid xyzabcd\n\t(envelope-from )\n\tfor " - ], - - return_path: vec![MailboxRef { - name: None, - addrspec: AddrSpec { - local_part: "gitlab".into(), - domain: "example.com".into(), - } - }], - - optional: HashMap::from([ - ("Delivered-To", "quentin@example.com".into()), - ("Unknown", "unknown".into()), - ]), - - unparsed: vec![ - "Héron: Raté\n Raté raté", - "Not a real header but should still recover", - ], - - bad_fields: vec![], - })) - ); - } } diff --git a/src/fragments/mod.rs b/src/fragments/mod.rs index bef25af..e077d6c 100644 --- a/src/fragments/mod.rs +++ b/src/fragments/mod.rs @@ -5,13 +5,13 @@ pub mod model; pub mod whitespace; mod words; mod quoted; -mod misc_token; +pub mod misc_token; // Header specific mod mailbox; mod address; mod identification; -mod trace; +pub mod trace; mod datetime; pub mod lazy; pub mod eager; diff --git a/src/multipass/field_eager.rs b/src/multipass/field_eager.rs index 37f8a08..d062f13 100644 --- a/src/multipass/field_eager.rs +++ b/src/multipass/field_eager.rs @@ -33,7 +33,7 @@ mod tests { #[test] fn test_field_body() { - assert_eq!(new(field_lazy::Parsed { + assert_eq!(new(&field_lazy::Parsed { fields: vec![ lazy::Field::From(lazy::MailboxList("hello@world.com,\r\n\talice@wonderlands.com\r\n")), lazy::Field::Date(lazy::DateTime("12 Mar 1997 07:33:25 Z\r\n")), diff --git a/src/multipass/field_lazy.rs b/src/multipass/field_lazy.rs index b68b581..811243e 100644 --- a/src/multipass/field_lazy.rs +++ b/src/multipass/field_lazy.rs @@ -27,7 +27,7 @@ mod tests { #[test] fn test_field_name() { - assert_eq!(new(extract_fields::Parsed { + assert_eq!(new(&extract_fields::Parsed { fields: vec![ "From: hello@world.com,\r\n\talice@wonderlands.com\r\n", "Date: 12 Mar 1997 07:33:25 Z\r\n", diff --git a/src/multipass/header_section.rs b/src/multipass/header_section.rs index 7f716a3..017482b 100644 --- a/src/multipass/header_section.rs +++ b/src/multipass/header_section.rs @@ -53,14 +53,14 @@ mod tests { Parsed { fields: Section { from: vec![ - model::MailboxRef { + &model::MailboxRef { name: None, addrspec: model::AddrSpec { local_part: "hello".into(), domain: "world.com".into() } }, - model::MailboxRef { + &model::MailboxRef { name: None, addrspec: model::AddrSpec { local_part: "alice".into(), @@ -69,7 +69,7 @@ mod tests { }, ], - date: Some(FixedOffset::east_opt(0) + date: Some(&FixedOffset::east_opt(0) .unwrap() .with_ymd_and_hms(1997, 03, 12, 7, 33, 25) .unwrap()), diff --git a/tests/known.rs b/tests/known.rs new file mode 100644 index 0000000..b2865af --- /dev/null +++ b/tests/known.rs @@ -0,0 +1,159 @@ +use chrono::{FixedOffset, TimeZone}; +use std::collections::HashMap; +use imf_codec::multipass; +use imf_codec::fragments::{model, misc_token, trace, section}; + +fn parser<'a, F>(input: &'a [u8], func: F) -> () +where F: FnOnce(§ion::Section) -> () { + let seg = multipass::segment::new(input).unwrap(); + let charset = seg.charset(); + let fields = charset.fields().unwrap(); + let field_names = fields.names(); + let field_body = field_names.body(); + let section = field_body.section(); + + func(§ion.fields); +} + +#[test] +fn test_headers() { + let fullmail: &[u8] = r#"Return-Path: +Delivered-To: quentin@example.com +Received: from smtp.example.com ([10.83.2.2]) + by doradille with LMTP + id xyzabcd + (envelope-from ) + for ; Tue, 13 Jun 2023 19:01:08 +0000 +Date: Tue, 13 Jun 2023 10:01:10 +0200 +From: Mary Smith + , "A\lan" +Sender: imf@example.com +Reply-To: "Mary Smith: Personal Account" +To: John Doe +Cc: imf2@example.com +Bcc: (hidden) +Subject: Re: Saying Hello +Comments: A simple message +Comments: Not that complicated +comments : not valid header name but should be accepted + by the parser. +Keywords: hello, world +Héron: Raté + Raté raté +Keywords: salut, le, monde +Not a real header but should still recover +Message-ID: <3456@example.net> +In-Reply-To: <1234@local.machine.example> +References: <1234@local.machine.example> +Unknown: unknown + +This is a reply to your hello. +"#.as_bytes(); + parser(fullmail, |parsed_section| + assert_eq!( + parsed_section, + §ion::Section { + date: Some(&FixedOffset::east_opt(2 * 3600) + .unwrap() + .with_ymd_and_hms(2023, 06, 13, 10, 01, 10) + .unwrap()), + + from: vec![&model::MailboxRef { + name: Some("Mary Smith".into()), + addrspec: model::AddrSpec { + local_part: "mary".into(), + domain: "example.net".into(), + } + }, &model::MailboxRef { + name: Some("Alan".into()), + addrspec: model::AddrSpec { + local_part: "alan".into(), + domain: "example".into(), + } + }], + + sender: Some(&model::MailboxRef { + name: None, + addrspec: model::AddrSpec { + local_part: "imf".into(), + domain: "example.com".into(), + } + }), + + reply_to: vec![&model::AddressRef::Single(model::MailboxRef { + name: Some("Mary Smith: Personal Account".into()), + addrspec: model::AddrSpec { + local_part: "smith".into(), + domain: "home.example".into(), + } + })], + + to: vec![&model::AddressRef::Single(model::MailboxRef { + name: Some("John Doe".into()), + addrspec: model::AddrSpec { + local_part: "jdoe".into(), + domain: "machine.example".into(), + } + })], + + cc: vec![&model::AddressRef::Single(model::MailboxRef { + name: None, + addrspec: model::AddrSpec { + local_part: "imf2".into(), + domain: "example.com".into(), + } + })], + + bcc: vec![], + + msg_id: Some(&model::MessageId { left: "3456", right: "example.net" }), + in_reply_to: vec![&model::MessageId { left: "1234", right: "local.machine.example" }], + references: vec![&model::MessageId { left: "1234", right: "local.machine.example" }], + + subject: Some(&misc_token::Unstructured("Re: Saying Hello".into())), + + comments: vec![ + &misc_token::Unstructured("A simple message".into()), + &misc_token::Unstructured("Not that complicated".into()), + &misc_token::Unstructured("not valid header name but should be accepted by the parser.".into()), + ], + + keywords: vec![ + &misc_token::PhraseList(vec![ + "hello".into(), + "world".into(), + ]), + &misc_token::PhraseList(vec![ + "salut".into(), + "le".into(), + "monde".into(), + ]), + ], + + received: vec![ + &trace::ReceivedLog("from smtp.example.com ([10.83.2.2])\n\tby doradille with LMTP\n\tid xyzabcd\n\t(envelope-from )\n\tfor ") + ], + + return_path: vec![&model::MailboxRef { + name: None, + addrspec: model::AddrSpec { + local_part: "gitlab".into(), + domain: "example.com".into(), + } + }], + + optional: HashMap::from([ + ("Delivered-To", &misc_token::Unstructured("quentin@example.com".into())), + ("Unknown", &misc_token::Unstructured("unknown".into())), + ]), + + bad_fields: vec![], + + unparsed: vec![ + "Héron: Raté\n Raté raté\n", + "Not a real header but should still recover\n", + ], + } + ) + ) +}