Skip to content

Commit 5d82c21

Browse files
James Houlahancuthix
James Houlahan
authored andcommitted
GODT-1165: Handle UID FETCH with sequence range of empty mailbox
1 parent 6ff4c8a commit 5d82c21

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

internal/store/mailbox_ids.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func (storeMailbox *Mailbox) GetAPIIDsFromUIDRange(start, stop uint32) (apiIDs [
3838
b := storeMailbox.txGetIMAPIDsBucket(tx)
3939
c := b.Cursor()
4040

41+
// GODT-1153 If the mailbox is empty we should reply BAD to client.
42+
if uid, _ := c.Last(); uid == nil {
43+
return nil
44+
}
45+
4146
// If the start range is a wildcard, the range can only refer to the last message in the mailbox.
4247
if start == 0 {
4348
_, apiID := c.Last()
@@ -74,6 +79,11 @@ func (storeMailbox *Mailbox) GetAPIIDsFromSequenceRange(start, stop uint32) (api
7479
b := storeMailbox.txGetIMAPIDsBucket(tx)
7580
c := b.Cursor()
7681

82+
// GODT-1153 If the mailbox is empty we should reply BAD to client.
83+
if uid, _ := c.Last(); uid == nil {
84+
return nil
85+
}
86+
7787
// If the start range is a wildcard, the range can only refer to the last message in the mailbox.
7888
if start == 0 {
7989
_, apiID := c.Last()
@@ -318,5 +328,10 @@ func (storeMailbox *Mailbox) GetUIDByHeader(header *mail.Header) (foundUID uint3
318328

319329
func (storeMailbox *Mailbox) txGetFinalUID(b *bolt.Bucket) uint32 {
320330
uid, _ := b.Cursor().Last()
331+
332+
if uid == nil {
333+
panic(errors.New("cannot get final UID of empty mailbox"))
334+
}
335+
321336
return btoi(uid)
322337
}

test/features/bridge/imap/message/fetch.feature

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Feature: IMAP fetch messages
2727
Then IMAP response is "OK"
2828
And IMAP response has 5 messages
2929

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

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

121+
Examples:
122+
| range |
123+
| 1 |
124+
| 1,5,6 |
125+
| 1:* |
126+
| * |
127+
118128
Scenario: Fetch of big mailbox
119129
Given there are 100 messages in mailbox "Folders/mbox" for "user"
120130
And there is IMAP client logged in as "user"
@@ -123,7 +133,26 @@ Feature: IMAP fetch messages
123133
Then IMAP response is "OK"
124134
And IMAP response has 100 messages
125135

136+
Scenario: Fetch of big mailbox by UID
137+
Given there are 100 messages in mailbox "Folders/mbox" for "user"
138+
And there is IMAP client logged in as "user"
139+
And there is IMAP client selected in "Folders/mbox"
140+
When IMAP client fetches by UID "1:*"
141+
Then IMAP response is "OK"
142+
And IMAP response has 100 messages
143+
126144
Scenario: Fetch returns also messages that are marked as deleted
145+
Given there are messages in mailbox "Folders/mbox" for "user"
146+
| from | to | subject | body | read | starred | deleted |
147+
| john.doe@mail.com | user@pm.me | foo | hello | false | false | false |
148+
| jane.doe@mail.com | name@pm.me | bar | world | true | true | true |
149+
And there is IMAP client logged in as "user"
150+
And there is IMAP client selected in "Folders/mbox"
151+
When IMAP client fetches "1:*"
152+
Then IMAP response is "OK"
153+
And IMAP response has 2 message
154+
155+
Scenario: Fetch by UID returns also messages that are marked as deleted
127156
Given there are messages in mailbox "Folders/mbox" for "user"
128157
| from | to | subject | body | read | starred | deleted |
129158
| john.doe@mail.com | user@pm.me | foo | hello | false | false | false |

0 commit comments

Comments
 (0)