Compare commits
4 Commits
fcb656d4ea
...
4f254db2bb
Author | SHA1 | Date |
---|---|---|
Alexander Andreev | 4f254db2bb | |
Alexander Andreev | e26bdd64f5 | |
Alexander Andreev | f3e68f4c41 | |
Alexander Andreev | f9154ab02f |
|
@ -8,6 +8,7 @@ import (
|
|||
"git.arav.top/Arav/justcaptcha/pkg/captcha"
|
||||
)
|
||||
|
||||
// InMemoryCaptchaDB implementation that lives in a memory (map).
|
||||
type InMemoryCaptchaDB struct {
|
||||
sync.Mutex
|
||||
|
||||
|
@ -17,7 +18,8 @@ type InMemoryCaptchaDB struct {
|
|||
}
|
||||
|
||||
// NewInMemoryCaptchaDB returns an initialised instance of an InMemoryCaptchaDB.
|
||||
// An expiry duration is a time for how long CAPTCHA will be valid.
|
||||
// An expiry is a scan interval for expired CAPTCHAs (if passed a longer one,
|
||||
// resets to a default (captcha.DefaultExpiredScanInterval)).
|
||||
func NewInMemoryCaptchaDB(expiry time.Duration) *InMemoryCaptchaDB {
|
||||
db := &InMemoryCaptchaDB{
|
||||
db: make(map[captcha.ID]captcha.Captcha),
|
||||
|
@ -34,7 +36,7 @@ func NewInMemoryCaptchaDB(expiry time.Duration) *InMemoryCaptchaDB {
|
|||
return db
|
||||
}
|
||||
|
||||
// New accepts a CAPTHA instance, generates an ID and store it in a database.
|
||||
// New accepts a CAPTCHA instance, generates an ID and store it in a database.
|
||||
// A data string is an additional random data used to generate an ID,
|
||||
// e.g. an IP-address.
|
||||
func (imcdb *InMemoryCaptchaDB) New(data string, cptcha captcha.Captcha) (captcha.Captcha, captcha.ID) {
|
||||
|
@ -47,13 +49,15 @@ func (imcdb *InMemoryCaptchaDB) New(data string, cptcha captcha.Captcha) (captch
|
|||
return cptcha, id
|
||||
}
|
||||
|
||||
// GetExpiry returns time for how long CAPTCHA will last.
|
||||
// GetExpiry returns an expiry for a CAPTCHA.
|
||||
func (imcdb *InMemoryCaptchaDB) GetExpiry() time.Duration {
|
||||
return imcdb.expiry
|
||||
}
|
||||
|
||||
// SetExpiry changes an expiry duration to a new one.
|
||||
// SetExpiry changes an expiry for a CAPTCHA and a scan interval. Scan interval
|
||||
// cannot be longer than a default, so if it is, then resets to a default.
|
||||
func (imcdb *InMemoryCaptchaDB) SetExpiry(expiry time.Duration) {
|
||||
imcdb.expiry = expiry
|
||||
if expiry < captcha.DefaultExpiredScanInterval {
|
||||
imcdb.expiryScanInterval = expiry
|
||||
} else {
|
||||
|
@ -61,7 +65,8 @@ func (imcdb *InMemoryCaptchaDB) SetExpiry(expiry time.Duration) {
|
|||
}
|
||||
}
|
||||
|
||||
// Image returns a freshly generated image for a CAPTCHA.
|
||||
// Image returns a freshly generated image for a CAPTCHA with style if
|
||||
// applicable.
|
||||
func (imcdb *InMemoryCaptchaDB) Image(id captcha.ID, style string) *image.Image {
|
||||
imcdb.Lock()
|
||||
defer imcdb.Unlock()
|
||||
|
@ -102,9 +107,7 @@ func (imcdb *InMemoryCaptchaDB) IsSolved(id captcha.ID) bool {
|
|||
func (imcdb *InMemoryCaptchaDB) Remove(id captcha.ID) {
|
||||
imcdb.Lock()
|
||||
defer imcdb.Unlock()
|
||||
if _, ok := imcdb.db[id]; ok {
|
||||
delete(imcdb.db, id)
|
||||
}
|
||||
delete(imcdb.db, id)
|
||||
}
|
||||
|
||||
// cleanExpired removes expired CAPTCHAs in a loop.
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
package inmemdb_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.arav.top/Arav/justcaptcha/pkg/captcha"
|
||||
"git.arav.top/Arav/justcaptcha/pkg/captcha/inmemdb"
|
||||
"git.arav.top/Arav/justcaptcha/pkg/dwcaptcha"
|
||||
)
|
||||
|
||||
const expiry = 10 * time.Minute
|
||||
const testData = "192.168.0.1"
|
||||
|
||||
func TestInMemDBDefaultInstance(t *testing.T) {
|
||||
if inmemdb.GetExpiry() != captcha.DefaultExpiredScanInterval {
|
||||
t.Errorf("expiration is different from a default one (%v != %v)",
|
||||
inmemdb.GetExpiry(), captcha.DefaultExpiredScanInterval)
|
||||
}
|
||||
|
||||
inmemdb.SetExpiry(expiry)
|
||||
|
||||
if expiry != inmemdb.GetExpiry() {
|
||||
t.Errorf("Expected %v, but got %v", expiry, inmemdb.GetExpiry())
|
||||
}
|
||||
|
||||
captcha, captchaID := inmemdb.New(testData, dwcaptcha.NewDwellingCaptcha(expiry))
|
||||
|
||||
if inmemdb.Image(captchaID, "") == nil {
|
||||
t.Error("nil returned instead of an image.Image struct")
|
||||
}
|
||||
|
||||
if !inmemdb.Solve(captchaID, captcha.Answer()) {
|
||||
t.Error("CAPTCHA not solved. Looks like provided ID wasn't found")
|
||||
}
|
||||
|
||||
if !inmemdb.IsSolved(captchaID) {
|
||||
t.Error("last test solved a CAPTCHA, but IsSolved() returned false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInMemDBNewInstance(t *testing.T) {
|
||||
db := inmemdb.NewInMemoryCaptchaDB(expiry)
|
||||
|
||||
if db.GetExpiry() != expiry {
|
||||
t.Errorf("expected expiry %v, but got %v", expiry, db.GetExpiry())
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package dwcaptcha_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.arav.top/Arav/justcaptcha/pkg/dwcaptcha"
|
||||
)
|
||||
|
||||
const expiry = 10 * time.Minute
|
||||
|
||||
func TestCaptcha(t *testing.T) {
|
||||
captcha := dwcaptcha.NewDwellingCaptcha(expiry)
|
||||
|
||||
if diff := time.Until(captcha.Expiry()) - expiry; diff >= 1*time.Microsecond {
|
||||
t.Errorf("Difference %s is more than 1 microsecond", diff.String())
|
||||
}
|
||||
|
||||
if captcha.Image("") == nil {
|
||||
t.Error("image cannot be nil, but it is!")
|
||||
}
|
||||
|
||||
if !captcha.Solve(captcha.Answer()) {
|
||||
t.Error("for whatever reason, its own answer doesn't match itself O_O")
|
||||
}
|
||||
|
||||
if !captcha.IsSolved() {
|
||||
t.Error("after last test it should be true, but it is not!")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue