From a8af48926df6531676c8e98f180afe9fb55dd859 Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 25 Apr 2024 22:29:39 +0200 Subject: [PATCH] Don't call `open()` from `RealImapFolder.search()` --- .../java/com/fsck/k9/backend/imap/CommandSearch.kt | 3 +++ .../com/fsck/k9/mail/store/imap/RealImapFolder.kt | 1 - .../fsck/k9/mail/store/imap/RealImapFolderTest.kt | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandSearch.kt b/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandSearch.kt index b7d3165f9..e0022fe78 100644 --- a/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandSearch.kt +++ b/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandSearch.kt @@ -2,6 +2,7 @@ package com.fsck.k9.backend.imap import com.fsck.k9.mail.Flag import com.fsck.k9.mail.store.imap.ImapStore +import com.fsck.k9.mail.store.imap.OpenMode internal class CommandSearch(private val imapStore: ImapStore) { @@ -14,6 +15,8 @@ internal class CommandSearch(private val imapStore: ImapStore) { ): List { val folder = imapStore.getFolder(folderServerId) try { + folder.open(OpenMode.READ_ONLY) + return folder.search(query, requiredFlags, forbiddenFlags, performFullTextSearch) .sortedWith(UidReverseComparator()) .map { it.uid } diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt index f5a0dcb42..01e34aa65 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt @@ -1191,7 +1191,6 @@ internal class RealImapFolder( performFullTextSearch: Boolean, ): List { try { - open(OpenMode.READ_ONLY) checkOpen() inSearch = true diff --git a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt index b20b06aaa..0d31032a7 100644 --- a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt +++ b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt @@ -1183,10 +1183,23 @@ class RealImapFolderTest { assertCommandWithIdsIssued("UID STORE 1 +FLAGS.SILENT (\\Seen)") } + @Test + fun `search() on closed folder should throw`() { + val folder = createFolder("Folder") + + assertFailure { + folder.search("query", setOf(Flag.SEEN), emptySet(), true) + }.isInstanceOf() + .hasMessage("Folder Folder is not open.") + + verifyNoMoreInteractions(imapConnection) + } + @Test fun search_withFullTextSearchEnabled_shouldIssueRespectiveCommand() { val folder = createFolder("Folder") prepareImapFolderForOpen(OpenMode.READ_ONLY) + folder.open(OpenMode.READ_ONLY) setupUidSearchResponses("1 OK SEARCH completed") folder.search("query", setOf(Flag.SEEN), emptySet(), true) @@ -1198,6 +1211,7 @@ class RealImapFolderTest { fun search_withFullTextSearchDisabled_shouldIssueRespectiveCommand() { val folder = createFolder("Folder") prepareImapFolderForOpen(OpenMode.READ_ONLY) + folder.open(OpenMode.READ_ONLY) setupUidSearchResponses("1 OK SEARCH completed") folder.search("query", emptySet(), emptySet(), false)