8 cryptorand "crypto/rand"
19 "github.com/mjl-/mox/config"
20 "github.com/mjl-/mox/dmarcdb"
21 "github.com/mjl-/mox/dns"
22 "github.com/mjl-/mox/mlog"
23 "github.com/mjl-/mox/mox-"
24 "github.com/mjl-/mox/mtastsdb"
25 "github.com/mjl-/mox/queue"
26 "github.com/mjl-/mox/smtp"
27 "github.com/mjl-/mox/store"
28 "github.com/mjl-/mox/tlsrptdb"
31var ctxbg = context.Background()
32var pkglog = mlog.New("ctl", nil)
34func tcheck(t *testing.T, err error, errmsg string) {
37 t.Fatalf("%s: %v", errmsg, err)
41// TestCtl executes commands through ctl. This tests at least the protocols (who
42// sends when/what) is tested. We often don't check the actual results, but
43// unhandled errors would cause a panic.
44func TestCtl(t *testing.T) {
45 os.RemoveAll("testdata/ctl/data")
46 mox.ConfigStaticPath = filepath.FromSlash("testdata/ctl/mox.conf")
47 mox.ConfigDynamicPath = filepath.FromSlash("testdata/ctl/domains.conf")
48 if errs := mox.LoadConfig(ctxbg, pkglog, true, false); len(errs) > 0 {
49 t.Fatalf("loading mox config: %v", errs)
51 defer store.Switchboard()()
54 tcheck(t, err, "queue init")
55 defer queue.Shutdown()
57 err = store.Init(ctxbg)
58 tcheck(t, err, "store init")
61 testctl := func(fn func(clientctl *ctl)) {
64 cconn, sconn := net.Pipe()
65 clientctl := ctl{conn: cconn, log: pkglog}
66 serverctl := ctl{conn: sconn, log: pkglog}
67 done := make(chan struct{})
69 servectlcmd(ctxbg, &serverctl, func() {})
79 testctl(func(ctl *ctl) {
80 ctlcmdDeliver(ctl, "mjl@mox.example")
83 // "setaccountpassword"
84 testctl(func(ctl *ctl) {
85 ctlcmdSetaccountpassword(ctl, "mjl", "test4321")
88 testctl(func(ctl *ctl) {
89 ctlcmdQueueHoldrulesList(ctl)
93 testctl(func(ctl *ctl) {
94 ctlcmdQueueHoldrulesAdd(ctl, "", "", "")
96 testctl(func(ctl *ctl) {
97 ctlcmdQueueHoldrulesAdd(ctl, "mjl", "", "")
99 testctl(func(ctl *ctl) {
100 ctlcmdQueueHoldrulesAdd(ctl, "", "☺.mox.example", "")
102 testctl(func(ctl *ctl) {
103 ctlcmdQueueHoldrulesAdd(ctl, "mox", "☺.mox.example", "example.com")
106 testctl(func(ctl *ctl) {
107 ctlcmdQueueHoldrulesRemove(ctl, 1)
110 // Queue a message to list/change/dump.
111 msg := "Subject: subject\r\n\r\nbody\r\n"
112 msgFile, err := store.CreateMessageTemp(pkglog, "queuedump-test")
113 tcheck(t, err, "temp file")
114 _, err = msgFile.Write([]byte(msg))
115 tcheck(t, err, "write message")
116 _, err = msgFile.Seek(0, 0)
117 tcheck(t, err, "rewind message")
118 defer os.Remove(msgFile.Name())
119 defer msgFile.Close()
120 addr, err := smtp.ParseAddress("mjl@mox.example")
121 tcheck(t, err, "parse address")
122 qml := []queue.Msg{queue.MakeMsg(addr.Path(), addr.Path(), false, false, int64(len(msg)), "<random@localhost>", nil, nil, time.Now(), "subject")}
123 queue.Add(ctxbg, pkglog, "mjl", msgFile, qml...)
127 testctl(func(ctl *ctl) {
128 ctlcmdQueueHoldrulesList(ctl)
132 testctl(func(ctl *ctl) {
133 ctlcmdQueueList(ctl, queue.Filter{}, queue.Sort{})
137 testctl(func(ctl *ctl) {
138 ctlcmdQueueHoldSet(ctl, queue.Filter{}, true)
140 testctl(func(ctl *ctl) {
141 ctlcmdQueueHoldSet(ctl, queue.Filter{}, false)
145 testctl(func(ctl *ctl) {
146 ctlcmdQueueSchedule(ctl, queue.Filter{}, true, time.Minute)
150 testctl(func(ctl *ctl) {
151 ctlcmdQueueTransport(ctl, queue.Filter{}, "socks")
155 testctl(func(ctl *ctl) {
156 ctlcmdQueueRequireTLS(ctl, queue.Filter{}, nil)
160 testctl(func(ctl *ctl) {
161 ctlcmdQueueDump(ctl, fmt.Sprintf("%d", qmid))
165 testctl(func(ctl *ctl) {
166 ctlcmdQueueFail(ctl, queue.Filter{})
170 testctl(func(ctl *ctl) {
171 ctlcmdQueueDrop(ctl, queue.Filter{})
174 // "queueholdruleslist"
175 testctl(func(ctl *ctl) {
176 ctlcmdQueueHoldrulesList(ctl)
179 // "queueholdrulesadd"
180 testctl(func(ctl *ctl) {
181 ctlcmdQueueHoldrulesAdd(ctl, "mjl", "", "")
183 testctl(func(ctl *ctl) {
184 ctlcmdQueueHoldrulesAdd(ctl, "mjl", "localhost", "")
187 // "queueholdrulesremove"
188 testctl(func(ctl *ctl) {
189 ctlcmdQueueHoldrulesRemove(ctl, 2)
191 testctl(func(ctl *ctl) {
192 ctlcmdQueueHoldrulesList(ctl)
195 // "queuesuppresslist"
196 testctl(func(ctl *ctl) {
197 ctlcmdQueueSuppressList(ctl, "mjl")
200 // "queuesuppressadd"
201 testctl(func(ctl *ctl) {
202 ctlcmdQueueSuppressAdd(ctl, "mjl", "base@localhost")
204 testctl(func(ctl *ctl) {
205 ctlcmdQueueSuppressAdd(ctl, "mjl", "other@localhost")
208 // "queuesuppresslookup"
209 testctl(func(ctl *ctl) {
210 ctlcmdQueueSuppressLookup(ctl, "mjl", "base@localhost")
213 // "queuesuppressremove"
214 testctl(func(ctl *ctl) {
215 ctlcmdQueueSuppressRemove(ctl, "mjl", "base@localhost")
217 testctl(func(ctl *ctl) {
218 ctlcmdQueueSuppressList(ctl, "mjl")
221 // "queueretiredlist"
222 testctl(func(ctl *ctl) {
223 ctlcmdQueueRetiredList(ctl, queue.RetiredFilter{}, queue.RetiredSort{})
226 // "queueretiredprint"
227 testctl(func(ctl *ctl) {
228 ctlcmdQueueRetiredPrint(ctl, "1")
232 testctl(func(ctl *ctl) {
233 ctlcmdQueueHookList(ctl, queue.HookFilter{}, queue.HookSort{})
236 // "queuehookschedule"
237 testctl(func(ctl *ctl) {
238 ctlcmdQueueHookSchedule(ctl, queue.HookFilter{}, true, time.Minute)
242 testctl(func(ctl *ctl) {
243 ctlcmdQueueHookPrint(ctl, "1")
247 testctl(func(ctl *ctl) {
248 ctlcmdQueueHookCancel(ctl, queue.HookFilter{})
251 // "queuehookretiredlist"
252 testctl(func(ctl *ctl) {
253 ctlcmdQueueHookRetiredList(ctl, queue.HookRetiredFilter{}, queue.HookRetiredSort{})
256 // "queuehookretiredprint"
257 testctl(func(ctl *ctl) {
258 ctlcmdQueueHookRetiredPrint(ctl, "1")
262 testctl(func(ctl *ctl) {
263 ctlcmdImport(ctl, true, "mjl", "inbox", "testdata/importtest.mbox")
267 testctl(func(ctl *ctl) {
268 ctlcmdImport(ctl, false, "mjl", "inbox", "testdata/importtest.maildir")
272 testctl(func(ctl *ctl) {
273 ctlcmdConfigDomainAdd(ctl, dns.Domain{ASCII: "mox2.example"}, "mjl", "")
277 testctl(func(ctl *ctl) {
278 ctlcmdConfigAccountAdd(ctl, "mjl2", "mjl2@mox2.example")
282 testctl(func(ctl *ctl) {
283 ctlcmdConfigAddressAdd(ctl, "mjl3@mox2.example", "mjl2")
287 testctl(func(ctl *ctl) {
288 ctlcmdDeliver(ctl, "mjl3@mox2.example")
290 // "retrain", retrain junk filter.
291 testctl(func(ctl *ctl) {
292 ctlcmdRetrain(ctl, "mjl2")
296 testctl(func(ctl *ctl) {
297 ctlcmdConfigAddressRemove(ctl, "mjl3@mox2.example")
301 testctl(func(ctl *ctl) {
302 ctlcmdConfigAccountRemove(ctl, "mjl2")
306 testctl(func(ctl *ctl) {
307 ctlcmdConfigDomainRemove(ctl, dns.Domain{ASCII: "mox2.example"})
311 testctl(func(ctl *ctl) {
312 ctlcmdConfigAliasAdd(ctl, "support@mox.example", config.Alias{Addresses: []string{"mjl@mox.example"}})
316 testctl(func(ctl *ctl) {
317 ctlcmdConfigAliasList(ctl, "mox.example")
321 testctl(func(ctl *ctl) {
322 ctlcmdConfigAliasPrint(ctl, "support@mox.example")
326 testctl(func(ctl *ctl) {
327 ctlcmdConfigAliasUpdate(ctl, "support@mox.example", "true", "true", "true")
331 testctl(func(ctl *ctl) {
332 ctlcmdConfigAliasAddaddr(ctl, "support@mox.example", []string{"mjl2@mox.example"})
336 testctl(func(ctl *ctl) {
337 ctlcmdConfigAliasRmaddr(ctl, "support@mox.example", []string{"mjl2@mox.example"})
341 testctl(func(ctl *ctl) {
342 ctlcmdConfigAliasRemove(ctl, "support@mox.example")
345 // accounttlspubkeyadd
346 certDER := fakeCert(t)
347 testctl(func(ctl *ctl) {
348 ctlcmdConfigTlspubkeyAdd(ctl, "mjl@mox.example", "testkey", false, certDER)
351 // "accounttlspubkeylist"
352 testctl(func(ctl *ctl) {
353 ctlcmdConfigTlspubkeyList(ctl, "")
355 testctl(func(ctl *ctl) {
356 ctlcmdConfigTlspubkeyList(ctl, "mjl")
359 tpkl, err := store.TLSPublicKeyList(ctxbg, "")
360 tcheck(t, err, "list tls public keys")
362 t.Fatalf("got %d tls public keys, expected 1", len(tpkl))
364 fingerprint := tpkl[0].Fingerprint
366 // "accounttlspubkeyget"
367 testctl(func(ctl *ctl) {
368 ctlcmdConfigTlspubkeyGet(ctl, fingerprint)
371 // "accounttlspubkeyrm"
372 testctl(func(ctl *ctl) {
373 ctlcmdConfigTlspubkeyRemove(ctl, fingerprint)
376 tpkl, err = store.TLSPublicKeyList(ctxbg, "")
377 tcheck(t, err, "list tls public keys")
379 t.Fatalf("got %d tls public keys, expected 0", len(tpkl))
383 testctl(func(ctl *ctl) {
388 testctl(func(ctl *ctl) {
389 ctlcmdSetLoglevels(ctl, "", "debug")
391 testctl(func(ctl *ctl) {
392 ctlcmdSetLoglevels(ctl, "smtpserver", "debug")
395 // Export data, import it again
396 xcmdExport(true, false, []string{filepath.FromSlash("testdata/ctl/data/tmp/export/mbox/"), filepath.FromSlash("testdata/ctl/data/accounts/mjl")}, &cmd{log: pkglog})
397 xcmdExport(false, false, []string{filepath.FromSlash("testdata/ctl/data/tmp/export/maildir/"), filepath.FromSlash("testdata/ctl/data/accounts/mjl")}, &cmd{log: pkglog})
398 testctl(func(ctl *ctl) {
399 ctlcmdImport(ctl, true, "mjl", "inbox", filepath.FromSlash("testdata/ctl/data/tmp/export/mbox/Inbox.mbox"))
401 testctl(func(ctl *ctl) {
402 ctlcmdImport(ctl, false, "mjl", "inbox", filepath.FromSlash("testdata/ctl/data/tmp/export/maildir/Inbox"))
405 // "recalculatemailboxcounts"
406 testctl(func(ctl *ctl) {
407 ctlcmdRecalculateMailboxCounts(ctl, "mjl")
411 testctl(func(ctl *ctl) {
412 ctlcmdFixmsgsize(ctl, "mjl")
414 testctl(func(ctl *ctl) {
415 acc, err := store.OpenAccount(ctl.log, "mjl")
416 tcheck(t, err, "open account")
422 content := []byte("Subject: hi\r\n\r\nbody\r\n")
424 deliver := func(m *store.Message) {
426 m.Size = int64(len(content))
427 msgf, err := store.CreateMessageTemp(ctl.log, "ctltest")
428 tcheck(t, err, "create temp file")
429 defer os.Remove(msgf.Name())
431 _, err = msgf.Write(content)
432 tcheck(t, err, "write message file")
433 err = acc.DeliverMailbox(ctl.log, "Inbox", m, msgf)
434 tcheck(t, err, "deliver message")
437 var msgBadSize store.Message
441 err = acc.DB.Update(ctxbg, &msgBadSize)
442 tcheck(t, err, "update message to bad size")
443 mb := store.Mailbox{ID: msgBadSize.MailboxID}
444 err = acc.DB.Get(ctxbg, &mb)
445 tcheck(t, err, "get db")
446 mb.Size -= int64(len(content))
448 err = acc.DB.Update(ctxbg, &mb)
449 tcheck(t, err, "update mailbox size")
452 ctlcmdFixmsgsize(ctl, "")
454 err = acc.DB.Get(ctxbg, &msgBadSize)
455 tcheck(t, err, "get message")
456 if msgBadSize.Size != int64(len(content)) {
457 t.Fatalf("after fixing, message size is %d, should be %d", msgBadSize.Size, len(content))
462 testctl(func(ctl *ctl) {
463 ctlcmdReparse(ctl, "mjl")
465 testctl(func(ctl *ctl) {
466 ctlcmdReparse(ctl, "")
470 testctl(func(ctl *ctl) {
471 ctlcmdReassignthreads(ctl, "mjl")
473 testctl(func(ctl *ctl) {
474 ctlcmdReassignthreads(ctl, "")
477 // "backup", backup account.
479 tcheck(t, err, "dmarcdb init")
480 defer dmarcdb.Close()
481 err = mtastsdb.Init(false)
482 tcheck(t, err, "mtastsdb init")
483 defer mtastsdb.Close()
484 err = tlsrptdb.Init()
485 tcheck(t, err, "tlsrptdb init")
486 defer tlsrptdb.Close()
487 testctl(func(ctl *ctl) {
488 os.RemoveAll("testdata/ctl/data/tmp/backup-data")
489 err := os.WriteFile("testdata/ctl/data/receivedid.key", make([]byte, 16), 0600)
490 tcheck(t, err, "writing receivedid.key")
491 ctlcmdBackup(ctl, filepath.FromSlash("testdata/ctl/data/tmp/backup-data"), false)
494 // Verify the backup.
496 flag: flag.NewFlagSet("", flag.ExitOnError),
497 flagArgs: []string{filepath.FromSlash("testdata/ctl/data/tmp/backup-data")},
502func fakeCert(t *testing.T) []byte {
504 seed := make([]byte, ed25519.SeedSize)
505 privKey := ed25519.NewKeyFromSeed(seed) // Fake key, don't use this for real!
506 template := &x509.Certificate{
507 SerialNumber: big.NewInt(1), // Required field...
509 localCertBuf, err := x509.CreateCertificate(cryptorand.Reader, template, template, privKey.Public(), privKey)
510 tcheck(t, err, "making certificate")