1package imapserver
2
3import (
4 "testing"
5
6 "github.com/mjl-/mox/imapclient"
7)
8
9func TestCreate(t *testing.T) {
10 tc := start(t)
11 defer tc.close()
12
13 tc2 := startNoSwitchboard(t)
14 defer tc2.closeNoWait()
15
16 tc.client.Login("mjl@mox.example", password0)
17 tc2.client.Login("mjl@mox.example", password0)
18
19 tc.transactf("no", "create inbox") // Already exists and not allowed. ../rfc/9051:1913
20 tc.transactf("no", "create Inbox") // Idem.
21
22 // Don't allow names that can cause trouble when exporting to directories.
23 tc.transactf("no", "create .")
24 tc.transactf("no", "create ..")
25 tc.transactf("no", "create legit/..")
26 tc.transactf("ok", "create ...") // No special meaning.
27
28 // ../rfc/9051:1937
29 tc.transactf("ok", "create inbox/a/c")
30 tc.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "Inbox/a"}, imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "Inbox/a/c"})
31
32 tc2.transactf("ok", "noop")
33 tc2.xuntagged(
34 imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "..."},
35 imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "Inbox/a"},
36 imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "Inbox/a/c"},
37 )
38
39 tc.transactf("no", "create inbox/a/c") // Exists.
40
41 tc.transactf("ok", "create inbox/a/x")
42 tc.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "Inbox/a/x"})
43
44 tc2.transactf("ok", "noop")
45 tc2.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "Inbox/a/x"})
46
47 // ../rfc/9051:1934
48 tc.transactf("ok", "create mailbox/")
49 tc.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "mailbox"})
50
51 // OldName is only set for IMAP4rev2 or NOTIFY.
52 tc.client.Enable("imap4rev2")
53 tc.transactf("ok", "create mailbox2/")
54 tc.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "mailbox2", OldName: "mailbox2/"})
55
56 tc2.transactf("ok", "noop")
57 tc2.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "mailbox"}, imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "mailbox2"})
58
59 // If we are already subscribed, create should still work, and we still want to see the subscribed flag.
60 tc.transactf("ok", "subscribe newbox")
61 tc2.transactf("ok", "noop")
62 tc2.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`, `\NonExistent`}, Separator: '/', Mailbox: "newbox"})
63
64 tc.transactf("ok", "create newbox")
65 tc.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "newbox"})
66 tc2.transactf("ok", "noop")
67 tc2.xuntagged(imapclient.UntaggedList{Flags: []string{`\Subscribed`}, Separator: '/', Mailbox: "newbox"})
68
69 // todo: test create+delete+create of a name results in a higher uidvalidity.
70
71 tc.transactf("no", "create /bad/root")
72 tc.transactf("no", "create bad//root") // Cannot have internal duplicate slashes.
73 tc.transactf("no", `create ""`) // Refuse empty mailbox name.
74 // We are not allowing special characters.
75 tc.transactf("bad", `create "\n"`)
76 tc.transactf("bad", `create "\x7f"`)
77 tc.transactf("bad", `create "\x9f"`)
78 tc.transactf("bad", `create "\u2028"`)
79 tc.transactf("bad", `create "\u2029"`)
80 tc.transactf("ok", `create "%%"`)
81 tc.transactf("ok", `create "*"`)
82 tc.transactf("no", `create "#"`) // Leading hash not allowed.
83 tc.transactf("ok", `create "test#"`)
84
85 // Create with flags.
86 tc.transactf("no", `create "newwithflags" (use (\unknown))`)
87 tc.transactf("no", `create "newwithflags" (use (\all))`)
88 tc.transactf("ok", `create "newwithflags" (use (\archive))`)
89 tc.transactf("ok", "noop")
90 tc.xuntagged()
91 tc.transactf("ok", `create "newwithflags2" (use (\archive) use (\drafts \sent))`)
92
93 // UTF-7 checks are only for IMAP4 before rev2 and without UTF8=ACCEPT.
94 tc.transactf("ok", `create "&"`) // Interpreted as UTF-8, no UTF-7.
95 tc2.transactf("bad", `create "&"`) // Bad UTF-7.
96 tc2.transactf("ok", `create "&Jjo-"`) // ☺, valid UTF-7.
97
98 tc.transactf("ok", "create expungebox") // Existed in past.
99 tc.transactf("ok", "delete expungebox") // Gone again.
100}
101