diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/behavior.rs | 118 | ||||
-rw-r--r-- | tests/common/fragments.rs | 194 | ||||
-rw-r--r-- | tests/common/mod.rs | 9 |
3 files changed, 250 insertions, 71 deletions
diff --git a/tests/behavior.rs b/tests/behavior.rs index 17f3a72..205f5e1 100644 --- a/tests/behavior.rs +++ b/tests/behavior.rs @@ -2,6 +2,7 @@ use anyhow::Context; mod common; use crate::common::fragments::*; +use crate::common::constants::*; fn main() { rfc3501_imap4rev1_base(); @@ -9,10 +10,12 @@ fn main() { rfc5161_imapext_enable(); rfc6851_imapext_move(); rfc7888_imapext_literal(); + rfc4551_imapext_condstore(); + println!("โ
SUCCESS ๐๐๐ฅณ๐๐ฅน"); } fn rfc3501_imap4rev1_base() { - println!("rfc3501_imap4rev1_base"); + println!("๐งช rfc3501_imap4rev1_base"); common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| { connect(imap_socket).context("server says hello")?; capability(imap_socket, Extension::None).context("check server capabilities")?; @@ -20,20 +23,26 @@ fn rfc3501_imap4rev1_base() { create_mailbox(imap_socket, Mailbox::Archive).context("created mailbox archive")?; // UNSUBSCRIBE IS NOT IMPLEMENTED YET //unsubscribe_mailbox(imap_socket).context("unsubscribe from archive")?; - select(imap_socket, Mailbox::Inbox, None).context("select inbox")?; + let select_res = select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?; + assert!(select_res.contains("* 0 EXISTS")); + check(imap_socket).context("check must run")?; - status_mailbox(imap_socket, Mailbox::Archive).context("status of archive from inbox")?; + status(imap_socket, Mailbox::Archive, StatusKind::UidNext).context("status of archive from inbox")?; lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; lmtp_deliver_email(lmtp_socket, Email::Multipart).context("mail delivered successfully")?; - noop_exists(imap_socket).context("noop loop must detect a new email")?; - fetch_rfc822(imap_socket, Selection::FirstId, Email::Multipart) + noop_exists(imap_socket, 1).context("noop loop must detect a new email")?; + + let srv_msg = fetch(imap_socket, Selection::FirstId, FetchKind::Rfc822, FetchMod::None) .context("fetch rfc822 message, should be our first message")?; + let orig_email = std::str::from_utf8(EMAIL1)?; + assert!(srv_msg.contains(orig_email)); + copy(imap_socket, Selection::FirstId, Mailbox::Archive) .context("copy message to the archive mailbox")?; append_email(imap_socket, Email::Basic).context("insert email in INBOX")?; - // SEARCH IS NOT IMPLEMENTED YET - //search(imap_socket).expect("search should return something"); - add_flags_email(imap_socket, Selection::FirstId, Flag::Deleted) + noop_exists(imap_socket, 2).context("noop loop must detect a new email")?; + search(imap_socket, SearchKind::Text("OoOoO")).expect("search should return something"); + store(imap_socket, Selection::FirstId, Flag::Deleted, StoreAction::AddFlags, StoreMod::None) .context("should add delete flag to the email")?; expunge(imap_socket).context("expunge emails")?; rename_mailbox(imap_socket, Mailbox::Archive, Mailbox::Drafts) @@ -45,7 +54,7 @@ fn rfc3501_imap4rev1_base() { } fn rfc3691_imapext_unselect() { - println!("rfc3691_imapext_unselect"); + println!("๐งช rfc3691_imapext_unselect"); common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| { connect(imap_socket).context("server says hello")?; @@ -54,18 +63,26 @@ fn rfc3691_imapext_unselect() { capability(imap_socket, Extension::Unselect).context("check server capabilities")?; login(imap_socket, Account::Alice).context("login test")?; - select(imap_socket, Mailbox::Inbox, None).context("select inbox")?; - noop_exists(imap_socket).context("noop loop must detect a new email")?; - add_flags_email(imap_socket, Selection::FirstId, Flag::Deleted) + let select_res = select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?; + assert!(select_res.contains("* 0 EXISTS")); + + noop_exists(imap_socket, 1).context("noop loop must detect a new email")?; + store(imap_socket, Selection::FirstId, Flag::Deleted, StoreAction::AddFlags, StoreMod::None) .context("add delete flags to the email")?; unselect(imap_socket) .context("unselect inbox while preserving email with the \\Delete flag")?; - select(imap_socket, Mailbox::Inbox, Some(1)).context("select inbox again")?; - fetch_rfc822(imap_socket, Selection::FirstId, Email::Basic) + let select_res = select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox again")?; + assert!(select_res.contains("* 1 EXISTS")); + + let srv_msg = fetch(imap_socket, Selection::FirstId, FetchKind::Rfc822, FetchMod::None) .context("message is still present")?; + let orig_email = std::str::from_utf8(EMAIL2)?; + assert!(srv_msg.contains(orig_email)); + close(imap_socket).context("close inbox and expunge message")?; - select(imap_socket, Mailbox::Inbox, Some(0)) + let select_res = select(imap_socket, Mailbox::Inbox, SelectMod::None) .context("select inbox again and check it's empty")?; + assert!(select_res.contains("* 0 EXISTS")); Ok(()) }) @@ -73,7 +90,7 @@ fn rfc3691_imapext_unselect() { } fn rfc5161_imapext_enable() { - println!("rfc5161_imapext_enable"); + println!("๐งช rfc5161_imapext_enable"); common::aerogramme_provider_daemon_dev(|imap_socket, _lmtp_socket| { connect(imap_socket).context("server says hello")?; login(imap_socket, Account::Alice).context("login test")?; @@ -87,26 +104,37 @@ fn rfc5161_imapext_enable() { } fn rfc6851_imapext_move() { - println!("rfc6851_imapext_move"); + println!("๐งช rfc6851_imapext_move"); common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| { connect(imap_socket).context("server says hello")?; capability(imap_socket, Extension::Move).context("check server capabilities")?; login(imap_socket, Account::Alice).context("login test")?; create_mailbox(imap_socket, Mailbox::Archive).context("created mailbox archive")?; - select(imap_socket, Mailbox::Inbox, None).context("select inbox")?; + let select_res = select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?; + assert!(select_res.contains("* 0 EXISTS")); lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?; - noop_exists(imap_socket).context("noop loop must detect a new email")?; + noop_exists(imap_socket, 1).context("noop loop must detect a new email")?; r#move(imap_socket, Selection::FirstId, Mailbox::Archive) .context("message from inbox moved to archive")?; unselect(imap_socket) .context("unselect inbox while preserving email with the \\Delete flag")?; - select(imap_socket, Mailbox::Archive, Some(1)).context("select archive")?; - fetch_rfc822(imap_socket, Selection::FirstId, Email::Basic).context("check mail exists")?; + let select_res = select(imap_socket, Mailbox::Archive, SelectMod::None).context("select archive")?; + assert!(select_res.contains("* 1 EXISTS")); + + let srv_msg = fetch( + imap_socket, + Selection::FirstId, + FetchKind::Rfc822, + FetchMod::None, + ).context("check mail exists")?; + let orig_email = std::str::from_utf8(EMAIL2)?; + assert!(srv_msg.contains(orig_email)); + logout(imap_socket).context("must quit")?; Ok(()) @@ -115,7 +143,7 @@ fn rfc6851_imapext_move() { } fn rfc7888_imapext_literal() { - println!("rfc7888_imapext_literal"); + println!("๐งช rfc7888_imapext_literal"); common::aerogramme_provider_daemon_dev(|imap_socket, _lmtp_socket| { connect(imap_socket).context("server says hello")?; @@ -126,3 +154,49 @@ fn rfc7888_imapext_literal() { }) .expect("test fully run"); } + +fn rfc4551_imapext_condstore() { + println!("๐งช rfc4551_imapext_condstore"); + common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| { + // Setup the test + connect(imap_socket).context("server says hello")?; + + // RFC 3.1.1 Advertising Support for CONDSTORE + capability(imap_socket, Extension::Condstore).context("check server capabilities")?; + login(imap_socket, Account::Alice).context("login test")?; + + // RFC 3.1.8. CONDSTORE Parameter to SELECT and EXAMINE + let select_res = select(imap_socket, Mailbox::Inbox, SelectMod::Condstore).context("select inbox")?; + // RFC 3.1.2 New OK Untagged Responses for SELECT and EXAMINE + assert!(select_res.contains("[HIGHESTMODSEQ 1]")); + + // RFC 3.1.3. STORE and UID STORE Commands + lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; + lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?; + lmtp_deliver_email(lmtp_socket, Email::Multipart).context("mail delivered successfully")?; + noop_exists(imap_socket, 2).context("noop loop must detect a new email")?; + let store_res = store(imap_socket, Selection::All, Flag::Important, StoreAction::AddFlags, StoreMod::UnchangedSince(1))?; + assert!(store_res.contains("[MODIFIED 2]")); + assert!(store_res.contains("* 1 FETCH (FLAGS (\\Important) MODSEQ (3))")); + assert!(!store_res.contains("* 2 FETCH")); + assert_eq!(store_res.lines().count(), 2); + + // RFC 3.1.4. FETCH and UID FETCH Commands + let fetch_res = fetch(imap_socket, Selection::All, FetchKind::Rfc822Size, FetchMod::ChangedSince(2))?; + assert!(fetch_res.contains("* 1 FETCH (RFC822.SIZE 84 MODSEQ (3))")); + assert!(!fetch_res.contains("* 2 FETCH")); + assert_eq!(store_res.lines().count(), 2); + + // RFC 3.1.5. MODSEQ Search Criterion in SEARCH + let search_res = search(imap_socket, SearchKind::ModSeq(3))?; + // RFC 3.1.6. Modified SEARCH Untagged Response + assert!(search_res.contains("* SEARCH 1 (MODSEQ 3)")); + + // RFC 3.1.7 HIGHESTMODSEQ Status Data Items + let status_res = status(imap_socket, Mailbox::Inbox, StatusKind::HighestModSeq)?; + assert!(status_res.contains("HIGHESTMODSEQ 3")); + + Ok(()) + }) + .expect("test fully run"); +} diff --git a/tests/common/fragments.rs b/tests/common/fragments.rs index 2e2fbd4..f9ad87e 100644 --- a/tests/common/fragments.rs +++ b/tests/common/fragments.rs @@ -34,7 +34,7 @@ pub enum Extension { None, Unselect, Move, - CondStore, + Condstore, LiteralPlus, } @@ -63,6 +63,46 @@ pub enum Email { pub enum Selection { FirstId, SecondId, + All, +} + +pub enum SelectMod { + None, + Condstore, +} + +pub enum StoreAction { + AddFlags, + DelFlags, + SetFlags, + AddFlagsSilent, + DelFlagsSilent, + SetFlagsSilent, +} + +pub enum StoreMod { + None, + UnchangedSince(u64), +} + +pub enum FetchKind { + Rfc822, + Rfc822Size, +} + +pub enum FetchMod { + None, + ChangedSince(u64), +} + +pub enum SearchKind<'a> { + Text(&'a str), + ModSeq(u64), +} + +pub enum StatusKind { + UidNext, + HighestModSeq, } pub fn capability(imap: &mut TcpStream, ext: Extension) -> Result<()> { @@ -72,7 +112,7 @@ pub fn capability(imap: &mut TcpStream, ext: Extension) -> Result<()> { Extension::None => None, Extension::Unselect => Some("UNSELECT"), Extension::Move => Some("MOVE"), - Extension::CondStore => Some("CONDSTORE"), + Extension::Condstore => Some("CONDSTORE"), Extension::LiteralPlus => Some("LITERAL+"), }; @@ -125,7 +165,7 @@ pub fn create_mailbox(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> { Ok(()) } -pub fn select(imap: &mut TcpStream, mbx: Mailbox, maybe_exists: Option<u64>) -> Result<()> { +pub fn select(imap: &mut TcpStream, mbx: Mailbox, modifier: SelectMod) -> Result<String> { let mut buffer: [u8; 6000] = [0; 6000]; let mbx_str = match mbx { @@ -133,16 +173,18 @@ pub fn select(imap: &mut TcpStream, mbx: Mailbox, maybe_exists: Option<u64>) -> Mailbox::Archive => "Archive", Mailbox::Drafts => "Drafts", }; - imap.write(format!("20 select {}\r\n", mbx_str).as_bytes())?; + + let mod_str = match modifier { + SelectMod::Condstore => " (CONDSTORE)", + SelectMod::None => "", + }; + + imap.write(format!("20 select {}{}\r\n", mbx_str, mod_str).as_bytes())?; let read = read_lines(imap, &mut buffer, Some(&b"20 OK"[..]))?; let srv_msg = std::str::from_utf8(read)?; - if let Some(exists) = maybe_exists { - let expected = format!("* {} EXISTS", exists); - assert!(srv_msg.contains(&expected)); - } - Ok(()) + Ok(srv_msg.to_string()) } pub fn unselect(imap: &mut TcpStream) -> Result<()> { @@ -162,13 +204,22 @@ pub fn check(imap: &mut TcpStream) -> Result<()> { Ok(()) } -pub fn status_mailbox(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> { - assert!(matches!(mbx, Mailbox::Archive)); - imap.write(&b"25 STATUS Archive (UIDNEXT MESSAGES)\r\n"[..])?; +pub fn status(imap: &mut TcpStream, mbx: Mailbox, sk: StatusKind) -> Result<String> { + let mbx_str = match mbx { + Mailbox::Inbox => "INBOX", + Mailbox::Archive => "Archive", + Mailbox::Drafts => "Drafts", + }; + let sk_str = match sk { + StatusKind::UidNext => "(UIDNEXT)", + StatusKind::HighestModSeq => "(HIGHESTMODSEQ)", + }; + imap.write(format!("25 STATUS {} {}\r\n", mbx_str, sk_str).as_bytes())?; let mut buffer: [u8; 6000] = [0; 6000]; - let _read = read_lines(imap, &mut buffer, Some(&b"25 OK"[..]))?; + let read = read_lines(imap, &mut buffer, Some(&b"25 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; - Ok(()) + Ok(srv_msg.to_string()) } pub fn lmtp_handshake(lmtp: &mut TcpStream) -> Result<()> { @@ -206,7 +257,7 @@ pub fn lmtp_deliver_email(lmtp: &mut TcpStream, email_type: Email) -> Result<()> Ok(()) } -pub fn noop_exists(imap: &mut TcpStream) -> Result<()> { +pub fn noop_exists(imap: &mut TcpStream, must_exists: u32) -> Result<()> { let mut buffer: [u8; 6000] = [0; 6000]; let mut max_retry = 20; @@ -216,35 +267,50 @@ pub fn noop_exists(imap: &mut TcpStream) -> Result<()> { let read = read_lines(imap, &mut buffer, Some(&b"30 OK"[..]))?; let srv_msg = std::str::from_utf8(read)?; - match (max_retry, srv_msg.lines().count()) { - (_, cnt) if cnt > 1 => break, - (0, _) => bail!("no more retry"), - _ => (), + for line in srv_msg.lines() { + if line.contains("EXISTS") { + let got = read_first_u32(line)?; + if got == must_exists { + // Done + return Ok(()); + } + } + } + + if max_retry <= 0 { + // Failed + bail!("no more retry"); } thread::sleep(SMALL_DELAY); } - - Ok(()) } -pub fn fetch_rfc822(imap: &mut TcpStream, selection: Selection, r#ref: Email) -> Result<()> { +pub fn fetch(imap: &mut TcpStream, selection: Selection, kind: FetchKind, modifier: FetchMod) -> Result<String> { let mut buffer: [u8; 65535] = [0; 65535]; - assert!(matches!(selection, Selection::FirstId)); - imap.write(&b"40 fetch 1 rfc822\r\n"[..])?; + let sel_str = match selection { + Selection::FirstId => "1", + Selection::SecondId => "2", + Selection::All => "1:*", + }; - let read = read_lines(imap, &mut buffer, Some(&b"40 OK FETCH"[..]))?; - let srv_msg = std::str::from_utf8(read)?; + let kind_str = match kind { + FetchKind::Rfc822 => "RFC822", + FetchKind::Rfc822Size => "RFC822.SIZE", + }; - let ref_mail = match r#ref { - Email::Basic => EMAIL2, - Email::Multipart => EMAIL1, + let mod_str = match modifier { + FetchMod::None => "".into(), + FetchMod::ChangedSince(val) => format!(" (CHANGEDSINCE {})", val), }; - let orig_email = std::str::from_utf8(ref_mail)?; - assert!(srv_msg.contains(orig_email)); - Ok(()) + imap.write(format!("40 fetch {} {}{}\r\n", sel_str, kind_str, mod_str).as_bytes())?; + + let read = read_lines(imap, &mut buffer, Some(&b"40 OK FETCH"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + + Ok(srv_msg.to_string()) } pub fn copy(imap: &mut TcpStream, selection: Selection, to: Mailbox) -> Result<()> { @@ -281,29 +347,59 @@ pub fn append_email(imap: &mut TcpStream, content: Email) -> Result<()> { let read = read_lines(imap, &mut buffer, None)?; assert_eq!(&read[..5], &b"47 OK"[..]); - // we check that noop detects the change - noop_exists(imap)?; - Ok(()) } -pub fn add_flags_email(imap: &mut TcpStream, selection: Selection, flag: Flag) -> Result<()> { +pub fn search(imap: &mut TcpStream, sk: SearchKind) -> Result<String> { + let sk_str = match sk { + SearchKind::Text(x) => format!("TEXT \"{}\"", x), + SearchKind::ModSeq(x) => format!("MODSEQ {}", x), + }; + imap.write(format!("55 SEARCH {}\r\n", sk_str).as_bytes())?; let mut buffer: [u8; 1500] = [0; 1500]; - assert!(matches!(selection, Selection::FirstId)); - assert!(matches!(flag, Flag::Deleted)); - imap.write(&b"50 store 1 +FLAGS (\\Deleted)\r\n"[..])?; - let _read = read_lines(imap, &mut buffer, Some(&b"50 OK STORE"[..]))?; - - Ok(()) + let read = read_lines(imap, &mut buffer, Some(&b"55 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + Ok(srv_msg.to_string()) } -#[allow(dead_code)] -/// Not yet implemented -pub fn search(imap: &mut TcpStream) -> Result<()> { - imap.write(&b"55 search text \"OoOoO\"\r\n"[..])?; - let mut buffer: [u8; 1500] = [0; 1500]; - let _read = read_lines(imap, &mut buffer, Some(&b"55 OK SEARCH"[..]))?; - Ok(()) +pub fn store( + imap: &mut TcpStream, + sel: Selection, + flag: Flag, + action: StoreAction, + modifier: StoreMod +) -> Result<String> { + let mut buffer: [u8; 6000] = [0; 6000]; + + let seq = match sel { + Selection::FirstId => "1", + Selection::SecondId => "2", + Selection::All => "1:*", + }; + + let modif = match modifier { + StoreMod::None => "".into(), + StoreMod::UnchangedSince(val) => format!(" (UNCHANGEDSINCE {})", val), + }; + + let flags_str = match flag { + Flag::Deleted => "(\\Deleted)", + Flag::Important => "(\\Important)", + }; + + let action_str = match action { + StoreAction::AddFlags => "+FLAGS", + StoreAction::DelFlags => "-FLAGS", + StoreAction::SetFlags => "FLAGS", + StoreAction::AddFlagsSilent => "+FLAGS.SILENT", + StoreAction::DelFlagsSilent => "-FLAGS.SILENT", + StoreAction::SetFlagsSilent => "FLAGS.SILENT", + }; + + imap.write(format!("57 STORE {}{} {} {}\r\n", seq, modif, action_str, flags_str).as_bytes())?; + let read = read_lines(imap, &mut buffer, Some(&b"57 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + Ok(srv_msg.to_string()) } pub fn expunge(imap: &mut TcpStream) -> Result<()> { diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 810fd79..cbe0271 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -88,3 +88,12 @@ pub fn read_lines<'a, F: Read>( println!("read: {}", std::str::from_utf8(&buffer[..nbytes])?); Ok(&buffer[..nbytes]) } + +pub fn read_first_u32(inp: &str) -> Result<u32> { + Ok(inp + .chars() + .skip_while(|c| !c.is_digit(10)) + .take_while(|c| c.is_digit(10)) + .collect::<String>() + .parse::<u32>()?) +} |