|
|
|
package htpasswd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseHtpassd(t *testing.T) {
|
|
|
|
passwords, err := ParseHtpasswd([]byte("sha:{SHA}IRRjboXT92QSYXm8lpGPCZUvU1E=\n"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(passwords) != 1 {
|
|
|
|
t.Fatalf("unexpected length in passwords, expected 1 got %v", len(passwords))
|
|
|
|
}
|
|
|
|
const expected = "{SHA}IRRjboXT92QSYXm8lpGPCZUvU1E="
|
|
|
|
if passwords["sha"] != expected {
|
|
|
|
t.Fatalf("sha password was wrong, got %s and expected %s", passwords["sha"], expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateUser(t *testing.T) {
|
|
|
|
f, err := os.Create("htpasswd_testdata")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testCases := []struct {
|
|
|
|
user, password string
|
|
|
|
hash HashAlgorithm
|
|
|
|
expected error
|
|
|
|
}{
|
|
|
|
{"apr1", "123456@", HashAPR1, nil},
|
|
|
|
{"bcrypt", "123456@", HashBCrypt, nil},
|
|
|
|
{"ssha", "123456@", HashSSHA, nil},
|
|
|
|
{"sha", "123456@", HashSHA, nil},
|
|
|
|
{"sha256", "123456@", HashSHA256, nil},
|
|
|
|
{"sha512", "123456@", HashSHA512, nil},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
err := CreateUser(f.Name(), tc.user, tc.password, tc.hash)
|
|
|
|
if err != tc.expected {
|
|
|
|
t.Errorf("CreateUser(%s %s, %s, %s) = %d; want %d", f.Name(),
|
|
|
|
tc.user, tc.password, tc.hash, err, tc.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestVerifyPassword(t *testing.T) {
|
|
|
|
f, err := os.Stat("htpasswd_testdata")
|
|
|
|
if err != nil {
|
|
|
|
TestCreateUser(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ParseHtpasswdFile(f.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
user, password string
|
|
|
|
expected error
|
|
|
|
}{
|
|
|
|
{"apr1", "123456@", nil},
|
|
|
|
{"bcrypt", "123456@", nil},
|
|
|
|
{"ssha", "123456@", nil},
|
|
|
|
{"sha", "123456@", nil},
|
|
|
|
{"sha256", "123456@", nil},
|
|
|
|
{"sha512", "123456@", nil},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
err := VerifyUser(f.Name(), tc.user, tc.password)
|
|
|
|
if err != tc.expected {
|
|
|
|
t.Errorf("VerifyUser(%s %s, %s) = %v; want %v",
|
|
|
|
f, tc.user, tc.password, err, tc.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerify(t *testing.T) {
|
|
|
|
f, err := os.Stat("htpasswd_testdata")
|
|
|
|
if err != nil {
|
|
|
|
TestCreateUser(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
pp, err := ParseHtpasswdFile(f.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = pp.Verify("bcrypt", "123456@")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
}
|