Skip to content

Commit

Permalink
GODT-1165: Handle UID FETCH with sequence range of empty mailbox
Browse files Browse the repository at this point in the history
  • Loading branch information
James Houlahan authored and cuthix committed May 19, 2021
1 parent 6ff4c8a commit 5d82c21
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
15 changes: 15 additions & 0 deletions internal/store/mailbox_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func (storeMailbox *Mailbox) GetAPIIDsFromUIDRange(start, stop uint32) (apiIDs [
b := storeMailbox.txGetIMAPIDsBucket(tx)
c := b.Cursor()

// GODT-1153 If the mailbox is empty we should reply BAD to client.
if uid, _ := c.Last(); uid == nil {
return nil
}

// If the start range is a wildcard, the range can only refer to the last message in the mailbox.
if start == 0 {
_, apiID := c.Last()
Expand Down Expand Up @@ -74,6 +79,11 @@ func (storeMailbox *Mailbox) GetAPIIDsFromSequenceRange(start, stop uint32) (api
b := storeMailbox.txGetIMAPIDsBucket(tx)
c := b.Cursor()

// GODT-1153 If the mailbox is empty we should reply BAD to client.
if uid, _ := c.Last(); uid == nil {
return nil
}

// If the start range is a wildcard, the range can only refer to the last message in the mailbox.
if start == 0 {
_, apiID := c.Last()
Expand Down Expand Up @@ -318,5 +328,10 @@ func (storeMailbox *Mailbox) GetUIDByHeader(header *mail.Header) (foundUID uint3

func (storeMailbox *Mailbox) txGetFinalUID(b *bolt.Bucket) uint32 {
uid, _ := b.Cursor().Last()

if uid == nil {
panic(errors.New("cannot get final UID of empty mailbox"))
}

return btoi(uid)
}
35 changes: 32 additions & 3 deletions test/features/bridge/imap/message/fetch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Feature: IMAP fetch messages
Then IMAP response is "OK"
And IMAP response has 5 messages

Scenario: Fetch first few messages of inbox
Scenario: Fetch first few messages of inbox by UID
Given there are 10 messages in mailbox "INBOX" for "user"
And there is IMAP client logged in as "user"
And there is IMAP client selected in "INBOX"
Expand Down Expand Up @@ -108,13 +108,23 @@ Feature: IMAP fetch messages
And IMAP response has 10 messages

# This test is wrong! RFC says it should return "BAD" (GODT-1153).
Scenario: Fetch of empty mailbox
Scenario Outline: Fetch range of empty mailbox
Given there is IMAP client logged in as "user"
And there is IMAP client selected in "Folders/mbox"
When IMAP client fetches "1:*"
When IMAP client fetches "<range>"
Then IMAP response is "OK"
And IMAP response has 0 messages
When IMAP client fetches by UID "<range>"
Then IMAP response is "OK"
And IMAP response has 0 messages

Examples:
| range |
| 1 |
| 1,5,6 |
| 1:* |
| * |

Scenario: Fetch of big mailbox
Given there are 100 messages in mailbox "Folders/mbox" for "user"
And there is IMAP client logged in as "user"
Expand All @@ -123,7 +133,26 @@ Feature: IMAP fetch messages
Then IMAP response is "OK"
And IMAP response has 100 messages

Scenario: Fetch of big mailbox by UID
Given there are 100 messages in mailbox "Folders/mbox" for "user"
And there is IMAP client logged in as "user"
And there is IMAP client selected in "Folders/mbox"
When IMAP client fetches by UID "1:*"
Then IMAP response is "OK"
And IMAP response has 100 messages

Scenario: Fetch returns also messages that are marked as deleted
Given there are messages in mailbox "Folders/mbox" for "user"
| from | to | subject | body | read | starred | deleted |
| john.doe@mail.com | user@pm.me | foo | hello | false | false | false |
| jane.doe@mail.com | name@pm.me | bar | world | true | true | true |
And there is IMAP client logged in as "user"
And there is IMAP client selected in "Folders/mbox"
When IMAP client fetches "1:*"
Then IMAP response is "OK"
And IMAP response has 2 message

Scenario: Fetch by UID returns also messages that are marked as deleted
Given there are messages in mailbox "Folders/mbox" for "user"
| from | to | subject | body | read | starred | deleted |
| john.doe@mail.com | user@pm.me | foo | hello | false | false | false |
Expand Down

0 comments on commit 5d82c21

Please sign in to comment.