| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"os" |
| 8 |
"os/exec" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
"codeberg.org/miekg/dns" |
| 14 |
"codeberg.org/miekg/dns/cmd/atomdns/atomtest" |
| 15 |
"codeberg.org/miekg/dns/internal/dnsperf" |
| 16 |
) |
| 17 |
|
| 18 |
const Conffile = ` |
| 19 |
{ |
| 20 |
dns { |
| 21 |
addr [::]:%s |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
whoami.example.org { |
| 26 |
whoami |
| 27 |
} |
| 28 |
` |
| 29 |
|
| 30 |
func TestAtomdnsPerf(t *testing.T) { |
| 31 |
const count = 8 |
| 32 |
ports := [2]string{"8053", "8054"} |
| 33 |
dir := t.TempDir() |
| 34 |
conffile := dir + "/Conffile" |
| 35 |
|
| 36 |
for p, network := range []string{"udp", "tcp"} { |
| 37 |
os.WriteFile(conffile, fmt.Appendf(nil, Conffile, ports[p]), 0600) |
| 38 |
|
| 39 |
t.Run("atomdns-"+network, func(t *testing.T) { |
| 40 |
timeout := count*2*time.Second + 5*time.Second |
| 41 |
ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 42 |
|
| 43 |
if _, err := os.Stat("./atomdns"); err != nil { |
| 44 |
t.Skip("no atomdns binary found in .") |
| 45 |
} |
| 46 |
|
| 47 |
cmd := exec.CommandContext(ctx, "./atomdns", conffile) |
| 48 |
go func() { |
| 49 |
if err := cmd.Run(); err != nil { |
| 50 |
if _, ok := err.(*exec.ExitError); !ok { |
| 51 |
log.Fatal("no working atomdns binary found in .") |
| 52 |
} |
| 53 |
} |
| 54 |
}() |
| 55 |
|
| 56 |
queries := strings.NewReader("whoami.example.org. A") |
| 57 |
if err := dnsperf.Run(t, queries, fmt.Sprintf("127.0.0.1:%s", ports[p]), network, 2*time.Second, count); err != nil { |
| 58 |
t.Fatal(err) |
| 59 |
} |
| 60 |
t.Logf("canceled executing: %s", network) |
| 61 |
cancel() |
| 62 |
}) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestAtomdns(t *testing.T) { |
| 67 |
testcases := []struct { |
| 68 |
qname string |
| 69 |
qtype uint16 |
| 70 |
qclass uint16 |
| 71 |
answerlen int |
| 72 |
}{ |
| 73 |
{"www.example.org.", dns.TypeA, dns.ClassINET, 2}, |
| 74 |
{"version.server.", dns.TypeTXT, dns.ClassCHAOS, 1}, |
| 75 |
{"version.server.", dns.TypeTXT, dns.ClassINET, 0}, // should be refused |
| 76 |
} |
| 77 |
s, cancel, err := atomtest.New(` |
| 78 |
10.0.0.0/24 { |
| 79 |
log |
| 80 |
whoami |
| 81 |
} |
| 82 |
|
| 83 |
example.org { |
| 84 |
log |
| 85 |
dbfile handlers/dbfile/zone/testdata/db.example.org { |
| 86 |
transfer |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
version.server/CH { |
| 91 |
log |
| 92 |
chaos |
| 93 |
} |
| 94 |
`) |
| 95 |
defer cancel() |
| 96 |
if err != nil { |
| 97 |
t.Fatal(err) |
| 98 |
} |
| 99 |
|
| 100 |
c := new(dns.Client) |
| 101 |
for _, tc := range testcases { |
| 102 |
t.Run(tc.qname+"/"+dns.TypeToString[tc.qtype], func(t *testing.T) { |
| 103 |
m := dns.NewMsg(tc.qname, tc.qtype) |
| 104 |
m.Question[0].Header().Class = tc.qclass |
| 105 |
|
| 106 |
r, _, err := c.Exchange(context.TODO(), m, "udp", s.Addr()[0]) |
| 107 |
if err != nil { |
| 108 |
t.Fatal(err) |
| 109 |
} |
| 110 |
if len(r.Answer) != tc.answerlen { |
| 111 |
t.Fatalf("expected %d answers, got %d", tc.answerlen, len(r.Answer)) |
| 112 |
} |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
if err := s.Reload(); err != nil { |
| 117 |
t.Fatal(err) |
| 118 |
} |
| 119 |
|
| 120 |
for _, tc := range testcases { |
| 121 |
t.Run("reload/"+tc.qname+"/"+dns.TypeToString[tc.qtype], func(t *testing.T) { |
| 122 |
m := dns.NewMsg(tc.qname, tc.qtype) |
| 123 |
m.Question[0].Header().Class = tc.qclass |
| 124 |
|
| 125 |
r, _, err := c.Exchange(context.TODO(), m, "udp", s.Addr()[0]) |
| 126 |
if err != nil { |
| 127 |
t.Fatal(err) |
| 128 |
} |
| 129 |
if len(r.Answer) != tc.answerlen { |
| 130 |
t.Fatalf("expected %d answers, got %d", tc.answerlen, len(r.Answer)) |
| 131 |
} |
| 132 |
}) |
| 133 |
} |
| 134 |
} |