From 18d8ef9ffc236e31866f61320d206045a0e80604 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sat, 8 Aug 2020 00:00:00 +0300 Subject: [PATCH 1/8] dehtml: handle empty tags --- src/dehtml.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/dehtml.rs b/src/dehtml.rs index 71864b9ec..9e2f6fb16 100644 --- a/src/dehtml.rs +++ b/src/dehtml.rs @@ -46,6 +46,12 @@ pub fn dehtml(buf: &str) -> String { Ok(quick_xml::events::Event::End(ref e)) => dehtml_endtag_cb(e, &mut dehtml), Ok(quick_xml::events::Event::Text(ref e)) => dehtml_text_cb(e, &mut dehtml), Ok(quick_xml::events::Event::CData(ref e)) => dehtml_cdata_cb(e, &mut dehtml), + Ok(quick_xml::events::Event::Empty(ref e)) => { + // Handle empty tags as a start tag immediately followed by end tag. + // For example, `

` is treated as `

`. + dehtml_starttag_cb(e, &mut dehtml, &reader); + dehtml_endtag_cb(&BytesEnd::borrowed(e.name()), &mut dehtml); + } Err(e) => { eprintln!( "Parse html error: Error at position {}: {:?}", @@ -182,6 +188,10 @@ mod tests { ("& bar", "& bar"), // Note missing ' ("", + "[](https://get.delta.chat/)", + ), ("", ""), ]; for (input, output) in cases { @@ -191,10 +201,10 @@ mod tests { #[test] fn test_dehtml_parse_br() { - let html = "\r\r\nline1
\r\n\r\n\r\rline2\n\r"; + let html = "\r\r\nline1
\r\n\r\n\r\rline2
line3\n\r"; let plain = dehtml(html); - assert_eq!(plain, "line1\n\r\r\rline2"); + assert_eq!(plain, "line1\n\r\r\rline2\nline3"); } #[test] From 8faf397af25d4f194513a11367af77d1ba9bc857 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sat, 8 Aug 2020 00:00:00 +0300 Subject: [PATCH 2/8] Add regression test for IMAP message deletion Test times out while trying to delete messages. Message deletion jobs don't complete in time because IMAP response parsing is broken in the Rust core. --- python/tests/test_account.py | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/python/tests/test_account.py b/python/tests/test_account.py index b340caa2d..a66b77311 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -1652,6 +1652,45 @@ class TestOnlineAccount: assert "Ephemeral timer: " not in system_message2.get_message_info() assert chat1.get_ephemeral_timer() == 0 + @pytest.mark.xfail + def test_delete_multiple_messages(self, acfactory, lp): + ac1, ac2 = acfactory.get_two_online_accounts() + chat12 = acfactory.get_accepted_chat(ac1, ac2) + + lp.sec("ac1: sending seven messages") + texts = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh"] + for text in texts: + chat12.send_text(text) + + lp.sec("ac2: waiting for all messages on the other side") + to_delete = [] + for text in texts: + msg = ac2._evtracker.wait_next_incoming_message() + assert msg.text == text + if text != "third": + to_delete.append(msg) + + lp.sec("ac2: deleting all messages except third") + assert len(to_delete) == len(texts) - 1 + ac2.delete_messages(to_delete) + for msg in to_delete: + ac2._evtracker.get_matching("DC_EVENT_IMAP_MESSAGE_DELETED") + + ac2._evtracker.get_info_contains("close/expunge succeeded") + + lp.sec("imap2: test that only one message is left") + imap2 = ac2.direct_imap + + # Flush unsolicited responses. IMAPClient has problems + # dealing with them: https://github.com/mjs/imapclient/issues/334 + # When this NOOP was introduced, next FETCH returned empty + # result instead of a single message, even though IMAP server + # can only return more untagged responses than required, not + # less. + imap2.conn.noop() + + assert len(imap2.get_all_messages()) == 1 + class TestGroupStressTests: def test_group_many_members_add_leave_remove(self, acfactory, lp): From 1bfc8d030059e6a7eade9176258608da14e0a35d Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Tue, 4 Aug 2020 01:00:00 +0300 Subject: [PATCH 3/8] Imap.delete_msg(): warn about unexpected FETCH responses Such responses indicate IMAP client or server bug. --- src/imap/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/imap/mod.rs b/src/imap/mod.rs index 9264c0544..0d46d7099 100644 --- a/src/imap/mod.rs +++ b/src/imap/mod.rs @@ -1085,6 +1085,22 @@ impl Imap { return ImapActionResult::AlreadyDone; }; + if let Some(fetched_uid) = fetch.uid { + if fetched_uid != uid { + warn!( + context, + "IMAP bug: fetched UID {} is not equal to requested UID {}", + fetched_uid, + uid + ); + } + } else { + warn!( + context, + "IMAP bug: fetch result doesn't contain requested UID {}", uid + ); + } + let remote_message_id = get_fetch_headers(&fetch) .and_then(|headers| prefetch_get_message_id(&headers)) .unwrap_or_default(); From 6c7d7f0c16fa4e9e1dd9e2239d31ac188d075686 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Tue, 4 Aug 2020 01:00:00 +0300 Subject: [PATCH 4/8] Imap.delete_msg(): read the whole UID FETCH response --- src/imap/mod.rs | 62 +++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/src/imap/mod.rs b/src/imap/mod.rs index 0d46d7099..b13cb0645 100644 --- a/src/imap/mod.rs +++ b/src/imap/mod.rs @@ -1073,8 +1073,35 @@ impl Imap { if let Some(ref mut session) = &mut self.session { match session.uid_fetch(set, DELETE_CHECK_FLAGS).await { Ok(mut msgs) => { - let fetch = if let Some(Ok(fetch)) = msgs.next().await { - fetch + let mut remote_message_id = None; + + while let Some(response) = msgs.next().await { + match response { + Ok(fetch) => { + if fetch.uid == Some(uid) { + remote_message_id = get_fetch_headers(&fetch) + .and_then(|headers| prefetch_get_message_id(&headers)) + .ok(); + } + } + Err(err) => { + warn!(context, "IMAP fetch error {}", err); + return ImapActionResult::RetryLater; + } + } + } + + if let Some(remote_message_id) = remote_message_id { + if remote_message_id != message_id { + warn!( + context, + "Cannot delete on IMAP, {}: remote message-id '{}' != '{}'", + display_imap_id, + remote_message_id, + message_id, + ); + return ImapActionResult::Failed; + } } else { warn!( context, @@ -1083,37 +1110,6 @@ impl Imap { message_id, ); return ImapActionResult::AlreadyDone; - }; - - if let Some(fetched_uid) = fetch.uid { - if fetched_uid != uid { - warn!( - context, - "IMAP bug: fetched UID {} is not equal to requested UID {}", - fetched_uid, - uid - ); - } - } else { - warn!( - context, - "IMAP bug: fetch result doesn't contain requested UID {}", uid - ); - } - - let remote_message_id = get_fetch_headers(&fetch) - .and_then(|headers| prefetch_get_message_id(&headers)) - .unwrap_or_default(); - - if remote_message_id != message_id { - warn!( - context, - "Cannot delete on IMAP, {}: remote message-id '{}' != '{}'", - display_imap_id, - remote_message_id, - message_id, - ); - return ImapActionResult::Failed; } } Err(err) => { From b83f3e5ea0ba49cbd36fe7877aa8bb5d4400a141 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Tue, 4 Aug 2020 01:00:00 +0300 Subject: [PATCH 5/8] imap: read all UID STORE responses Otherwise these FETCH responses will remain unread and may be confused with the actual FETCH response later. --- src/imap/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/imap/mod.rs b/src/imap/mod.rs index b13cb0645..a8833ce15 100644 --- a/src/imap/mod.rs +++ b/src/imap/mod.rs @@ -970,7 +970,11 @@ impl Imap { if let Some(ref mut session) = &mut self.session { let query = format!("+FLAGS ({})", flag); match session.uid_store(uid_set, &query).await { - Ok(_) => {} + Ok(mut responses) => { + while let Some(_response) = responses.next().await { + // Read all the responses + } + } Err(err) => { warn!( context, From ea0aa4a93f89e2e7bafb643142c696724686dc27 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Tue, 4 Aug 2020 01:00:00 +0300 Subject: [PATCH 6/8] Imap.select_with_uidvalidity(): read all the IMAP responses --- src/imap/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/imap/mod.rs b/src/imap/mod.rs index a8833ce15..c597c629f 100644 --- a/src/imap/mod.rs +++ b/src/imap/mod.rs @@ -573,8 +573,14 @@ impl Imap { let set = format!("{}", mailbox.exists); match session.fetch(set, JUST_UID).await { Ok(mut list) => { - if let Some(Ok(msg)) = list.next().await { - msg.uid.unwrap_or_default() + let mut new_last_seen_uid = None; + while let Some(fetch) = list.next().await.transpose()? { + if fetch.message == mailbox.exists && fetch.uid.is_some() { + new_last_seen_uid = fetch.uid; + } + } + if let Some(new_last_seen_uid) = new_last_seen_uid { + new_last_seen_uid } else { return Err(Error::Other("failed to fetch".into())); } From ec4d68af2b90c473152ae58d9784a2bd0c53498c Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sat, 8 Aug 2020 00:00:00 +0300 Subject: [PATCH 7/8] Remove xfail mark on regression test --- python/tests/test_account.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/tests/test_account.py b/python/tests/test_account.py index a66b77311..20ab2e310 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -1652,7 +1652,6 @@ class TestOnlineAccount: assert "Ephemeral timer: " not in system_message2.get_message_info() assert chat1.get_ephemeral_timer() == 0 - @pytest.mark.xfail def test_delete_multiple_messages(self, acfactory, lp): ac1, ac2 = acfactory.get_two_online_accounts() chat12 = acfactory.get_accepted_chat(ac1, ac2) From 528b5e946959c48c01b4ab4d7f2d1d572f5ce947 Mon Sep 17 00:00:00 2001 From: Alexander Krotov Date: Sat, 8 Aug 2020 00:00:00 +0300 Subject: [PATCH 8/8] Attempt to eliminate test flakiness --- python/tests/test_account.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/tests/test_account.py b/python/tests/test_account.py index 20ab2e310..c15fe4b5b 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -1661,6 +1661,10 @@ class TestOnlineAccount: for text in texts: chat12.send_text(text) + # Ensure messages are delivered in exactly this order. + ac1._evtracker.get_matching("DC_EVENT_SMTP_MESSAGE_SENT") + ac1._evtracker.get_matching("DC_EVENT_MSG_DELIVERED") + lp.sec("ac2: waiting for all messages on the other side") to_delete = [] for text in texts: