92 lines
1.7 KiB
Go
92 lines
1.7 KiB
Go
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")
|
|
}
|
|
}
|