2016-11-17 05:23:47 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-28 06:55:57 +07:00
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"github.com/erikstmartin/go-testdb"
|
2016-11-17 05:23:47 +07:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-11-28 06:55:57 +07:00
|
|
|
type testResult struct {
|
|
|
|
lastID int64
|
|
|
|
affectedRows int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r testResult) LastInsertId() (int64, error) {
|
|
|
|
return r.lastID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r testResult) RowsAffected() (int64, error) {
|
|
|
|
return r.affectedRows, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDBInit(t *testing.T) {
|
|
|
|
fakeDB := new(acmedb)
|
|
|
|
err := fakeDB.Init("notarealegine", "connectionstring")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Was expecting error, didn't get one.")
|
|
|
|
}
|
|
|
|
|
|
|
|
testdb.SetExecWithArgsFunc(func(query string, args []driver.Value) (result driver.Result, err error) {
|
|
|
|
return testResult{1, 0}, errors.New("Prepared query error")
|
|
|
|
})
|
|
|
|
defer testdb.Reset()
|
|
|
|
|
|
|
|
errorDB := new(acmedb)
|
|
|
|
err = errorDB.Init("testdb", "")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Was expecting DB initiation error but got none")
|
|
|
|
}
|
|
|
|
errorDB.Close()
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:03:08 +07:00
|
|
|
func TestRegisterNoCIDR(t *testing.T) {
|
2016-11-17 05:23:47 +07:00
|
|
|
// Register tests
|
2016-12-01 05:03:08 +07:00
|
|
|
_, err := DB.Register(cidrslice{})
|
2016-11-17 05:23:47 +07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Registration failed, got error [%v]", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:03:08 +07:00
|
|
|
func TestRegisterMany(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
input cidrslice
|
|
|
|
output cidrslice
|
|
|
|
}{
|
|
|
|
{cidrslice{"127.0.0.1/8", "8.8.8.8/32", "1.0.0.1/1"}, cidrslice{"127.0.0.1/8", "8.8.8.8/32", "1.0.0.1/1"}},
|
|
|
|
{cidrslice{"1.1.1./32", "1922.168.42.42/8", "1.1.1.1/33", "1.2.3.4/"}, cidrslice{}},
|
|
|
|
{cidrslice{"7.6.5.4/32", "invalid", "1.0.0.1/2"}, cidrslice{"7.6.5.4/32", "1.0.0.1/2"}},
|
|
|
|
} {
|
|
|
|
user, err := DB.Register(test.input)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d: Got error from register method: [%v]", i, err)
|
|
|
|
}
|
|
|
|
res, err := DB.GetByUsername(user.Username)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d: Got error when fetching username: [%v]", i, err)
|
|
|
|
}
|
|
|
|
if len(user.AllowFrom) != len(test.output) {
|
2018-03-27 04:22:23 +07:00
|
|
|
t.Errorf("Test %d: Expected to receive struct with [%d] entries in AllowFrom, but got [%d] records", i, len(test.output), len(user.AllowFrom))
|
2016-12-01 05:03:08 +07:00
|
|
|
}
|
|
|
|
if len(res.AllowFrom) != len(test.output) {
|
2018-03-27 04:22:23 +07:00
|
|
|
t.Errorf("Test %d: Expected to receive struct with [%d] entries in AllowFrom, but got [%d] records", i, len(test.output), len(res.AllowFrom))
|
2016-12-01 05:03:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 05:23:47 +07:00
|
|
|
func TestGetByUsername(t *testing.T) {
|
|
|
|
// Create reg to refer to
|
2016-12-01 05:03:08 +07:00
|
|
|
reg, err := DB.Register(cidrslice{})
|
2016-11-17 05:23:47 +07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Registration failed, got error [%v]", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
regUser, err := DB.GetByUsername(reg.Username)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not get test user, got error [%v]", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reg.Username != regUser.Username {
|
|
|
|
t.Errorf("GetByUsername username [%q] did not match the original [%q]", regUser.Username, reg.Username)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reg.Subdomain != regUser.Subdomain {
|
|
|
|
t.Errorf("GetByUsername subdomain [%q] did not match the original [%q]", regUser.Subdomain, reg.Subdomain)
|
|
|
|
}
|
|
|
|
|
|
|
|
// regUser password already is a bcrypt hash
|
2016-11-23 22:11:31 +07:00
|
|
|
if !correctPassword(reg.Password, regUser.Password) {
|
2016-11-17 05:23:47 +07:00
|
|
|
t.Errorf("The password [%s] does not match the hash [%s]", reg.Password, regUser.Password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 06:55:57 +07:00
|
|
|
func TestPrepareErrors(t *testing.T) {
|
2016-12-01 05:03:08 +07:00
|
|
|
reg, _ := DB.Register(cidrslice{})
|
2016-11-28 06:55:57 +07:00
|
|
|
tdb, err := sql.Open("testdb", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error: %v", err)
|
|
|
|
}
|
|
|
|
oldDb := DB.GetBackend()
|
|
|
|
DB.SetBackend(tdb)
|
|
|
|
defer DB.SetBackend(oldDb)
|
|
|
|
defer testdb.Reset()
|
|
|
|
|
|
|
|
_, err = DB.GetByUsername(reg.Username)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error, but didn't get one")
|
|
|
|
}
|
|
|
|
|
2018-01-22 14:53:07 +07:00
|
|
|
_, err = DB.GetTXTForDomain(reg.Subdomain)
|
2016-11-28 06:55:57 +07:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error, but didn't get one")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryExecErrors(t *testing.T) {
|
2016-12-01 05:03:08 +07:00
|
|
|
reg, _ := DB.Register(cidrslice{})
|
2016-11-28 06:55:57 +07:00
|
|
|
testdb.SetExecWithArgsFunc(func(query string, args []driver.Value) (result driver.Result, err error) {
|
|
|
|
return testResult{1, 0}, errors.New("Prepared query error")
|
|
|
|
})
|
|
|
|
|
|
|
|
testdb.SetQueryWithArgsFunc(func(query string, args []driver.Value) (result driver.Rows, err error) {
|
|
|
|
columns := []string{"Username", "Password", "Subdomain", "Value", "LastActive"}
|
|
|
|
return testdb.RowsFromSlice(columns, [][]driver.Value{}), errors.New("Prepared query error")
|
|
|
|
})
|
|
|
|
|
|
|
|
defer testdb.Reset()
|
|
|
|
|
|
|
|
tdb, err := sql.Open("testdb", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error: %v", err)
|
|
|
|
}
|
|
|
|
oldDb := DB.GetBackend()
|
|
|
|
|
|
|
|
DB.SetBackend(tdb)
|
|
|
|
defer DB.SetBackend(oldDb)
|
|
|
|
|
|
|
|
_, err = DB.GetByUsername(reg.Username)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error from exec, but got none")
|
|
|
|
}
|
|
|
|
|
2018-01-22 14:53:07 +07:00
|
|
|
_, err = DB.GetTXTForDomain(reg.Subdomain)
|
2016-11-28 06:55:57 +07:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error from exec in GetByDomain, but got none")
|
|
|
|
}
|
|
|
|
|
2016-12-01 05:03:08 +07:00
|
|
|
_, err = DB.Register(cidrslice{})
|
2016-11-28 06:55:57 +07:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error from exec in Register, but got none")
|
|
|
|
}
|
|
|
|
reg.Value = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
2019-06-12 19:41:02 +07:00
|
|
|
err = DB.Update(reg.ACMETxtPost)
|
2016-11-28 06:55:57 +07:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error from exec in Update, but got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryScanErrors(t *testing.T) {
|
2016-12-01 05:03:08 +07:00
|
|
|
reg, _ := DB.Register(cidrslice{})
|
2016-11-28 06:55:57 +07:00
|
|
|
|
|
|
|
testdb.SetExecWithArgsFunc(func(query string, args []driver.Value) (result driver.Result, err error) {
|
|
|
|
return testResult{1, 0}, errors.New("Prepared query error")
|
|
|
|
})
|
|
|
|
|
|
|
|
testdb.SetQueryWithArgsFunc(func(query string, args []driver.Value) (result driver.Rows, err error) {
|
|
|
|
columns := []string{"Only one"}
|
|
|
|
resultrows := "this value"
|
|
|
|
return testdb.RowsFromCSVString(columns, resultrows), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
defer testdb.Reset()
|
|
|
|
tdb, err := sql.Open("testdb", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error: %v", err)
|
|
|
|
}
|
|
|
|
oldDb := DB.GetBackend()
|
|
|
|
|
|
|
|
DB.SetBackend(tdb)
|
|
|
|
defer DB.SetBackend(oldDb)
|
|
|
|
|
|
|
|
_, err = DB.GetByUsername(reg.Username)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error from scan in, but got none")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadDBValues(t *testing.T) {
|
2016-12-01 05:03:08 +07:00
|
|
|
reg, _ := DB.Register(cidrslice{})
|
2016-11-28 06:55:57 +07:00
|
|
|
|
|
|
|
testdb.SetQueryWithArgsFunc(func(query string, args []driver.Value) (result driver.Rows, err error) {
|
|
|
|
columns := []string{"Username", "Password", "Subdomain", "Value", "LastActive"}
|
|
|
|
resultrows := "invalid,invalid,invalid,invalid,"
|
|
|
|
return testdb.RowsFromCSVString(columns, resultrows), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
defer testdb.Reset()
|
|
|
|
tdb, err := sql.Open("testdb", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got error: %v", err)
|
|
|
|
}
|
|
|
|
oldDb := DB.GetBackend()
|
|
|
|
|
|
|
|
DB.SetBackend(tdb)
|
|
|
|
defer DB.SetBackend(oldDb)
|
|
|
|
|
|
|
|
_, err = DB.GetByUsername(reg.Username)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error from scan in, but got none")
|
|
|
|
}
|
|
|
|
|
2018-01-22 14:53:07 +07:00
|
|
|
_, err = DB.GetTXTForDomain(reg.Subdomain)
|
2016-11-28 06:55:57 +07:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error from scan in GetByDomain, but got none")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 14:53:07 +07:00
|
|
|
func TestGetTXTForDomain(t *testing.T) {
|
2016-11-17 05:23:47 +07:00
|
|
|
// Create reg to refer to
|
2016-12-01 05:03:08 +07:00
|
|
|
reg, err := DB.Register(cidrslice{})
|
2016-11-17 05:23:47 +07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Registration failed, got error [%v]", err)
|
|
|
|
}
|
|
|
|
|
2018-01-22 14:53:07 +07:00
|
|
|
txtval1 := "___validation_token_received_from_the_ca___"
|
|
|
|
txtval2 := "___validation_token_received_YEAH_the_ca___"
|
|
|
|
|
|
|
|
reg.Value = txtval1
|
2019-06-12 19:41:02 +07:00
|
|
|
_ = DB.Update(reg.ACMETxtPost)
|
2018-01-22 14:53:07 +07:00
|
|
|
|
|
|
|
reg.Value = txtval2
|
2019-06-12 19:41:02 +07:00
|
|
|
_ = DB.Update(reg.ACMETxtPost)
|
2018-01-22 14:53:07 +07:00
|
|
|
|
|
|
|
regDomainSlice, err := DB.GetTXTForDomain(reg.Subdomain)
|
2016-11-17 05:23:47 +07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not get test user, got error [%v]", err)
|
|
|
|
}
|
|
|
|
if len(regDomainSlice) == 0 {
|
2018-01-22 14:53:07 +07:00
|
|
|
t.Errorf("No rows returned for GetTXTForDomain [%s]", reg.Subdomain)
|
2016-11-17 05:23:47 +07:00
|
|
|
}
|
|
|
|
|
2018-01-22 14:53:07 +07:00
|
|
|
var val1found = false
|
|
|
|
var val2found = false
|
|
|
|
for _, v := range regDomainSlice {
|
|
|
|
if v == txtval1 {
|
|
|
|
val1found = true
|
|
|
|
}
|
|
|
|
if v == txtval2 {
|
|
|
|
val2found = true
|
|
|
|
}
|
2016-11-17 05:23:47 +07:00
|
|
|
}
|
2018-01-22 14:53:07 +07:00
|
|
|
if !val1found {
|
|
|
|
t.Errorf("No TXT value found for val1")
|
2016-11-17 05:23:47 +07:00
|
|
|
}
|
2018-01-22 14:53:07 +07:00
|
|
|
if !val2found {
|
|
|
|
t.Errorf("No TXT value found for val2")
|
2016-11-17 05:23:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not found
|
2018-01-22 14:53:07 +07:00
|
|
|
regNotfound, _ := DB.GetTXTForDomain("does-not-exist")
|
2016-11-17 05:23:47 +07:00
|
|
|
if len(regNotfound) > 0 {
|
|
|
|
t.Errorf("No records should be returned.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
|
|
// Create reg to refer to
|
2016-12-01 05:03:08 +07:00
|
|
|
reg, err := DB.Register(cidrslice{})
|
2016-11-17 05:23:47 +07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Registration failed, got error [%v]", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
regUser, err := DB.GetByUsername(reg.Username)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not get test user, got error [%v]", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set new values (only TXT should be updated) (matches by username and subdomain)
|
|
|
|
|
|
|
|
validTXT := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
|
|
|
|
|
|
regUser.Password = "nevergonnagiveyouup"
|
|
|
|
regUser.Value = validTXT
|
|
|
|
|
2019-06-12 19:41:02 +07:00
|
|
|
err = DB.Update(regUser.ACMETxtPost)
|
2016-11-17 05:23:47 +07:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("DB Update failed, got error: [%v]", err)
|
|
|
|
}
|
|
|
|
}
|