11 "github.com/mjl-/bstore"
13 "github.com/mjl-/mox/mox-"
16func TestLoginAttempt(t *testing.T) {
17 os.RemoveAll("../testdata/store/data")
18 mox.ConfigStaticPath = filepath.FromSlash("../testdata/store/mox.conf")
19 mox.MustLoadConfig(true, false)
21 xctx, xcancel := context.WithCancel(ctxbg)
22 defer xcancel() // Stop clearing of LoginAttempts.
24 tcheck(t, err, "store init")
25 stopc := make(chan struct{})
26 writeLoginAttemptStop <- stopc
29 // Ensure Close() below finishes
31 c := <-writeLoginAttemptStop
36 tcheck(t, err, "store close")
43 UserAgent: "0", // "0" so we update instead of insert when testing automatic cleanup below.
47 a2.AccountName = "mjl2"
49 a3.AccountName = "mjl3"
50 a3.Last = a3.Last.Add(-31 * 24 * time.Hour) // Will be cleaned up.
52 LoginAttemptAdd(ctxbg, pkglog, a1)
53 LoginAttemptAdd(ctxbg, pkglog, a2)
54 LoginAttemptAdd(ctxbg, pkglog, a3)
56 // Ensure there are no LoginAttempts that still need to be written.
57 loginAttemptDrain := func() {
60 case la := <-writeLoginAttempt:
70 l, err := LoginAttemptList(ctxbg, "", 0)
71 tcheck(t, err, "list login attempts")
72 tcompare(t, len(l), 3)
75 l, err = LoginAttemptList(ctxbg, "", 2)
76 tcheck(t, err, "list login attempts")
77 tcompare(t, len(l), 2)
79 // Test account filter.
80 l, err = LoginAttemptList(ctxbg, "mjl1", 2)
81 tcheck(t, err, "list login attempts")
82 tcompare(t, len(l), 1)
84 // Cleanup will remove the entry for mjl3 and leave others.
85 err = LoginAttemptCleanup(ctxbg)
86 tcheck(t, err, "cleanup login attempt")
87 l, err = LoginAttemptList(ctxbg, "", 0)
88 tcheck(t, err, "list login attempts")
89 tcompare(t, len(l), 2)
91 // Removing account will keep last entry for mjl2.
92 err = AuthDB.Write(ctxbg, func(tx *bstore.Tx) error {
93 return loginAttemptRemoveAccount(tx, "mjl1")
95 tcheck(t, err, "remove login attempt account")
96 l, err = LoginAttemptList(ctxbg, "", 0)
97 tcheck(t, err, "list login attempts")
98 tcompare(t, len(l), 1)
100 l, err = LoginAttemptList(ctxbg, "mjl2", 0)
101 tcheck(t, err, "list login attempts")
102 tcompare(t, len(l), 1)
104 // Insert 3 failing entries. Then add another and see we still have 3.
105 loginAttemptsMaxPerAccount = 3
106 for i := range loginAttemptsMaxPerAccount {
108 a.UserAgent = fmt.Sprintf("%d", i)
109 LoginAttemptAdd(ctxbg, pkglog, a)
112 l, err = LoginAttemptList(ctxbg, "", 0)
113 tcheck(t, err, "list login attempts")
114 tcompare(t, len(l), loginAttemptsMaxPerAccount)
117 a.UserAgent = fmt.Sprintf("%d", loginAttemptsMaxPerAccount)
118 LoginAttemptAdd(ctxbg, pkglog, a)
120 l, err = LoginAttemptList(ctxbg, "", 0)
121 tcheck(t, err, "list login attempts")
122 tcompare(t, len(l), loginAttemptsMaxPerAccount)