1package smtpserver
2
3import (
4 "fmt"
5 "net"
6 "os"
7 "path/filepath"
8 "testing"
9 "time"
10
11 "github.com/mjl-/mox/dns"
12 "github.com/mjl-/mox/mlog"
13 "github.com/mjl-/mox/mox-"
14 "github.com/mjl-/mox/queue"
15 "github.com/mjl-/mox/store"
16)
17
18// Fuzz the server. For each fuzz string, we set up servers in various connection states, and write the string as command.
19func FuzzServer(f *testing.F) {
20 f.Add("HELO remote")
21 f.Add("EHLO remote")
22 f.Add("AUTH PLAIN")
23 f.Add("MAIL FROM:<remote@remote>")
24 f.Add("RCPT TO:<local@mox.example>")
25 f.Add("DATA")
26 f.Add(".")
27 f.Add("RSET")
28 f.Add("VRFY x")
29 f.Add("EXPN x")
30 f.Add("HELP")
31 f.Add("NOOP")
32 f.Add("QUIT")
33
34 log := mlog.New("smtpserver", nil)
35 mox.Context = ctxbg
36 mox.ConfigStaticPath = filepath.FromSlash("../testdata/smtpserverfuzz/mox.conf")
37 mox.MustLoadConfig(true, false)
38 store.Close() // May not be open, we ignore error.
39 dataDir := mox.ConfigDirPath(mox.Conf.Static.DataDir)
40 os.RemoveAll(dataDir)
41 err := store.Init(ctxbg)
42 if err != nil {
43 f.Fatalf("store init: %v", err)
44 }
45 defer store.Switchboard()()
46
47 acc, err := store.OpenAccount(log, "mjl", false)
48 if err != nil {
49 f.Fatalf("open account: %v", err)
50 }
51 defer func() {
52 acc.Close()
53 acc.WaitClosed()
54 }()
55 err = acc.SetPassword(log, "testtest")
56 if err != nil {
57 f.Fatalf("set password: %v", err)
58 }
59
60 err = queue.Init()
61 if err != nil {
62 f.Fatalf("queue init: %v", err)
63 }
64 defer queue.Shutdown()
65
66 comm := store.RegisterComm(acc)
67 defer comm.Unregister()
68
69 var cid int64 = 1
70
71 var fl *os.File
72 if false {
73 fl, err = os.Create("fuzz.log")
74 if err != nil {
75 f.Fatalf("fuzz log")
76 }
77 defer fl.Close()
78 }
79 flog := func(err error, msg string) {
80 if fl != nil && err != nil {
81 fmt.Fprintf(fl, "%s: %v\n", msg, err)
82 }
83 }
84
85 f.Fuzz(func(t *testing.T, s string) {
86 run := func(cmds []string) {
87 limitersInit() // Reset rate limiters.
88 serverConn, clientConn := net.Pipe()
89 defer serverConn.Close()
90 defer clientConn.Close()
91
92 go func() {
93 err := clientConn.SetDeadline(time.Now().Add(time.Second))
94 flog(err, "set client deadline")
95 _, err = clientConn.Read(make([]byte, 1024))
96 flog(err, "read ehlo")
97 for _, cmd := range cmds {
98 _, err = clientConn.Write([]byte(cmd + "\r\n"))
99 flog(err, "write command")
100 _, err = clientConn.Read(make([]byte, 1024))
101 flog(err, "read response")
102 }
103 _, err = clientConn.Write([]byte(s + "\r\n"))
104 flog(err, "write test command")
105 _, err = clientConn.Read(make([]byte, 1024))
106 flog(err, "read test response")
107 clientConn.Close()
108 serverConn.Close()
109 }()
110
111 resolver := dns.MockResolver{}
112 const submission = false
113 const viaHTTPS = false
114 err := serverConn.SetDeadline(time.Now().Add(time.Second))
115 flog(err, "set server deadline")
116 serve("test", cid, dns.Domain{ASCII: "mox.example"}, nil, serverConn, resolver, submission, false, viaHTTPS, 100<<10, false, false, false, nil, 0)
117 cid++
118 }
119
120 run([]string{})
121 run([]string{"EHLO remote"})
122 run([]string{"EHLO remote", "MAIL FROM:<remote@example.org>"})
123 run([]string{"EHLO remote", "MAIL FROM:<remote@example.org>", "RCPT TO:<mjl@mox.example>"})
124 // todo: submission with login
125 })
126}
127