From a059585cb423d527763ef0131773d6620ebcafd5 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 3 Jan 2024 10:28:10 +0100 Subject: add test for the unselect extension --- Cargo.toml | 5 + tests/common.rs | 29 +++++- tests/rfc3501_imap4rev1_base.rs | 129 +++++++++++--------------- tests/rfc3691_imapext_unselect.rs | 190 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 278 insertions(+), 75 deletions(-) create mode 100644 tests/rfc3691_imapext_unselect.rs diff --git a/Cargo.toml b/Cargo.toml index 4e39a66..93f2a3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,3 +71,8 @@ imap-codec = { git = "https://github.com/duesee/imap-codec", branch = "v2" } name = "rfc3501_imap4rev1_base" path = "tests/rfc3501_imap4rev1_base.rs" harness = false + +[[test]] +name = "rfc3691_imapext_unselect" +path = "tests/rfc3691_imapext_unselect.rs" +harness = false diff --git a/tests/common.rs b/tests/common.rs index 0d99ce8..f89f26c 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -1,11 +1,14 @@ use anyhow::{bail, Context, Result}; +use std::io::Read; use std::net::{Shutdown, TcpStream}; use std::process::Command; use std::{thread, time}; static SMALL_DELAY: time::Duration = time::Duration::from_millis(200); -pub fn aerogramme_provider_daemon_dev(mut fx: impl FnMut(&mut TcpStream, &mut TcpStream) -> Result<()>) -> Result<()> { +pub fn aerogramme_provider_daemon_dev( + mut fx: impl FnMut(&mut TcpStream, &mut TcpStream) -> Result<()>, +) -> Result<()> { let mut daemon = Command::new(env!("CARGO_BIN_EXE_aerogramme")) .arg("--dev") .arg("provider") @@ -25,7 +28,8 @@ pub fn aerogramme_provider_daemon_dev(mut fx: impl FnMut(&mut TcpStream, &mut Tc thread::sleep(SMALL_DELAY); }; - let mut lmtp_socket = TcpStream::connect("[::1]:1025").context("lmtp socket must be connected")?; + let mut lmtp_socket = + TcpStream::connect("[::1]:1025").context("lmtp socket must be connected")?; println!("-- ready to test imap features --"); let result = fx(&mut imap_socket, &mut lmtp_socket); @@ -41,3 +45,24 @@ pub fn aerogramme_provider_daemon_dev(mut fx: impl FnMut(&mut TcpStream, &mut Tc result.context("all tests passed") } + +pub fn read_lines<'a, F: Read>( + reader: &mut F, + buffer: &'a mut [u8], + stop_marker: Option<&[u8]>, +) -> Result<&'a [u8]> { + let mut nbytes = 0; + loop { + nbytes += reader.read(&mut buffer[nbytes..])?; + //println!("partial read: {}", std::str::from_utf8(&buffer[..nbytes])?); + let pre_condition = match stop_marker { + None => true, + Some(mark) => buffer[..nbytes].windows(mark.len()).any(|w| w == mark), + }; + if pre_condition && &buffer[nbytes - 2..nbytes] == &b"\r\n"[..] { + break; + } + } + println!("read: {}", std::str::from_utf8(&buffer[..nbytes])?); + Ok(&buffer[..nbytes]) +} diff --git a/tests/rfc3501_imap4rev1_base.rs b/tests/rfc3501_imap4rev1_base.rs index f416e51..c0985dd 100644 --- a/tests/rfc3501_imap4rev1_base.rs +++ b/tests/rfc3501_imap4rev1_base.rs @@ -1,61 +1,12 @@ use anyhow::{bail, Context, Result}; -use std::io::{Read, Write}; +use std::io::Write; use std::net::TcpStream; use std::{thread, time}; mod common; +use crate::common::read_lines; static SMALL_DELAY: time::Duration = time::Duration::from_millis(200); -static EMAIL1: &[u8] = b"Date: Sat, 8 Jul 2023 07:14:29 +0200\r -From: Bob Robert \r -To: Alice Malice \r -CC: =?ISO-8859-1?Q?Andr=E9?= Pirard \r -Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\r - =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\r -X-Unknown: something something\r -Bad entry\r - on multiple lines\r -Message-ID: \r -MIME-Version: 1.0\r -Content-Type: multipart/alternative;\r - boundary=\"b1_e376dc71bafc953c0b0fdeb9983a9956\"\r -Content-Transfer-Encoding: 7bit\r -\r -This is a multi-part message in MIME format.\r -\r ---b1_e376dc71bafc953c0b0fdeb9983a9956\r -Content-Type: text/plain; charset=utf-8\r -Content-Transfer-Encoding: quoted-printable\r -\r -GZ\r -OoOoO\r -oOoOoOoOo\r -oOoOoOoOoOoOoOoOo\r -oOoOoOoOoOoOoOoOoOoOoOo\r -oOoOoOoOoOoOoOoOoOoOoOoOoOoOo\r -OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO\r -\r ---b1_e376dc71bafc953c0b0fdeb9983a9956\r -Content-Type: text/html; charset=us-ascii\r -\r -
GZ
\r -OoOoO
\r -oOoOoOoOo
\r -oOoOoOoOoOoOoOoOo
\r -oOoOoOoOoOoOoOoOoOoOoOo
\r -oOoOoOoOoOoOoOoOoOoOoOoOoOoOo
\r -OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO
\r -
\r -\r ---b1_e376dc71bafc953c0b0fdeb9983a9956--\r -"; - -static EMAIL2: &[u8] = b"From: alice@example.com\r -To: alice@example.tld\r -Subject: Test\r -\r -Hello world!\r -"; fn main() { common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| { @@ -76,12 +27,14 @@ fn main() { append_email(imap_socket, EMAIL2).context("insert email in INBOX")?; // SEARCH IS NOT IMPLEMENTED YET //search(imap_socket).expect("search should return something"); - add_flags_email(imap_socket).context("should add delete and important flags to the email")?; + add_flags_email(imap_socket) + .context("should add delete and important flags to the email")?; expunge(imap_socket).context("expunge emails")?; rename_mailbox(imap_socket).context("archive mailbox is renamed my-archives")?; delete_mailbox(imap_socket).context("my-archives mailbox is deleted")?; Ok(()) - }).expect("test fully run"); + }) + .expect("test fully run"); } fn connect(imap: &mut TcpStream) -> Result<()> { @@ -336,23 +289,53 @@ fn delete_mailbox(imap: &mut TcpStream) -> Result<()> { Ok(()) } -fn read_lines<'a, F: Read>( - reader: &mut F, - buffer: &'a mut [u8], - stop_marker: Option<&[u8]>, -) -> Result<&'a [u8]> { - let mut nbytes = 0; - loop { - nbytes += reader.read(&mut buffer[nbytes..])?; - //println!("partial read: {}", std::str::from_utf8(&buffer[..nbytes])?); - let pre_condition = match stop_marker { - None => true, - Some(mark) => buffer[..nbytes].windows(mark.len()).any(|w| w == mark), - }; - if pre_condition && &buffer[nbytes - 2..nbytes] == &b"\r\n"[..] { - break; - } - } - println!("read: {}", std::str::from_utf8(&buffer[..nbytes])?); - Ok(&buffer[..nbytes]) -} +static EMAIL1: &[u8] = b"Date: Sat, 8 Jul 2023 07:14:29 +0200\r +From: Bob Robert \r +To: Alice Malice \r +CC: =?ISO-8859-1?Q?Andr=E9?= Pirard \r +Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=\r + =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=\r +X-Unknown: something something\r +Bad entry\r + on multiple lines\r +Message-ID: \r +MIME-Version: 1.0\r +Content-Type: multipart/alternative;\r + boundary=\"b1_e376dc71bafc953c0b0fdeb9983a9956\"\r +Content-Transfer-Encoding: 7bit\r +\r +This is a multi-part message in MIME format.\r +\r +--b1_e376dc71bafc953c0b0fdeb9983a9956\r +Content-Type: text/plain; charset=utf-8\r +Content-Transfer-Encoding: quoted-printable\r +\r +GZ\r +OoOoO\r +oOoOoOoOo\r +oOoOoOoOoOoOoOoOo\r +oOoOoOoOoOoOoOoOoOoOoOo\r +oOoOoOoOoOoOoOoOoOoOoOoOoOoOo\r +OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO\r +\r +--b1_e376dc71bafc953c0b0fdeb9983a9956\r +Content-Type: text/html; charset=us-ascii\r +\r +
GZ
\r +OoOoO
\r +oOoOoOoOo
\r +oOoOoOoOoOoOoOoOo
\r +oOoOoOoOoOoOoOoOoOoOoOo
\r +oOoOoOoOoOoOoOoOoOoOoOoOoOoOo
\r +OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO
\r +
\r +\r +--b1_e376dc71bafc953c0b0fdeb9983a9956--\r +"; + +static EMAIL2: &[u8] = b"From: alice@example.com\r +To: alice@example.tld\r +Subject: Test\r +\r +Hello world!\r +"; diff --git a/tests/rfc3691_imapext_unselect.rs b/tests/rfc3691_imapext_unselect.rs new file mode 100644 index 0000000..28e4bc8 --- /dev/null +++ b/tests/rfc3691_imapext_unselect.rs @@ -0,0 +1,190 @@ +use anyhow::{bail, Context, Result}; +use std::io::Write; +use std::net::TcpStream; +use std::{thread, time}; + +mod common; +use crate::common::read_lines; + +static SMALL_DELAY: time::Duration = time::Duration::from_millis(200); +static EMAIL: &[u8] = b"From: alice@example.com\r +To: alice@example.tld\r +Subject: Test\r +\r +Hello world!\r +"; + +fn main() { + common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| { + lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; + lmtp_deliver_email(lmtp_socket, EMAIL).context("mail delivered successfully")?; + + connect(imap_socket).context("server says hello")?; + capability(imap_socket).context("check server capabilities")?; + login(imap_socket).context("login test")?; + select_inbox(imap_socket).context("select inbox")?; + noop_exists(imap_socket).context("noop loop must detect a new email")?; + add_flags_email(imap_socket).context("add delete flags to the email")?; + unselect(imap_socket) + .context("unselect inbox while preserving email with the \\Delete flag")?; + select_inbox_one_msg(imap_socket).context("select inbox again")?; + fetch_rfc822(imap_socket, EMAIL).context("message is still present")?; + close(imap_socket).context("close inbox and expunge message")?; + select_inbox_zero_msg(imap_socket).context("select inbox again and check it's empty")?; + + Ok(()) + }) + .expect("test fully run"); +} + +fn connect(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..4], &b"* OK"[..]); + + Ok(()) +} + +fn capability(imap: &mut TcpStream) -> Result<()> { + imap.write(&b"5 capability\r\n"[..])?; + + let mut buffer: [u8; 1500] = [0; 1500]; + let read = read_lines(imap, &mut buffer, Some(&b"5 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + assert!(srv_msg.contains("IMAP4REV1")); + assert!(srv_msg.contains("UNSELECT")); + + Ok(()) +} + +fn login(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + imap.write(&b"10 login alice hunter2\r\n"[..])?; + + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"10 OK"[..]); + + Ok(()) +} + +fn select_inbox_one_msg(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 6000] = [0; 6000]; + + imap.write(&b"22 select inbox\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"22 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + assert!(srv_msg.contains("* 1 EXISTS")); + + Ok(()) +} + +fn select_inbox_zero_msg(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 6000] = [0; 6000]; + + imap.write(&b"21 select inbox\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"21 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + assert!(srv_msg.contains("* 0 EXISTS")); + + Ok(()) +} + +fn select_inbox(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 6000] = [0; 6000]; + + imap.write(&b"20 select inbox\r\n"[..])?; + let _read = read_lines(imap, &mut buffer, Some(&b"20 OK"[..]))?; + + Ok(()) +} + +fn noop_exists(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 6000] = [0; 6000]; + + let mut max_retry = 20; + loop { + max_retry -= 1; + imap.write(&b"30 NOOP\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"30 OK NOOP"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + + match (max_retry, srv_msg.contains("* 1 EXISTS")) { + (_, true) => break, + (0, _) => bail!("no more retry"), + _ => (), + } + + thread::sleep(SMALL_DELAY); + } + + Ok(()) +} + +fn lmtp_handshake(lmtp: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + let _read = read_lines(lmtp, &mut buffer, None)?; + assert_eq!(&buffer[..4], &b"220 "[..]); + + lmtp.write(&b"LHLO example.tld\r\n"[..])?; + let _read = read_lines(lmtp, &mut buffer, Some(&b"250 "[..]))?; + + Ok(()) +} + +fn lmtp_deliver_email(lmtp: &mut TcpStream, email: &[u8]) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + lmtp.write(&b"MAIL FROM:\r\n"[..])?; + let _read = read_lines(lmtp, &mut buffer, Some(&b"250 2.0.0"[..]))?; + + lmtp.write(&b"RCPT TO:\r\n"[..])?; + let _read = read_lines(lmtp, &mut buffer, Some(&b"250 2.1.5"[..]))?; + + lmtp.write(&b"DATA\r\n"[..])?; + let _read = read_lines(lmtp, &mut buffer, Some(&b"354 "[..]))?; + + lmtp.write(email)?; + lmtp.write(&b"\r\n.\r\n"[..])?; + let _read = read_lines(lmtp, &mut buffer, Some(&b"250 2.0.0"[..]))?; + + Ok(()) +} + +fn fetch_rfc822(imap: &mut TcpStream, ref_mail: &[u8]) -> Result<()> { + let mut buffer: [u8; 65535] = [0; 65535]; + imap.write(&b"40 fetch 1 rfc822\r\n"[..])?; + + let read = read_lines(imap, &mut buffer, Some(&b"40 OK FETCH"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + let orig_email = std::str::from_utf8(ref_mail)?; + assert!(srv_msg.contains(orig_email)); + + Ok(()) +} + +fn add_flags_email(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + imap.write(&b"50 store 1 +FLAGS (\\Deleted)\r\n"[..])?; + let _read = read_lines(imap, &mut buffer, Some(&b"50 OK STORE"[..]))?; + + Ok(()) +} + +fn close(imap: &mut TcpStream) -> Result<()> { + imap.write(&b"60 close\r\n"[..])?; + let mut buffer: [u8; 1500] = [0; 1500]; + let _read = read_lines(imap, &mut buffer, Some(&b"60 OK"[..]))?; + + Ok(()) +} + +fn unselect(imap: &mut TcpStream) -> Result<()> { + imap.write(&b"70 unselect\r\n"[..])?; + let mut buffer: [u8; 1500] = [0; 1500]; + let _read = read_lines(imap, &mut buffer, Some(&b"70 OK"[..]))?; + + Ok(()) +} -- cgit v1.2.3