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 dataDir := mox.ConfigDirPath(mox.Conf.Static.DataDir)
39 os.RemoveAll(dataDir)
40 acc, err := store.OpenAccount(log, "mjl")
41 if err != nil {
42 f.Fatalf("open account: %v", err)
43 }
44 defer func() {
45 acc.Close()
46 acc.CheckClosed()
47 }()
48 err = acc.SetPassword(log, "testtest")
49 if err != nil {
50 f.Fatalf("set password: %v", err)
51 }
52 defer store.Switchboard()()
53 err = queue.Init()
54 if err != nil {
55 f.Fatalf("queue init: %v", err)
56 }
57 defer queue.Shutdown()
58
59 comm := store.RegisterComm(acc)
60 defer comm.Unregister()
61
62 var cid int64 = 1
63
64 var fl *os.File
65 if false {
66 fl, err = os.Create("fuzz.log")
67 if err != nil {
68 f.Fatalf("fuzz log")
69 }
70 defer fl.Close()
71 }
72 flog := func(err error, msg string) {
73 if fl != nil && err != nil {
74 fmt.Fprintf(fl, "%s: %v\n", msg, err)
75 }
76 }
77
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()
84
85 go func() {
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")
95 }
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")
100 clientConn.Close()
101 serverConn.Close()
102 }()
103
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)
109 cid++
110 }
111
112 run([]string{})
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
117 })
118}
119