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