1//go:build !integration
2
3package main
4
5import (
6 "context"
7 "crypto/ed25519"
8 cryptorand "crypto/rand"
9 "crypto/x509"
10 "flag"
11 "fmt"
12 "log/slog"
13 "math/big"
14 "net"
15 "os"
16 "path/filepath"
17 "testing"
18 "time"
19
20 "github.com/mjl-/mox/config"
21 "github.com/mjl-/mox/dmarcdb"
22 "github.com/mjl-/mox/dns"
23 "github.com/mjl-/mox/imapclient"
24 "github.com/mjl-/mox/mlog"
25 "github.com/mjl-/mox/mox-"
26 "github.com/mjl-/mox/mtastsdb"
27 "github.com/mjl-/mox/queue"
28 "github.com/mjl-/mox/smtp"
29 "github.com/mjl-/mox/store"
30 "github.com/mjl-/mox/tlsrptdb"
31)
32
33var ctxbg = context.Background()
34var pkglog = mlog.New("ctl", nil)
35
36func tcheck(t *testing.T, err error, errmsg string) {
37 if err != nil {
38 t.Helper()
39 t.Fatalf("%s: %v", errmsg, err)
40 }
41}
42
43// TestCtl executes commands through ctl. This tests at least the protocols (who
44// sends when/what) is tested. We often don't check the actual results, but
45// unhandled errors would cause a panic.
46func TestCtl(t *testing.T) {
47 os.RemoveAll("testdata/ctl/data")
48 mox.ConfigStaticPath = filepath.FromSlash("testdata/ctl/config/mox.conf")
49 mox.ConfigDynamicPath = filepath.FromSlash("testdata/ctl/config/domains.conf")
50 if errs := mox.LoadConfig(ctxbg, pkglog, true, false); len(errs) > 0 {
51 t.Fatalf("loading mox config: %v", errs)
52 }
53 err := store.Init(ctxbg)
54 tcheck(t, err, "store init")
55 defer store.Close()
56 defer store.Switchboard()()
57
58 err = queue.Init()
59 tcheck(t, err, "queue init")
60 defer queue.Shutdown()
61
62 var cid int64
63
64 testctl := func(fn func(clientxctl *ctl)) {
65 t.Helper()
66
67 cconn, sconn := net.Pipe()
68 clientxctl := ctl{conn: cconn, log: pkglog}
69 serverxctl := ctl{conn: sconn, log: pkglog}
70 done := make(chan struct{})
71 go func() {
72 cid++
73 servectlcmd(ctxbg, &serverxctl, cid, func() {})
74 close(done)
75 }()
76 fn(&clientxctl)
77 cconn.Close()
78 <-done
79 sconn.Close()
80 }
81
82 // "deliver"
83 testctl(func(xctl *ctl) {
84 ctlcmdDeliver(xctl, "mjl@mox.example")
85 })
86
87 // "setaccountpassword"
88 testctl(func(xctl *ctl) {
89 ctlcmdSetaccountpassword(xctl, "mjl", "test4321")
90 })
91
92 testctl(func(xctl *ctl) {
93 ctlcmdQueueHoldrulesList(xctl)
94 })
95
96 // All messages.
97 testctl(func(xctl *ctl) {
98 ctlcmdQueueHoldrulesAdd(xctl, "", "", "")
99 })
100 testctl(func(xctl *ctl) {
101 ctlcmdQueueHoldrulesAdd(xctl, "mjl", "", "")
102 })
103 testctl(func(xctl *ctl) {
104 ctlcmdQueueHoldrulesAdd(xctl, "", "☺.mox.example", "")
105 })
106 testctl(func(xctl *ctl) {
107 ctlcmdQueueHoldrulesAdd(xctl, "mox", "☺.mox.example", "example.com")
108 })
109
110 testctl(func(xctl *ctl) {
111 ctlcmdQueueHoldrulesRemove(xctl, 1)
112 })
113
114 // Queue a message to list/change/dump.
115 msg := "Subject: subject\r\n\r\nbody\r\n"
116 msgFile, err := store.CreateMessageTemp(pkglog, "queuedump-test")
117 tcheck(t, err, "temp file")
118 _, err = msgFile.Write([]byte(msg))
119 tcheck(t, err, "write message")
120 _, err = msgFile.Seek(0, 0)
121 tcheck(t, err, "rewind message")
122 defer os.Remove(msgFile.Name())
123 defer msgFile.Close()
124 addr, err := smtp.ParseAddress("mjl@mox.example")
125 tcheck(t, err, "parse address")
126 qml := []queue.Msg{queue.MakeMsg(addr.Path(), addr.Path(), false, false, int64(len(msg)), "<random@localhost>", nil, nil, time.Now(), "subject")}
127 queue.Add(ctxbg, pkglog, "mjl", msgFile, qml...)
128 qmid := qml[0].ID
129
130 // Has entries now.
131 testctl(func(xctl *ctl) {
132 ctlcmdQueueHoldrulesList(xctl)
133 })
134
135 // "queuelist"
136 testctl(func(xctl *ctl) {
137 ctlcmdQueueList(xctl, queue.Filter{}, queue.Sort{})
138 })
139
140 // "queueholdset"
141 testctl(func(xctl *ctl) {
142 ctlcmdQueueHoldSet(xctl, queue.Filter{}, true)
143 })
144 testctl(func(xctl *ctl) {
145 ctlcmdQueueHoldSet(xctl, queue.Filter{}, false)
146 })
147
148 // "queueschedule"
149 testctl(func(xctl *ctl) {
150 ctlcmdQueueSchedule(xctl, queue.Filter{}, true, time.Minute)
151 })
152
153 // "queuetransport"
154 testctl(func(xctl *ctl) {
155 ctlcmdQueueTransport(xctl, queue.Filter{}, "socks")
156 })
157
158 // "queuerequiretls"
159 testctl(func(xctl *ctl) {
160 ctlcmdQueueRequireTLS(xctl, queue.Filter{}, nil)
161 })
162
163 // "queuedump"
164 testctl(func(xctl *ctl) {
165 ctlcmdQueueDump(xctl, fmt.Sprintf("%d", qmid))
166 })
167
168 // "queuefail"
169 testctl(func(xctl *ctl) {
170 ctlcmdQueueFail(xctl, queue.Filter{})
171 })
172
173 // "queuedrop"
174 testctl(func(xctl *ctl) {
175 ctlcmdQueueDrop(xctl, queue.Filter{})
176 })
177
178 // "queueholdruleslist"
179 testctl(func(xctl *ctl) {
180 ctlcmdQueueHoldrulesList(xctl)
181 })
182
183 // "queueholdrulesadd"
184 testctl(func(xctl *ctl) {
185 ctlcmdQueueHoldrulesAdd(xctl, "mjl", "", "")
186 })
187 testctl(func(xctl *ctl) {
188 ctlcmdQueueHoldrulesAdd(xctl, "mjl", "localhost", "")
189 })
190
191 // "queueholdrulesremove"
192 testctl(func(xctl *ctl) {
193 ctlcmdQueueHoldrulesRemove(xctl, 2)
194 })
195 testctl(func(xctl *ctl) {
196 ctlcmdQueueHoldrulesList(xctl)
197 })
198
199 // "queuesuppresslist"
200 testctl(func(xctl *ctl) {
201 ctlcmdQueueSuppressList(xctl, "mjl")
202 })
203
204 // "queuesuppressadd"
205 testctl(func(xctl *ctl) {
206 ctlcmdQueueSuppressAdd(xctl, "mjl", "base@localhost")
207 })
208 testctl(func(xctl *ctl) {
209 ctlcmdQueueSuppressAdd(xctl, "mjl", "other@localhost")
210 })
211
212 // "queuesuppresslookup"
213 testctl(func(xctl *ctl) {
214 ctlcmdQueueSuppressLookup(xctl, "mjl", "base@localhost")
215 })
216
217 // "queuesuppressremove"
218 testctl(func(xctl *ctl) {
219 ctlcmdQueueSuppressRemove(xctl, "mjl", "base@localhost")
220 })
221 testctl(func(xctl *ctl) {
222 ctlcmdQueueSuppressList(xctl, "mjl")
223 })
224
225 // "queueretiredlist"
226 testctl(func(xctl *ctl) {
227 ctlcmdQueueRetiredList(xctl, queue.RetiredFilter{}, queue.RetiredSort{})
228 })
229
230 // "queueretiredprint"
231 testctl(func(xctl *ctl) {
232 ctlcmdQueueRetiredPrint(xctl, "1")
233 })
234
235 // "queuehooklist"
236 testctl(func(xctl *ctl) {
237 ctlcmdQueueHookList(xctl, queue.HookFilter{}, queue.HookSort{})
238 })
239
240 // "queuehookschedule"
241 testctl(func(xctl *ctl) {
242 ctlcmdQueueHookSchedule(xctl, queue.HookFilter{}, true, time.Minute)
243 })
244
245 // "queuehookprint"
246 testctl(func(xctl *ctl) {
247 ctlcmdQueueHookPrint(xctl, "1")
248 })
249
250 // "queuehookcancel"
251 testctl(func(xctl *ctl) {
252 ctlcmdQueueHookCancel(xctl, queue.HookFilter{})
253 })
254
255 // "queuehookretiredlist"
256 testctl(func(xctl *ctl) {
257 ctlcmdQueueHookRetiredList(xctl, queue.HookRetiredFilter{}, queue.HookRetiredSort{})
258 })
259
260 // "queuehookretiredprint"
261 testctl(func(xctl *ctl) {
262 ctlcmdQueueHookRetiredPrint(xctl, "1")
263 })
264
265 // "importmbox"
266 testctl(func(xctl *ctl) {
267 ctlcmdImport(xctl, true, "mjl", "inbox", "testdata/importtest.mbox")
268 })
269
270 // "importmaildir"
271 testctl(func(xctl *ctl) {
272 ctlcmdImport(xctl, false, "mjl", "inbox", "testdata/importtest.maildir")
273 })
274
275 // "domainadd"
276 testctl(func(xctl *ctl) {
277 ctlcmdConfigDomainAdd(xctl, false, dns.Domain{ASCII: "mox2.example"}, "mjl", "")
278 })
279
280 // "accountadd"
281 testctl(func(xctl *ctl) {
282 ctlcmdConfigAccountAdd(xctl, "mjl2", "mjl2@mox2.example")
283 })
284
285 // "addressadd"
286 testctl(func(xctl *ctl) {
287 ctlcmdConfigAddressAdd(xctl, "mjl3@mox2.example", "mjl2")
288 })
289
290 // Add a message.
291 testctl(func(xctl *ctl) {
292 ctlcmdDeliver(xctl, "mjl3@mox2.example")
293 })
294 // "retrain", retrain junk filter.
295 testctl(func(xctl *ctl) {
296 ctlcmdRetrain(xctl, "mjl2")
297 })
298
299 // "addressrm"
300 testctl(func(xctl *ctl) {
301 ctlcmdConfigAddressRemove(xctl, "mjl3@mox2.example")
302 })
303
304 // "accountdisabled"
305 testctl(func(xctl *ctl) {
306 ctlcmdConfigAccountDisabled(xctl, "mjl2", "testing")
307 })
308
309 // "accountlist"
310 testctl(func(xctl *ctl) {
311 ctlcmdConfigAccountList(xctl)
312 })
313
314 testctl(func(xctl *ctl) {
315 ctlcmdConfigAccountDisabled(xctl, "mjl2", "")
316 })
317
318 // "accountrm"
319 testctl(func(xctl *ctl) {
320 ctlcmdConfigAccountRemove(xctl, "mjl2")
321 })
322
323 // "domaindisabled"
324 testctl(func(xctl *ctl) {
325 ctlcmdConfigDomainDisabled(xctl, dns.Domain{ASCII: "mox2.example"}, true)
326 })
327 testctl(func(xctl *ctl) {
328 ctlcmdConfigDomainDisabled(xctl, dns.Domain{ASCII: "mox2.example"}, false)
329 })
330
331 // "domainrm"
332 testctl(func(xctl *ctl) {
333 ctlcmdConfigDomainRemove(xctl, dns.Domain{ASCII: "mox2.example"})
334 })
335
336 // "aliasadd"
337 testctl(func(xctl *ctl) {
338 ctlcmdConfigAliasAdd(xctl, "support@mox.example", config.Alias{Addresses: []string{"mjl@mox.example"}})
339 })
340
341 // "aliaslist"
342 testctl(func(xctl *ctl) {
343 ctlcmdConfigAliasList(xctl, "mox.example")
344 })
345
346 // "aliasprint"
347 testctl(func(xctl *ctl) {
348 ctlcmdConfigAliasPrint(xctl, "support@mox.example")
349 })
350
351 // "aliasupdate"
352 testctl(func(xctl *ctl) {
353 ctlcmdConfigAliasUpdate(xctl, "support@mox.example", "true", "true", "true")
354 })
355
356 // "aliasaddaddr"
357 testctl(func(xctl *ctl) {
358 ctlcmdConfigAliasAddaddr(xctl, "support@mox.example", []string{"mjl2@mox.example"})
359 })
360
361 // "aliasrmaddr"
362 testctl(func(xctl *ctl) {
363 ctlcmdConfigAliasRmaddr(xctl, "support@mox.example", []string{"mjl2@mox.example"})
364 })
365
366 // "aliasrm"
367 testctl(func(xctl *ctl) {
368 ctlcmdConfigAliasRemove(xctl, "support@mox.example")
369 })
370
371 // accounttlspubkeyadd
372 certDER := fakeCert(t)
373 testctl(func(xctl *ctl) {
374 ctlcmdConfigTlspubkeyAdd(xctl, "mjl@mox.example", "testkey", false, certDER)
375 })
376
377 // "accounttlspubkeylist"
378 testctl(func(xctl *ctl) {
379 ctlcmdConfigTlspubkeyList(xctl, "")
380 })
381 testctl(func(xctl *ctl) {
382 ctlcmdConfigTlspubkeyList(xctl, "mjl")
383 })
384
385 tpkl, err := store.TLSPublicKeyList(ctxbg, "")
386 tcheck(t, err, "list tls public keys")
387 if len(tpkl) != 1 {
388 t.Fatalf("got %d tls public keys, expected 1", len(tpkl))
389 }
390 fingerprint := tpkl[0].Fingerprint
391
392 // "accounttlspubkeyget"
393 testctl(func(xctl *ctl) {
394 ctlcmdConfigTlspubkeyGet(xctl, fingerprint)
395 })
396
397 // "accounttlspubkeyrm"
398 testctl(func(xctl *ctl) {
399 ctlcmdConfigTlspubkeyRemove(xctl, fingerprint)
400 })
401
402 tpkl, err = store.TLSPublicKeyList(ctxbg, "")
403 tcheck(t, err, "list tls public keys")
404 if len(tpkl) != 0 {
405 t.Fatalf("got %d tls public keys, expected 0", len(tpkl))
406 }
407
408 // "loglevels"
409 testctl(func(xctl *ctl) {
410 ctlcmdLoglevels(xctl)
411 })
412
413 // "setloglevels"
414 testctl(func(xctl *ctl) {
415 ctlcmdSetLoglevels(xctl, "", "debug")
416 })
417 testctl(func(xctl *ctl) {
418 ctlcmdSetLoglevels(xctl, "smtpserver", "debug")
419 })
420
421 // Export data, import it again
422 xcmdExport(true, false, []string{filepath.FromSlash("testdata/ctl/data/tmp/export/mbox/"), filepath.FromSlash("testdata/ctl/data/accounts/mjl")}, &cmd{log: pkglog})
423 xcmdExport(false, false, []string{filepath.FromSlash("testdata/ctl/data/tmp/export/maildir/"), filepath.FromSlash("testdata/ctl/data/accounts/mjl")}, &cmd{log: pkglog})
424 testctl(func(xctl *ctl) {
425 ctlcmdImport(xctl, true, "mjl", "inbox", filepath.FromSlash("testdata/ctl/data/tmp/export/mbox/Inbox.mbox"))
426 })
427 testctl(func(xctl *ctl) {
428 ctlcmdImport(xctl, false, "mjl", "inbox", filepath.FromSlash("testdata/ctl/data/tmp/export/maildir/Inbox"))
429 })
430
431 // "recalculatemailboxcounts"
432 testctl(func(xctl *ctl) {
433 ctlcmdRecalculateMailboxCounts(xctl, "mjl")
434 })
435
436 // "fixmsgsize"
437 testctl(func(xctl *ctl) {
438 ctlcmdFixmsgsize(xctl, "mjl")
439 })
440 testctl(func(xctl *ctl) {
441 acc, err := store.OpenAccount(xctl.log, "mjl", false)
442 tcheck(t, err, "open account")
443 defer func() {
444 acc.Close()
445 acc.WaitClosed()
446 }()
447
448 content := []byte("Subject: hi\r\n\r\nbody\r\n")
449
450 deliver := func(m *store.Message) {
451 t.Helper()
452 m.Size = int64(len(content))
453 msgf, err := store.CreateMessageTemp(xctl.log, "ctltest")
454 tcheck(t, err, "create temp file")
455 defer os.Remove(msgf.Name())
456 defer msgf.Close()
457 _, err = msgf.Write(content)
458 tcheck(t, err, "write message file")
459
460 acc.WithWLock(func() {
461 err = acc.DeliverMailbox(xctl.log, "Inbox", m, msgf)
462 tcheck(t, err, "deliver message")
463 })
464 }
465
466 var msgBadSize store.Message
467 deliver(&msgBadSize)
468
469 msgBadSize.Size = 1
470 err = acc.DB.Update(ctxbg, &msgBadSize)
471 tcheck(t, err, "update message to bad size")
472 mb := store.Mailbox{ID: msgBadSize.MailboxID}
473 err = acc.DB.Get(ctxbg, &mb)
474 tcheck(t, err, "get db")
475 mb.Size -= int64(len(content))
476 mb.Size += 1
477 err = acc.DB.Update(ctxbg, &mb)
478 tcheck(t, err, "update mailbox size")
479
480 // Fix up the size.
481 ctlcmdFixmsgsize(xctl, "")
482
483 err = acc.DB.Get(ctxbg, &msgBadSize)
484 tcheck(t, err, "get message")
485 if msgBadSize.Size != int64(len(content)) {
486 t.Fatalf("after fixing, message size is %d, should be %d", msgBadSize.Size, len(content))
487 }
488 })
489
490 // "reparse"
491 testctl(func(xctl *ctl) {
492 ctlcmdReparse(xctl, "mjl")
493 })
494 testctl(func(xctl *ctl) {
495 ctlcmdReparse(xctl, "")
496 })
497
498 // "reassignthreads"
499 testctl(func(xctl *ctl) {
500 ctlcmdReassignthreads(xctl, "mjl")
501 })
502 testctl(func(xctl *ctl) {
503 ctlcmdReassignthreads(xctl, "")
504 })
505
506 // "backup", backup account.
507 err = dmarcdb.Init()
508 tcheck(t, err, "dmarcdb init")
509 defer dmarcdb.Close()
510 err = mtastsdb.Init(false)
511 tcheck(t, err, "mtastsdb init")
512 defer mtastsdb.Close()
513 err = tlsrptdb.Init()
514 tcheck(t, err, "tlsrptdb init")
515 defer tlsrptdb.Close()
516 testctl(func(xctl *ctl) {
517 os.RemoveAll("testdata/ctl/data/tmp/backup")
518 err := os.WriteFile("testdata/ctl/data/receivedid.key", make([]byte, 16), 0600)
519 tcheck(t, err, "writing receivedid.key")
520 ctlcmdBackup(xctl, filepath.FromSlash("testdata/ctl/data/tmp/backup"), false)
521 })
522
523 // Verify the backup.
524 xcmd := cmd{
525 flag: flag.NewFlagSet("", flag.ExitOnError),
526 flagArgs: []string{filepath.FromSlash("testdata/ctl/data/tmp/backup/data")},
527 }
528 cmdVerifydata(&xcmd)
529
530 // IMAP connection.
531 testctl(func(xctl *ctl) {
532 a, b := net.Pipe()
533 go func() {
534 opts := imapclient.Opts{
535 Logger: slog.Default().With("cid", mox.Cid()),
536 Error: func(err error) { panic(err) },
537 }
538 client, err := imapclient.New(a, &opts)
539 tcheck(t, err, "new imapclient")
540 client.Select("inbox")
541 client.Logout()
542 defer a.Close()
543 }()
544 ctlcmdIMAPServe(xctl, "mjl@mox.example", b, b)
545 })
546}
547
548func fakeCert(t *testing.T) []byte {
549 t.Helper()
550 seed := make([]byte, ed25519.SeedSize)
551 privKey := ed25519.NewKeyFromSeed(seed) // Fake key, don't use this for real!
552 template := &x509.Certificate{
553 SerialNumber: big.NewInt(1), // Required field...
554 }
555 localCertBuf, err := x509.CreateCertificate(cryptorand.Reader, template, template, privKey.Public(), privKey)
556 tcheck(t, err, "making certificate")
557 return localCertBuf
558}
559