2016-11-28 03:09:13 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-28 04:21:46 +07:00
|
|
|
"os"
|
2018-08-12 22:49:17 +07:00
|
|
|
"syscall"
|
2016-11-28 03:09:13 +07:00
|
|
|
"testing"
|
2018-08-12 22:49:17 +07:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2016-11-28 03:09:13 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetupLogging(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
format string
|
|
|
|
level string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"text", "warning", "warning"},
|
|
|
|
{"json", "debug", "debug"},
|
|
|
|
{"text", "info", "info"},
|
|
|
|
{"json", "error", "error"},
|
|
|
|
{"text", "something", "warning"},
|
|
|
|
} {
|
|
|
|
setupLogging(test.format, test.level)
|
|
|
|
if log.GetLevel().String() != test.expected {
|
|
|
|
t.Errorf("Test %d: Expected loglevel %s but got %s", i, test.expected, log.GetLevel().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 04:21:46 +07:00
|
|
|
|
|
|
|
func TestReadConfig(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
inFile []byte
|
|
|
|
output DNSConfig
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]byte("[general]\nlisten = \":53\"\ndebug = true\n[api]\napi_domain = \"something.strange\""),
|
|
|
|
DNSConfig{
|
|
|
|
General: general{
|
|
|
|
Listen: ":53",
|
|
|
|
Debug: true,
|
|
|
|
},
|
|
|
|
API: httpapi{
|
|
|
|
Domain: "something.strange",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
[]byte("[\x00[[[[[[[[[de\nlisten =]"),
|
|
|
|
DNSConfig{},
|
|
|
|
},
|
|
|
|
} {
|
2024-12-15 18:00:37 +07:00
|
|
|
tmpfile, err := os.CreateTemp("", "acmedns")
|
2016-11-28 04:21:46 +07:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Could not create temporary file")
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
|
|
|
|
if _, err := tmpfile.Write(test.inFile); err != nil {
|
|
|
|
t.Error("Could not write to temporary file")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpfile.Close(); err != nil {
|
|
|
|
t.Error("Could not close temporary file")
|
|
|
|
}
|
2018-08-12 22:49:17 +07:00
|
|
|
ret, _ := readConfig(tmpfile.Name())
|
2016-11-28 04:21:46 +07:00
|
|
|
if ret.General.Listen != test.output.General.Listen {
|
|
|
|
t.Errorf("Test %d: Expected listen value %s, but got %s", i, test.output.General.Listen, ret.General.Listen)
|
|
|
|
}
|
|
|
|
if ret.API.Domain != test.output.API.Domain {
|
|
|
|
t.Errorf("Test %d: Expected HTTP API domain %s, but got %s", i, test.output.API.Domain, ret.API.Domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-02 22:04:16 +07:00
|
|
|
|
|
|
|
func TestGetIPListFromHeader(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
input string
|
|
|
|
output []string
|
|
|
|
}{
|
|
|
|
{"1.1.1.1, 2.2.2.2", []string{"1.1.1.1", "2.2.2.2"}},
|
|
|
|
{" 1.1.1.1 , 2.2.2.2", []string{"1.1.1.1", "2.2.2.2"}},
|
|
|
|
{",1.1.1.1 ,2.2.2.2", []string{"1.1.1.1", "2.2.2.2"}},
|
|
|
|
} {
|
|
|
|
res := getIPListFromHeader(test.input)
|
|
|
|
if len(res) != len(test.output) {
|
|
|
|
t.Errorf("Test %d: Expected [%d] items in return list, but got [%d]", i, len(test.output), len(res))
|
|
|
|
} else {
|
|
|
|
|
|
|
|
for j, vv := range test.output {
|
|
|
|
if res[j] != vv {
|
|
|
|
t.Errorf("Test %d: Expected return value [%v] but got [%v]", j, test.output, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 22:49:17 +07:00
|
|
|
|
|
|
|
func TestFileCheckPermissionDenied(t *testing.T) {
|
2024-12-15 18:00:37 +07:00
|
|
|
tmpfile, err := os.CreateTemp("", "acmedns")
|
2018-08-12 22:49:17 +07:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Could not create temporary file")
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpfile.Name())
|
2021-01-11 22:31:09 +07:00
|
|
|
_ = syscall.Chmod(tmpfile.Name(), 0000)
|
2018-08-12 22:49:17 +07:00
|
|
|
if fileIsAccessible(tmpfile.Name()) {
|
|
|
|
t.Errorf("File should not be accessible")
|
|
|
|
}
|
2021-01-11 22:31:09 +07:00
|
|
|
_ = syscall.Chmod(tmpfile.Name(), 0644)
|
2018-08-12 22:49:17 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileCheckNotExists(t *testing.T) {
|
|
|
|
if fileIsAccessible("/path/that/does/not/exist") {
|
|
|
|
t.Errorf("File should not be accessible")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileCheckOK(t *testing.T) {
|
2024-12-15 18:00:37 +07:00
|
|
|
tmpfile, err := os.CreateTemp("", "acmedns")
|
2018-08-12 22:49:17 +07:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Could not create temporary file")
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
if !fileIsAccessible(tmpfile.Name()) {
|
|
|
|
t.Errorf("File should be accessible")
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 23:48:39 +07:00
|
|
|
|
|
|
|
func TestPrepareConfig(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
input DNSConfig
|
|
|
|
shoulderror bool
|
|
|
|
}{
|
|
|
|
{DNSConfig{Database: dbsettings{Engine: "whatever", Connection: "whatever_too"}}, false},
|
|
|
|
{DNSConfig{Database: dbsettings{Engine: "", Connection: "whatever_too"}}, true},
|
|
|
|
{DNSConfig{Database: dbsettings{Engine: "whatever", Connection: ""}}, true},
|
|
|
|
} {
|
|
|
|
_, err := prepareConfig(test.input)
|
|
|
|
if test.shoulderror {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Test %d: Expected error with prepareConfig input data [%v]", i, test.input)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d: Expected no error with prepareConfig input data [%v]", i, test.input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|