1package imapserver
2
3import (
4 "strings"
5 "testing"
6
7 "github.com/mjl-/mox/imapclient"
8)
9
10func TestSelect(t *testing.T) {
11 testSelectExamine(t, false)
12}
13
14func TestExamine(t *testing.T) {
15 testSelectExamine(t, true)
16}
17
18// select and examine are pretty much the same. but examine opens readonly instead of readwrite.
19func testSelectExamine(t *testing.T, examine bool) {
20 defer mockUIDValidity()()
21 tc := start(t)
22 defer tc.close()
23
24 tc.client.Login("mjl@mox.example", password0)
25
26 cmd := "select"
27 okcode := "READ-WRITE"
28 if examine {
29 cmd = "examine"
30 okcode = "READ-ONLY"
31 }
32
33 uclosed := imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "CLOSED", More: "x"}}
34 flags := strings.Split(`\Seen \Answered \Flagged \Deleted \Draft $Forwarded $Junk $NotJunk $Phishing $MDNSent`, " ")
35 permflags := strings.Split(`\Seen \Answered \Flagged \Deleted \Draft $Forwarded $Junk $NotJunk $Phishing $MDNSent \*`, " ")
36 uflags := imapclient.UntaggedFlags(flags)
37 upermflags := imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "PERMANENTFLAGS", CodeArg: imapclient.CodeList{Code: "PERMANENTFLAGS", Args: permflags}, More: "x"}}
38 urecent := imapclient.UntaggedRecent(0)
39 uexists0 := imapclient.UntaggedExists(0)
40 uexists1 := imapclient.UntaggedExists(1)
41 uuidval1 := imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "UIDVALIDITY", CodeArg: imapclient.CodeUint{Code: "UIDVALIDITY", Num: 1}, More: "x"}}
42 uuidnext1 := imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "UIDNEXT", CodeArg: imapclient.CodeUint{Code: "UIDNEXT", Num: 1}, More: "x"}}
43 ulist := imapclient.UntaggedList{Separator: '/', Mailbox: "Inbox"}
44 uunseen := imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "UNSEEN", CodeArg: imapclient.CodeUint{Code: "UNSEEN", Num: 1}, More: "x"}}
45 uuidnext2 := imapclient.UntaggedResult{Status: imapclient.OK, RespText: imapclient.RespText{Code: "UIDNEXT", CodeArg: imapclient.CodeUint{Code: "UIDNEXT", Num: 2}, More: "x"}}
46
47 // Parameter required.
48 tc.transactf("bad", cmd)
49
50 // Mailbox does not exist.
51 tc.transactf("no", cmd+" bogus")
52
53 tc.transactf("ok", cmd+" inbox")
54 tc.xuntagged(uflags, upermflags, urecent, uexists0, uuidval1, uuidnext1, ulist)
55 tc.xcode(okcode)
56
57 tc.transactf("ok", cmd+` "inbox"`)
58 tc.xuntagged(uclosed, uflags, upermflags, urecent, uexists0, uuidval1, uuidnext1, ulist)
59 tc.xcode(okcode)
60
61 // Append a message. It will be reported as UNSEEN.
62 tc.client.Append("inbox", nil, nil, []byte(exampleMsg))
63 tc.transactf("ok", cmd+" inbox")
64 tc.xuntagged(uclosed, uflags, upermflags, urecent, uunseen, uexists1, uuidval1, uuidnext2, ulist)
65 tc.xcode(okcode)
66
67 // With imap4rev2, we no longer get untagged RECENT or untagged UNSEEN.
68 tc.client.Enable("imap4rev2")
69 tc.transactf("ok", cmd+" inbox")
70 tc.xuntagged(uclosed, uflags, upermflags, uexists1, uuidval1, uuidnext2, ulist)
71 tc.xcode(okcode)
72}
73