diff options
author | Quentin <quentin@dufour.io> | 2024-01-15 07:07:06 +0000 |
---|---|---|
committer | Quentin <quentin@dufour.io> | 2024-01-15 07:07:06 +0000 |
commit | 55e26d24a08519ded6a6898453dcd6db287f45c8 (patch) | |
tree | 074d9f8dbc161d2aa8f59be26826b40c09d3d658 /tests/common | |
parent | d49a2355f71fe555a67a815c31800f901a0d0a71 (diff) | |
parent | 81bfed3b7df354148f284ed4e3708c1a086d6e58 (diff) | |
download | aerogramme-55e26d24a08519ded6a6898453dcd6db287f45c8.tar.gz aerogramme-55e26d24a08519ded6a6898453dcd6db287f45c8.zip |
Merge pull request 'CONDSTORE' (#71) from feat/condstore-try-2 into main
Reviewed-on: https://git.deuxfleurs.fr/Deuxfleurs/aerogramme/pulls/71
Diffstat (limited to 'tests/common')
-rw-r--r-- | tests/common/fragments.rs | 194 | ||||
-rw-r--r-- | tests/common/mod.rs | 9 |
2 files changed, 154 insertions, 49 deletions
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>()?) +} |