pdns-auth-proxy/auth_test.go

92 lines
2.4 KiB
Go

package main
import (
"log"
"os"
"testing"
"github.com/pyke369/golang-support/uconfig"
)
var fakeserver *HTTPServer
func init() {
config, err := uconfig.New("pdns-proxy-test.conf")
if err != nil {
log.Println(err)
os.Exit(1)
}
fakeserver = NewHTTPServer(
"127.0.0.01:8080",
"fixtures/test/server-key.pem",
"fixtures/test/server-cert.pem",
"",
"fixtures/test/ca.crt",
"",
"",
3,
3600,
)
populateHTTPServerAcls(config, fakeserver)
populateHTTPServerProfiles(config, fakeserver)
go fakeserver.Run()
}
// Testing Functions
func TestAuthRegexpProfiles(t *testing.T) {
v := fakeserver.getProfiles("validserver")
if len(v) != 1 || v[0] != "testS2S" {
t.Errorf("cannot valid test profile")
}
v = fakeserver.getProfiles("bidule.example.com")
if len(v) != 0 {
t.Errorf("too many validations")
}
}
func TestAuth(t *testing.T) {
for k, testCase := range []struct {
path, user, method string
expected bool
message string
}{
{"zones/specificdomain.example", "validserver", "POST", true, "valid user and path"},
{"zones/dev.example.com", "validserver", "GET", true, "valid user and path"},
{"zones/developper.example.com", "validserver", "GET", false, "valid user and invalid path"},
{"zones/otherdomain.example", "validserver", "GET", false, "valid user and invalid path"},
{"zones/dev.example.com", "validserver", "POST", false, "valid user, valid path, invalid method"},
{"zones/specificdomain.example", "invalidserver", "GET", false, "invalid user"},
} {
if fakeserver.nativeValidAuth(testCase.path, testCase.user, testCase.method) != testCase.expected {
result := "worked"
if testCase.expected {
result = "failed"
}
t.Errorf("%s but it %s on URL %d", testCase.message, result, k+1)
}
}
}
func TestInArray(t *testing.T) {
for k, testCase := range []struct {
search []string
list []string
expected bool
message string
}{
{[]string{"apple", "orange"}, []string{"kiwi", "apple"}, true, "apple should have matched"},
{[]string{"apple", "orange"}, []string{"kiwi", "apple"}, true, "apple should have matched"},
{[]string{"citrus", "orange"}, []string{"kiwi", "apple"}, false, "nothing should have matched"},
} {
if inArray(testCase.search, testCase.list) != testCase.expected {
result := "worked"
if testCase.expected {
result = "failed"
}
t.Errorf("%s but it %s on test %d", testCase.message, result, k+1)
}
}
}