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 dataDir := mox.ConfigDirPath(mox.Conf.Static.DataDir)
40 acc, err := store.OpenAccount(log, "mjl")
42 f.Fatalf("open account: %v", err)
48 err = acc.SetPassword(log, "testtest")
50 f.Fatalf("set password: %v", err)
52 defer store.Switchboard()()
55 f.Fatalf("queue init: %v", err)
57 defer queue.Shutdown()
59 comm := store.RegisterComm(acc)
60 defer comm.Unregister()
66 fl, err = os.Create("fuzz.log")
72 flog := func(err error, msg string) {
73 if fl != nil && err != nil {
74 fmt.Fprintf(fl, "%s: %v\n", msg, err)
78 f.Fuzz(func(t *testing.T, s string) {
79 run := func(cmds []string) {
80 limitersInit() // Reset rate limiters.
81 serverConn, clientConn := net.Pipe()
82 defer serverConn.Close()
83 defer clientConn.Close()
86 err := clientConn.SetDeadline(time.Now().Add(time.Second))
87 flog(err, "set client deadline")
88 _, err = clientConn.Read(make([]byte, 1024))
89 flog(err, "read ehlo")
90 for _, cmd := range cmds {
91 _, err = clientConn.Write([]byte(cmd + "\r\n"))
92 flog(err, "write command")
93 _, err = clientConn.Read(make([]byte, 1024))
94 flog(err, "read response")
96 _, err = clientConn.Write([]byte(s + "\r\n"))
97 flog(err, "write test command")
98 _, err = clientConn.Read(make([]byte, 1024))
99 flog(err, "read test response")
104 resolver := dns.MockResolver{}
105 const submission = false
106 err := serverConn.SetDeadline(time.Now().Add(time.Second))
107 flog(err, "set server deadline")
108 serve("test", cid, dns.Domain{ASCII: "mox.example"}, nil, serverConn, resolver, submission, false, 100<<10, false, false, false, nil, 0)
113 run([]string{"EHLO remote"})
114 run([]string{"EHLO remote", "MAIL FROM:<remote@example.org>"})
115 run([]string{"EHLO remote", "MAIL FROM:<remote@example.org>", "RCPT TO:<mjl@mox.example>"})
116 // todo: submission with login