Compare commits

..

2 Commits

Author SHA1 Message Date
9adc685a73 first impementation of tests 2021-01-08 23:35:04 +01:00
644e0ce398 moved some stuff from main into own func 2021-01-08 23:34:16 +01:00
5 changed files with 158 additions and 58 deletions

View File

@@ -76,6 +76,47 @@ var anyRecordTypes = []uint16{
dns.TypeCAA,
}
func start(configPath string) {
config, err := loadConfig(configPath)
if err != nil {
log.Fatalf("Failed to load config: %s\n", err.Error())
}
err = os.Chdir(filepath.Dir(configPath))
if err != nil {
log.Fatalf("Failed to goto config dir: %s", err.Error())
}
zones, err := loadZones(config.Zones)
if err != nil {
log.Fatalf("Failed to load zones: %s\n", err.Error())
}
aclList, err := createACLList(config.ACL)
if err != nil {
log.Fatalf("Failed to parse ACL rules: %s\n", err.Error())
}
blacklist := loadBlacklist(config.Blacklist)
var acmeMap *legoMap
if config.Lego.Enable {
acmeMap = startLEGOWebSever(config.Lego)
}
server := createServer(zones, *config, aclList, blacklist, acmeMap)
listenAndServer(server, config.Address)
if config.TLS.Enable {
listenAndServerTLS(server, config.TLS.Address, config.TLS.Cert, config.TLS.Key)
log.Printf("Start listening on tcp %s for tls", config.TLS.Address)
}
log.Printf("Start listening on udp %s and tcp %s\n", config.Address, config.Address)
}
func loadConfig(configPath string) (*config, error) {
file, err := ioutil.ReadFile(configPath)
if err != nil {
@@ -384,44 +425,7 @@ func main() {
configPath := flag.String("c", "/etc/cool-dns/config.yaml", "path to the config file")
flag.Parse()
config, err := loadConfig(*configPath)
if err != nil {
log.Fatalf("Failed to load config: %s\n", err.Error())
}
err = os.Chdir(filepath.Dir(*configPath))
if err != nil {
log.Fatalf("Failed to goto config dir: %s", err.Error())
}
zones, err := loadZones(config.Zones)
if err != nil {
log.Fatalf("Failed to load zones: %s\n", err.Error())
}
aclList, err := createACLList(config.ACL)
if err != nil {
log.Fatalf("Failed to parse ACL rules: %s\n", err.Error())
}
blacklist := loadBlacklist(config.Blacklist)
var acmeMap *legoMap
if config.Lego.Enable {
acmeMap = startLEGOWebSever(config.Lego)
}
server := createServer(zones, *config, aclList, blacklist, acmeMap)
listenAndServer(server, config.Address)
if config.TLS.Enable {
listenAndServerTLS(server, config.TLS.Address, config.TLS.Cert, config.TLS.Key)
log.Printf("Start listening on tcp %s for tls", config.TLS.Address)
}
log.Printf("Start listening on udp %s and tcp %s\n", config.Address, config.Address)
start(*configPath)
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)

91
rr_test.go Normal file
View File

@@ -0,0 +1,91 @@
package main
import (
"net"
"testing"
"github.com/miekg/dns"
)
func init() {
start("test/rrConfig.yaml")
}
// Helper
func request(name string, rrType uint16) (*dns.Msg, error) {
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(name), rrType)
return dns.Exchange(m, "127.0.0.1:8053")
}
func containsA(haystack []dns.RR, name, ip string) bool {
searchIP := net.ParseIP(ip)
for _, v := range haystack {
if v.Header().Name == dns.Fqdn(name) && v.Header().Rrtype == dns.TypeA {
if t, ok := v.(*dns.A); ok {
if t.A.Equal(searchIP) {
return true
}
}
}
}
return false
}
func containsAAAA(haystack []dns.RR, name, ip string) bool {
searchIP := net.ParseIP(ip)
for _, v := range haystack {
if v.Header().Name == dns.Fqdn(name) && v.Header().Rrtype == dns.TypeAAAA {
if t, ok := v.(*dns.AAAA); ok {
if t.AAAA.Equal(searchIP) {
return true
}
}
}
}
return false
}
func TestNormalA(t *testing.T) {
res, err := request("example.com", dns.TypeA)
if err != nil {
t.Error(err)
}
if !containsA(res.Answer, "example.com", "1.2.3.1") || !containsA(res.Answer, "example.com", "1.2.3.2") {
t.FailNow()
}
}
func TestNormalAAAA(t *testing.T) {
res, err := request("example.com", dns.TypeAAAA)
if err != nil {
t.Error(err)
}
if !containsAAAA(res.Answer, "example.com", "2001:db8:10::1") {
t.FailNow()
}
}
func TestNormalSOA(t *testing.T) {
res, err := request("example.com", dns.TypeSOA)
if err != nil {
t.Error(err)
}
if len(res.Answer) != 1 {
t.Fatalf("Should only be 1 SOA got %d", len(res.Answer))
}
if soa, ok := res.Answer[0].(*dns.SOA); ok {
if soa.Ns != "ns.example.com." {
t.Fatal("Wrong SOA rr")
}
} else {
t.Fatal("Answer is not a SOA rr")
}
}

5
test/rrConfig.yaml Normal file
View File

@@ -0,0 +1,5 @@
zones:
- zone: example.com.
file: zonefile.txt
address: 0.0.0.0:8053

20
test/zonefile.txt Normal file
View File

@@ -0,0 +1,20 @@
$ORIGIN example.com.
$TTL 3600
example.com. IN SOA ns.example.com. username.example.com. ( 2020091025 7200 3600 1209600 3600 )
example.com. IN NS ns
example.com. IN NS ns.somewhere.example.
example.com. IN MX 10 mail.example.com.
@ IN MX 20 mail2.example.com.
@ IN MX 50 mail3
example.com. IN A 1.2.3.1
example.com. IN A 1.2.3.2
IN AAAA 2001:db8:10::1
ns IN A 1.2.3.3
IN AAAA 2001:db8:10::2
www IN CNAME example.com.
wwwtest IN CNAME www
mail IN A 1.2.3.4
mail2 IN A 1.2.3.5
mail3 IN A 1.2.3.6
*.www IN A 1.2.3.7
a.www IN A 1.2.3.8

View File

@@ -1,20 +0,0 @@
$ORIGIN example.com. ; designates the start of this zone file in the namespace
$TTL 3600 ; default expiration time (in seconds) of all RRs without their own TTL value
example.com. IN SOA ns.example.com. username.example.com. ( 2020091025 7200 3600 1209600 3600 )
example.com. IN NS ns ; ns.example.com is a nameserver for example.com
example.com. IN NS ns.somewhere.example. ; ns.somewhere.example is a backup nameserver for example.com
example.com. IN MX 10 mail.example.com. ; mail.example.com is the mailserver for example.com
@ IN MX 20 mail2.example.com. ; equivalent to above line, "@" represents zone origin
@ IN MX 50 mail3 ; equivalent to above line, but using a relative host name
example.com. IN A 192.0.2.1 ; IPv4 address for example.com
example.com. IN A 192.0.3.1 ; IPv4 address for example.com
IN AAAA 2001:db8:10::1 ; IPv6 address for example.com
ns IN A 192.0.2.2 ; IPv4 address for ns.example.com
IN AAAA 2001:db8:10::2 ; IPv6 address for ns.example.com
www IN CNAME example.com. ; www.example.com is an alias for example.com
wwwtest IN CNAME www ; wwwtest.example.com is another alias for www.example.com
mail IN A 192.0.2.3 ; IPv4 address for mail.example.com
mail2 IN A 192.0.2.4 ; IPv4 address for mail2.example.com
mail3 IN A 192.0.2.5 ; IPv4 address for mail3.example.com
*.www IN A 192.1.0.1
a.www IN A 192.1.0.11