15 "github.com/mjl-/mox/config"
16 "github.com/mjl-/mox/dmarcdb"
17 "github.com/mjl-/mox/dns"
18 "github.com/mjl-/mox/mlog"
19 "github.com/mjl-/mox/mox-"
20 "github.com/mjl-/mox/mtastsdb"
21 "github.com/mjl-/mox/queue"
22 "github.com/mjl-/mox/smtp"
23 "github.com/mjl-/mox/store"
24 "github.com/mjl-/mox/tlsrptdb"
27var ctxbg = context.Background()
28var pkglog = mlog.New("ctl", nil)
30func tcheck(t *testing.T, err error, errmsg string) {
33 t.Fatalf("%s: %v", errmsg, err)
37// TestCtl executes commands through ctl. This tests at least the protocols (who
38// sends when/what) is tested. We often don't check the actual results, but
39// unhandled errors would cause a panic.
40func TestCtl(t *testing.T) {
41 os.RemoveAll("testdata/ctl/data")
42 mox.ConfigStaticPath = filepath.FromSlash("testdata/ctl/mox.conf")
43 mox.ConfigDynamicPath = filepath.FromSlash("testdata/ctl/domains.conf")
44 if errs := mox.LoadConfig(ctxbg, pkglog, true, false); len(errs) > 0 {
45 t.Fatalf("loading mox config: %v", errs)
47 defer store.Switchboard()()
50 tcheck(t, err, "queue init")
52 testctl := func(fn func(clientctl *ctl)) {
55 cconn, sconn := net.Pipe()
56 clientctl := ctl{conn: cconn, log: pkglog}
57 serverctl := ctl{conn: sconn, log: pkglog}
58 go servectlcmd(ctxbg, &serverctl, func() {})
65 testctl(func(ctl *ctl) {
66 ctlcmdDeliver(ctl, "mjl@mox.example")
69 // "setaccountpassword"
70 testctl(func(ctl *ctl) {
71 ctlcmdSetaccountpassword(ctl, "mjl", "test4321")
74 testctl(func(ctl *ctl) {
75 ctlcmdQueueHoldrulesList(ctl)
79 testctl(func(ctl *ctl) {
80 ctlcmdQueueHoldrulesAdd(ctl, "", "", "")
82 testctl(func(ctl *ctl) {
83 ctlcmdQueueHoldrulesAdd(ctl, "mjl", "", "")
85 testctl(func(ctl *ctl) {
86 ctlcmdQueueHoldrulesAdd(ctl, "", "☺.mox.example", "")
88 testctl(func(ctl *ctl) {
89 ctlcmdQueueHoldrulesAdd(ctl, "mox", "☺.mox.example", "example.com")
92 testctl(func(ctl *ctl) {
93 ctlcmdQueueHoldrulesRemove(ctl, 1)
96 // Queue a message to list/change/dump.
97 msg := "Subject: subject\r\n\r\nbody\r\n"
98 msgFile, err := store.CreateMessageTemp(pkglog, "queuedump-test")
99 tcheck(t, err, "temp file")
100 _, err = msgFile.Write([]byte(msg))
101 tcheck(t, err, "write message")
102 _, err = msgFile.Seek(0, 0)
103 tcheck(t, err, "rewind message")
104 defer os.Remove(msgFile.Name())
105 defer msgFile.Close()
106 addr, err := smtp.ParseAddress("mjl@mox.example")
107 tcheck(t, err, "parse address")
108 qml := []queue.Msg{queue.MakeMsg(addr.Path(), addr.Path(), false, false, int64(len(msg)), "<random@localhost>", nil, nil, time.Now(), "subject")}
109 queue.Add(ctxbg, pkglog, "mjl", msgFile, qml...)
113 testctl(func(ctl *ctl) {
114 ctlcmdQueueHoldrulesList(ctl)
118 testctl(func(ctl *ctl) {
119 ctlcmdQueueList(ctl, queue.Filter{}, queue.Sort{})
123 testctl(func(ctl *ctl) {
124 ctlcmdQueueHoldSet(ctl, queue.Filter{}, true)
126 testctl(func(ctl *ctl) {
127 ctlcmdQueueHoldSet(ctl, queue.Filter{}, false)
131 testctl(func(ctl *ctl) {
132 ctlcmdQueueSchedule(ctl, queue.Filter{}, true, time.Minute)
136 testctl(func(ctl *ctl) {
137 ctlcmdQueueTransport(ctl, queue.Filter{}, "socks")
141 testctl(func(ctl *ctl) {
142 ctlcmdQueueRequireTLS(ctl, queue.Filter{}, nil)
146 testctl(func(ctl *ctl) {
147 ctlcmdQueueDump(ctl, fmt.Sprintf("%d", qmid))
151 testctl(func(ctl *ctl) {
152 ctlcmdQueueFail(ctl, queue.Filter{})
156 testctl(func(ctl *ctl) {
157 ctlcmdQueueDrop(ctl, queue.Filter{})
160 // "queueholdruleslist"
161 testctl(func(ctl *ctl) {
162 ctlcmdQueueHoldrulesList(ctl)
165 // "queueholdrulesadd"
166 testctl(func(ctl *ctl) {
167 ctlcmdQueueHoldrulesAdd(ctl, "mjl", "", "")
169 testctl(func(ctl *ctl) {
170 ctlcmdQueueHoldrulesAdd(ctl, "mjl", "localhost", "")
173 // "queueholdrulesremove"
174 testctl(func(ctl *ctl) {
175 ctlcmdQueueHoldrulesRemove(ctl, 2)
177 testctl(func(ctl *ctl) {
178 ctlcmdQueueHoldrulesList(ctl)
181 // "queuesuppresslist"
182 testctl(func(ctl *ctl) {
183 ctlcmdQueueSuppressList(ctl, "mjl")
186 // "queuesuppressadd"
187 testctl(func(ctl *ctl) {
188 ctlcmdQueueSuppressAdd(ctl, "mjl", "base@localhost")
190 testctl(func(ctl *ctl) {
191 ctlcmdQueueSuppressAdd(ctl, "mjl", "other@localhost")
194 // "queuesuppresslookup"
195 testctl(func(ctl *ctl) {
196 ctlcmdQueueSuppressLookup(ctl, "mjl", "base@localhost")
199 // "queuesuppressremove"
200 testctl(func(ctl *ctl) {
201 ctlcmdQueueSuppressRemove(ctl, "mjl", "base@localhost")
203 testctl(func(ctl *ctl) {
204 ctlcmdQueueSuppressList(ctl, "mjl")
207 // "queueretiredlist"
208 testctl(func(ctl *ctl) {
209 ctlcmdQueueRetiredList(ctl, queue.RetiredFilter{}, queue.RetiredSort{})
212 // "queueretiredprint"
213 testctl(func(ctl *ctl) {
214 ctlcmdQueueRetiredPrint(ctl, "1")
218 testctl(func(ctl *ctl) {
219 ctlcmdQueueHookList(ctl, queue.HookFilter{}, queue.HookSort{})
222 // "queuehookschedule"
223 testctl(func(ctl *ctl) {
224 ctlcmdQueueHookSchedule(ctl, queue.HookFilter{}, true, time.Minute)
228 testctl(func(ctl *ctl) {
229 ctlcmdQueueHookPrint(ctl, "1")
233 testctl(func(ctl *ctl) {
234 ctlcmdQueueHookCancel(ctl, queue.HookFilter{})
237 // "queuehookretiredlist"
238 testctl(func(ctl *ctl) {
239 ctlcmdQueueHookRetiredList(ctl, queue.HookRetiredFilter{}, queue.HookRetiredSort{})
242 // "queuehookretiredprint"
243 testctl(func(ctl *ctl) {
244 ctlcmdQueueHookRetiredPrint(ctl, "1")
248 testctl(func(ctl *ctl) {
249 ctlcmdImport(ctl, true, "mjl", "inbox", "testdata/importtest.mbox")
253 testctl(func(ctl *ctl) {
254 ctlcmdImport(ctl, false, "mjl", "inbox", "testdata/importtest.maildir")
258 testctl(func(ctl *ctl) {
259 ctlcmdConfigDomainAdd(ctl, dns.Domain{ASCII: "mox2.example"}, "mjl", "")
263 testctl(func(ctl *ctl) {
264 ctlcmdConfigAccountAdd(ctl, "mjl2", "mjl2@mox2.example")
268 testctl(func(ctl *ctl) {
269 ctlcmdConfigAddressAdd(ctl, "mjl3@mox2.example", "mjl2")
273 testctl(func(ctl *ctl) {
274 ctlcmdDeliver(ctl, "mjl3@mox2.example")
276 // "retrain", retrain junk filter.
277 testctl(func(ctl *ctl) {
278 ctlcmdRetrain(ctl, "mjl2")
282 testctl(func(ctl *ctl) {
283 ctlcmdConfigAddressRemove(ctl, "mjl3@mox2.example")
287 testctl(func(ctl *ctl) {
288 ctlcmdConfigAccountRemove(ctl, "mjl2")
292 testctl(func(ctl *ctl) {
293 ctlcmdConfigDomainRemove(ctl, dns.Domain{ASCII: "mox2.example"})
297 testctl(func(ctl *ctl) {
298 ctlcmdConfigAliasAdd(ctl, "support@mox.example", config.Alias{Addresses: []string{"mjl@mox.example"}})
302 testctl(func(ctl *ctl) {
303 ctlcmdConfigAliasList(ctl, "mox.example")
307 testctl(func(ctl *ctl) {
308 ctlcmdConfigAliasPrint(ctl, "support@mox.example")
312 testctl(func(ctl *ctl) {
313 ctlcmdConfigAliasUpdate(ctl, "support@mox.example", "true", "true", "true")
317 testctl(func(ctl *ctl) {
318 ctlcmdConfigAliasAddaddr(ctl, "support@mox.example", []string{"mjl2@mox.example"})
322 testctl(func(ctl *ctl) {
323 ctlcmdConfigAliasRmaddr(ctl, "support@mox.example", []string{"mjl2@mox.example"})
327 testctl(func(ctl *ctl) {
328 ctlcmdConfigAliasRemove(ctl, "support@mox.example")
332 testctl(func(ctl *ctl) {
337 testctl(func(ctl *ctl) {
338 ctlcmdSetLoglevels(ctl, "", "debug")
340 testctl(func(ctl *ctl) {
341 ctlcmdSetLoglevels(ctl, "smtpserver", "debug")
344 // Export data, import it again
345 xcmdExport(true, false, []string{filepath.FromSlash("testdata/ctl/data/tmp/export/mbox/"), filepath.FromSlash("testdata/ctl/data/accounts/mjl")}, &cmd{log: pkglog})
346 xcmdExport(false, false, []string{filepath.FromSlash("testdata/ctl/data/tmp/export/maildir/"), filepath.FromSlash("testdata/ctl/data/accounts/mjl")}, &cmd{log: pkglog})
347 testctl(func(ctl *ctl) {
348 ctlcmdImport(ctl, true, "mjl", "inbox", filepath.FromSlash("testdata/ctl/data/tmp/export/mbox/Inbox.mbox"))
350 testctl(func(ctl *ctl) {
351 ctlcmdImport(ctl, false, "mjl", "inbox", filepath.FromSlash("testdata/ctl/data/tmp/export/maildir/Inbox"))
354 // "recalculatemailboxcounts"
355 testctl(func(ctl *ctl) {
356 ctlcmdRecalculateMailboxCounts(ctl, "mjl")
360 testctl(func(ctl *ctl) {
361 ctlcmdFixmsgsize(ctl, "mjl")
363 testctl(func(ctl *ctl) {
364 acc, err := store.OpenAccount(ctl.log, "mjl")
365 tcheck(t, err, "open account")
371 content := []byte("Subject: hi\r\n\r\nbody\r\n")
373 deliver := func(m *store.Message) {
375 m.Size = int64(len(content))
376 msgf, err := store.CreateMessageTemp(ctl.log, "ctltest")
377 tcheck(t, err, "create temp file")
378 defer os.Remove(msgf.Name())
380 _, err = msgf.Write(content)
381 tcheck(t, err, "write message file")
382 err = acc.DeliverMailbox(ctl.log, "Inbox", m, msgf)
383 tcheck(t, err, "deliver message")
386 var msgBadSize store.Message
390 err = acc.DB.Update(ctxbg, &msgBadSize)
391 tcheck(t, err, "update message to bad size")
392 mb := store.Mailbox{ID: msgBadSize.MailboxID}
393 err = acc.DB.Get(ctxbg, &mb)
394 tcheck(t, err, "get db")
395 mb.Size -= int64(len(content))
397 err = acc.DB.Update(ctxbg, &mb)
398 tcheck(t, err, "update mailbox size")
401 ctlcmdFixmsgsize(ctl, "")
403 err = acc.DB.Get(ctxbg, &msgBadSize)
404 tcheck(t, err, "get message")
405 if msgBadSize.Size != int64(len(content)) {
406 t.Fatalf("after fixing, message size is %d, should be %d", msgBadSize.Size, len(content))
411 testctl(func(ctl *ctl) {
412 ctlcmdReparse(ctl, "mjl")
414 testctl(func(ctl *ctl) {
415 ctlcmdReparse(ctl, "")
419 testctl(func(ctl *ctl) {
420 ctlcmdReassignthreads(ctl, "mjl")
422 testctl(func(ctl *ctl) {
423 ctlcmdReassignthreads(ctl, "")
426 // "backup", backup account.
428 tcheck(t, err, "dmarcdb init")
429 err = mtastsdb.Init(false)
430 tcheck(t, err, "mtastsdb init")
431 err = tlsrptdb.Init()
432 tcheck(t, err, "tlsrptdb init")
433 testctl(func(ctl *ctl) {
434 os.RemoveAll("testdata/ctl/data/tmp/backup-data")
435 err := os.WriteFile("testdata/ctl/data/receivedid.key", make([]byte, 16), 0600)
436 tcheck(t, err, "writing receivedid.key")
437 ctlcmdBackup(ctl, filepath.FromSlash("testdata/ctl/data/tmp/backup-data"), false)
440 // Verify the backup.
442 flag: flag.NewFlagSet("", flag.ExitOnError),
443 flagArgs: []string{filepath.FromSlash("testdata/ctl/data/tmp/backup-data")},