From c1e7f7264a68533262bfe1d181061e577e508030 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 12 Jan 2024 13:01:22 +0100 Subject: fix a condstore bug --- tests/common/fragments.rs | 53 +++++++++++++++++++++++++++++++++-------------- tests/common/mod.rs | 9 ++++++++ 2 files changed, 46 insertions(+), 16 deletions(-) (limited to 'tests/common') diff --git a/tests/common/fragments.rs b/tests/common/fragments.rs index 2e2fbd4..0fd1c45 100644 --- a/tests/common/fragments.rs +++ b/tests/common/fragments.rs @@ -34,7 +34,7 @@ pub enum Extension { None, Unselect, Move, - CondStore, + Condstore, LiteralPlus, } @@ -65,6 +65,11 @@ pub enum Selection { SecondId, } +pub enum SelectMod { + None, + Condstore, +} + pub fn capability(imap: &mut TcpStream, ext: Extension) -> Result<()> { imap.write(&b"5 capability\r\n"[..])?; @@ -72,7 +77,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 +130,7 @@ pub fn create_mailbox(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> { Ok(()) } -pub fn select(imap: &mut TcpStream, mbx: Mailbox, maybe_exists: Option) -> Result<()> { +pub fn select(imap: &mut TcpStream, mbx: Mailbox, modifier: SelectMod, maybe_exists: Option) -> Result<()> { let mut buffer: [u8; 6000] = [0; 6000]; let mbx_str = match mbx { @@ -133,7 +138,13 @@ pub fn select(imap: &mut TcpStream, mbx: Mailbox, maybe_exists: Option) -> 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)?; @@ -141,6 +152,12 @@ pub fn select(imap: &mut TcpStream, mbx: Mailbox, maybe_exists: Option) -> let expected = format!("* {} EXISTS", exists); assert!(srv_msg.contains(&expected)); } + match modifier { + SelectMod::Condstore => { + assert!(srv_msg.contains("[HIGHESTMODSEQ")); + } + _ => (), + } Ok(()) } @@ -206,7 +223,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,16 +233,23 @@ 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<()> { @@ -281,9 +305,6 @@ 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(()) } @@ -292,7 +313,7 @@ pub fn add_flags_email(imap: &mut TcpStream, selection: Selection, flag: Flag) - 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"[..]))?; + let _read = read_lines(imap, &mut buffer, Some(&b"50 OK"[..]))?; Ok(()) } @@ -302,7 +323,7 @@ pub fn add_flags_email(imap: &mut TcpStream, selection: Selection, flag: Flag) - 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"[..]))?; + let _read = read_lines(imap, &mut buffer, Some(&b"55 OK"[..]))?; Ok(()) } 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 { + Ok(inp + .chars() + .skip_while(|c| !c.is_digit(10)) + .take_while(|c| c.is_digit(10)) + .collect::() + .parse::()?) +} -- cgit v1.2.3 From 22cd0764d810e94e8ea9258b71d4dc25085585bd Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Fri, 12 Jan 2024 15:02:02 +0100 Subject: rewrite store testing logic --- tests/common/fragments.rs | 67 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 12 deletions(-) (limited to 'tests/common') diff --git a/tests/common/fragments.rs b/tests/common/fragments.rs index 0fd1c45..e07413e 100644 --- a/tests/common/fragments.rs +++ b/tests/common/fragments.rs @@ -63,6 +63,7 @@ pub enum Email { pub enum Selection { FirstId, SecondId, + All, } pub enum SelectMod { @@ -70,6 +71,20 @@ pub enum SelectMod { Condstore, } +pub enum StoreAction { + AddFlags, + DelFlags, + SetFlags, + AddFlagsSilent, + DelFlagsSilent, + SetFlagsSilent, +} + +pub enum StoreMod { + None, + UnchangedSince(u64), +} + pub fn capability(imap: &mut TcpStream, ext: Extension) -> Result<()> { imap.write(&b"5 capability\r\n"[..])?; @@ -308,18 +323,6 @@ pub fn append_email(imap: &mut TcpStream, content: Email) -> Result<()> { Ok(()) } -pub fn add_flags_email(imap: &mut TcpStream, selection: Selection, flag: Flag) -> Result<()> { - 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"[..]))?; - - Ok(()) -} - -#[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]; @@ -327,6 +330,46 @@ pub fn search(imap: &mut TcpStream) -> Result<()> { Ok(()) } +pub fn store( + imap: &mut TcpStream, + sel: Selection, + flag: Flag, + action: StoreAction, + modifier: StoreMod +) -> Result { + 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<()> { imap.write(&b"60 expunge\r\n"[..])?; let mut buffer: [u8; 1500] = [0; 1500]; -- cgit v1.2.3 From 81bfed3b7df354148f284ed4e3708c1a086d6e58 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Mon, 15 Jan 2024 08:06:04 +0100 Subject: testing condstore --- tests/common/fragments.rs | 96 +++++++++++++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 32 deletions(-) (limited to 'tests/common') diff --git a/tests/common/fragments.rs b/tests/common/fragments.rs index e07413e..f9ad87e 100644 --- a/tests/common/fragments.rs +++ b/tests/common/fragments.rs @@ -85,6 +85,26 @@ pub enum StoreMod { 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<()> { imap.write(&b"5 capability\r\n"[..])?; @@ -145,7 +165,7 @@ pub fn create_mailbox(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> { Ok(()) } -pub fn select(imap: &mut TcpStream, mbx: Mailbox, modifier: SelectMod, maybe_exists: Option) -> Result<()> { +pub fn select(imap: &mut TcpStream, mbx: Mailbox, modifier: SelectMod) -> Result { let mut buffer: [u8; 6000] = [0; 6000]; let mbx_str = match mbx { @@ -163,18 +183,8 @@ pub fn select(imap: &mut TcpStream, mbx: Mailbox, modifier: SelectMod, maybe_exi 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)); - } - match modifier { - SelectMod::Condstore => { - assert!(srv_msg.contains("[HIGHESTMODSEQ")); - } - _ => (), - } - Ok(()) + Ok(srv_msg.to_string()) } pub fn unselect(imap: &mut TcpStream) -> Result<()> { @@ -194,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 { + 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<()> { @@ -267,23 +286,31 @@ pub fn noop_exists(imap: &mut TcpStream, must_exists: u32) -> Result<()> { } } -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 { 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<()> { @@ -323,11 +350,16 @@ pub fn append_email(imap: &mut TcpStream, content: Email) -> Result<()> { Ok(()) } -pub fn search(imap: &mut TcpStream) -> Result<()> { - imap.write(&b"55 search text \"OoOoO\"\r\n"[..])?; +pub fn search(imap: &mut TcpStream, sk: SearchKind) -> Result { + 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]; - let _read = read_lines(imap, &mut buffer, Some(&b"55 OK"[..]))?; - 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()) } pub fn store( -- cgit v1.2.3