first impementation of tests

This commit is contained in:
Niklas 2021-01-08 23:35:04 +01:00
parent 644e0ce398
commit 9adc685a73
4 changed files with 116 additions and 20 deletions

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