From 3fe765dce9ec0152fd632909d2fe0d2cf4a61f9c Mon Sep 17 00:00:00 2001 From: Mechiel Lukkien Date: Wed, 16 Apr 2025 11:21:01 +0200 Subject: [PATCH] imapserver: fix fuzz tests The acc.Close() at the end of the fuzzing would find inconsistencies. For example, message files on disk that aren't in the database file. I don't understand what is happening there, the database file on disk does have those messages, and it seems the database file is getting replaced. When running the same code not as a fuzzing test but as a regular Go test doesn't show the problem. So it seems to be some interaction with fuzzing. The problem is "solved" (feels more like side-stepped), by starting each fuzz test with a clean database. We still open & close the account in each fuzz test, and it doesn't find consistency problems. --- imapserver/fuzz_test.go | 57 +++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/imapserver/fuzz_test.go b/imapserver/fuzz_test.go index 74b2c53..271964d 100644 --- a/imapserver/fuzz_test.go +++ b/imapserver/fuzz_test.go @@ -60,38 +60,11 @@ func FuzzServer(f *testing.F) { f.Add(tag + cmd) } - log := mlog.New("imapserver", nil) - mox.ConfigStaticPath = filepath.FromSlash("../testdata/imapserverfuzz/mox.conf") - mox.MustLoadConfig(true, false) - store.Close() // May not be open, we ignore error. - dataDir := mox.ConfigDirPath(mox.Conf.Static.DataDir) - os.RemoveAll(dataDir) - err := store.Init(ctxbg) - if err != nil { - f.Fatalf("store init: %v", err) - } - defer store.Switchboard()() - - acc, err := store.OpenAccount(log, "mjl", false) - if err != nil { - f.Fatalf("open account: %v", err) - } - defer func() { - acc.Close() - acc.WaitClosed() - }() - err = acc.SetPassword(log, password0) - if err != nil { - f.Fatalf("set password: %v", err) - } - - comm := store.RegisterComm(acc) - defer comm.Unregister() - var cid int64 = 1 var fl *os.File if false { + var err error fl, err = os.OpenFile("fuzz.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) if err != nil { f.Fatalf("fuzz log") @@ -105,6 +78,34 @@ func FuzzServer(f *testing.F) { } f.Fuzz(func(t *testing.T, s string) { + log := mlog.New("imapserver", nil) + mox.ConfigStaticPath = filepath.FromSlash("../testdata/imapserverfuzz/mox.conf") + mox.MustLoadConfig(true, false) + store.Close() // May not be open, we ignore error. + dataDir := mox.ConfigDirPath(mox.Conf.Static.DataDir) + os.RemoveAll(dataDir) + err := store.Init(ctxbg) + if err != nil { + t.Fatalf("store init: %v", err) + } + defer store.Switchboard()() + + acc, err := store.OpenAccount(log, "mjl", false) + if err != nil { + t.Fatalf("open account: %v", err) + } + defer func() { + acc.Close() + acc.WaitClosed() + }() + err = acc.SetPassword(log, password0) + if err != nil { + t.Fatalf("set password: %v", err) + } + + comm := store.RegisterComm(acc) + defer comm.Unregister() + run := func(cmds []string) { limitersInit() // Reset rate limiters. serverConn, clientConn := net.Pipe()