12 "github.com/mjl-/bstore"
13 "github.com/mjl-/sconf"
15 "github.com/mjl-/mox/config"
16 "github.com/mjl-/mox/message"
17 "github.com/mjl-/mox/mlog"
18 "github.com/mjl-/mox/mox-"
21var ctxbg = context.Background()
22var pkglog = mlog.New("store", nil)
24func tcheck(t *testing.T, err error, msg string) {
27 t.Fatalf("%s: %s", msg, err)
31func TestMailbox(t *testing.T) {
32 log := mlog.New("store", nil)
33 os.RemoveAll("../testdata/store/data")
34 mox.ConfigStaticPath = filepath.FromSlash("../testdata/store/mox.conf")
35 mox.MustLoadConfig(true, false)
36 acc, err := OpenAccount(log, "mjl")
37 tcheck(t, err, "open account")
40 tcheck(t, err, "closing account")
45 msgFile, err := CreateMessageTemp(log, "account-test")
47 t.Fatalf("creating temp msg file: %s", err)
49 defer os.Remove(msgFile.Name())
51 msgWriter := message.NewWriter(msgFile)
52 if _, err := msgWriter.Write([]byte(" message")); err != nil {
53 t.Fatalf("writing to temp message: %s", err)
56 msgPrefix := []byte("From: <mjl@mox.example\r\nTo: <mjl@mox.example>\r\nCc: <mjl@mox.example>Subject: test\r\nMessage-Id: <m01@mox.example>\r\n\r\n")
57 msgPrefixCatchall := []byte("Subject: catchall\r\n\r\n")
60 Size: int64(len(msgPrefix)) + msgWriter.Size,
65 m.ThreadCollapsed = true
67 mbrejects := Mailbox{Name: "Rejects", UIDValidity: 1, UIDNext: 1, HaveCounts: true}
71 Size: int64(len(msgPrefixCatchall)) + msgWriter.Size,
72 MsgPrefix: msgPrefixCatchall,
74 acc.WithWLock(func() {
76 err := acc.DeliverDestination(log, conf.Destinations["mjl"], &m, msgFile)
77 tcheck(t, err, "deliver without consume")
79 err = acc.DB.Write(ctxbg, func(tx *bstore.Tx) error {
81 mbsent, err = bstore.QueryTx[Mailbox](tx).FilterNonzero(Mailbox{Name: "Sent"}).Get()
82 tcheck(t, err, "sent mailbox")
83 msent.MailboxID = mbsent.ID
84 msent.MailboxOrigID = mbsent.ID
85 err = acc.DeliverMessage(pkglog, tx, &msent, msgFile, true, false, false, true)
86 tcheck(t, err, "deliver message")
87 if !msent.ThreadMuted || !msent.ThreadCollapsed {
88 t.Fatalf("thread muted & collapsed should have been copied from parent (duplicate message-id) m")
92 tcheck(t, err, "get mbsent")
93 mbsent.Add(msent.MailboxCounts())
94 err = tx.Update(&mbsent)
95 tcheck(t, err, "update mbsent")
97 err = tx.Insert(&mbrejects)
98 tcheck(t, err, "insert rejects mailbox")
99 mreject.MailboxID = mbrejects.ID
100 mreject.MailboxOrigID = mbrejects.ID
101 err = acc.DeliverMessage(pkglog, tx, &mreject, msgFile, true, false, false, true)
102 tcheck(t, err, "deliver message")
104 err = tx.Get(&mbrejects)
105 tcheck(t, err, "get mbrejects")
106 mbrejects.Add(mreject.MailboxCounts())
107 err = tx.Update(&mbrejects)
108 tcheck(t, err, "update mbrejects")
112 tcheck(t, err, "deliver as sent and rejects")
114 err = acc.DeliverDestination(pkglog, conf.Destinations["mjl"], &mconsumed, msgFile)
115 tcheck(t, err, "deliver with consume")
117 err = acc.DB.Write(ctxbg, func(tx *bstore.Tx) error {
120 err = acc.RetrainMessages(ctxbg, log, tx, l, false)
121 tcheck(t, err, "train as junk")
125 tcheck(t, err, "train messages")
130 jf, _, err := acc.OpenJunkFilter(ctxbg, log)
131 tcheck(t, err, "open junk filter")
132 err = acc.DB.Write(ctxbg, func(tx *bstore.Tx) error {
133 return acc.RetrainMessage(ctxbg, log, tx, jf, &m, false)
135 tcheck(t, err, "retraining as non-junk")
137 tcheck(t, err, "close junk filter")
140 err = acc.DB.Write(ctxbg, func(tx *bstore.Tx) error {
141 return acc.RetrainMessages(ctxbg, log, tx, []Message{m}, false)
143 tcheck(t, err, "untraining non-junk")
145 err = acc.SetPassword(log, "testtest")
146 tcheck(t, err, "set password")
148 key0, err := acc.Subjectpass("test@localhost")
149 tcheck(t, err, "subjectpass")
150 key1, err := acc.Subjectpass("test@localhost")
151 tcheck(t, err, "subjectpass")
153 t.Fatalf("different keys for same address")
155 key2, err := acc.Subjectpass("test2@localhost")
156 tcheck(t, err, "subjectpass")
158 t.Fatalf("same key for different address")
161 acc.WithWLock(func() {
162 err := acc.DB.Write(ctxbg, func(tx *bstore.Tx) error {
163 _, _, err := acc.MailboxEnsure(tx, "Testbox", true)
166 tcheck(t, err, "ensure mailbox exists")
167 err = acc.DB.Read(ctxbg, func(tx *bstore.Tx) error {
168 _, _, err := acc.MailboxEnsure(tx, "Testbox", true)
171 tcheck(t, err, "ensure mailbox exists")
173 err = acc.DB.Write(ctxbg, func(tx *bstore.Tx) error {
174 _, _, err := acc.MailboxEnsure(tx, "Testbox2", false)
175 tcheck(t, err, "create mailbox")
177 exists, err := acc.MailboxExists(tx, "Testbox2")
178 tcheck(t, err, "checking that mailbox exists")
180 t.Fatalf("mailbox does not exist")
183 exists, err = acc.MailboxExists(tx, "Testbox3")
184 tcheck(t, err, "checking that mailbox does not exist")
186 t.Fatalf("mailbox does exist")
189 xmb, err := acc.MailboxFind(tx, "Testbox3")
190 tcheck(t, err, "finding non-existing mailbox")
192 t.Fatalf("did find Testbox3: %v", xmb)
194 xmb, err = acc.MailboxFind(tx, "Testbox2")
195 tcheck(t, err, "finding existing mailbox")
197 t.Fatalf("did not find Testbox2")
200 changes, err := acc.SubscriptionEnsure(tx, "Testbox2")
201 tcheck(t, err, "ensuring new subscription")
202 if len(changes) == 0 {
203 t.Fatalf("new subscription did not result in changes")
205 changes, err = acc.SubscriptionEnsure(tx, "Testbox2")
206 tcheck(t, err, "ensuring already present subscription")
207 if len(changes) != 0 {
208 t.Fatalf("already present subscription resulted in changes")
213 tcheck(t, err, "write tx")
215 // todo: check that messages are removed and changes sent.
216 hasSpace, err := acc.TidyRejectsMailbox(log, "Rejects")
217 tcheck(t, err, "tidy rejects mailbox")
219 t.Fatalf("no space for more rejects")
222 acc.RejectsRemove(log, "Rejects", "m01@mox.example")
225 // Run the auth tests twice for possible cache effects.
226 for i := 0; i < 2; i++ {
227 _, err := OpenEmailAuth(log, "mjl@mox.example", "bogus")
228 if err != ErrUnknownCredentials {
229 t.Fatalf("got %v, expected ErrUnknownCredentials", err)
233 for i := 0; i < 2; i++ {
234 acc2, err := OpenEmailAuth(log, "mjl@mox.example", "testtest")
235 tcheck(t, err, "open for email with auth")
237 tcheck(t, err, "close account")
240 acc2, err := OpenEmailAuth(log, "other@mox.example", "testtest")
241 tcheck(t, err, "open for email with auth")
243 tcheck(t, err, "close account")
245 _, err = OpenEmailAuth(log, "bogus@mox.example", "testtest")
246 if err != ErrUnknownCredentials {
247 t.Fatalf("got %v, expected ErrUnknownCredentials", err)
250 _, err = OpenEmailAuth(log, "mjl@test.example", "testtest")
251 if err != ErrUnknownCredentials {
252 t.Fatalf("got %v, expected ErrUnknownCredentials", err)
256func TestMessageRuleset(t *testing.T) {
257 f, err := CreateMessageTemp(pkglog, "msgruleset")
258 tcheck(t, err, "creating temp msg file")
259 defer os.Remove(f.Name())
262 msgBuf := []byte(strings.ReplaceAll(`List-ID: <test.mox.example>
271 list-id: <test\.mox\.example>
274 var dest config.Destination
275 err = sconf.Parse(strings.NewReader(destConf), &dest)
276 tcheck(t, err, "parse config")
277 // todo: should use regular config initialization functions for this.
278 var hdrs [][2]*regexp.Regexp
279 for k, v := range dest.Rulesets[0].HeadersRegexp {
280 rk, err := regexp.Compile(k)
281 tcheck(t, err, "compile key")
282 rv, err := regexp.Compile(v)
283 tcheck(t, err, "compile value")
284 hdrs = append(hdrs, [...]*regexp.Regexp{rk, rv})
286 dest.Rulesets[0].HeadersRegexpCompiled = hdrs
288 c := MessageRuleset(pkglog, dest, &Message{}, msgBuf, f)
290 t.Fatalf("expected ruleset match")
293 msg2Buf := []byte(strings.ReplaceAll(`From: <mjl@mox.example>
297 c = MessageRuleset(pkglog, dest, &Message{}, msg2Buf, f)
299 t.Fatalf("expected no ruleset match")
302 // todo: test the SMTPMailFrom and VerifiedDomains rule.