mirror of
https://github.com/restic/rest-server.git
synced 2025-12-06 17:15:45 -08:00
Add basic test
This commit is contained in:
committed by
Leo R. Lundgren
parent
df9eb337d3
commit
46b020fd9e
46
htpasswd_test.go
Normal file
46
htpasswd_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package restserver
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
user := "restic"
|
||||
pwd := "$2y$05$z/OEmNQamd6m6LSegUErh.r/Owk9Xwmc5lxDheIuHY2Z7XiS6FtJm"
|
||||
rawPwd := "test"
|
||||
wrongPwd := "wrong"
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "rest-validate-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = tmpfile.Write([]byte(user + ":" + pwd + "\n")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = tmpfile.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
htpass, err := NewHtpasswdFromFile(tmpfile.Name())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
isValid := htpass.Validate(user, rawPwd)
|
||||
if !isValid {
|
||||
t.Fatal("correct password not accepted")
|
||||
}
|
||||
|
||||
isValid = htpass.Validate(user, wrongPwd)
|
||||
if isValid {
|
||||
t.Fatal("wrong password accepted")
|
||||
}
|
||||
}
|
||||
|
||||
if err = os.Remove(tmpfile.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user