From 9ce8e18fb81c4ef0bf146c5d28981d8f2a6fddd9 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 3 Jan 2024 09:47:52 +0100 Subject: Common module in test created --- tests/common.rs | 43 +++++ tests/imap_features.rs | 394 ---------------------------------------- tests/rfc3501_imap4rev1_base.rs | 358 ++++++++++++++++++++++++++++++++++++ 3 files changed, 401 insertions(+), 394 deletions(-) create mode 100644 tests/common.rs delete mode 100644 tests/imap_features.rs create mode 100644 tests/rfc3501_imap4rev1_base.rs (limited to 'tests') diff --git a/tests/common.rs b/tests/common.rs new file mode 100644 index 0000000..0d99ce8 --- /dev/null +++ b/tests/common.rs @@ -0,0 +1,43 @@ +use anyhow::{bail, Context, Result}; +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<()> { + let mut daemon = Command::new(env!("CARGO_BIN_EXE_aerogramme")) + .arg("--dev") + .arg("provider") + .arg("daemon") + .spawn()?; + + let mut max_retry = 20; + let mut imap_socket = loop { + max_retry -= 1; + match (TcpStream::connect("[::1]:1143"), max_retry) { + (Err(e), 0) => bail!("no more retry, last error is: {}", e), + (Err(e), _) => { + println!("unable to connect: {} ; will retry in 1 sec", e); + } + (Ok(v), _) => break v, + } + thread::sleep(SMALL_DELAY); + }; + + 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); + println!("-- test teardown --"); + + imap_socket + .shutdown(Shutdown::Both) + .context("closing imap socket at the end of the test")?; + lmtp_socket + .shutdown(Shutdown::Both) + .context("closing lmtp socket at the end of the test")?; + daemon.kill().context("daemon should be killed")?; + + result.context("all tests passed") +} diff --git a/tests/imap_features.rs b/tests/imap_features.rs deleted file mode 100644 index 9e8d587..0000000 --- a/tests/imap_features.rs +++ /dev/null @@ -1,394 +0,0 @@ -use anyhow::{bail, Context, Result}; -use std::io::{Read, Write}; -use std::net::{Shutdown, TcpStream}; -use std::process::Command; -use std::{thread, time}; - -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() { - let mut daemon = Command::new(env!("CARGO_BIN_EXE_aerogramme")) - .arg("--dev") - .arg("provider") - .arg("daemon") - .spawn() - .expect("daemon should be started"); - - let mut max_retry = 20; - let mut imap_socket = loop { - max_retry -= 1; - match (TcpStream::connect("[::1]:1143"), max_retry) { - (Err(e), 0) => panic!("no more retry, last error is: {}", e), - (Err(e), _) => { - println!("unable to connect: {} ; will retry in 1 sec", e); - } - (Ok(v), _) => break v, - } - thread::sleep(SMALL_DELAY); - }; - - let mut lmtp_socket = TcpStream::connect("[::1]:1025").expect("lmtp socket must be connected"); - - println!("-- ready to test imap features --"); - let result = generic_test(&mut imap_socket, &mut lmtp_socket); - println!("-- test teardown --"); - - imap_socket - .shutdown(Shutdown::Both) - .expect("closing imap socket at the end of the test"); - lmtp_socket - .shutdown(Shutdown::Both) - .expect("closing lmtp socket at the end of the test"); - daemon.kill().expect("daemon should be killed"); - - result.expect("all tests passed"); -} - -fn generic_test(imap_socket: &mut TcpStream, lmtp_socket: &mut TcpStream) -> Result<()> { - connect(imap_socket).context("server says hello")?; - capability(imap_socket).context("check server capabilities")?; - login(imap_socket).context("login test")?; - create_mailbox(imap_socket).context("created mailbox archive")?; - // UNSUBSCRIBE IS NOT IMPLEMENTED YET - //unsubscribe_mailbox(imap_socket).context("unsubscribe from archive")?; - select_inbox(imap_socket).context("select inbox")?; - check(imap_socket).context("check must run")?; - status_mailbox(imap_socket).context("status of archive from inbox")?; - lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; - lmtp_deliver_email(lmtp_socket, EMAIL1).context("mail delivered successfully")?; - noop_exists(imap_socket).context("noop loop must detect a new email")?; - fetch_rfc822(imap_socket, EMAIL1).context("fetch rfc822 message")?; - copy_email(imap_socket).context("copy message to the archive mailbox")?; - 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")?; - 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(()) -} - -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("IDLE")); - - 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 create_mailbox(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 1500] = [0; 1500]; - - imap.write(&b"15 create archive\r\n"[..])?; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..12], &b"15 OK CREATE"[..]); - - Ok(()) -} - -#[allow(dead_code)] -fn unsubscribe_mailbox(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 6000] = [0; 6000]; - - imap.write(&b"16 lsub \"\" *\r\n"[..])?; - let read = read_lines(imap, &mut buffer, Some(&b"16 OK LSUB"[..]))?; - let srv_msg = std::str::from_utf8(read)?; - assert!(srv_msg.contains(" INBOX\r\n")); - assert!(srv_msg.contains(" archive\r\n")); - - imap.write(&b"17 unsubscribe archive\r\n"[..])?; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..5], &b"17 OK"[..]); - - imap.write(&b"18 lsub \"\" *\r\n"[..])?; - let read = read_lines(imap, &mut buffer, Some(&b"18 OK LSUB"[..]))?; - let srv_msg = std::str::from_utf8(read)?; - assert!(srv_msg.contains(" INBOX\r\n")); - assert!(!srv_msg.contains(" archive\r\n")); - - 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 check(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 1500] = [0; 1500]; - - imap.write(&b"21 check\r\n"[..])?; - let _read = read_lines(imap, &mut buffer, Some(&b"21 OK"[..]))?; - - Ok(()) -} - -fn status_mailbox(imap: &mut TcpStream) -> Result<()> { - imap.write(&b"25 STATUS archive (UIDNEXT MESSAGES)\r\n"[..])?; - let mut buffer: [u8; 6000] = [0; 6000]; - let _read = read_lines(imap, &mut buffer, Some(&b"25 OK"[..]))?; - - 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 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 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 copy_email(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 65535] = [0; 65535]; - imap.write(&b"45 copy 1 archive\r\n"[..])?; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..5], &b"45 OK"[..]); - - Ok(()) -} - -fn append_email(imap: &mut TcpStream, ref_mail: &[u8]) -> Result<()> { - let mut buffer: [u8; 6000] = [0; 6000]; - assert_ne!(ref_mail.len(), 0); - let append_cmd = format!("47 append inbox (\\Seen) {{{}}}\r\n", ref_mail.len()); - println!("append cmd: {}", append_cmd); - imap.write(append_cmd.as_bytes())?; - - // wait for continuation - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(read[0], b'+'); - - // write our stuff - imap.write(ref_mail)?; - imap.write(&b"\r\n"[..])?; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..5], &b"47 OK"[..]); - - // noop to force a sync - imap.write(&b"48 NOOP\r\n"[..])?; - let _read = read_lines(imap, &mut buffer, Some(&b"48 OK NOOP"[..]))?; - - // check it is stored successfully - imap.write(&b"49 fetch 2 rfc822.size\r\n"[..])?; - let read = read_lines(imap, &mut buffer, Some(&b"49 OK"[..]))?; - let expected = format!("* 2 FETCH (RFC822.SIZE {})", ref_mail.len()); - let expbytes = expected.as_bytes(); - assert_eq!(&read[..expbytes.len()], expbytes); - - Ok(()) -} - -fn add_flags_email(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 1500] = [0; 1500]; - imap.write(&b"50 store 1 +FLAGS (\\Deleted \\Important)\r\n"[..])?; - let _read = read_lines(imap, &mut buffer, Some(&b"50 OK STORE"[..]))?; - - Ok(()) -} - -#[allow(dead_code)] -/// Not yet implemented -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(()) -} - -fn expunge(imap: &mut TcpStream) -> Result<()> { - imap.write(&b"60 expunge\r\n"[..])?; - let mut buffer: [u8; 1500] = [0; 1500]; - let _read = read_lines(imap, &mut buffer, Some(&b"60 OK EXPUNGE"[..]))?; - - Ok(()) -} - -fn rename_mailbox(imap: &mut TcpStream) -> Result<()> { - imap.write(&b"70 rename archive my-archives\r\n"[..])?; - let mut buffer: [u8; 1500] = [0; 1500]; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..5], &b"70 OK"[..]); - - imap.write(&b"71 list \"\" *\r\n"[..])?; - let read = read_lines(imap, &mut buffer, Some(&b"71 OK LIST"[..]))?; - let srv_msg = std::str::from_utf8(read)?; - assert!(!srv_msg.contains(" archive\r\n")); - assert!(srv_msg.contains(" INBOX\r\n")); - assert!(srv_msg.contains(" my-archives\r\n")); - - Ok(()) -} - -fn delete_mailbox(imap: &mut TcpStream) -> Result<()> { - imap.write(&b"80 delete my-archives\r\n"[..])?; - let mut buffer: [u8; 1500] = [0; 1500]; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..5], &b"80 OK"[..]); - - imap.write(&b"81 list \"\" *\r\n"[..])?; - let read = read_lines(imap, &mut buffer, Some(&b"81 OK LIST"[..]))?; - let srv_msg = std::str::from_utf8(read)?; - assert!(!srv_msg.contains(" archive\r\n")); - assert!(!srv_msg.contains(" my-archives\r\n")); - assert!(srv_msg.contains(" INBOX\r\n")); - - 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]) -} diff --git a/tests/rfc3501_imap4rev1_base.rs b/tests/rfc3501_imap4rev1_base.rs new file mode 100644 index 0000000..f416e51 --- /dev/null +++ b/tests/rfc3501_imap4rev1_base.rs @@ -0,0 +1,358 @@ +use anyhow::{bail, Context, Result}; +use std::io::{Read, Write}; +use std::net::TcpStream; +use std::{thread, time}; + +mod common; + +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| { + connect(imap_socket).context("server says hello")?; + capability(imap_socket).context("check server capabilities")?; + login(imap_socket).context("login test")?; + create_mailbox(imap_socket).context("created mailbox archive")?; + // UNSUBSCRIBE IS NOT IMPLEMENTED YET + //unsubscribe_mailbox(imap_socket).context("unsubscribe from archive")?; + select_inbox(imap_socket).context("select inbox")?; + check(imap_socket).context("check must run")?; + status_mailbox(imap_socket).context("status of archive from inbox")?; + lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; + lmtp_deliver_email(lmtp_socket, EMAIL1).context("mail delivered successfully")?; + noop_exists(imap_socket).context("noop loop must detect a new email")?; + fetch_rfc822(imap_socket, EMAIL1).context("fetch rfc822 message")?; + copy_email(imap_socket).context("copy message to the archive mailbox")?; + 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")?; + 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"); +} + +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")); + + 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 create_mailbox(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + imap.write(&b"15 create archive\r\n"[..])?; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..12], &b"15 OK CREATE"[..]); + + Ok(()) +} + +#[allow(dead_code)] +fn unsubscribe_mailbox(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 6000] = [0; 6000]; + + imap.write(&b"16 lsub \"\" *\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"16 OK LSUB"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + assert!(srv_msg.contains(" INBOX\r\n")); + assert!(srv_msg.contains(" archive\r\n")); + + imap.write(&b"17 unsubscribe archive\r\n"[..])?; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"17 OK"[..]); + + imap.write(&b"18 lsub \"\" *\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"18 OK LSUB"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + assert!(srv_msg.contains(" INBOX\r\n")); + assert!(!srv_msg.contains(" archive\r\n")); + + 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 check(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + imap.write(&b"21 check\r\n"[..])?; + let _read = read_lines(imap, &mut buffer, Some(&b"21 OK"[..]))?; + + Ok(()) +} + +fn status_mailbox(imap: &mut TcpStream) -> Result<()> { + imap.write(&b"25 STATUS archive (UIDNEXT MESSAGES)\r\n"[..])?; + let mut buffer: [u8; 6000] = [0; 6000]; + let _read = read_lines(imap, &mut buffer, Some(&b"25 OK"[..]))?; + + 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 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 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 copy_email(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 65535] = [0; 65535]; + imap.write(&b"45 copy 1 archive\r\n"[..])?; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"45 OK"[..]); + + Ok(()) +} + +fn append_email(imap: &mut TcpStream, ref_mail: &[u8]) -> Result<()> { + let mut buffer: [u8; 6000] = [0; 6000]; + assert_ne!(ref_mail.len(), 0); + let append_cmd = format!("47 append inbox (\\Seen) {{{}}}\r\n", ref_mail.len()); + println!("append cmd: {}", append_cmd); + imap.write(append_cmd.as_bytes())?; + + // wait for continuation + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(read[0], b'+'); + + // write our stuff + imap.write(ref_mail)?; + imap.write(&b"\r\n"[..])?; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"47 OK"[..]); + + // noop to force a sync + imap.write(&b"48 NOOP\r\n"[..])?; + let _read = read_lines(imap, &mut buffer, Some(&b"48 OK NOOP"[..]))?; + + // check it is stored successfully + imap.write(&b"49 fetch 2 rfc822.size\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"49 OK"[..]))?; + let expected = format!("* 2 FETCH (RFC822.SIZE {})", ref_mail.len()); + let expbytes = expected.as_bytes(); + assert_eq!(&read[..expbytes.len()], expbytes); + + Ok(()) +} + +fn add_flags_email(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + imap.write(&b"50 store 1 +FLAGS (\\Deleted \\Important)\r\n"[..])?; + let _read = read_lines(imap, &mut buffer, Some(&b"50 OK STORE"[..]))?; + + Ok(()) +} + +#[allow(dead_code)] +/// Not yet implemented +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(()) +} + +fn expunge(imap: &mut TcpStream) -> Result<()> { + imap.write(&b"60 expunge\r\n"[..])?; + let mut buffer: [u8; 1500] = [0; 1500]; + let _read = read_lines(imap, &mut buffer, Some(&b"60 OK EXPUNGE"[..]))?; + + Ok(()) +} + +fn rename_mailbox(imap: &mut TcpStream) -> Result<()> { + imap.write(&b"70 rename archive my-archives\r\n"[..])?; + let mut buffer: [u8; 1500] = [0; 1500]; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"70 OK"[..]); + + imap.write(&b"71 list \"\" *\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"71 OK LIST"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + assert!(!srv_msg.contains(" archive\r\n")); + assert!(srv_msg.contains(" INBOX\r\n")); + assert!(srv_msg.contains(" my-archives\r\n")); + + Ok(()) +} + +fn delete_mailbox(imap: &mut TcpStream) -> Result<()> { + imap.write(&b"80 delete my-archives\r\n"[..])?; + let mut buffer: [u8; 1500] = [0; 1500]; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"80 OK"[..]); + + imap.write(&b"81 list \"\" *\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"81 OK LIST"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + assert!(!srv_msg.contains(" archive\r\n")); + assert!(!srv_msg.contains(" my-archives\r\n")); + assert!(srv_msg.contains(" INBOX\r\n")); + + 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]) +} -- cgit v1.2.3 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 --- tests/common.rs | 29 +++++- tests/rfc3501_imap4rev1_base.rs | 129 +++++++++++--------------- tests/rfc3691_imapext_unselect.rs | 190 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 273 insertions(+), 75 deletions(-) create mode 100644 tests/rfc3691_imapext_unselect.rs (limited to 'tests') 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 From b91c64920d7454d50b60ad3abb58fad9d09f0511 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 3 Jan 2024 15:21:51 +0100 Subject: add test for imap move --- tests/rfc6851_imapext_move.rs | 190 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 tests/rfc6851_imapext_move.rs (limited to 'tests') diff --git a/tests/rfc6851_imapext_move.rs b/tests/rfc6851_imapext_move.rs new file mode 100644 index 0000000..d379f8d --- /dev/null +++ b/tests/rfc6851_imapext_move.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 +"; + +enum Mailbox { + Inbox, + Archive, +} + +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")?; + create_mailbox(imap_socket).context("created mailbox archive")?; + select(imap_socket, Mailbox::Inbox).context("select inbox")?; + noop_exists(imap_socket).context("noop loop must detect a new email")?; + + r#move(imap_socket).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).context("select archive")?; + fetch_rfc822(imap_socket, EMAIL).context("check mail exists")?; + logout(imap_socket).context("must quit")?; + + 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 create_mailbox(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + imap.write(&b"15 create archive\r\n"[..])?; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..12], &b"15 OK CREATE"[..]); + + 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(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> { + let mut buffer: [u8; 6000] = [0; 6000]; + + match mbx { + Mailbox::Inbox => imap.write(&b"20 select inbox\r\n"[..])?, + Mailbox::Archive => imap.write(&b"20 select archive\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 r#move(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + imap.write(&b"35 move 1 archive\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"35 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + assert!(srv_msg.contains("* 1 EXPUNGE")); + + 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 logout(imap: &mut TcpStream) -> Result<()> { + imap.write(&b"99 logout\r\n"[..])?; + let mut buffer: [u8; 1500] = [0; 1500]; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"* BYE"[..]); + 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 From a0a7dd0ed6afac48c5d5c0b149efb8288d03e115 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 4 Jan 2024 11:23:26 +0100 Subject: BDD pattern is clarified --- tests/common.rs | 68 ------- tests/common/constants.rs | 54 ++++++ tests/common/fragments.rs | 366 ++++++++++++++++++++++++++++++++++++++ tests/common/mod.rs | 90 ++++++++++ tests/rfc3501_imap4rev1_base.rs | 338 ++--------------------------------- tests/rfc3691_imapext_unselect.rs | 186 ++----------------- tests/rfc6851_imapext_move.rs | 186 ++----------------- 7 files changed, 550 insertions(+), 738 deletions(-) delete mode 100644 tests/common.rs create mode 100644 tests/common/constants.rs create mode 100644 tests/common/fragments.rs create mode 100644 tests/common/mod.rs (limited to 'tests') diff --git a/tests/common.rs b/tests/common.rs deleted file mode 100644 index f89f26c..0000000 --- a/tests/common.rs +++ /dev/null @@ -1,68 +0,0 @@ -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<()> { - let mut daemon = Command::new(env!("CARGO_BIN_EXE_aerogramme")) - .arg("--dev") - .arg("provider") - .arg("daemon") - .spawn()?; - - let mut max_retry = 20; - let mut imap_socket = loop { - max_retry -= 1; - match (TcpStream::connect("[::1]:1143"), max_retry) { - (Err(e), 0) => bail!("no more retry, last error is: {}", e), - (Err(e), _) => { - println!("unable to connect: {} ; will retry in 1 sec", e); - } - (Ok(v), _) => break v, - } - thread::sleep(SMALL_DELAY); - }; - - 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); - println!("-- test teardown --"); - - imap_socket - .shutdown(Shutdown::Both) - .context("closing imap socket at the end of the test")?; - lmtp_socket - .shutdown(Shutdown::Both) - .context("closing lmtp socket at the end of the test")?; - daemon.kill().context("daemon should be killed")?; - - 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/common/constants.rs b/tests/common/constants.rs new file mode 100644 index 0000000..c11a04d --- /dev/null +++ b/tests/common/constants.rs @@ -0,0 +1,54 @@ +use std::time; + +pub static SMALL_DELAY: time::Duration = time::Duration::from_millis(200); + +pub 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 +"; + +pub 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/common/fragments.rs b/tests/common/fragments.rs new file mode 100644 index 0000000..530f1f7 --- /dev/null +++ b/tests/common/fragments.rs @@ -0,0 +1,366 @@ +use anyhow::{bail, Result}; +use std::io::Write; +use std::net::TcpStream; +use std::thread; + +use crate::common::constants::*; +use crate::common::*; + +/// These fragments are not a generic IMAP client +/// but specialized to our specific tests. They can't take +/// arbitrary values, only enum for which the code is known +/// to be correct. The idea is that the generated message is more +/// or less hardcoded by the developer, so its clear what's expected, +/// and not generated by a library. + +pub 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(()) +} + +pub enum Account { + Alice, +} + +pub enum Extension { + None, + Unselect, + Move, + CondStore, +} + +pub enum Mailbox { + Inbox, + Archive, + Drafts, +} + +pub enum Flag { + Deleted, + Important +} + +pub enum Email { + Basic, + Multipart, +} + +pub enum Selection { + FirstId, + SecondId, +} + +pub fn capability(imap: &mut TcpStream, ext: Extension) -> Result<()> { + imap.write(&b"5 capability\r\n"[..])?; + + let maybe_ext = match ext { + Extension::None => None, + Extension::Unselect => Some("UNSELECT"), + Extension::Move => Some("MOVE"), + Extension::CondStore => Some("CONDSTORE"), + }; + + 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")); + if let Some(ext) = maybe_ext { + assert!(srv_msg.contains(ext)); + } + + Ok(()) +} + +pub fn login(imap: &mut TcpStream, account: Account) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + assert!(matches!(account, Account::Alice)); + 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(()) +} + +pub fn create_mailbox(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + let mbx_str = match mbx { + Mailbox::Inbox => "INBOX", + Mailbox::Archive => "Archive", + Mailbox::Drafts => "Drafts", + }; + + let cmd = format!("15 create {}\r\n", mbx_str); + imap.write(cmd.as_bytes())?; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..12], &b"15 OK CREATE"[..]); + + Ok(()) +} + +pub fn select(imap: &mut TcpStream, mbx: Mailbox, maybe_exists: Option) -> Result<()> { + let mut buffer: [u8; 6000] = [0; 6000]; + + let mbx_str = match mbx { + Mailbox::Inbox => "INBOX", + Mailbox::Archive => "Archive", + Mailbox::Drafts => "Drafts", + }; + imap.write(format!("20 select {}\r\n", mbx_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(()) +} + +pub 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(()) +} + +pub fn check(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + imap.write(&b"21 check\r\n"[..])?; + let _read = read_lines(imap, &mut buffer, Some(&b"21 OK"[..]))?; + + 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"[..])?; + let mut buffer: [u8; 6000] = [0; 6000]; + let _read = read_lines(imap, &mut buffer, Some(&b"25 OK"[..]))?; + + Ok(()) +} + +pub 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(()) +} + +pub fn lmtp_deliver_email(lmtp: &mut TcpStream, email_type: Email) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + let email = match email_type { + Email::Basic => EMAIL2, + Email::Multipart => EMAIL1, + }; + 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(()) +} + +pub 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"[..]))?; + 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"), + _ => (), + } + + thread::sleep(SMALL_DELAY); + } + + Ok(()) +} + +pub fn fetch_rfc822(imap: &mut TcpStream, selection: Selection, r#ref: Email) -> Result<()> { + let mut buffer: [u8; 65535] = [0; 65535]; + + assert!(matches!(selection, Selection::FirstId)); + 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 ref_mail = match r#ref { + Email::Basic => EMAIL2, + Email::Multipart => EMAIL1, + }; + let orig_email = std::str::from_utf8(ref_mail)?; + assert!(srv_msg.contains(orig_email)); + + Ok(()) +} + +pub fn copy(imap: &mut TcpStream, selection: Selection, to: Mailbox) -> Result<()> { + let mut buffer: [u8; 65535] = [0; 65535]; + assert!(matches!(selection, Selection::FirstId)); + assert!(matches!(to, Mailbox::Archive)); + + imap.write(&b"45 copy 1 Archive\r\n"[..])?; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"45 OK"[..]); + + Ok(()) +} + +pub fn append_email(imap: &mut TcpStream, content: Email) -> Result<()> { + let mut buffer: [u8; 6000] = [0; 6000]; + + let ref_mail = match content { + Email::Multipart => EMAIL1, + Email::Basic => EMAIL2, + }; + + let append_cmd = format!("47 append inbox (\\Seen) {{{}}}\r\n", ref_mail.len()); + println!("append cmd: {}", append_cmd); + imap.write(append_cmd.as_bytes())?; + + // wait for continuation + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(read[0], b'+'); + + // write our stuff + imap.write(ref_mail)?; + imap.write(&b"\r\n"[..])?; + 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<()> { + 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(()) +} + +#[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 expunge(imap: &mut TcpStream) -> Result<()> { + imap.write(&b"60 expunge\r\n"[..])?; + let mut buffer: [u8; 1500] = [0; 1500]; + let _read = read_lines(imap, &mut buffer, Some(&b"60 OK EXPUNGE"[..]))?; + + Ok(()) +} + +pub fn rename_mailbox(imap: &mut TcpStream, from: Mailbox, to: Mailbox) -> Result<()> { + assert!(matches!(from, Mailbox::Archive)); + assert!(matches!(to, Mailbox::Drafts)); + + imap.write(&b"70 rename Archive Drafts\r\n"[..])?; + let mut buffer: [u8; 1500] = [0; 1500]; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"70 OK"[..]); + + imap.write(&b"71 list \"\" *\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"71 OK LIST"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + assert!(!srv_msg.contains(" Archive\r\n")); + assert!(srv_msg.contains(" INBOX\r\n")); + assert!(srv_msg.contains(" Drafts\r\n")); + + Ok(()) +} + +pub fn delete_mailbox(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> { + let mbx_str = match mbx { + Mailbox::Inbox => "INBOX", + Mailbox::Archive => "Archive", + Mailbox::Drafts => "Drafts", + }; + let cmd = format!("80 delete {}\r\n", mbx_str); + + imap.write(cmd.as_bytes())?; + let mut buffer: [u8; 1500] = [0; 1500]; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"80 OK"[..]); + + imap.write(&b"81 list \"\" *\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"81 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + assert!(srv_msg.contains(" INBOX\r\n")); + assert!(!srv_msg.contains(format!(" {}\r\n", mbx_str).as_str())); + + Ok(()) +} + +pub 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(()) +} + +pub fn r#move(imap: &mut TcpStream, selection: Selection, to: Mailbox) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + assert!(matches!(to, Mailbox::Archive)); + assert!(matches!(selection, Selection::FirstId)); + + imap.write(&b"35 move 1 Archive\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"35 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + assert!(srv_msg.contains("* 1 EXPUNGE")); + + Ok(()) +} + +pub fn logout(imap: &mut TcpStream) -> Result<()> { + imap.write(&b"99 logout\r\n"[..])?; + let mut buffer: [u8; 1500] = [0; 1500]; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"* BYE"[..]); + Ok(()) +} diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..d0f4ed8 --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,90 @@ +#![allow(dead_code)] +pub mod constants; +pub mod fragments; + +use anyhow::{bail, Context, Result}; +use std::io::Read; +use std::net::{Shutdown, TcpStream}; +use std::process::Command; +use std::thread; + +use constants::SMALL_DELAY; + +pub fn aerogramme_provider_daemon_dev( + mut fx: impl FnMut(&mut TcpStream, &mut TcpStream) -> Result<()>, +) -> Result<()> { + // Check port is not used (= free) before starting the test + let mut max_retry = 20; + loop { + max_retry -= 1; + match (TcpStream::connect("[::1]:1143"), max_retry) { + (Ok(_), 0) => bail!("something is listening on [::1]:1143 and prevent the test from starting"), + (Ok(_), _) => println!("something is listening on [::1]:1143, maybe a previous daemon quitting, retrying soon..."), + (Err(_), _) => { + println!("test ready to start, [::1]:1143 is free!"); + break + } + } + thread::sleep(SMALL_DELAY); + } + + // Start daemon + let mut daemon = Command::new(env!("CARGO_BIN_EXE_aerogramme")) + .arg("--dev") + .arg("provider") + .arg("daemon") + .spawn()?; + + // Check that our daemon is correctly listening on the free port + let mut max_retry = 20; + let mut imap_socket = loop { + max_retry -= 1; + match (TcpStream::connect("[::1]:1143"), max_retry) { + (Err(e), 0) => bail!("no more retry, last error is: {}", e), + (Err(e), _) => { + println!("unable to connect: {} ; will retry soon...", e); + } + (Ok(v), _) => break v, + } + thread::sleep(SMALL_DELAY); + }; + + // Assuming now it's safe to open a LMTP socket + 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); + println!("-- test teardown --"); + + imap_socket + .shutdown(Shutdown::Both) + .context("closing imap socket at the end of the test")?; + lmtp_socket + .shutdown(Shutdown::Both) + .context("closing lmtp socket at the end of the test")?; + daemon.kill().context("daemon should be killed")?; + + 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 c0985dd..690913a 100644 --- a/tests/rfc3501_imap4rev1_base.rs +++ b/tests/rfc3501_imap4rev1_base.rs @@ -1,341 +1,33 @@ -use anyhow::{bail, Context, Result}; -use std::io::Write; -use std::net::TcpStream; -use std::{thread, time}; +use anyhow::Context; mod common; -use crate::common::read_lines; - -static SMALL_DELAY: time::Duration = time::Duration::from_millis(200); +use crate::common::fragments::*; fn main() { common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| { connect(imap_socket).context("server says hello")?; - capability(imap_socket).context("check server capabilities")?; - login(imap_socket).context("login test")?; - create_mailbox(imap_socket).context("created mailbox archive")?; + capability(imap_socket, Extension::None).context("check server capabilities")?; + login(imap_socket, Account::Alice).context("login test")?; + create_mailbox(imap_socket, Mailbox::Archive).context("created mailbox archive")?; // UNSUBSCRIBE IS NOT IMPLEMENTED YET //unsubscribe_mailbox(imap_socket).context("unsubscribe from archive")?; - select_inbox(imap_socket).context("select inbox")?; + select(imap_socket, Mailbox::Inbox, None).context("select inbox")?; check(imap_socket).context("check must run")?; - status_mailbox(imap_socket).context("status of archive from inbox")?; + status_mailbox(imap_socket, Mailbox::Archive).context("status of archive from inbox")?; lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; - lmtp_deliver_email(lmtp_socket, EMAIL1).context("mail delivered successfully")?; + 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, EMAIL1).context("fetch rfc822 message")?; - copy_email(imap_socket).context("copy message to the archive mailbox")?; - append_email(imap_socket, EMAIL2).context("insert email in INBOX")?; + fetch_rfc822(imap_socket, Selection::FirstId, Email::Multipart).context("fetch rfc822 message, should be our first message")?; + 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) - .context("should add delete and important flags to the email")?; + add_flags_email(imap_socket, Selection::FirstId, Flag::Deleted) + .context("should add delete flag 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")?; + rename_mailbox(imap_socket, Mailbox::Archive, Mailbox::Drafts).context("Archive mailbox is renamed Drafts")?; + delete_mailbox(imap_socket, Mailbox::Drafts).context("Drafts mailbox is deleted")?; 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")); - - 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 create_mailbox(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 1500] = [0; 1500]; - - imap.write(&b"15 create archive\r\n"[..])?; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..12], &b"15 OK CREATE"[..]); - - Ok(()) -} - -#[allow(dead_code)] -fn unsubscribe_mailbox(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 6000] = [0; 6000]; - - imap.write(&b"16 lsub \"\" *\r\n"[..])?; - let read = read_lines(imap, &mut buffer, Some(&b"16 OK LSUB"[..]))?; - let srv_msg = std::str::from_utf8(read)?; - assert!(srv_msg.contains(" INBOX\r\n")); - assert!(srv_msg.contains(" archive\r\n")); - - imap.write(&b"17 unsubscribe archive\r\n"[..])?; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..5], &b"17 OK"[..]); - - imap.write(&b"18 lsub \"\" *\r\n"[..])?; - let read = read_lines(imap, &mut buffer, Some(&b"18 OK LSUB"[..]))?; - let srv_msg = std::str::from_utf8(read)?; - assert!(srv_msg.contains(" INBOX\r\n")); - assert!(!srv_msg.contains(" archive\r\n")); - - 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 check(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 1500] = [0; 1500]; - - imap.write(&b"21 check\r\n"[..])?; - let _read = read_lines(imap, &mut buffer, Some(&b"21 OK"[..]))?; - - Ok(()) -} - -fn status_mailbox(imap: &mut TcpStream) -> Result<()> { - imap.write(&b"25 STATUS archive (UIDNEXT MESSAGES)\r\n"[..])?; - let mut buffer: [u8; 6000] = [0; 6000]; - let _read = read_lines(imap, &mut buffer, Some(&b"25 OK"[..]))?; - - 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 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 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 copy_email(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 65535] = [0; 65535]; - imap.write(&b"45 copy 1 archive\r\n"[..])?; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..5], &b"45 OK"[..]); - - Ok(()) -} - -fn append_email(imap: &mut TcpStream, ref_mail: &[u8]) -> Result<()> { - let mut buffer: [u8; 6000] = [0; 6000]; - assert_ne!(ref_mail.len(), 0); - let append_cmd = format!("47 append inbox (\\Seen) {{{}}}\r\n", ref_mail.len()); - println!("append cmd: {}", append_cmd); - imap.write(append_cmd.as_bytes())?; - - // wait for continuation - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(read[0], b'+'); - - // write our stuff - imap.write(ref_mail)?; - imap.write(&b"\r\n"[..])?; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..5], &b"47 OK"[..]); - - // noop to force a sync - imap.write(&b"48 NOOP\r\n"[..])?; - let _read = read_lines(imap, &mut buffer, Some(&b"48 OK NOOP"[..]))?; - - // check it is stored successfully - imap.write(&b"49 fetch 2 rfc822.size\r\n"[..])?; - let read = read_lines(imap, &mut buffer, Some(&b"49 OK"[..]))?; - let expected = format!("* 2 FETCH (RFC822.SIZE {})", ref_mail.len()); - let expbytes = expected.as_bytes(); - assert_eq!(&read[..expbytes.len()], expbytes); - - Ok(()) -} - -fn add_flags_email(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 1500] = [0; 1500]; - imap.write(&b"50 store 1 +FLAGS (\\Deleted \\Important)\r\n"[..])?; - let _read = read_lines(imap, &mut buffer, Some(&b"50 OK STORE"[..]))?; - - Ok(()) -} - -#[allow(dead_code)] -/// Not yet implemented -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(()) -} - -fn expunge(imap: &mut TcpStream) -> Result<()> { - imap.write(&b"60 expunge\r\n"[..])?; - let mut buffer: [u8; 1500] = [0; 1500]; - let _read = read_lines(imap, &mut buffer, Some(&b"60 OK EXPUNGE"[..]))?; - - Ok(()) -} - -fn rename_mailbox(imap: &mut TcpStream) -> Result<()> { - imap.write(&b"70 rename archive my-archives\r\n"[..])?; - let mut buffer: [u8; 1500] = [0; 1500]; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..5], &b"70 OK"[..]); - - imap.write(&b"71 list \"\" *\r\n"[..])?; - let read = read_lines(imap, &mut buffer, Some(&b"71 OK LIST"[..]))?; - let srv_msg = std::str::from_utf8(read)?; - assert!(!srv_msg.contains(" archive\r\n")); - assert!(srv_msg.contains(" INBOX\r\n")); - assert!(srv_msg.contains(" my-archives\r\n")); - - Ok(()) -} - -fn delete_mailbox(imap: &mut TcpStream) -> Result<()> { - imap.write(&b"80 delete my-archives\r\n"[..])?; - let mut buffer: [u8; 1500] = [0; 1500]; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..5], &b"80 OK"[..]); - - imap.write(&b"81 list \"\" *\r\n"[..])?; - let read = read_lines(imap, &mut buffer, Some(&b"81 OK LIST"[..]))?; - let srv_msg = std::str::from_utf8(read)?; - assert!(!srv_msg.contains(" archive\r\n")); - assert!(!srv_msg.contains(" my-archives\r\n")); - assert!(srv_msg.contains(" INBOX\r\n")); - - Ok(()) -} - -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 index 28e4bc8..4c5a45d 100644 --- a/tests/rfc3691_imapext_unselect.rs +++ b/tests/rfc3691_imapext_unselect.rs @@ -1,190 +1,28 @@ -use anyhow::{bail, Context, Result}; -use std::io::Write; -use std::net::TcpStream; -use std::{thread, time}; +use anyhow::Context; 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 -"; +use crate::common::fragments::*; fn main() { common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| { + connect(imap_socket).context("server says hello")?; + lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; - lmtp_deliver_email(lmtp_socket, EMAIL).context("mail delivered successfully")?; + lmtp_deliver_email(lmtp_socket, Email::Basic).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")?; + 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).context("add delete flags to the email")?; + add_flags_email(imap_socket, Selection::FirstId, Flag::Deleted).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")?; + select(imap_socket, Mailbox::Inbox, Some(1)).context("select inbox again")?; + fetch_rfc822(imap_socket, Selection::FirstId, Email::Basic).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")?; + select(imap_socket, Mailbox::Inbox, Some(0)).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(()) -} diff --git a/tests/rfc6851_imapext_move.rs b/tests/rfc6851_imapext_move.rs index d379f8d..a535bce 100644 --- a/tests/rfc6851_imapext_move.rs +++ b/tests/rfc6851_imapext_move.rs @@ -1,190 +1,30 @@ -use anyhow::{bail, Context, Result}; -use std::io::Write; -use std::net::TcpStream; -use std::{thread, time}; +use anyhow::Context; 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 -"; - -enum Mailbox { - Inbox, - Archive, -} +use common::fragments::*; fn main() { 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")?; + lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; - lmtp_deliver_email(lmtp_socket, EMAIL).context("mail delivered successfully")?; + lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?; - connect(imap_socket).context("server says hello")?; - capability(imap_socket).context("check server capabilities")?; - login(imap_socket).context("login test")?; - create_mailbox(imap_socket).context("created mailbox archive")?; - select(imap_socket, Mailbox::Inbox).context("select inbox")?; noop_exists(imap_socket).context("noop loop must detect a new email")?; - - r#move(imap_socket).context("message from inbox moved to archive")?; + 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).context("select archive")?; - fetch_rfc822(imap_socket, EMAIL).context("check mail exists")?; + select(imap_socket, Mailbox::Archive, Some(1)).context("select archive")?; + fetch_rfc822(imap_socket, Selection::FirstId, Email::Basic).context("check mail exists")?; logout(imap_socket).context("must quit")?; 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 create_mailbox(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 1500] = [0; 1500]; - - imap.write(&b"15 create archive\r\n"[..])?; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..12], &b"15 OK CREATE"[..]); - - 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(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> { - let mut buffer: [u8; 6000] = [0; 6000]; - - match mbx { - Mailbox::Inbox => imap.write(&b"20 select inbox\r\n"[..])?, - Mailbox::Archive => imap.write(&b"20 select archive\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 r#move(imap: &mut TcpStream) -> Result<()> { - let mut buffer: [u8; 1500] = [0; 1500]; - imap.write(&b"35 move 1 archive\r\n"[..])?; - let read = read_lines(imap, &mut buffer, Some(&b"35 OK"[..]))?; - let srv_msg = std::str::from_utf8(read)?; - assert!(srv_msg.contains("* 1 EXPUNGE")); - - 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 logout(imap: &mut TcpStream) -> Result<()> { - imap.write(&b"99 logout\r\n"[..])?; - let mut buffer: [u8; 1500] = [0; 1500]; - let read = read_lines(imap, &mut buffer, None)?; - assert_eq!(&read[..5], &b"* BYE"[..]); - 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 From a93967a6f8f1c881c64879e61edc46d64c4c662e Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 4 Jan 2024 11:51:14 +0100 Subject: create a single behavior test with all files --- tests/behavior.rs | 107 ++++++++++++++++++++++++++++++++++++++++++++++ tests/common/fragments.rs | 31 +++++++++++++- 2 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 tests/behavior.rs (limited to 'tests') diff --git a/tests/behavior.rs b/tests/behavior.rs new file mode 100644 index 0000000..e62832b --- /dev/null +++ b/tests/behavior.rs @@ -0,0 +1,107 @@ +use anyhow::Context; + +mod common; +use crate::common::fragments::*; + +fn main() { + rfc3501_imap4rev1_base(); + rfc3691_imapext_unselect(); + rfc5161_imapext_enable(); + rfc6851_imapext_move(); +} + +fn 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")?; + login(imap_socket, Account::Alice).context("login test")?; + 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")?; + check(imap_socket).context("check must run")?; + status_mailbox(imap_socket, Mailbox::Archive).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).context("fetch rfc822 message, should be our first message")?; + 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) + .context("should add delete flag to the email")?; + expunge(imap_socket).context("expunge emails")?; + rename_mailbox(imap_socket, Mailbox::Archive, Mailbox::Drafts).context("Archive mailbox is renamed Drafts")?; + delete_mailbox(imap_socket, Mailbox::Drafts).context("Drafts mailbox is deleted")?; + Ok(()) + }) + .expect("test fully run"); +} + +fn rfc3691_imapext_unselect() { + println!("rfc3691_imapext_unselect"); + common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| { + connect(imap_socket).context("server says hello")?; + + lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; + lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?; + + 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).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).context("message is still present")?; + close(imap_socket).context("close inbox and expunge message")?; + select(imap_socket, Mailbox::Inbox, Some(0)).context("select inbox again and check it's empty")?; + + Ok(()) + }) + .expect("test fully run"); +} + +fn 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")?; + enable(imap_socket, Enable::Utf8Accept, Some(Enable::Utf8Accept))?; + enable(imap_socket, Enable::Utf8Accept, None)?; + logout(imap_socket)?; + + Ok(()) + }) + .expect("test fully run"); +} + +fn 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")?; + + 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")?; + 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")?; + logout(imap_socket).context("must quit")?; + + Ok(()) + }) + .expect("test fully run"); +} diff --git a/tests/common/fragments.rs b/tests/common/fragments.rs index 530f1f7..c8d5ef1 100644 --- a/tests/common/fragments.rs +++ b/tests/common/fragments.rs @@ -11,7 +11,11 @@ use crate::common::*; /// arbitrary values, only enum for which the code is known /// to be correct. The idea is that the generated message is more /// or less hardcoded by the developer, so its clear what's expected, -/// and not generated by a library. +/// and not generated by a library. Also don't use vector of enum, +/// as it again introduce some kind of genericity we try so hard to avoid: +/// instead add a dedicated enum, for example "All" or anything relaevent that would +/// describe your list and then hardcode it in your fragment. +/// DON'T. TRY. TO. BE. GENERIC. HERE. pub fn connect(imap: &mut TcpStream) -> Result<()> { let mut buffer: [u8; 1500] = [0; 1500]; @@ -33,6 +37,12 @@ pub enum Extension { CondStore, } +pub enum Enable { + Utf8Accept, + CondStore, + All, +} + pub enum Mailbox { Inbox, Archive, @@ -357,6 +367,25 @@ pub fn r#move(imap: &mut TcpStream, selection: Selection, to: Mailbox) -> Result Ok(()) } +pub fn enable(imap: &mut TcpStream, ask: Enable, done: Option) -> Result<()> { + let mut buffer: [u8; 6000] = [0; 6000]; + assert!(matches!(ask, Enable::Utf8Accept)); + + imap.write(&b"36 enable UTF8=ACCEPT\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"36 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + match done { + None => assert_eq!(srv_msg.lines().count(), 1), + Some(Enable::Utf8Accept) => { + assert_eq!(srv_msg.lines().count(), 2); + assert!(srv_msg.contains("* ENABLED UTF8=ACCEPT")); + }, + _ => unimplemented!(), + } + + Ok(()) +} + pub fn logout(imap: &mut TcpStream) -> Result<()> { imap.write(&b"99 logout\r\n"[..])?; let mut buffer: [u8; 1500] = [0; 1500]; -- cgit v1.2.3 From 3f5d7fa766c268923c0c2c6e1dd50f4693f51a44 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 4 Jan 2024 11:53:49 +0100 Subject: remove old tests --- tests/rfc3501_imap4rev1_base.rs | 33 --------------------------------- tests/rfc3691_imapext_unselect.rs | 28 ---------------------------- tests/rfc6851_imapext_move.rs | 30 ------------------------------ 3 files changed, 91 deletions(-) delete mode 100644 tests/rfc3501_imap4rev1_base.rs delete mode 100644 tests/rfc3691_imapext_unselect.rs delete mode 100644 tests/rfc6851_imapext_move.rs (limited to 'tests') diff --git a/tests/rfc3501_imap4rev1_base.rs b/tests/rfc3501_imap4rev1_base.rs deleted file mode 100644 index 690913a..0000000 --- a/tests/rfc3501_imap4rev1_base.rs +++ /dev/null @@ -1,33 +0,0 @@ -use anyhow::Context; - -mod common; -use crate::common::fragments::*; - -fn main() { - 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")?; - login(imap_socket, Account::Alice).context("login test")?; - 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")?; - check(imap_socket).context("check must run")?; - status_mailbox(imap_socket, Mailbox::Archive).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).context("fetch rfc822 message, should be our first message")?; - 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) - .context("should add delete flag to the email")?; - expunge(imap_socket).context("expunge emails")?; - rename_mailbox(imap_socket, Mailbox::Archive, Mailbox::Drafts).context("Archive mailbox is renamed Drafts")?; - delete_mailbox(imap_socket, Mailbox::Drafts).context("Drafts mailbox is deleted")?; - Ok(()) - }) - .expect("test fully run"); -} diff --git a/tests/rfc3691_imapext_unselect.rs b/tests/rfc3691_imapext_unselect.rs deleted file mode 100644 index 4c5a45d..0000000 --- a/tests/rfc3691_imapext_unselect.rs +++ /dev/null @@ -1,28 +0,0 @@ -use anyhow::Context; - -mod common; -use crate::common::fragments::*; - -fn main() { - common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| { - connect(imap_socket).context("server says hello")?; - - lmtp_handshake(lmtp_socket).context("handshake lmtp done")?; - lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?; - - 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).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).context("message is still present")?; - close(imap_socket).context("close inbox and expunge message")?; - select(imap_socket, Mailbox::Inbox, Some(0)).context("select inbox again and check it's empty")?; - - Ok(()) - }) - .expect("test fully run"); -} diff --git a/tests/rfc6851_imapext_move.rs b/tests/rfc6851_imapext_move.rs deleted file mode 100644 index a535bce..0000000 --- a/tests/rfc6851_imapext_move.rs +++ /dev/null @@ -1,30 +0,0 @@ -use anyhow::Context; - -mod common; -use common::fragments::*; - -fn main() { - 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")?; - - 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")?; - 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")?; - logout(imap_socket).context("must quit")?; - - Ok(()) - }) - .expect("test fully run"); -} -- cgit v1.2.3 From 7ae9966675c85b34f1a99d81062b44b74385a15b Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Thu, 4 Jan 2024 12:09:16 +0100 Subject: test enable --- tests/behavior.rs | 13 +++++++++++++ tests/common/fragments.rs | 13 ++++++++++++- tests/common/mod.rs | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/behavior.rs b/tests/behavior.rs index e62832b..82fdc53 100644 --- a/tests/behavior.rs +++ b/tests/behavior.rs @@ -8,6 +8,7 @@ fn main() { rfc3691_imapext_unselect(); rfc5161_imapext_enable(); rfc6851_imapext_move(); + rfc7888_imapext_literal(); } fn rfc3501_imap4rev1_base() { @@ -105,3 +106,15 @@ fn rfc6851_imapext_move() { }) .expect("test fully run"); } + +fn rfc7888_imapext_literal() { + println!("rfc7888_imapext_literal"); + common::aerogramme_provider_daemon_dev(|imap_socket, _lmtp_socket| { + connect(imap_socket).context("server says hello")?; + + capability(imap_socket, Extension::LiteralPlus).context("check server capabilities")?; + login_with_literal(imap_socket, Account::Alice).context("use literal to connect Alice")?; + + Ok(()) + }).expect("test fully run"); +} diff --git a/tests/common/fragments.rs b/tests/common/fragments.rs index c8d5ef1..3ed14cc 100644 --- a/tests/common/fragments.rs +++ b/tests/common/fragments.rs @@ -35,6 +35,7 @@ pub enum Extension { Unselect, Move, CondStore, + LiteralPlus, } pub enum Enable { @@ -72,9 +73,10 @@ pub fn capability(imap: &mut TcpStream, ext: Extension) -> Result<()> { Extension::Unselect => Some("UNSELECT"), Extension::Move => Some("MOVE"), Extension::CondStore => Some("CONDSTORE"), + Extension::LiteralPlus => Some("LITERAL+"), }; - let mut buffer: [u8; 1500] = [0; 1500]; + let mut buffer: [u8; 6000] = [0; 6000]; let read = read_lines(imap, &mut buffer, Some(&b"5 OK"[..]))?; let srv_msg = std::str::from_utf8(read)?; assert!(srv_msg.contains("IMAP4REV1")); @@ -97,6 +99,15 @@ pub fn login(imap: &mut TcpStream, account: Account) -> Result<()> { Ok(()) } +pub fn login_with_literal(imap: &mut TcpStream, account: Account) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + + assert!(matches!(account, Account::Alice)); + imap.write(&b"10 login {5+}\r\nalice {7+}\r\nhunter2\r\n"[..])?; + let _read = read_lines(imap, &mut buffer, Some(&b"10 OK"[..]))?; + Ok(()) +} + pub fn create_mailbox(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> { let mut buffer: [u8; 1500] = [0; 1500]; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index d0f4ed8..810fd79 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -81,7 +81,7 @@ pub fn read_lines<'a, F: Read>( None => true, Some(mark) => buffer[..nbytes].windows(mark.len()).any(|w| w == mark), }; - if pre_condition && &buffer[nbytes - 2..nbytes] == &b"\r\n"[..] { + if pre_condition && nbytes >= 2 && &buffer[nbytes - 2..nbytes] == &b"\r\n"[..] { break; } } -- cgit v1.2.3