diff options
Diffstat (limited to 'aerogramme/tests')
-rw-r--r-- | aerogramme/tests/behavior.rs | 1292 | ||||
-rw-r--r-- | aerogramme/tests/common/constants.rs | 243 | ||||
-rw-r--r-- | aerogramme/tests/common/fragments.rs | 570 | ||||
-rw-r--r-- | aerogramme/tests/common/mod.rs | 136 |
4 files changed, 2241 insertions, 0 deletions
diff --git a/aerogramme/tests/behavior.rs b/aerogramme/tests/behavior.rs new file mode 100644 index 0000000..f8d609a --- /dev/null +++ b/aerogramme/tests/behavior.rs @@ -0,0 +1,1292 @@ +use anyhow::Context; + +mod common; +use crate::common::constants::*; +use crate::common::fragments::*; + +fn main() { + // IMAP + rfc3501_imap4rev1_base(); + rfc6851_imapext_move(); + rfc4551_imapext_condstore(); + rfc2177_imapext_idle(); + rfc5161_imapext_enable(); + rfc3691_imapext_unselect(); + rfc7888_imapext_literal(); + rfc4315_imapext_uidplus(); + rfc5819_imapext_liststatus(); + + // WebDAV + rfc4918_webdav_core(); + rfc5397_webdav_principal(); + rfc4791_webdav_caldav(); + rfc6578_webdav_sync(); + println!("✅ SUCCESS 🌟🚀🥳🙏🥹"); +} + +fn rfc3501_imap4rev1_base() { + println!("🧪 rfc3501_imap4rev1_base"); + common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket, _dav_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")?; + let select_res = + select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?; + assert!(select_res.contains("* 0 EXISTS")); + + check(imap_socket).context("check must run")?; + 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")?; + noop_exists(imap_socket, 1).context("noop loop must detect a new email")?; + + let srv_msg = fetch( + imap_socket, + Selection::FirstId, + FetchKind::Rfc822, + FetchMod::None, + ) + .context("fetch rfc822 message, should be our first message")?; + let orig_email = std::str::from_utf8(EMAIL1)?; + assert!(srv_msg.contains(orig_email)); + + copy(imap_socket, Selection::FirstId, Mailbox::Archive) + .context("copy message to the archive mailbox")?; + append(imap_socket, Email::Basic).context("insert email in INBOX")?; + noop_exists(imap_socket, 2).context("noop loop must detect a new email")?; + search(imap_socket, SearchKind::Text("OoOoO")).expect("search should return something"); + 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")?; + 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, _dav_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")?; + let select_res = + select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?; + assert!(select_res.contains("* 0 EXISTS")); + + noop_exists(imap_socket, 1).context("noop loop must detect a new email")?; + 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")?; + let select_res = + select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox again")?; + assert!(select_res.contains("* 1 EXISTS")); + + let srv_msg = fetch( + imap_socket, + Selection::FirstId, + FetchKind::Rfc822, + FetchMod::None, + ) + .context("message is still present")?; + let orig_email = std::str::from_utf8(EMAIL2)?; + assert!(srv_msg.contains(orig_email)); + + close(imap_socket).context("close inbox and expunge message")?; + let select_res = select(imap_socket, Mailbox::Inbox, SelectMod::None) + .context("select inbox again and check it's empty")?; + assert!(select_res.contains("* 0 EXISTS")); + + Ok(()) + }) + .expect("test fully run"); +} + +fn rfc5161_imapext_enable() { + println!("🧪 rfc5161_imapext_enable"); + common::aerogramme_provider_daemon_dev(|imap_socket, _lmtp_socket, _dav_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, _dav_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")?; + let select_res = + select(imap_socket, Mailbox::Inbox, SelectMod::None).context("select inbox")?; + 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")?; + + noop_exists(imap_socket, 1).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")?; + let select_res = + select(imap_socket, Mailbox::Archive, SelectMod::None).context("select archive")?; + assert!(select_res.contains("* 1 EXISTS")); + + let srv_msg = fetch( + imap_socket, + Selection::FirstId, + FetchKind::Rfc822, + FetchMod::None, + ) + .context("check mail exists")?; + 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"); +} + +fn rfc7888_imapext_literal() { + println!("🧪 rfc7888_imapext_literal"); + common::aerogramme_provider_daemon_dev(|imap_socket, _lmtp_socket, _dav_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"); +} + +fn rfc4551_imapext_condstore() { + println!("🧪 rfc4551_imapext_condstore"); + common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket, _dav_socket| { + // Setup the test + connect(imap_socket).context("server says hello")?; + + // RFC 3.1.1 Advertising Support for CONDSTORE + capability(imap_socket, Extension::Condstore).context("check server capabilities")?; + login(imap_socket, Account::Alice).context("login test")?; + + // RFC 3.1.8. CONDSTORE Parameter to SELECT and EXAMINE + let select_res = + select(imap_socket, Mailbox::Inbox, SelectMod::Condstore).context("select inbox")?; + // RFC 3.1.2 New OK Untagged Responses for SELECT and EXAMINE + assert!(select_res.contains("[HIGHESTMODSEQ 1]")); + + // RFC 3.1.3. STORE and UID STORE Commands + 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")?; + let store_res = store( + imap_socket, + Selection::All, + Flag::Important, + StoreAction::AddFlags, + StoreMod::UnchangedSince(1), + )?; + 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 + let fetch_res = fetch( + imap_socket, + Selection::All, + FetchKind::Rfc822Size, + FetchMod::ChangedSince(2), + )?; + assert!(fetch_res.contains("* 1 FETCH (RFC822.SIZE 81 MODSEQ (3))")); + 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")); + + Ok(()) + }) + .expect("test fully run"); +} + +fn rfc2177_imapext_idle() { + println!("🧪 rfc2177_imapext_idle"); + common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket, _dav_socket| { + // Test setup, check capability + 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"); +} + +fn rfc4315_imapext_uidplus() { + println!("🧪 rfc4315_imapext_uidplus"); + common::aerogramme_provider_daemon_dev(|imap_socket, lmtp_socket, _dav_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"); +} + +/// +/// 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, _dav_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"); +} + +use aero_dav::acltypes as acl; +use aero_dav::caltypes as cal; +use aero_dav::realization::{self, All}; +use aero_dav::synctypes as sync; +use aero_dav::types as dav; +use aero_dav::versioningtypes as vers; + +use crate::common::{dav_deserialize, dav_serialize}; + +fn rfc4918_webdav_core() { + println!("🧪 rfc4918_webdav_core"); + common::aerogramme_provider_daemon_dev(|_imap, _lmtp, http| { + // --- PROPFIND --- + // empty request body (assume "allprop") + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087").send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + let root_propstats = multistatus.responses.iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/" => Some(x), + _ => None, + }) + .expect("propstats for root must exist"); + + let root_success = root_propstats.iter().find(|p| p.status.0.as_u16() == 200).expect("some propstats for root must be 200"); + let display_name = root_success.prop.0.iter() + .find_map(|v| match v { dav::AnyProperty::Value(dav::Property::DisplayName(x)) => Some(x), _ => None } ) + .expect("root has a display name"); + let content_type = root_success.prop.0.iter() + .find_map(|v| match v { dav::AnyProperty::Value(dav::Property::GetContentType(x)) => Some(x), _ => None } ) + .expect("root has a content type"); + let resource_type = root_success.prop.0.iter() + .find_map(|v| match v { dav::AnyProperty::Value(dav::Property::ResourceType(x)) => Some(x), _ => None } ) + .expect("root has a resource type"); + + assert_eq!(display_name, "DAV Root"); + assert_eq!(content_type, "httpd/unix-directory"); + assert_eq!(resource_type, &[ dav::ResourceType::Collection ]); + + // propname + let propfind_req = r#"<?xml version="1.0" encoding="utf-8" ?><propfind xmlns="DAV:"><propname/></propfind>"#; + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087").body(propfind_req).send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + let root_propstats = multistatus.responses.iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/" => Some(x), + _ => None, + }) + .expect("propstats for root must exist"); + let root_success = root_propstats.iter().find(|p| p.status.0.as_u16() == 200).expect("some propstats for root must be 200"); + assert!(root_success.prop.0.iter().find(|p| matches!(p, dav::AnyProperty::Request(dav::PropertyRequest::DisplayName))).is_some()); + assert!(root_success.prop.0.iter().find(|p| matches!(p, dav::AnyProperty::Request(dav::PropertyRequest::ResourceType))).is_some()); + assert!(root_success.prop.0.iter().find(|p| matches!(p, dav::AnyProperty::Request(dav::PropertyRequest::GetContentType))).is_some()); + + // list of properties + let propfind_req = r#"<?xml version="1.0" encoding="utf-8" ?><propfind xmlns="DAV:"><prop><displayname/><getcontentlength/></prop></propfind>"#; + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087").body(propfind_req).send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + let root_propstats = multistatus.responses.iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/" => Some(x), + _ => None, + }) + .expect("propstats for root must exist"); + + let root_success = root_propstats.iter().find(|p| p.status.0.as_u16() == 200).expect("some propstats for root must be 200"); + let root_not_found = root_propstats.iter().find(|p| p.status.0.as_u16() == 404).expect("some propstats for root must be not found"); + + assert!(root_success.prop.0.iter().find(|p| matches!(p, dav::AnyProperty::Value(dav::Property::DisplayName(x)) if x == "DAV Root")).is_some()); + assert!(root_success.prop.0.iter().find(|p| matches!(p, dav::AnyProperty::Value(dav::Property::ResourceType(_)))).is_none()); + assert!(root_success.prop.0.iter().find(|p| matches!(p, dav::AnyProperty::Value(dav::Property::GetContentType(_)))).is_none()); + assert!(root_not_found.prop.0.iter().find(|p| matches!(p, dav::AnyProperty::Request(dav::PropertyRequest::GetContentLength))).is_some()); + + // -- HIERARCHY EXPLORATION WITH THE DEPTH: X HEADER FIELD -- + // depth 1 / -> /alice/ + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087").header("Depth", "1").send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + let _user_propstats = multistatus.responses.iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/alice/" => Some(x), + _ => None, + }) + .expect("user collection must exist"); + + // depth 1 /alice/ -> /alice/calendar/ + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/").header("Depth", "1").send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + let _user_calendars_propstats = multistatus.responses.iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/alice/calendar/" => Some(x), + _ => None, + }) + .expect("user collection must exist"); + + // depth 1 /alice/calendar/ -> /alice/calendar/Personal/ + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/").header("Depth", "1").send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + let _user_calendars_propstats = multistatus.responses.iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/alice/calendar/Personal/" => Some(x), + _ => None, + }) + .expect("Personal calendar must exist"); + + // depth 1 /alice/calendar/Personal/ -> empty for now... + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").header("Depth", "1").send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + assert_eq!(multistatus.responses.len(), 1); + + // --- PUT (add objets) --- + // first object + let resp = http.put("http://localhost:8087/alice/calendar/Personal/rfc2.ics").header("If-None-Match", "*").body(ICAL_RFC2).send()?; + let obj1_etag = resp.headers().get("etag").expect("etag must be set"); + assert_eq!(resp.status(), 201); + + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").header("Depth", "1").send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + assert_eq!(multistatus.responses.len(), 2); + + // second object + let resp = http.put("http://localhost:8087/alice/calendar/Personal/rfc3.ics").header("If-None-Match", "*").body(ICAL_RFC3).send()?; + assert_eq!(resp.status(), 201); + + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").header("Depth", "1").send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + assert_eq!(multistatus.responses.len(), 3); + + // can't create an event on an existing path + let resp = http.put("http://localhost:8087/alice/calendar/Personal/rfc2.ics").header("If-None-Match", "*").body(ICAL_RFC1).send()?; + assert_eq!(resp.status(), 412); + + // update first object by knowing its ETag + let resp = http.put("http://localhost:8087/alice/calendar/Personal/rfc2.ics").header("If-Match", obj1_etag).body(ICAL_RFC1).send()?; + assert_eq!(resp.status(), 201); + + // --- GET (fetch objects) --- + let body = http.get("http://localhost:8087/alice/calendar/Personal/rfc2.ics").send()?.text()?; + assert_eq!(body.as_bytes(), ICAL_RFC1); + + let body = http.get("http://localhost:8087/alice/calendar/Personal/rfc3.ics").send()?.text()?; + assert_eq!(body.as_bytes(), ICAL_RFC3); + + // --- DELETE (delete objects) --- + // delete 1st object + let resp = http.delete("http://localhost:8087/alice/calendar/Personal/rfc2.ics").send()?; + assert_eq!(resp.status(), 204); + + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").header("Depth", "1").send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + assert_eq!(multistatus.responses.len(), 2); + + // delete 2nd object + let resp = http.delete("http://localhost:8087/alice/calendar/Personal/rfc3.ics").send()?; + assert_eq!(resp.status(), 204); + + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").header("Depth", "1").send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + assert_eq!(multistatus.responses.len(), 1); + + Ok(()) + }) + .expect("test fully run"); +} + +fn rfc5397_webdav_principal() { + println!("🧪 rfc5397_webdav_principal"); + common::aerogramme_provider_daemon_dev(|_imap, _lmtp, http| { + // -- AUTODISCOVERY: FIND "PRINCIPAL" AS DEFINED IN WEBDAV ACL (~USER'S HOME) -- + let propfind_req = r#"<?xml version="1.0" encoding="utf-8" ?><propfind xmlns="DAV:"><prop><current-user-principal/></prop></propfind>"#; + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087").body(propfind_req).send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + let root_propstats = multistatus.responses.iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/" => Some(x), + _ => None, + }) + .expect("propstats for root must exist"); + + let root_success = root_propstats.iter().find(|p| p.status.0.as_u16() == 200).expect("current-user-principal must exist"); + let principal = root_success.prop.0.iter() + .find_map(|v| match v { + dav::AnyProperty::Value(dav::Property::Extension(realization::Property::Acl(acl::Property::CurrentUserPrincipal(acl::User::Authenticated(dav::Href(x)))))) => Some(x), + _ => None, + }) + .expect("request returned an authenticated principal"); + assert_eq!(principal, "/alice/"); + + Ok(()) + }) + .expect("test fully run") +} + +fn rfc4791_webdav_caldav() { + println!("🧪 rfc4791_webdav_caldav"); + common::aerogramme_provider_daemon_dev(|_imap, _lmtp, http| { + // --- INITIAL TEST SETUP --- + // Add entries + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc1.ics") + .header("If-None-Match", "*") + .body(ICAL_RFC1) + .send()?; + let obj1_etag = resp.headers().get("etag").expect("etag must be set"); + assert_eq!(resp.status(), 201); + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc2.ics") + .header("If-None-Match", "*") + .body(ICAL_RFC2) + .send()?; + let obj2_etag = resp.headers().get("etag").expect("etag must be set"); + assert_eq!(resp.status(), 201); + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc3.ics") + .header("If-None-Match", "*") + .body(ICAL_RFC3) + .send()?; + let obj3_etag = resp.headers().get("etag").expect("etag must be set"); + assert_eq!(resp.status(), 201); + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc4.ics") + .header("If-None-Match", "*") + .body(ICAL_RFC4) + .send()?; + let _obj4_etag = resp.headers().get("etag").expect("etag must be set"); + assert_eq!(resp.status(), 201); + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc5.ics") + .header("If-None-Match", "*") + .body(ICAL_RFC5) + .send()?; + let _obj5_etag = resp.headers().get("etag").expect("etag must be set"); + assert_eq!(resp.status(), 201); + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc6.ics") + .header("If-None-Match", "*") + .body(ICAL_RFC6) + .send()?; + let obj6_etag = resp.headers().get("etag").expect("etag must be set"); + assert_eq!(resp.status(), 201); + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc7.ics") + .header("If-None-Match", "*") + .body(ICAL_RFC7) + .send()?; + let obj7_etag = resp.headers().get("etag").expect("etag must be set"); + assert_eq!(resp.status(), 201); + + // A generic function to check a <calendar-data/> query result + let check_cal = + |multistatus: &dav::Multistatus<All>, + (ref_path, ref_etag, ref_ical): (&str, Option<&str>, Option<&[u8]>)| { + let obj_stats = multistatus + .responses + .iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) + if p.as_str() == ref_path => + { + Some(x) + } + _ => None, + }) + .expect("propstats must exist"); + let obj_success = obj_stats + .iter() + .find(|p| p.status.0.as_u16() == 200) + .expect("some propstats must be 200"); + let etag = obj_success.prop.0.iter().find_map(|p| match p { + dav::AnyProperty::Value(dav::Property::GetEtag(x)) => Some(x.as_str()), + _ => None, + }); + assert_eq!(etag, ref_etag); + let calendar_data = obj_success.prop.0.iter().find_map(|p| match p { + dav::AnyProperty::Value(dav::Property::Extension( + realization::Property::Cal(cal::Property::CalendarData(x)), + )) => Some(x.payload.as_bytes()), + _ => None, + }); + assert_eq!(calendar_data, ref_ical); + }; + + // --- AUTODISCOVERY --- + // Check calendar discovery from principal + let propfind_req = r#"<?xml version="1.0" encoding="utf-8" ?> + <D:propfind xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"> + <D:prop><C:calendar-home-set/></D:prop> + </D:propfind>"#; + + let body = http + .request( + reqwest::Method::from_bytes(b"PROPFIND")?, + "http://localhost:8087/alice/", + ) + .body(propfind_req) + .send()? + .text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + let principal_propstats = multistatus + .responses + .iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/alice/" => { + Some(x) + } + _ => None, + }) + .expect("propstats for root must exist"); + let principal_success = principal_propstats + .iter() + .find(|p| p.status.0.as_u16() == 200) + .expect("current-user-principal must exist"); + let calendar_home_set = principal_success + .prop + .0 + .iter() + .find_map(|v| match v { + dav::AnyProperty::Value(dav::Property::Extension(realization::Property::Cal( + cal::Property::CalendarHomeSet(dav::Href(x)), + ))) => Some(x), + _ => None, + }) + .expect("request returns a calendar home set"); + assert_eq!(calendar_home_set, "/alice/calendar/"); + + // Check calendar access support + let _resp = http + .request( + reqwest::Method::from_bytes(b"OPTIONS")?, + "http://localhost:8087/alice/calendar/", + ) + .send()?; + //@FIXME not yet supported. returns DAV: 1 ; expects DAV: 1 calendar-access + // Not used by any client I know, so not implementing it now. + + // --- REPORT calendar-multiget --- + let cal_query = r#"<?xml version="1.0" encoding="utf-8" ?> + <C:calendar-multiget xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"> + <D:prop> + <D:getetag/> + <C:calendar-data/> + </D:prop> + <D:href>/alice/calendar/Personal/rfc1.ics</D:href> + <D:href>/alice/calendar/Personal/rfc3.ics</D:href> + </C:calendar-multiget>"#; + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(cal_query) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 2); + [ + ("/alice/calendar/Personal/rfc1.ics", obj1_etag, ICAL_RFC1), + ("/alice/calendar/Personal/rfc3.ics", obj3_etag, ICAL_RFC3), + ] + .iter() + .for_each(|(ref_path, ref_etag, ref_ical)| { + check_cal( + &multistatus, + ( + ref_path, + Some(ref_etag.to_str().expect("etag header convertible to str")), + Some(ref_ical), + ), + ) + }); + + // --- REPORT calendar-query, only filtering --- + // 7.8.8. Example: Retrieval of Events Only + let cal_query = r#"<?xml version="1.0" encoding="utf-8" ?> + <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav"> + <D:prop xmlns:D="DAV:"> + <D:getetag/> + <C:calendar-data/> + </D:prop> + <C:filter> + <C:comp-filter name="VCALENDAR"> + <C:comp-filter name="VEVENT"/> + </C:comp-filter> + </C:filter> + </C:calendar-query>"#; + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(cal_query) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 4); + + [ + ("/alice/calendar/Personal/rfc1.ics", obj1_etag, ICAL_RFC1), + ("/alice/calendar/Personal/rfc2.ics", obj2_etag, ICAL_RFC2), + ("/alice/calendar/Personal/rfc3.ics", obj3_etag, ICAL_RFC3), + ("/alice/calendar/Personal/rfc7.ics", obj7_etag, ICAL_RFC7), + ] + .iter() + .for_each(|(ref_path, ref_etag, ref_ical)| { + check_cal( + &multistatus, + ( + ref_path, + Some(ref_etag.to_str().expect("etag header convertible to str")), + Some(ref_ical), + ), + ) + }); + + // 8.2.1.2. Synchronize by Time Range (here: July 2006) + let cal_query = r#"<?xml version="1.0" encoding="utf-8" ?> + <C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"> + <D:prop> + <D:getetag/> + </D:prop> + <C:filter> + <C:comp-filter name="VCALENDAR"> + <C:comp-filter name="VEVENT"> + <C:time-range start="20060701T000000Z" end="20060801T000000Z"/> + </C:comp-filter> + </C:comp-filter> + </C:filter> + </C:calendar-query>"#; + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(cal_query) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 1); + check_cal( + &multistatus, + ( + "/alice/calendar/Personal/rfc2.ics", + Some(obj2_etag.to_str().expect("etag header convertible to str")), + None, + ), + ); + + // 7.8.5. Example: Retrieval of To-Dos by Alarm Time Range + let cal_query = r#"<?xml version="1.0" encoding="utf-8" ?> + <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav"> + <D:prop xmlns:D="DAV:"> + <D:getetag/> + <C:calendar-data/> + </D:prop> + <C:filter> + <C:comp-filter name="VCALENDAR"> + <C:comp-filter name="VTODO"> + <C:comp-filter name="VALARM"> + <C:time-range start="20060201T000000Z" end="20060301T000000Z"/> + </C:comp-filter> + </C:comp-filter> + </C:comp-filter> + </C:filter> + </C:calendar-query>"#; + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(cal_query) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 1); + check_cal( + &multistatus, + ( + "/alice/calendar/Personal/rfc6.ics", + Some(obj6_etag.to_str().expect("etag header convertible to str")), + Some(ICAL_RFC6), + ), + ); + + // 7.8.6. Example: Retrieval of Event by UID + let cal_query = r#"<?xml version="1.0" encoding="utf-8" ?> + <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav"> + <D:prop xmlns:D="DAV:"> + <D:getetag/> + <C:calendar-data/> + </D:prop> + <C:filter> + <C:comp-filter name="VCALENDAR"> + <C:comp-filter name="VEVENT"> + <C:prop-filter name="UID"> + <C:text-match collation="i;octet">DC6C50A017428C5216A2F1CD@example.com</C:text-match> + </C:prop-filter> + </C:comp-filter> + </C:comp-filter> + </C:filter> + </C:calendar-query>"#; + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(cal_query) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 1); + check_cal( + &multistatus, + ( + "/alice/calendar/Personal/rfc3.ics", + Some(obj3_etag.to_str().expect("etag header convertible to str")), + Some(ICAL_RFC3), + ), + ); + + + // 7.8.7. Example: Retrieval of Events by PARTSTAT + let cal_query = r#"<?xml version="1.0" encoding="utf-8" ?> + <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav"> + <D:prop xmlns:D="DAV:"> + <D:getetag/> + <C:calendar-data/> + </D:prop> + <C:filter> + <C:comp-filter name="VCALENDAR"> + <C:comp-filter name="VEVENT"> + <C:prop-filter name="ATTENDEE"> + <C:text-match collation="i;ascii-casemap">mailto:lisa@example.com</C:text-match> + <C:param-filter name="PARTSTAT"> + <C:text-match collation="i;ascii-casemap">NEEDS-ACTION</C:text-match> + </C:param-filter> + </C:prop-filter> + </C:comp-filter> + </C:comp-filter> + </C:filter> + </C:calendar-query>"#; + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(cal_query) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 1); + check_cal( + &multistatus, + ( + "/alice/calendar/Personal/rfc7.ics", + Some(obj7_etag.to_str().expect("etag header convertible to str")), + Some(ICAL_RFC7), + ), + ); + + // 7.8.9. Example: Retrieval of All Pending To-Dos + let cal_query = r#"<?xml version="1.0" encoding="utf-8" ?> + <C:calendar-query xmlns:C="urn:ietf:params:xml:ns:caldav"> + <D:prop xmlns:D="DAV:"> + <D:getetag/> + <C:calendar-data/> + </D:prop> + <C:filter> + <C:comp-filter name="VCALENDAR"> + <C:comp-filter name="VTODO"> + <C:prop-filter name="COMPLETED"> + <C:is-not-defined/> + </C:prop-filter> + <C:prop-filter name="STATUS"> + <C:text-match negate-condition="yes">CANCELLED</C:text-match> + </C:prop-filter> + </C:comp-filter> + </C:comp-filter> + </C:filter> + </C:calendar-query>"#; + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(cal_query) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 1); + check_cal( + &multistatus, + ( + "/alice/calendar/Personal/rfc6.ics", + Some(obj6_etag.to_str().expect("etag header convertible to str")), + Some(ICAL_RFC6), + ), + ); + + // --- REPORT calendar-query, with calendar-data tx --- + let cal_query = r#"<?xml version="1.0" encoding="utf-8" ?> + <C:calendar-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav"> + <D:prop> + <D:getetag/> + <C:calendar-data> + <C:comp name="VCALENDAR"> + <C:prop name="VERSION"/> + <C:comp name="VEVENT"> + <C:prop name="UID"/> + <C:prop name="DTSTART"/> + <C:prop name="DTEND"/> + <C:prop name="DURATION"/> + <C:prop name="RRULE"/> + <C:prop name="RDATE"/> + <C:prop name="EXRULE"/> + <C:prop name="EXDATE"/> + <C:prop name="RECURRENCE-ID"/> + </C:comp> + <C:comp name="VTIMEZONE"/> + </C:comp> + </C:calendar-data> + </D:prop> + <C:filter> + <C:comp-filter name="VCALENDAR"> + <C:comp-filter name="VEVENT"> + <C:time-range start="20060104T000000Z" end="20060105T000000Z"/> + </C:comp-filter> + </C:comp-filter> + </C:filter> + </C:calendar-query>"#; + + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(cal_query) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 1); + check_cal( + &multistatus, + ( + "/alice/calendar/Personal/rfc3.ics", + Some(obj3_etag.to_str().expect("etag header convertible to str")), + Some(ICAL_RFC3_STRIPPED), + ), + ); + + Ok(()) + }) + .expect("test fully run") +} + +fn rfc6578_webdav_sync() { + println!("🧪 rfc6578_webdav_sync"); + common::aerogramme_provider_daemon_dev(|_imap, _lmtp, http| { + // -- PROPFIND -- + // propname must return sync-token & supported-report-set (from webdav versioning) + let propfind_req = r#"<?xml version="1.0" encoding="utf-8" ?><propfind xmlns="DAV:"><propname/></propfind>"#; + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").body(propfind_req).send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + let root_propstats = multistatus.responses.iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/alice/calendar/Personal/" => Some(x), + _ => None, + }) + .expect("propstats for target must exist"); + let root_success = root_propstats.iter().find(|p| p.status.0.as_u16() == 200).expect("some propstats for root must be 200"); + assert!(root_success.prop.0.iter().find(|p| matches!(p, dav::AnyProperty::Request(dav::PropertyRequest::Extension( + realization::PropertyRequest::Sync(sync::PropertyRequest::SyncToken) + )))).is_some()); + assert!(root_success.prop.0.iter().find(|p| matches!(p, dav::AnyProperty::Request(dav::PropertyRequest::Extension( + realization::PropertyRequest::Vers(vers::PropertyRequest::SupportedReportSet) + )))).is_some()); + + // synctoken and supported report set must contains a meaningful value when queried + let propfind_req = r#"<?xml version="1.0" encoding="utf-8" ?><propfind xmlns="DAV:"><prop><sync-token/><supported-report-set/></prop></propfind>"#; + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").body(propfind_req).send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + let root_propstats = multistatus.responses.iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/alice/calendar/Personal/" => Some(x), + _ => None, + }) + .expect("propstats for target must exist"); + + let root_success = root_propstats.iter().find(|p| p.status.0.as_u16() == 200).expect("some propstats for root must be 200"); + + let init_sync_token = root_success.prop.0.iter().find_map(|p| match p { + dav::AnyProperty::Value(dav::Property::Extension(realization::Property::Sync(sync::Property::SyncToken(st)))) => Some(st), + _ => None, + }).expect("sync_token exists"); + + let supported = root_success.prop.0.iter().find_map(|p| match p { + dav::AnyProperty::Value(dav::Property::Extension(realization::Property::Vers(vers::Property::SupportedReportSet(s)))) => Some(s), + _ => None + }).expect("supported report set exists"); + assert_eq!(&supported[..], &[ + vers::SupportedReport(vers::ReportName::Extension(realization::ReportTypeName::Cal(cal::ReportTypeName::Multiget))), + vers::SupportedReport(vers::ReportName::Extension(realization::ReportTypeName::Cal(cal::ReportTypeName::Query))), + vers::SupportedReport(vers::ReportName::Extension(realization::ReportTypeName::Sync(sync::ReportTypeName::SyncCollection))), + ]); + + + // synctoken must change if we add a file + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc1.ics") + .header("If-None-Match", "*") + .body(ICAL_RFC1) + .send()?; + assert_eq!(resp.status(), 201); + + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").body(propfind_req).send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + + let root_propstats = multistatus.responses.iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/alice/calendar/Personal/" => Some(x), + _ => None, + }) + .expect("propstats for target must exist"); + let root_success = root_propstats.iter().find(|p| p.status.0.as_u16() == 200).expect("some propstats for root must be 200"); + let rfc1_sync_token = root_success.prop.0.iter().find_map(|p| match p { + dav::AnyProperty::Value(dav::Property::Extension(realization::Property::Sync(sync::Property::SyncToken(st)))) => Some(st), + _ => None, + }).expect("sync_token exists"); + assert!(init_sync_token != rfc1_sync_token); + + + // synctoken must change if we delete a file + let resp = http.delete("http://localhost:8087/alice/calendar/Personal/rfc1.ics").send()?; + assert_eq!(resp.status(), 204); + + let body = http.request(reqwest::Method::from_bytes(b"PROPFIND")?, "http://localhost:8087/alice/calendar/Personal/").body(propfind_req).send()?.text()?; + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&body); + + let root_propstats = multistatus.responses.iter() + .find_map(|v| match &v.status_or_propstat { + dav::StatusOrPropstat::PropStat(dav::Href(p), x) if p.as_str() == "/alice/calendar/Personal/" => Some(x), + _ => None, + }) + .expect("propstats for target must exist"); + let root_success = root_propstats.iter().find(|p| p.status.0.as_u16() == 200).expect("some propstats for root must be 200"); + let del_sync_token = root_success.prop.0.iter().find_map(|p| match p { + dav::AnyProperty::Value(dav::Property::Extension(realization::Property::Sync(sync::Property::SyncToken(st)))) => Some(st), + _ => None, + }).expect("sync_token exists"); + assert!(init_sync_token != del_sync_token); + assert!(rfc1_sync_token != del_sync_token); + + // -- TEST SYNC CUSTOM REPORT: SYNC-COLLECTION -- + // 3.8. Example: Initial DAV:sync-collection Report + // Part 1: check the empty case + let sync_query = r#"<?xml version="1.0" encoding="utf-8" ?> + <D:sync-collection xmlns:D="DAV:"> + <D:sync-token/> + <D:sync-level>1</D:sync-level> + <D:prop> + <D:getetag/> + </D:prop> + </D:sync-collection> + "#; + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(sync_query) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 0); + let empty_token = match &multistatus.extension { + Some(realization::Multistatus::Sync(sync::Multistatus { sync_token: sync::SyncToken(x) } )) => x, + _ => anyhow::bail!("wrong content"), + }; + + // Part 2: check with one file + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc1.ics") + .header("If-None-Match", "*") + .body(ICAL_RFC1) + .send()?; + assert_eq!(resp.status(), 201); + + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(sync_query) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 1); + let initial_one_file_token = match &multistatus.extension { + Some(realization::Multistatus::Sync(sync::Multistatus { sync_token: sync::SyncToken(x) } )) => x, + _ => anyhow::bail!("wrong content"), + }; + assert!(empty_token != initial_one_file_token); + + // 3.9. Example: DAV:sync-collection Report with Token + // Part 1: nothing changed, response must be empty + let sync_query = |token: &str| vers::Report::<realization::All>::Extension(realization::ReportType::Sync(sync::SyncCollection { + sync_token: sync::SyncTokenRequest::IncrementalSync(token.into()), + sync_level: sync::SyncLevel::One, + limit: None, + prop: dav::PropName(vec![ + dav::PropertyRequest::GetEtag, + ]), + })); + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(dav_serialize(&sync_query(initial_one_file_token))) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 0); + let no_change = match &multistatus.extension { + Some(realization::Multistatus::Sync(sync::Multistatus { sync_token: sync::SyncToken(x) } )) => x, + _ => anyhow::bail!("wrong content"), + }; + assert_eq!(initial_one_file_token, no_change); + + // Part 2: add a new node (rfc2) + remove a node (rfc1) + // add rfc2 + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc2.ics") + .header("If-None-Match", "*") + .body(ICAL_RFC2) + .send()?; + assert_eq!(resp.status(), 201); + + // delete rfc1 + let resp = http.delete("http://localhost:8087/alice/calendar/Personal/rfc1.ics").send()?; + assert_eq!(resp.status(), 204); + + // call REPORT <sync-collection> + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(dav_serialize(&sync_query(initial_one_file_token))) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 2); + let token_addrm = match &multistatus.extension { + Some(realization::Multistatus::Sync(sync::Multistatus { sync_token: sync::SyncToken(x) } )) => x, + _ => anyhow::bail!("wrong content"), + }; + assert!(initial_one_file_token != token_addrm); + + // Part 3: remove a node (rfc2) and add it again with new content + // delete rfc2 + let resp = http.delete("http://localhost:8087/alice/calendar/Personal/rfc2.ics").send()?; + assert_eq!(resp.status(), 204); + + // add rfc2 with ICAL_RFC3 content + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc2.ics") + .header("If-None-Match", "*") + .body(ICAL_RFC3) + .send()?; + let rfc2_etag = resp.headers().get("etag").expect("etag must be set"); + assert_eq!(resp.status(), 201); + + // call REPORT <sync-collection> + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(dav_serialize(&sync_query(token_addrm))) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 1); + let token_addrm_same = match &multistatus.extension { + Some(realization::Multistatus::Sync(sync::Multistatus { sync_token: sync::SyncToken(x) } )) => x, + _ => anyhow::bail!("wrong content"), + }; + assert!(token_addrm_same != token_addrm); + + // Part 4: overwrite an event (rfc1) with new content + let resp = http + .put("http://localhost:8087/alice/calendar/Personal/rfc1.ics") + .header("If-Match", rfc2_etag) + .body(ICAL_RFC4) + .send()?; + assert_eq!(resp.status(), 201); + + // call REPORT <sync-collection> + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(dav_serialize(&sync_query(token_addrm_same))) + .send()?; + assert_eq!(resp.status(), 207); + let multistatus = dav_deserialize::<dav::Multistatus<All>>(&resp.text()?); + assert_eq!(multistatus.responses.len(), 1); + let token_addrm_same = match &multistatus.extension { + Some(realization::Multistatus::Sync(sync::Multistatus { sync_token: sync::SyncToken(x) } )) => x, + _ => anyhow::bail!("wrong content"), + }; + assert!(token_addrm_same != token_addrm); + + // Unknown token must return 410 GONE. + // Token can be forgotten as we garbage collect the DAG. + let resp = http + .request( + reqwest::Method::from_bytes(b"REPORT")?, + "http://localhost:8087/alice/calendar/Personal/", + ) + .body(dav_serialize(&sync_query("https://aerogramme.0/sync/000000000000000000000000000000000000000000000000"))) + .send()?; + assert_eq!(resp.status(), 410); + + Ok(()) + }) + .expect("test fully run") +} diff --git a/aerogramme/tests/common/constants.rs b/aerogramme/tests/common/constants.rs new file mode 100644 index 0000000..16daec6 --- /dev/null +++ b/aerogramme/tests/common/constants.rs @@ -0,0 +1,243 @@ +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 <bob@example.tld>\r +To: Alice Malice <alice@example.tld>\r +CC: =?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>\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: <NTAxNzA2AC47634Y366BAMTY4ODc5MzQyODY0ODY5@www.grrrndzero.org>\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 +<div style=\"text-align: center;\"><strong>GZ</strong><br />\r +OoOoO<br />\r +oOoOoOoOo<br />\r +oOoOoOoOoOoOoOoOo<br />\r +oOoOoOoOoOoOoOoOoOoOoOo<br />\r +oOoOoOoOoOoOoOoOoOoOoOoOoOoOo<br />\r +OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO<br />\r +</div>\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 +"; + +pub static ICAL_RFC1: &[u8] = b"BEGIN:VCALENDAR +PRODID:-//Example Corp.//CalDAV Client//EN +VERSION:2.0 +BEGIN:VEVENT +UID:1@example.com +SUMMARY:One-off Meeting +DTSTAMP:20041210T183904Z +DTSTART:20041207T120000Z +DTEND:20041207T130000Z +END:VEVENT +BEGIN:VEVENT +UID:2@example.com +SUMMARY:Weekly Meeting +DTSTAMP:20041210T183838Z +DTSTART:20041206T120000Z +DTEND:20041206T130000Z +RRULE:FREQ=WEEKLY +END:VEVENT +BEGIN:VEVENT +UID:2@example.com +SUMMARY:Weekly Meeting +RECURRENCE-ID:20041213T120000Z +DTSTAMP:20041210T183838Z +DTSTART:20041213T130000Z +DTEND:20041213T140000Z +END:VEVENT +END:VCALENDAR +"; + +pub static ICAL_RFC2: &[u8] = b"BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Example Corp.//CalDAV Client//EN +BEGIN:VEVENT +UID:20010712T182145Z-123401@example.com +DTSTAMP:20060712T182145Z +DTSTART:20060714T170000Z +DTEND:20060715T040000Z +SUMMARY:Bastille Day Party +END:VEVENT +END:VCALENDAR +"; + +pub static ICAL_RFC3: &[u8] = b"BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Example Corp.//CalDAV Client//EN +BEGIN:VTIMEZONE +LAST-MODIFIED:20040110T032845Z +TZID:US/Eastern +BEGIN:DAYLIGHT +DTSTART:20000404T020000 +RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4 +TZNAME:EDT +TZOFFSETFROM:-0500 +TZOFFSETTO:-0400 +END:DAYLIGHT +BEGIN:STANDARD +DTSTART:20001026T020000 +RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10 +TZNAME:EST +TZOFFSETFROM:-0400 +TZOFFSETTO:-0500 +END:STANDARD +END:VTIMEZONE +BEGIN:VEVENT +DTSTART;TZID=US/Eastern:20060104T100000 +DURATION:PT1H +SUMMARY:Event #3 +UID:DC6C50A017428C5216A2F1CD@example.com +END:VEVENT +END:VCALENDAR +"; + +pub static ICAL_RFC3_STRIPPED: &[u8] = b"BEGIN:VCALENDAR\r +VERSION:2.0\r +BEGIN:VTIMEZONE\r +LAST-MODIFIED:20040110T032845Z\r +TZID:US/Eastern\r +BEGIN:DAYLIGHT\r +DTSTART:20000404T020000\r +RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4\r +TZNAME:EDT\r +TZOFFSETFROM:-0500\r +TZOFFSETTO:-0400\r +END:DAYLIGHT\r +BEGIN:STANDARD\r +DTSTART:20001026T020000\r +RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10\r +TZNAME:EST\r +TZOFFSETFROM:-0400\r +TZOFFSETTO:-0500\r +END:STANDARD\r +END:VTIMEZONE\r +BEGIN:VEVENT\r +DTSTART;TZID=US/Eastern:20060104T100000\r +DURATION:PT1H\r +UID:DC6C50A017428C5216A2F1CD@example.com\r +END:VEVENT\r +END:VCALENDAR\r +"; + +pub static ICAL_RFC4: &[u8] = br#"BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Example Corp.//CalDAV Client//EN +BEGIN:VFREEBUSY +ORGANIZER;CN="Bernard Desruisseaux":mailto:bernard@example.com +UID:76ef34-54a3d2@example.com +DTSTAMP:20050530T123421Z +DTSTART:20060101T000000Z +DTEND:20060108T000000Z +FREEBUSY:20050531T230000Z/20050601T010000Z +FREEBUSY;FBTYPE=BUSY-TENTATIVE:20060102T100000Z/20060102T120000Z +FREEBUSY:20060103T100000Z/20060103T120000Z +FREEBUSY:20060104T100000Z/20060104T120000Z +FREEBUSY;FBTYPE=BUSY-UNAVAILABLE:20060105T100000Z/20060105T120000Z +FREEBUSY:20060106T100000Z/20060106T120000Z +END:VFREEBUSY +END:VCALENDAR +"#; + +pub static ICAL_RFC5: &[u8] = br#"BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Example Corp.//CalDAV Client//EN +BEGIN:VTODO +DTSTAMP:20060205T235600Z +DUE;VALUE=DATE:20060101 +LAST-MODIFIED:20060205T235308Z +SEQUENCE:1 +STATUS:CANCELLED +SUMMARY:Task #4 +UID:E10BA47467C5C69BB74E8725@example.com +END:VTODO +END:VCALENDAR +"#; + +pub static ICAL_RFC6: &[u8] = br#"BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Example Corp.//CalDAV Client//EN +BEGIN:VTODO +DTSTART:20060205T235335Z +DUE;VALUE=DATE:20060104 +STATUS:NEEDS-ACTION +SUMMARY:Task #1 +UID:DDDEEB7915FA61233B861457@example.com +BEGIN:VALARM +ACTION:AUDIO +TRIGGER;RELATED=START:-PT10M +END:VALARM +END:VTODO +END:VCALENDAR +"#; + +pub static ICAL_RFC7: &[u8] = br#"BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//Example Corp.//CalDAV Client//EN +BEGIN:VTIMEZONE +LAST-MODIFIED:20040110T032845Z +TZID:US/Eastern +BEGIN:DAYLIGHT +DTSTART:20000404T020000 +RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4 +TZNAME:EDT +TZOFFSETFROM:-0500 +TZOFFSETTO:-0400 +END:DAYLIGHT +BEGIN:STANDARD +DTSTART:20001026T020000 +RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10 +TZNAME:EST +TZOFFSETFROM:-0400 +TZOFFSETTO:-0500 +END:STANDARD +END:VTIMEZONE +BEGIN:VEVENT +ATTENDEE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:cyrus@example.com +ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:lisa@example.com +DTSTAMP:20090206T001220Z +DTSTART;TZID=US/Eastern:20090104T100000 +DURATION:PT1H +LAST-MODIFIED:20090206T001330Z +ORGANIZER:mailto:cyrus@example.com +SEQUENCE:1 +STATUS:TENTATIVE +SUMMARY:Event #3 +UID:DC6C50A017428C5216A2F1CA@example.com +X-ABC-GUID:E1CX5Dr-0007ym-Hz@example.com +END:VEVENT +END:VCALENDAR +"#; diff --git a/aerogramme/tests/common/fragments.rs b/aerogramme/tests/common/fragments.rs new file mode 100644 index 0000000..606af2b --- /dev/null +++ b/aerogramme/tests/common/fragments.rs @@ -0,0 +1,570 @@ +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. 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]; + + 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, + LiteralPlus, + Idle, + UidPlus, + ListStatus, +} + +pub enum Enable { + Utf8Accept, + CondStore, + All, +} + +pub enum Mailbox { + Inbox, + Archive, + Drafts, +} + +pub enum Flag { + Deleted, + Important, +} + +pub enum Email { + Basic, + Multipart, +} + +pub enum Selection { + FirstId, + SecondId, + All, +} + +pub enum SelectMod { + None, + Condstore, +} + +pub enum StoreAction { + AddFlags, + DelFlags, + SetFlags, + AddFlagsSilent, + DelFlagsSilent, + SetFlagsSilent, +} + +pub enum StoreMod { + None, + UnchangedSince(u64), +} + +pub enum FetchKind { + Rfc822, + Rfc822Size, +} + +pub enum FetchMod { + None, + ChangedSince(u64), +} + +pub enum SearchKind<'a> { + Text(&'a str), + ModSeq(u64), +} + +pub enum StatusKind { + UidNext, + HighestModSeq, +} + +pub enum MbxSelect { + All, +} + +pub enum ListReturn { + None, + StatusMessagesUnseen, +} + +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"), + Extension::LiteralPlus => Some("LITERAL+"), + Extension::Idle => Some("IDLE"), + Extension::UidPlus => Some("UIDPLUS"), + Extension::ListStatus => Some("LIST-STATUS"), + }; + + 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")); + 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 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]; + + let mbx_str = match mbx { + Mailbox::Inbox => "INBOX", + Mailbox::Archive => "ArchiveCustom", + Mailbox::Drafts => "DraftsCustom", + }; + + 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 list(imap: &mut TcpStream, select: MbxSelect, mod_return: ListReturn) -> Result<String> { + let mut buffer: [u8; 6000] = [0; 6000]; + + let select_str = match select { + MbxSelect::All => "%", + }; + + let mod_return_str = match mod_return { + ListReturn::None => "", + ListReturn::StatusMessagesUnseen => " RETURN (STATUS (MESSAGES UNSEEN))", + }; + + imap.write(format!("19 LIST \"\" \"{}\"{}\r\n", select_str, mod_return_str).as_bytes())?; + + let read = read_lines(imap, &mut buffer, Some(&b"19 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + Ok(srv_msg.to_string()) +} + +pub fn select(imap: &mut TcpStream, mbx: Mailbox, modifier: SelectMod) -> Result<String> { + let mut buffer: [u8; 6000] = [0; 6000]; + + let mbx_str = match mbx { + Mailbox::Inbox => "INBOX", + Mailbox::Archive => "ArchiveCustom", + Mailbox::Drafts => "DraftsCustom", + }; + + let mod_str = match modifier { + SelectMod::Condstore => " (CONDSTORE)", + SelectMod::None => "", + }; + + imap.write(format!("20 select {}{}\r\n", mbx_str, mod_str).as_bytes())?; + + let read = read_lines(imap, &mut buffer, Some(&b"20 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + + Ok(srv_msg.to_string()) +} + +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(imap: &mut TcpStream, mbx: Mailbox, sk: StatusKind) -> Result<String> { + let mbx_str = match mbx { + Mailbox::Inbox => "INBOX", + Mailbox::Archive => "ArchiveCustom", + Mailbox::Drafts => "DraftsCustom", + }; + let sk_str = match sk { + StatusKind::UidNext => "(UIDNEXT)", + StatusKind::HighestModSeq => "(HIGHESTMODSEQ)", + }; + imap.write(format!("25 STATUS {} {}\r\n", mbx_str, sk_str).as_bytes())?; + let mut buffer: [u8; 6000] = [0; 6000]; + let read = read_lines(imap, &mut buffer, Some(&b"25 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + + Ok(srv_msg.to_string()) +} + +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:<bob@example.tld>\r\n"[..])?; + let _read = read_lines(lmtp, &mut buffer, Some(&b"250 2.0.0"[..]))?; + + lmtp.write(&b"RCPT TO:<alice@example.tld>\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, must_exists: u32) -> 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)?; + + for line in srv_msg.lines() { + if line.contains("EXISTS") { + let got = read_first_u32(line)?; + if got == must_exists { + // Done + return Ok(()); + } + } + } + + if max_retry <= 0 { + // Failed + bail!("no more retry"); + } + + thread::sleep(SMALL_DELAY); + } +} + +pub fn fetch( + imap: &mut TcpStream, + selection: Selection, + kind: FetchKind, + modifier: FetchMod, +) -> Result<String> { + let mut buffer: [u8; 65535] = [0; 65535]; + + let sel_str = match selection { + Selection::FirstId => "1", + Selection::SecondId => "2", + Selection::All => "1:*", + }; + + let kind_str = match kind { + FetchKind::Rfc822 => "RFC822", + FetchKind::Rfc822Size => "RFC822.SIZE", + }; + + let mod_str = match modifier { + FetchMod::None => "".into(), + FetchMod::ChangedSince(val) => format!(" (CHANGEDSINCE {})", val), + }; + + imap.write(format!("40 fetch {} {}{}\r\n", sel_str, kind_str, mod_str).as_bytes())?; + + let read = read_lines(imap, &mut buffer, Some(&b"40 OK FETCH"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + + Ok(srv_msg.to_string()) +} + +pub fn copy(imap: &mut TcpStream, selection: Selection, to: Mailbox) -> Result<String> { + let mut buffer: [u8; 65535] = [0; 65535]; + assert!(matches!(selection, Selection::FirstId)); + assert!(matches!(to, Mailbox::Archive)); + + imap.write(&b"45 copy 1 ArchiveCustom\r\n"[..])?; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(&read[..5], &b"45 OK"[..]); + let srv_msg = std::str::from_utf8(read)?; + + Ok(srv_msg.to_string()) +} + +pub fn append(imap: &mut TcpStream, content: Email) -> Result<String> { + 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, Some(&b"47 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + + Ok(srv_msg.to_string()) +} + +pub fn search(imap: &mut TcpStream, sk: SearchKind) -> Result<String> { + let sk_str = match sk { + SearchKind::Text(x) => format!("TEXT \"{}\"", x), + SearchKind::ModSeq(x) => format!("MODSEQ {}", x), + }; + imap.write(format!("55 SEARCH {}\r\n", sk_str).as_bytes())?; + let mut buffer: [u8; 1500] = [0; 1500]; + let read = read_lines(imap, &mut buffer, Some(&b"55 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + Ok(srv_msg.to_string()) +} + +pub fn store( + imap: &mut TcpStream, + sel: Selection, + flag: Flag, + action: StoreAction, + modifier: StoreMod, +) -> Result<String> { + let mut buffer: [u8; 6000] = [0; 6000]; + + let seq = match sel { + Selection::FirstId => "1", + Selection::SecondId => "2", + Selection::All => "1:*", + }; + + let modif = match modifier { + StoreMod::None => "".into(), + StoreMod::UnchangedSince(val) => format!(" (UNCHANGEDSINCE {})", val), + }; + + let flags_str = match flag { + Flag::Deleted => "(\\Deleted)", + Flag::Important => "(\\Important)", + }; + + let action_str = match action { + StoreAction::AddFlags => "+FLAGS", + StoreAction::DelFlags => "-FLAGS", + StoreAction::SetFlags => "FLAGS", + StoreAction::AddFlagsSilent => "+FLAGS.SILENT", + StoreAction::DelFlagsSilent => "-FLAGS.SILENT", + StoreAction::SetFlagsSilent => "FLAGS.SILENT", + }; + + imap.write(format!("57 STORE {}{} {} {}\r\n", seq, modif, action_str, flags_str).as_bytes())?; + let read = read_lines(imap, &mut buffer, Some(&b"57 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + Ok(srv_msg.to_string()) +} + +pub fn expunge(imap: &mut TcpStream) -> Result<()> { + 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 uid_expunge(imap: &mut TcpStream, sel: Selection) -> Result<String> { + use Selection::*; + let mut buffer: [u8; 6000] = [0; 6000]; + let selstr = match sel { + FirstId => "1", + SecondId => "2", + All => "1:*", + }; + imap.write(format!("61 UID EXPUNGE {}\r\n", selstr).as_bytes())?; + let read = read_lines(imap, &mut buffer, Some(&b"61 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + Ok(srv_msg.to_string()) +} + +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 ArchiveCustom DraftsCustom\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(" ArchiveCustom\r\n")); + assert!(srv_msg.contains(" INBOX\r\n")); + assert!(srv_msg.contains(" DraftsCustom\r\n")); + + Ok(()) +} + +pub fn delete_mailbox(imap: &mut TcpStream, mbx: Mailbox) -> Result<()> { + let mbx_str = match mbx { + Mailbox::Inbox => "INBOX", + Mailbox::Archive => "ArchiveCustom", + Mailbox::Drafts => "DraftsCustom", + }; + 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<String> { + let mut buffer: [u8; 1500] = [0; 1500]; + assert!(matches!(to, Mailbox::Archive)); + assert!(matches!(selection, Selection::FirstId)); + + imap.write(&b"35 move 1 ArchiveCustom\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(srv_msg.to_string()) +} + +pub fn enable(imap: &mut TcpStream, ask: Enable, done: Option<Enable>) -> 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 start_idle(imap: &mut TcpStream) -> Result<()> { + let mut buffer: [u8; 1500] = [0; 1500]; + imap.write(&b"98 IDLE\r\n"[..])?; + let read = read_lines(imap, &mut buffer, None)?; + assert_eq!(read[0], b'+'); + Ok(()) +} + +pub fn stop_idle(imap: &mut TcpStream) -> Result<String> { + let mut buffer: [u8; 16536] = [0; 16536]; + imap.write(&b"DONE\r\n"[..])?; + let read = read_lines(imap, &mut buffer, Some(&b"98 OK"[..]))?; + let srv_msg = std::str::from_utf8(read)?; + Ok(srv_msg.to_string()) +} + +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/aerogramme/tests/common/mod.rs b/aerogramme/tests/common/mod.rs new file mode 100644 index 0000000..bc65305 --- /dev/null +++ b/aerogramme/tests/common/mod.rs @@ -0,0 +1,136 @@ +#![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 reqwest::blocking::Client; +use reqwest::header; + +use constants::SMALL_DELAY; + +pub fn aerogramme_provider_daemon_dev( + mut fx: impl FnMut(&mut TcpStream, &mut TcpStream, &mut Client) -> 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")?; + + let mut headers = header::HeaderMap::new(); + headers.insert( + header::AUTHORIZATION, + header::HeaderValue::from_static("Basic YWxpY2U6aHVudGVyMg=="), + ); + let mut http_client = Client::builder().default_headers(headers).build()?; + + println!("-- ready to test features --"); + let result = fx(&mut imap_socket, &mut lmtp_socket, &mut http_client); + 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 && nbytes >= 2 && &buffer[nbytes - 2..nbytes] == &b"\r\n"[..] { + break; + } + } + println!("read: {}", std::str::from_utf8(&buffer[..nbytes])?); + Ok(&buffer[..nbytes]) +} + +pub fn read_first_u32(inp: &str) -> Result<u32> { + Ok(inp + .chars() + .skip_while(|c| !c.is_digit(10)) + .take_while(|c| c.is_digit(10)) + .collect::<String>() + .parse::<u32>()?) +} + +use aero_dav::xml::{Node, Reader, Writer}; +use tokio::io::AsyncWriteExt; +pub fn dav_deserialize<T: Node<T>>(src: &str) -> T { + futures::executor::block_on(async { + let mut rdr = Reader::new(quick_xml::NsReader::from_reader(src.as_bytes())) + .await + .expect("build reader"); + rdr.find().await.expect("parse XML") + }) +} +pub fn dav_serialize<T: Node<T>>(src: &T) -> String { + futures::executor::block_on(async { + let mut buffer = Vec::new(); + let mut tokio_buffer = tokio::io::BufWriter::new(&mut buffer); + let q = quick_xml::writer::Writer::new_with_indent(&mut tokio_buffer, b' ', 4); + let ns_to_apply = vec![ + ("xmlns:D".into(), "DAV:".into()), + ("xmlns:C".into(), "urn:ietf:params:xml:ns:caldav".into()), + ]; + let mut writer = Writer { q, ns_to_apply }; + + src.qwrite(&mut writer).await.expect("xml serialization"); + tokio_buffer.flush().await.expect("tokio buffer flush"); + std::str::from_utf8(buffer.as_slice()).unwrap().into() + }) +} |