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"
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) {
23 f.Add("MAIL FROM:<remote@remote>")
24 f.Add("RCPT TO:<local@mox.example>")
34 log := mlog.New("smtpserver", nil)
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)
41 err := store.Init(ctxbg)
43 f.Fatalf("store init: %v", err)
45 defer store.Switchboard()()
47 acc, err := store.OpenAccount(log, "mjl", false)
49 f.Fatalf("open account: %v", err)
55 err = acc.SetPassword(log, "testtest")
57 f.Fatalf("set password: %v", err)
62 f.Fatalf("queue init: %v", err)
64 defer queue.Shutdown()
66 comm := store.RegisterComm(acc)
67 defer comm.Unregister()
73 fl, err = os.Create("fuzz.log")
79 flog := func(err error, msg string) {
80 if fl != nil && err != nil {
81 fmt.Fprintf(fl, "%s: %v\n", msg, err)
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()
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")
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")
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)
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