aerogramme/tests/behavior.rs

358 lines
14 KiB
Rust
Raw Normal View History

use anyhow::Context;
mod common;
2024-01-15 07:06:04 +00:00
use crate::common::constants::*;
2024-01-18 17:03:21 +00:00
use crate::common::fragments::*;
fn main() {
rfc3501_imap4rev1_base();
rfc6851_imapext_move();
2024-01-12 12:01:22 +00:00
rfc4551_imapext_condstore();
2024-01-19 13:13:43 +00:00
rfc2177_imapext_idle();
2024-01-20 18:23:44 +00:00
rfc5161_imapext_enable(); // 1
rfc3691_imapext_unselect(); // 2
rfc7888_imapext_literal(); // 3
rfc4315_imapext_uidplus(); // 4
rfc5819_imapext_liststatus(); // 5
2024-01-12 12:01:22 +00:00
println!("✅ SUCCESS 🌟🚀🥳🙏🥹");
}
fn rfc3501_imap4rev1_base() {
2024-01-12 12:01:22 +00:00
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")?;
2024-01-18 17:03:21 +00:00
let select_res =
select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?;
2024-01-15 07:06:04 +00:00
assert!(select_res.contains("* 0 EXISTS"));
check(imap_socket).context("check must run")?;
2024-01-18 17:03:21 +00:00
status(imap_socket, Mailbox::Archive, StatusKind::UidNext)
.context("status of archive from inbox")?;
lmtp_handshake(lmtp_socket).context("handshake lmtp done")?;
lmtp_deliver_email(lmtp_socket, Email::Multipart).context("mail delivered successfully")?;
2024-01-12 12:01:22 +00:00
noop_exists(imap_socket, 1).context("noop loop must detect a new email")?;
2024-01-15 07:06:04 +00:00
2024-01-18 17:03:21 +00:00
let srv_msg = fetch(
imap_socket,
Selection::FirstId,
FetchKind::Rfc822,
FetchMod::None,
)
.context("fetch rfc822 message, should be our first message")?;
2024-01-15 07:06:04 +00:00
let orig_email = std::str::from_utf8(EMAIL1)?;
assert!(srv_msg.contains(orig_email));
2024-01-18 17:03:21 +00:00
2024-01-05 09:05:30 +00:00
copy(imap_socket, Selection::FirstId, Mailbox::Archive)
.context("copy message to the archive mailbox")?;
2024-01-20 10:45:32 +00:00
append(imap_socket, Email::Basic).context("insert email in INBOX")?;
2024-01-12 12:01:22 +00:00
noop_exists(imap_socket, 2).context("noop loop must detect a new email")?;
2024-01-15 07:06:04 +00:00
search(imap_socket, SearchKind::Text("OoOoO")).expect("search should return something");
2024-01-18 17:03:21 +00:00
store(
imap_socket,
Selection::FirstId,
Flag::Deleted,
StoreAction::AddFlags,
StoreMod::None,
)
.context("should add delete flag to the email")?;
expunge(imap_socket).context("expunge emails")?;
2024-01-05 09:05:30 +00:00
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() {
2024-01-12 12:01:22 +00:00
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")?;
2024-01-18 17:03:21 +00:00
let select_res =
select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?;
2024-01-15 07:06:04 +00:00
assert!(select_res.contains("* 0 EXISTS"));
2024-01-12 12:01:22 +00:00
noop_exists(imap_socket, 1).context("noop loop must detect a new email")?;
2024-01-18 17:03:21 +00:00
store(
imap_socket,
Selection::FirstId,
Flag::Deleted,
StoreAction::AddFlags,
StoreMod::None,
)
.context("add delete flags to the email")?;
unselect(imap_socket)
.context("unselect inbox while preserving email with the \\Delete flag")?;
2024-01-18 17:03:21 +00:00
let select_res =
select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox again")?;
2024-01-15 07:06:04 +00:00
assert!(select_res.contains("* 1 EXISTS"));
2024-01-18 17:03:21 +00:00
let srv_msg = fetch(
imap_socket,
Selection::FirstId,
FetchKind::Rfc822,
FetchMod::None,
)
.context("message is still present")?;
2024-01-15 07:06:04 +00:00
let orig_email = std::str::from_utf8(EMAIL2)?;
assert!(srv_msg.contains(orig_email));
close(imap_socket).context("close inbox and expunge message")?;
2024-01-15 07:06:04 +00:00
let select_res = select(imap_socket, Mailbox::Inbox, SelectMod::None)
2024-01-05 09:05:30 +00:00
.context("select inbox again and check it's empty")?;
2024-01-15 07:06:04 +00:00
assert!(select_res.contains("* 0 EXISTS"));
Ok(())
})
.expect("test fully run");
}
fn rfc5161_imapext_enable() {
2024-01-12 12:01:22 +00:00
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() {
2024-01-12 12:01:22 +00:00
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")?;
2024-01-18 17:03:21 +00:00
let select_res =
select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?;
2024-01-15 07:06:04 +00:00
assert!(select_res.contains("* 0 EXISTS"));
lmtp_handshake(lmtp_socket).context("handshake lmtp done")?;
lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?;
2024-01-12 12:01:22 +00:00
noop_exists(imap_socket, 1).context("noop loop must detect a new email")?;
2024-01-05 09:05:30 +00:00
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")?;
2024-01-18 17:03:21 +00:00
let select_res =
select(imap_socket, Mailbox::Archive, SelectMod::None).context("select archive")?;
2024-01-15 07:06:04 +00:00
assert!(select_res.contains("* 1 EXISTS"));
let srv_msg = fetch(
2024-01-18 17:03:21 +00:00
imap_socket,
Selection::FirstId,
FetchKind::Rfc822,
2024-01-15 07:06:04 +00:00
FetchMod::None,
2024-01-18 17:03:21 +00:00
)
.context("check mail exists")?;
2024-01-15 07:06:04 +00:00
let orig_email = std::str::from_utf8(EMAIL2)?;
assert!(srv_msg.contains(orig_email));
logout(imap_socket).context("must quit")?;
Ok(())
})
.expect("test fully run");
}
2024-01-04 11:09:16 +00:00
fn rfc7888_imapext_literal() {
2024-01-12 12:01:22 +00:00
println!("🧪 rfc7888_imapext_literal");
2024-01-04 11:09:16 +00:00
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(())
2024-01-05 09:05:30 +00:00
})
.expect("test fully run");
2024-01-04 11:09:16 +00:00
}
2024-01-12 12:01:22 +00:00
fn rfc4551_imapext_condstore() {
println!("🧪 rfc4551_imapext_condstore");
common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| {
2024-01-12 14:02:02 +00:00
// Setup the test
2024-01-12 12:01:22 +00:00
connect(imap_socket).context("server says hello")?;
2024-01-15 07:06:04 +00:00
// RFC 3.1.1 Advertising Support for CONDSTORE
2024-01-12 12:01:22 +00:00
capability(imap_socket, Extension::Condstore).context("check server capabilities")?;
login(imap_socket, Account::Alice).context("login test")?;
2024-01-12 14:02:02 +00:00
2024-01-15 07:06:04 +00:00
// RFC 3.1.8. CONDSTORE Parameter to SELECT and EXAMINE
2024-01-18 17:03:21 +00:00
let select_res =
select(imap_socket, Mailbox::Inbox, SelectMod::Condstore).context("select inbox")?;
2024-01-15 07:06:04 +00:00
// RFC 3.1.2 New OK Untagged Responses for SELECT and EXAMINE
assert!(select_res.contains("[HIGHESTMODSEQ 1]"));
2024-01-12 14:02:02 +00:00
2024-01-15 07:06:04 +00:00
// RFC 3.1.3. STORE and UID STORE Commands
2024-01-12 14:02:02 +00:00
lmtp_handshake(lmtp_socket).context("handshake lmtp done")?;
lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?;
lmtp_deliver_email(lmtp_socket, Email::Multipart).context("mail delivered successfully")?;
2024-01-12 12:01:22 +00:00
noop_exists(imap_socket, 2).context("noop loop must detect a new email")?;
2024-01-18 17:03:21 +00:00
let store_res = store(
imap_socket,
Selection::All,
Flag::Important,
StoreAction::AddFlags,
StoreMod::UnchangedSince(1),
)?;
2024-01-15 07:06:04 +00:00
assert!(store_res.contains("[MODIFIED 2]"));
assert!(store_res.contains("* 1 FETCH (FLAGS (\\Important) MODSEQ (3))"));
assert!(!store_res.contains("* 2 FETCH"));
assert_eq!(store_res.lines().count(), 2);
// RFC 3.1.4. FETCH and UID FETCH Commands
2024-01-18 17:03:21 +00:00
let fetch_res = fetch(
imap_socket,
Selection::All,
FetchKind::Rfc822Size,
FetchMod::ChangedSince(2),
)?;
2024-02-20 10:42:51 +00:00
assert!(fetch_res.contains("* 1 FETCH (RFC822.SIZE 81 MODSEQ (3))"));
2024-01-15 07:06:04 +00:00
assert!(!fetch_res.contains("* 2 FETCH"));
assert_eq!(store_res.lines().count(), 2);
// RFC 3.1.5. MODSEQ Search Criterion in SEARCH
let search_res = search(imap_socket, SearchKind::ModSeq(3))?;
// RFC 3.1.6. Modified SEARCH Untagged Response
assert!(search_res.contains("* SEARCH 1 (MODSEQ 3)"));
// RFC 3.1.7 HIGHESTMODSEQ Status Data Items
let status_res = status(imap_socket, Mailbox::Inbox, StatusKind::HighestModSeq)?;
assert!(status_res.contains("HIGHESTMODSEQ 3"));
2024-01-12 14:02:02 +00:00
2024-01-12 12:01:22 +00:00
Ok(())
})
.expect("test fully run");
}
2024-01-19 13:13:43 +00:00
fn rfc2177_imapext_idle() {
println!("🧪 rfc2177_imapext_idle");
common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| {
2024-01-20 10:45:32 +00:00
// Test setup, check capability
2024-01-19 13:13:43 +00:00
connect(imap_socket).context("server says hello")?;
capability(imap_socket, Extension::Idle).context("check server capabilities")?;
login(imap_socket, Account::Alice).context("login test")?;
select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?;
// Check that new messages from LMTP are correctly detected during idling
start_idle(imap_socket).context("can't start idling")?;
lmtp_handshake(lmtp_socket).context("handshake lmtp done")?;
lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?;
let srv_msg = stop_idle(imap_socket).context("stop idling")?;
assert!(srv_msg.contains("* 1 EXISTS"));
Ok(())
})
.expect("test fully run");
}
2024-01-20 10:45:32 +00:00
fn rfc4315_imapext_uidplus() {
println!("🧪 rfc4315_imapext_uidplus");
common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| {
// Test setup, check capability, insert 2 emails
connect(imap_socket).context("server says hello")?;
capability(imap_socket, Extension::UidPlus).context("check server capabilities")?;
login(imap_socket, Account::Alice).context("login test")?;
select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?;
lmtp_handshake(lmtp_socket).context("handshake lmtp done")?;
lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?;
lmtp_deliver_email(lmtp_socket, Email::Multipart).context("mail delivered successfully")?;
noop_exists(imap_socket, 2).context("noop loop must detect a new email")?;
// Check UID EXPUNGE seqset
store(
imap_socket,
Selection::All,
Flag::Deleted,
StoreAction::AddFlags,
StoreMod::None,
)?;
let res = uid_expunge(imap_socket, Selection::FirstId)?;
assert_eq!(res.lines().count(), 2);
assert!(res.contains("* 1 EXPUNGE"));
// APPENDUID check UID + UID VALIDITY
// Note: 4 and not 3, as we update the UID counter when we delete an email
// it's part of our UID proof
let res = append(imap_socket, Email::Multipart)?;
assert!(res.contains("[APPENDUID 1 4]"));
// COPYUID, check
create_mailbox(imap_socket, Mailbox::Archive).context("created mailbox archive")?;
let res = copy(imap_socket, Selection::FirstId, Mailbox::Archive)?;
assert!(res.contains("[COPYUID 1 2 1]"));
// MOVEUID, check
let res = r#move(imap_socket, Selection::FirstId, Mailbox::Archive)?;
assert!(res.contains("[COPYUID 1 2 2]"));
Ok(())
})
.expect("test fully run");
}
2024-01-20 18:23:44 +00:00
///
/// Example
///
/// ```text
/// 30 list "" "*" RETURN (STATUS (MESSAGES UNSEEN))
/// * LIST (\Subscribed) "." INBOX
/// * STATUS INBOX (MESSAGES 2 UNSEEN 1)
/// 30 OK LIST completed
/// ```
fn rfc5819_imapext_liststatus() {
println!("🧪 rfc5819_imapext_liststatus");
common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket| {
// Test setup, check capability, add 2 emails, read 1
connect(imap_socket).context("server says hello")?;
capability(imap_socket, Extension::ListStatus).context("check server capabilities")?;
login(imap_socket, Account::Alice).context("login test")?;
select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?;
lmtp_handshake(lmtp_socket).context("handshake lmtp done")?;
lmtp_deliver_email(lmtp_socket, Email::Basic).context("mail delivered successfully")?;
lmtp_deliver_email(lmtp_socket, Email::Multipart).context("mail delivered successfully")?;
noop_exists(imap_socket, 2).context("noop loop must detect a new email")?;
fetch(
imap_socket,
Selection::FirstId,
FetchKind::Rfc822,
FetchMod::None,
)
.context("read one message")?;
close(imap_socket).context("close inbox")?;
// Test return status MESSAGES UNSEEN
let ret = list(
imap_socket,
MbxSelect::All,
ListReturn::StatusMessagesUnseen,
)?;
assert!(ret.contains("* STATUS INBOX (MESSAGES 2 UNSEEN 1)"));
// Test that without RETURN, no status is sent
let ret = list(imap_socket, MbxSelect::All, ListReturn::None)?;
assert!(!ret.contains("* STATUS"));
Ok(())
})
.expect("test fully run");
}