8 "github.com/mjl-/bstore"
10 "github.com/mjl-/mox/imapclient"
11 "github.com/mjl-/mox/mox-"
12 "github.com/mjl-/mox/store"
15func TestCondstore(t *testing.T) {
16 testCondstoreQresync(t, false)
19func TestQresync(t *testing.T) {
20 testCondstoreQresync(t, true)
23func testCondstoreQresync(t *testing.T, qresync bool) {
24 defer mockUIDValidity()()
28 // todo: check whether marking \seen will cause modseq to be returned in case of qresync.
30 // Check basic requirements of CONDSTORE.
32 capability := "Condstore"
34 capability = "Qresync"
37 tc.client.Login("mjl@mox.example", password0)
38 tc.client.Enable(capability)
39 tc.transactf("ok", "Select inbox")
40 tc.xuntaggedOpt(false, imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "HIGHESTMODSEQ", CodeArg: imapclient.CodeHighestModSeq(1), More: "x"}})
42 // First some tests without any messages.
44 tc.transactf("ok", "Status inbox (Highestmodseq)")
45 tc.xuntagged(imapclient.UntaggedStatus{Mailbox: "Inbox", Attrs: map[imapclient.StatusAttr]int64{imapclient.StatusHighestModSeq: 1}})
47 // No messages, no matches.
48 tc.transactf("ok", "Uid Fetch 1:* (Flags) (Changedsince 12345)")
51 // Also no messages with modseq 1, which we internally turn into modseq 0.
52 tc.transactf("ok", "Uid Fetch 1:* (Flags) (Changedsince 1)")
55 // Also try with modseq attribute.
56 tc.transactf("ok", "Uid Fetch 1:* (Flags Modseq) (Changedsince 1)")
59 // Search with modseq search criteria.
60 tc.transactf("ok", "Search Modseq 0") // Zero is valid, matches all.
63 tc.transactf("ok", "Search Modseq 1") // Converted to zero internally.
66 tc.transactf("ok", "Search Modseq 12345")
69 tc.transactf("ok", `Search Modseq "/Flags/\\Draft" All 12345`)
72 tc.transactf("ok", `Search Or Modseq 12345 Modseq 54321`)
76 tc.transactf("ok", "Search Return (All) Modseq 123")
77 tc.xesearch(imapclient.UntaggedEsearch{})
79 // Now we add, delete, expunge, modify some message flags and check if the
80 // responses are correct. We check in both a condstore-enabled and one without that
81 // we get the correct notifications.
83 // First we add 3 messages as if they were added before we implemented CONDSTORE.
84 // Later on, we'll update the second, and delete the third, leaving the first
85 // unmodified. Those messages have modseq 0 in the database. We use append for
86 // convenience, then adjust the records in the database.
87 // We have a workaround below to prevent triggering the consistency checker.
88 tc.transactf("ok", "Append inbox () \" 1-Jan-2022 10:10:00 +0100\" {1+}\r\nx")
89 tc.transactf("ok", "Append inbox () \" 1-Jan-2022 10:10:00 +0100\" {1+}\r\nx")
90 tc.transactf("ok", "Append inbox () \" 1-Jan-2022 10:10:00 +0100\" {1+}\r\nx")
91 _, err := bstore.QueryDB[store.Message](ctxbg, tc.account.DB).UpdateFields(map[string]any{
95 tcheck(t, err, "clearing modseq from messages")
96 err = tc.account.DB.Update(ctxbg, &store.SyncState{ID: 1, LastModSeq: 1})
97 tcheck(t, err, "resetting modseq state")
99 tc.client.Create("otherbox")
101 // tc2 is a client without condstore, so no modseq responses.
102 tc2 := startNoSwitchboard(t)
104 tc2.client.Login("mjl@mox.example", password0)
105 tc2.client.Select("inbox")
107 // tc3 is a client with condstore, so with modseq responses.
108 tc3 := startNoSwitchboard(t)
110 tc3.client.Login("mjl@mox.example", password0)
111 tc3.client.Enable(capability)
112 tc3.client.Select("inbox")
114 var clientModseq int64 = 1 // We track the client-side modseq for inbox. Not a store.ModSeq.
116 // Add messages to: inbox, otherbox, inbox, inbox.
117 // We have these messages in order of modseq: 2+1 in inbox, 1 in otherbox, 2 in inbox.
118 // The original two in inbox appear to have modseq 1 (with 0 stored in the database).
119 // The ones we insert below will start with modseq 2. So we'll have modseq 1-5.
120 tc.transactf("ok", "Append inbox () \" 1-Jan-2022 10:10:00 +0100\" {1+}\r\nx")
121 tc.xuntagged(imapclient.UntaggedExists(4))
122 tc.xcodeArg(imapclient.CodeAppendUID{UIDValidity: 1, UID: 4})
124 tc.transactf("ok", "Append otherbox () \" 1-Jan-2022 10:10:00 +0100\" {1+}\r\nx")
126 tc.xcodeArg(imapclient.CodeAppendUID{UIDValidity: 2, UID: 1})
128 tc.transactf("ok", "Append inbox () \" 1-Jan-2022 10:10:00 +0100\" {1+}\r\nx")
129 tc.xuntagged(imapclient.UntaggedExists(5))
130 tc.xcodeArg(imapclient.CodeAppendUID{UIDValidity: 1, UID: 5})
132 tc.transactf("ok", "Append inbox () \" 1-Jan-2022 10:10:00 +0100\" {1+}\r\nx")
133 tc.xuntagged(imapclient.UntaggedExists(6))
134 tc.xcodeArg(imapclient.CodeAppendUID{UIDValidity: 1, UID: 6})
136 tc2.transactf("ok", "Noop")
137 noflags := imapclient.FetchFlags(nil)
139 imapclient.UntaggedExists(6),
140 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(4), noflags}},
141 imapclient.UntaggedFetch{Seq: 5, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(5), noflags}},
142 imapclient.UntaggedFetch{Seq: 6, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), noflags}},
145 tc3.transactf("ok", "Noop")
147 imapclient.UntaggedExists(6),
148 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(4), noflags, imapclient.FetchModSeq(clientModseq + 1)}},
149 imapclient.UntaggedFetch{Seq: 5, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(5), noflags, imapclient.FetchModSeq(clientModseq + 3)}},
150 imapclient.UntaggedFetch{Seq: 6, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), noflags, imapclient.FetchModSeq(clientModseq + 4)}},
153 mox.SetPedantic(true)
154 tc.transactf("bad", `Fetch 1 Flags (Changedsince 0)`) // 0 not allowed in syntax.
155 mox.SetPedantic(false)
156 tc.transactf("ok", "Uid fetch 1 (Flags) (Changedsince 0)")
157 tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), noflags, imapclient.FetchModSeq(clientModseq)}})
159 clientModseq += 4 // Four messages, over two mailboxes, modseq is per account.
161 // Check highestmodseq for mailboxes.
162 tc.transactf("ok", "Status inbox (highestmodseq)")
163 tc.xuntagged(imapclient.UntaggedStatus{Mailbox: "Inbox", Attrs: map[imapclient.StatusAttr]int64{imapclient.StatusHighestModSeq: clientModseq}})
165 tc.transactf("ok", "Status otherbox (highestmodseq)")
166 tc.xuntagged(imapclient.UntaggedStatus{Mailbox: "otherbox", Attrs: map[imapclient.StatusAttr]int64{imapclient.StatusHighestModSeq: 3}})
168 // Check highestmodseq when we select.
169 tc.transactf("ok", "Examine otherbox")
170 tc.xuntaggedOpt(false, imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "HIGHESTMODSEQ", CodeArg: imapclient.CodeHighestModSeq(3), More: "x"}})
172 tc.transactf("ok", "Select inbox")
173 tc.xuntaggedOpt(false, imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "HIGHESTMODSEQ", CodeArg: imapclient.CodeHighestModSeq(clientModseq), More: "x"}})
175 // Check fetch modseq response and changedsince.
176 tc.transactf("ok", `Fetch 1 (Modseq)`)
177 tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), imapclient.FetchModSeq(1)}})
179 // Without modseq attribute, even with condseq enabled, there is no modseq response.
180 // For QRESYNC, we must always send MODSEQ for UID FETCH commands, but not for FETCH commands.
../rfc/7162:1427
181 tc.transactf("ok", `Uid Fetch 1 Flags`)
183 tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), noflags, imapclient.FetchModSeq(1)}})
185 tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), noflags}})
187 tc.transactf("ok", `Fetch 1 Flags`)
188 tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), noflags}})
190 // When CHANGEDSINCE is present, MODSEQ is automatically added to the response.
193 tc.transactf("ok", `Fetch 1 Flags (Changedsince 1)`)
195 tc.transactf("ok", `Fetch 1,4 Flags (Changedsince 1)`)
196 tc.xuntagged(imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(4), noflags, imapclient.FetchModSeq(2)}})
197 tc.transactf("ok", `Fetch 2 Flags (Changedsince 2)`)
200 // store and uid store.
203 tc.transactf("ok", `Store 1 (Unchangedsince 0) +Flags ()`)
204 tc.xcodeArg(imapclient.CodeModified(xparseNumSet("1")))
205 tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), noflags, imapclient.FetchModSeq(1)}})
207 // Modseq is 2 for first condstore-aware-appended message, so also no match.
208 tc.transactf("ok", `Uid Store 4 (Unchangedsince 1) +Flags ()`)
209 tc.xcodeArg(imapclient.CodeModified(xparseNumSet("4")))
211 // Modseq is 1 for original message.
212 tc.transactf("ok", `Store 1 (Unchangedsince 1) +Flags (label1)`)
213 tc.xcode("") // No MODIFIED.
215 tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), imapclient.FetchFlags{"label1"}, imapclient.FetchModSeq(clientModseq)}})
216 tc2.transactf("ok", "Noop")
218 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), imapclient.FetchFlags{"label1"}}},
220 tc3.transactf("ok", "Noop")
222 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), imapclient.FetchFlags{"label1"}, imapclient.FetchModSeq(clientModseq)}},
225 // Modify same message twice. Check that second application doesn't fail due to
227 tc.transactf("ok", `Uid Store 1,1 (Unchangedsince %d) -Flags (label1)`, clientModseq)
229 tc.xcode("") // No MODIFIED.
231 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), imapclient.FetchFlags(nil), imapclient.FetchModSeq(clientModseq)}},
233 // We do broadcast the changes twice. Not great, but doesn't hurt. This isn't common.
234 tc2.transactf("ok", "Noop")
236 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), imapclient.FetchFlags(nil)}},
238 tc3.transactf("ok", "Noop")
240 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), imapclient.FetchFlags(nil), imapclient.FetchModSeq(clientModseq)}},
243 // Modify without actually changing flags, there will be no new modseq and no broadcast.
244 tc.transactf("ok", `Store 1 (Unchangedsince %d) -Flags (label1)`, clientModseq)
245 tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), imapclient.FetchFlags(nil), imapclient.FetchModSeq(clientModseq)}})
246 tc.xcode("") // No MODIFIED.
247 tc2.transactf("ok", "Noop")
249 tc3.transactf("ok", "Noop")
252 // search with modseq criteria and modseq in response
253 tc.transactf("ok", "Search Modseq %d", clientModseq)
254 tc.xsearchmodseq(clientModseq, 1)
256 tc.transactf("ok", "Uid Search Or Modseq %d Modseq %d", clientModseq, clientModseq)
257 tc.xsearchmodseq(clientModseq, 1)
260 tc.transactf("ok", "Search Return (Min Max All) 1:* Modseq %d", clientModseq)
261 tc.xesearch(imapclient.UntaggedEsearch{Min: 1, Max: 1, All: esearchall0("1"), ModSeq: clientModseq})
263 uint32ptr := func(v uint32) *uint32 {
266 tc.transactf("ok", "Search Return (Count) 1:* Modseq 0")
267 tc.xesearch(imapclient.UntaggedEsearch{Count: uint32ptr(6), ModSeq: clientModseq})
269 tc.transactf("ok", "Search Return (Min Max) 1:* Modseq 0")
270 tc.xesearch(imapclient.UntaggedEsearch{Min: 1, Max: 6, ModSeq: clientModseq})
272 tc.transactf("ok", "Search Return (Min) 1:* Modseq 0")
273 tc.xesearch(imapclient.UntaggedEsearch{Min: 1, ModSeq: clientModseq})
275 // expunge, we expunge the third and fourth messages. The third was originally with
276 // modseq 0, the fourth was added with condstore-aware append.
277 tc.transactf("ok", `Store 3:4 +Flags (\Deleted)`)
279 tc2.transactf("ok", "Noop")
280 tc3.transactf("ok", "Noop")
281 tc.transactf("ok", "Expunge")
284 tc.xuntagged(imapclient.UntaggedVanished{UIDs: xparseNumSet("3:4")})
286 tc.xuntagged(imapclient.UntaggedExpunge(3), imapclient.UntaggedExpunge(3))
288 tc.xcodeArg(imapclient.CodeHighestModSeq(clientModseq))
289 tc2.transactf("ok", "Noop")
290 tc2.xuntagged(imapclient.UntaggedExpunge(3), imapclient.UntaggedExpunge(3))
291 tc3.transactf("ok", "Noop")
293 tc3.xuntagged(imapclient.UntaggedVanished{UIDs: xparseNumSet("3:4")})
295 tc3.xuntagged(imapclient.UntaggedExpunge(3), imapclient.UntaggedExpunge(3))
298 // Again after expunge: status, select, conditional store/fetch/search
299 tc.transactf("ok", "Status inbox (Highestmodseq Messages Unseen Deleted)")
300 tc.xuntagged(imapclient.UntaggedStatus{Mailbox: "Inbox", Attrs: map[imapclient.StatusAttr]int64{imapclient.StatusMessages: 4, imapclient.StatusUnseen: 4, imapclient.StatusDeleted: 0, imapclient.StatusHighestModSeq: clientModseq}})
302 tc.transactf("ok", "Close")
303 tc.transactf("ok", "Select inbox")
304 tc.xuntaggedOpt(false,
305 imapclient.UntaggedExists(4),
306 imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "HIGHESTMODSEQ", CodeArg: imapclient.CodeHighestModSeq(clientModseq), More: "x"}},
309 tc.transactf("ok", `Fetch 1:* (Modseq)`)
311 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), imapclient.FetchModSeq(7)}},
312 imapclient.UntaggedFetch{Seq: 2, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(2), imapclient.FetchModSeq(1)}},
313 imapclient.UntaggedFetch{Seq: 3, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(5), imapclient.FetchModSeq(4)}},
314 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), imapclient.FetchModSeq(5)}},
316 // Expunged messages, with higher modseq, should not show up.
317 tc.transactf("ok", "Uid Fetch 1:* (flags) (Changedsince 7)")
321 tc.transactf("ok", "Search Modseq 7")
322 tc.xsearchmodseq(7, 1)
323 tc.transactf("ok", "Search Modseq 8")
327 tc.transactf("ok", "Search Return (Min Max All) 1:* Modseq 7")
328 tc.xesearch(imapclient.UntaggedEsearch{Min: 1, Max: 1, All: esearchall0("1"), ModSeq: 7})
329 tc.transactf("ok", "Search Return (Min Max All) 1:* Modseq 8")
330 tc.xuntagged(imapclient.UntaggedEsearch{Correlator: tc.client.LastTag})
332 // store, cannot modify expunged messages.
333 tc.transactf("ok", `Uid Store 3,4 (Unchangedsince %d) +Flags (label2)`, clientModseq)
335 tc.xcode("") // Not MODIFIED.
336 tc.transactf("ok", `Uid Store 3,4 +Flags (label2)`)
338 tc.xcode("") // Not MODIFIED.
342 // We start a new connection, do the thing that should enable condstore, then
343 // change flags of a message in another connection, do a noop in the new connection
344 // which should result in an untagged fetch that includes modseq, the indicator
345 // that condstore was indeed enabled. It's a bit complicated, but i don't think
346 // there is a clearly specified mechanism to find out which capabilities are
347 // enabled at any point.
349 checkCondstoreEnabled := func(fn func(xtc *testconn)) {
352 xtc := startNoSwitchboard(t)
353 // We have modified modseq & createseq to 0 above for testing that case. Don't
354 // trigger the consistency checker.
355 store.CheckConsistencyOnClose = false
358 store.CheckConsistencyOnClose = true
360 xtc.client.Login("mjl@mox.example", password0)
363 label := fmt.Sprintf("l%d", tagcount)
364 tc.transactf("ok", "Store 4 Flags (%s)", label)
366 xtc.transactf("ok", "Noop")
367 xtc.xuntagged(imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), imapclient.FetchFlags{label}, imapclient.FetchModSeq(clientModseq)}})
370 checkCondstoreEnabled(func(xtc *testconn) {
372 xtc.transactf("ok", "Select inbox (Condstore)")
375 checkCondstoreEnabled(func(xtc *testconn) {
377 xtc.transactf("ok", "Status otherbox (Highestmodseq)")
378 xtc.transactf("ok", "Select inbox")
381 checkCondstoreEnabled(func(xtc *testconn) {
383 xtc.transactf("ok", "Select inbox")
384 xtc.transactf("ok", "Fetch 4 (Modseq)")
387 checkCondstoreEnabled(func(xtc *testconn) {
389 xtc.transactf("ok", "Select inbox")
390 xtc.transactf("ok", "Search 4 Modseq 1")
393 checkCondstoreEnabled(func(xtc *testconn) {
395 xtc.transactf("ok", "Select inbox")
396 xtc.transactf("ok", "Fetch 4 (Flags) (Changedsince %d)", clientModseq)
399 checkCondstoreEnabled(func(xtc *testconn) {
401 xtc.transactf("ok", "Select inbox")
402 xtc.transactf("ok", "Store 4 (Unchangedsince 0) Flags ()")
405 checkCondstoreEnabled(func(xtc *testconn) {
407 xtc.transactf("ok", "Enable Condstore")
408 xtc.transactf("ok", "Select inbox")
411 checkCondstoreEnabled(func(xtc *testconn) {
413 xtc.transactf("ok", "Enable Qresync")
414 xtc.transactf("ok", "Select inbox")
416 tc.transactf("ok", "Store 4 Flags ()")
420 testQresync(t, tc, clientModseq)
423 // Continue with some tests that further change the data.
424 // First we copy messages to a new mailbox, and check we get new modseq for those
426 tc.transactf("ok", "Select otherbox")
427 tc2.transactf("ok", "Noop")
428 tc3.transactf("ok", "Noop")
429 tc.transactf("ok", "Copy 1 inbox")
431 tc2.transactf("ok", "Noop")
432 tc3.transactf("ok", "Noop")
434 imapclient.UntaggedExists(5),
435 imapclient.UntaggedFetch{Seq: 5, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(7), noflags}},
438 imapclient.UntaggedExists(5),
439 imapclient.UntaggedFetch{Seq: 5, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(7), noflags, imapclient.FetchModSeq(clientModseq)}},
442 // Then we move some messages, and check if we get expunged/vanished in original
443 // and untagged fetch with modseq in destination mailbox.
444 // tc2o is a client without condstore, so no modseq responses.
445 tc2o := startNoSwitchboard(t)
447 tc2o.client.Login("mjl@mox.example", password0)
448 tc2o.client.Select("otherbox")
450 // tc3o is a client with condstore, so with modseq responses.
451 tc3o := startNoSwitchboard(t)
453 tc3o.client.Login("mjl@mox.example", password0)
454 tc3o.client.Enable(capability)
455 tc3o.client.Select("otherbox")
457 tc.transactf("ok", "Select inbox")
458 tc.transactf("ok", "Uid Move 2:4 otherbox") // Only UID 2, because UID 3 and 4 have already been expunged.
461 tc.xuntaggedOpt(false, imapclient.UntaggedVanished{UIDs: xparseNumSet("2")})
462 tc.xcodeArg(imapclient.CodeHighestModSeq(clientModseq))
464 tc.xuntaggedOpt(false, imapclient.UntaggedExpunge(2))
467 tc2.transactf("ok", "Noop")
468 tc2.xuntagged(imapclient.UntaggedExpunge(2))
469 tc3.transactf("ok", "Noop")
471 tc3.xuntagged(imapclient.UntaggedVanished{UIDs: xparseNumSet("2")})
473 tc3.xuntagged(imapclient.UntaggedExpunge(2))
475 tc2o.transactf("ok", "Noop")
477 imapclient.UntaggedExists(2),
478 imapclient.UntaggedFetch{Seq: 2, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(2), noflags}},
480 tc3o.transactf("ok", "Noop")
482 imapclient.UntaggedExists(2),
483 imapclient.UntaggedFetch{Seq: 2, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(2), noflags, imapclient.FetchModSeq(clientModseq)}},
486 // Restore valid modseq/createseq for the consistency checker.
487 _, err = bstore.QueryDB[store.Message](ctxbg, tc.account.DB).FilterEqual("CreateSeq", int64(0)).UpdateNonzero(store.Message{CreateSeq: 2})
488 tcheck(t, err, "updating modseq/createseq to valid values")
489 _, err = bstore.QueryDB[store.Message](ctxbg, tc.account.DB).FilterEqual("ModSeq", int64(0)).UpdateNonzero(store.Message{ModSeq: 2})
490 tcheck(t, err, "updating modseq/createseq to valid values")
496 // Then we rename inbox, which is special because it moves messages away instead of
497 // actually moving the mailbox. The mailbox stays and is cleared, so we check if we
498 // get expunged/vanished messages.
499 tc.transactf("ok", "Rename inbox oldbox")
500 // todo spec: server doesn't respond with untagged responses, find rfc reference that says this is ok.
501 tc2.transactf("ok", "Noop")
503 imapclient.UntaggedList{Separator: '/', Mailbox: "oldbox"},
504 imapclient.UntaggedExpunge(1), imapclient.UntaggedExpunge(1), imapclient.UntaggedExpunge(1), imapclient.UntaggedExpunge(1),
506 tc3.transactf("ok", "Noop")
509 imapclient.UntaggedList{Separator: '/', Mailbox: "oldbox"},
510 imapclient.UntaggedVanished{UIDs: xparseNumSet("1,5:7")},
514 imapclient.UntaggedList{Separator: '/', Mailbox: "oldbox"},
515 imapclient.UntaggedExpunge(1), imapclient.UntaggedExpunge(1), imapclient.UntaggedExpunge(1), imapclient.UntaggedExpunge(1),
519 // Then we delete otherbox (we cannot delete inbox). We don't keep any history for removed mailboxes, so not actually a special case.
520 tc.transactf("ok", "Delete otherbox")
523func testQresync(t *testing.T, tc *testconn, clientModseq int64) {
525 tc.transactf("bad", "fetch 1:* (Flags) (Changedsince 1 Vanished)")
528 tc.transactf("bad", "Uid Fetch 1:* (Flags) (Vanished)")
531 xtc := startNoSwitchboard(t)
532 xtc.client.Login("mjl@mox.example", password0)
533 xtc.transactf("ok", "Select inbox (Condstore)")
534 xtc.transactf("bad", "Uid Fetch 1:* (Flags) (Changedsince 1 Vanished)")
535 // Prevent triggering the consistency checker, we still have modseq/createseq at 0.
536 store.CheckConsistencyOnClose = false
538 store.CheckConsistencyOnClose = true
541 // Check that we get proper vanished responses.
542 tc.transactf("ok", "Uid Fetch 1:* (Flags) (Changedsince 1 Vanished)")
543 noflags := imapclient.FetchFlags(nil)
545 imapclient.UntaggedVanished{Earlier: true, UIDs: xparseNumSet("3:4")},
546 imapclient.UntaggedFetch{Seq: 3, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(5), noflags, imapclient.FetchModSeq(4)}},
547 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), noflags, imapclient.FetchModSeq(7)}},
548 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), noflags, imapclient.FetchModSeq(clientModseq)}},
551 // select/examine with qresync parameters, including the various optional fields.
552 tc.transactf("ok", "Close")
555 xtc = startNoSwitchboard(t)
556 xtc.client.Login("mjl@mox.example", password0)
557 xtc.transactf("bad", "Select inbox (Qresync 1 0)")
558 // Prevent triggering the consistency checker, we still have modseq/createseq at 0.
559 store.CheckConsistencyOnClose = false
561 store.CheckConsistencyOnClose = true
564 tc.transactf("bad", "Select inbox (Qresync (0 1))") // Both args must be > 0.
565 tc.transactf("bad", "Select inbox (Qresync (1 0))") // Both args must be > 0.
566 tc.transactf("bad", "Select inbox (Qresync)") // Two args are minimum.
567 tc.transactf("bad", "Select inbox (Qresync (1))") // Two args are minimum.
568 tc.transactf("bad", "Select inbox (Qresync (1 1 1:*))") // Known UIDs, * not allowed.
569 tc.transactf("bad", "Select inbox (Qresync (1 1 1:6 (1:* 1:6)))") // Known seqset cannot have *.
570 tc.transactf("bad", "Select inbox (Qresync (1 1 1:6 (1:6 1:*)))") // Known uidset cannot have *.
571 tc.transactf("bad", "Select inbox (Qresync (1 1) qresync (1 1))") // Duplicate qresync.
573 flags := strings.Split(`\Seen \Answered \Flagged \Deleted \Draft $Forwarded $Junk $NotJunk $Phishing $MDNSent l1 l2 l3 l4 l5 l6 l7 l8 label1`, " ")
574 permflags := strings.Split(`\Seen \Answered \Flagged \Deleted \Draft $Forwarded $Junk $NotJunk $Phishing $MDNSent \*`, " ")
575 uflags := imapclient.UntaggedFlags(flags)
576 upermflags := imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "PERMANENTFLAGS", CodeArg: imapclient.CodeList{Code: "PERMANENTFLAGS", Args: permflags}, More: "x"}}
578 baseUntagged := []imapclient.Untagged{
581 imapclient.UntaggedList{Separator: '/', Mailbox: "Inbox"},
582 imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "UIDNEXT", CodeArg: imapclient.CodeUint{Code: "UIDNEXT", Num: 7}, More: "x"}},
583 imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "UIDVALIDITY", CodeArg: imapclient.CodeUint{Code: "UIDVALIDITY", Num: 1}, More: "x"}},
584 imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "UNSEEN", CodeArg: imapclient.CodeUint{Code: "UNSEEN", Num: 1}, More: "x"}},
585 imapclient.UntaggedRecent(0),
586 imapclient.UntaggedExists(4),
587 imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "HIGHESTMODSEQ", CodeArg: imapclient.CodeHighestModSeq(clientModseq), More: "x"}},
590 makeUntagged := func(l ...imapclient.Untagged) []imapclient.Untagged {
591 return append(append([]imapclient.Untagged{}, baseUntagged...), l...)
594 // uidvalidity 1, highest known modseq 1, sends full current state.
595 tc.transactf("ok", "Select inbox (Qresync (1 1))")
598 imapclient.UntaggedVanished{Earlier: true, UIDs: xparseNumSet("3:4")},
599 imapclient.UntaggedFetch{Seq: 3, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(5), noflags, imapclient.FetchModSeq(4)}},
600 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), noflags, imapclient.FetchModSeq(7)}},
601 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), noflags, imapclient.FetchModSeq(clientModseq)}},
605 // Uidvalidity mismatch, server will not send any changes, so it's just a regular open.
606 tc.transactf("ok", "Close")
607 tc.transactf("ok", "Select inbox (Qresync (2 1))")
608 tc.xuntagged(baseUntagged...)
610 // We can tell which UIDs we know. First, send broader range then exist, should work.
611 tc.transactf("ok", "Close")
612 tc.transactf("ok", "Select inbox (Qresync (1 1 1:7))")
615 imapclient.UntaggedVanished{Earlier: true, UIDs: xparseNumSet("3:4")},
616 imapclient.UntaggedFetch{Seq: 3, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(5), noflags, imapclient.FetchModSeq(4)}},
617 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), noflags, imapclient.FetchModSeq(7)}},
618 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), noflags, imapclient.FetchModSeq(clientModseq)}},
622 // Now send just the ones that exist. We won't get the vanished messages.
623 tc.transactf("ok", "Close")
624 tc.transactf("ok", "Select inbox (Qresync (1 1 1,2,5:6))")
627 imapclient.UntaggedFetch{Seq: 3, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(5), noflags, imapclient.FetchModSeq(4)}},
628 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), noflags, imapclient.FetchModSeq(7)}},
629 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), noflags, imapclient.FetchModSeq(clientModseq)}},
633 // We'll only get updates for UIDs we specify.
634 tc.transactf("ok", "Close")
635 tc.transactf("ok", "Select inbox (Qresync (1 1 5))")
638 imapclient.UntaggedFetch{Seq: 3, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(5), noflags, imapclient.FetchModSeq(4)}},
643 tc.transactf("ok", "Close")
644 tc.transactf("ok", "Select inbox (Qresync (1 1 3))")
647 imapclient.UntaggedVanished{Earlier: true, UIDs: xparseNumSet("3")},
651 // If we specify the latest modseq, we'll get no changes.
652 tc.transactf("ok", "Close")
653 tc.transactf("ok", "Select inbox (Qresync (1 %d))", clientModseq)
654 tc.xuntagged(baseUntagged...)
656 // We can provide our own seqs & uids, and have server determine which uids we
657 // know. But the seqs & uids must be of equal length. First try with a few combinations
659 tc.transactf("ok", "Close")
660 tc.transactf("bad", "Select inbox (Qresync (1 1 1:6 (1 1,2)))") // Not same length.
661 tc.transactf("bad", "Select inbox (Qresync (1 1 1:6 (1,2 1)))") // Not same length.
662 tc.transactf("no", "Select inbox (Qresync (1 1 1:6 (1,2 1,1)))") // Not ascending.
663 tc.transactf("bad", "Select inbox (Qresync (1 1 1:6 (1:* 1:4)))") // Star not allowed.
665 // With valid parameters, based on what a client would know at this stage.
666 tc.transactf("ok", "Select inbox (Qresync (1 1 1:6 (1,3,6 1,3,6)))")
669 imapclient.UntaggedVanished{Earlier: true, UIDs: xparseNumSet("3:4")},
670 imapclient.UntaggedFetch{Seq: 3, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(5), noflags, imapclient.FetchModSeq(4)}},
671 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), noflags, imapclient.FetchModSeq(7)}},
672 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), noflags, imapclient.FetchModSeq(clientModseq)}},
676 // The 3rd parameter is optional, try without.
677 tc.transactf("ok", "Close")
678 tc.transactf("ok", "Select inbox (Qresync (1 5 (1,3,6 1,3,6)))")
681 imapclient.UntaggedVanished{Earlier: true, UIDs: xparseNumSet("3:4")},
682 imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(1), noflags, imapclient.FetchModSeq(7)}},
683 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), noflags, imapclient.FetchModSeq(clientModseq)}},
687 tc.transactf("ok", "Close")
688 tc.transactf("ok", "Select inbox (Qresync (1 8 (1,3,6 1,3,6)))")
691 imapclient.UntaggedVanished{Earlier: true, UIDs: xparseNumSet("3:4")},
692 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), noflags, imapclient.FetchModSeq(clientModseq)}},
696 // Client will claim a highestmodseq but then include uids that have been removed
697 // since that time. Server detects this, sends full vanished history and continues
698 // working with modseq changed to 1 before the expunged uid.
699 tc.transactf("ok", "Close")
700 tc.transactf("ok", "Select inbox (Qresync (1 9 (1,3,6 1,3,6)))")
703 imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "ALERT", More: "Synchronization inconsistency in client detected. Client tried to sync with a UID that was removed at or after the MODSEQ it sent in the request. Sending all historic message removals for selected mailbox. Full synchronization recommended."}},
704 imapclient.UntaggedVanished{Earlier: true, UIDs: xparseNumSet("3:4")},
705 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), noflags, imapclient.FetchModSeq(clientModseq)}},
709 // Client will claim a highestmodseq but then include uids that have been removed
710 // since that time. Server detects this, sends full vanished history and continues
711 // working with modseq changed to 1 before the expunged uid.
712 tc.transactf("ok", "Close")
713 tc.transactf("ok", "Select inbox (Qresync (1 18 (1,3,6 1,3,6)))")
716 imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "ALERT", More: "Synchronization inconsistency in client detected. Client tried to sync with a UID that was removed at or after the MODSEQ it sent in the request. Sending all historic message removals for selected mailbox. Full synchronization recommended."}},
717 imapclient.UntaggedVanished{Earlier: true, UIDs: xparseNumSet("3:4")},
718 imapclient.UntaggedFetch{Seq: 4, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6), noflags, imapclient.FetchModSeq(clientModseq)}},