14 "github.com/mjl-/mox/imapclient"
15 "github.com/mjl-/mox/mlog"
16 "github.com/mjl-/mox/mox-"
17 "github.com/mjl-/mox/store"
20// Fuzz the server. For each fuzz string, we set up servers in various connection states, and write the string as command.
21func FuzzServer(f *testing.F) {
23 fmt.Sprintf("authenticate plain %s", base64.StdEncoding.EncodeToString([]byte("\u0000mjl@mox.example\u0000testtest"))),
41 "rename tmpbox ntmpbox",
43 "status inbox (uidnext messages uidvalidity deleted size unseen recent)",
44 "append inbox (\\seen) {2+}\r\nhi",
47 "fetch 1 (bodystructure)",
48 `store 1 flags (\seen \answered)`,
49 `store 1 +flags ($junk)`,
50 `store 1 -flags ($junk)`,
57 for _, cmd := range seed {
62 log := mlog.New("imapserver", nil)
64 mox.ConfigStaticPath = filepath.FromSlash("../testdata/imapserverfuzz/mox.conf")
65 mox.MustLoadConfig(true, false)
66 dataDir := mox.ConfigDirPath(mox.Conf.Static.DataDir)
68 acc, err := store.OpenAccount(log, "mjl")
70 f.Fatalf("open account: %v", err)
73 err = acc.SetPassword(log, password0)
75 f.Fatalf("set password: %v", err)
77 defer store.Switchboard()()
79 comm := store.RegisterComm(acc)
80 defer comm.Unregister()
86 fl, err = os.OpenFile("fuzz.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
92 flog := func(err error, msg string) {
93 if fl != nil && err != nil {
94 fmt.Fprintf(fl, "%s: %v\n", msg, err)
98 f.Fuzz(func(t *testing.T, s string) {
99 run := func(cmds []string) {
100 limitersInit() // Reset rate limiters.
101 serverConn, clientConn := net.Pipe()
102 defer serverConn.Close()
107 // Protocol can become botched, when fuzzer sends literals.
112 if !ok || (!errors.Is(err, os.ErrDeadlineExceeded) && !errors.Is(err, io.EOF)) {
117 defer clientConn.Close()
119 err := clientConn.SetDeadline(time.Now().Add(time.Second))
120 flog(err, "set client deadline")
121 client, _ := imapclient.New(clientConn, true)
123 for _, cmd := range cmds {
124 client.Commandf("", "%s", cmd)
127 client.Commandf("", "%s", s)
131 err = serverConn.SetDeadline(time.Now().Add(time.Second))
132 flog(err, "set server deadline")
133 serve("test", cid, nil, serverConn, false, true)
137 // Each command brings the connection state one step further. We try the fuzzing
138 // input for each state.
140 run([]string{"login mjl@mox.example testtest"})
141 run([]string{"login mjl@mox.example testtest", "select inbox"})
142 xappend := fmt.Sprintf("append inbox () {%d+}\r\n%s", len(exampleMsg), exampleMsg)
143 run([]string{"login mjl@mox.example testtest", "select inbox", xappend})