8 "github.com/mjl-/mox/dns"
11func TestDNSBL(t *testing.T) {
12 ctx := context.Background()
14 resolver := dns.MockResolver{
15 A: map[string][]string{
16 "2.0.0.127.example.com.": {"127.0.0.2"}, // required for health
17 "1.0.0.10.example.com.": {"127.0.0.2"},
18 "b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.2.0.0.0.1.0.0.0.8.b.d.0.1.0.0.2.example.com.": {"127.0.0.2"},
20 TXT: map[string][]string{
21 "1.0.0.10.example.com.": {"listed!"},
22 "b.a.9.8.7.6.5.0.4.0.0.0.3.0.0.0.2.0.0.0.1.0.0.0.8.b.d.0.1.0.0.2.example.com.": {"listed!"},
26 if status, expl, err := Lookup(ctx, resolver, dns.Domain{ASCII: "example.com"}, net.ParseIP("10.0.0.1")); err != nil {
27 t.Fatalf("lookup: %v", err)
28 } else if status != StatusFail {
29 t.Fatalf("lookup, got status %v, expected fail", status)
30 } else if expl != "listed!" {
31 t.Fatalf("lookup, got explanation %q", expl)
34 if status, expl, err := Lookup(ctx, resolver, dns.Domain{ASCII: "example.com"}, net.ParseIP("2001:db8:1:2:3:4:567:89ab")); err != nil {
35 t.Fatalf("lookup: %v", err)
36 } else if status != StatusFail {
37 t.Fatalf("lookup, got status %v, expected fail", status)
38 } else if expl != "listed!" {
39 t.Fatalf("lookup, got explanation %q", expl)
42 if status, _, err := Lookup(ctx, resolver, dns.Domain{ASCII: "example.com"}, net.ParseIP("10.0.0.2")); err != nil {
43 t.Fatalf("lookup: %v", err)
44 } else if status != StatusPass {
45 t.Fatalf("lookup, got status %v, expected pass", status)
49 if err := CheckHealth(ctx, resolver, dns.Domain{ASCII: "example.com"}); err != nil {
50 t.Fatalf("dnsbl not healthy: %v", err)
52 if err := CheckHealth(ctx, resolver, dns.Domain{ASCII: "example.org"}); err == nil {
53 t.Fatalf("bad dnsbl is healthy")
56 unhealthyResolver := dns.MockResolver{
57 A: map[string][]string{
58 "1.0.0.127.example.com.": {"127.0.0.2"}, // Should not be present in healthy dnsbl.
61 if err := CheckHealth(ctx, unhealthyResolver, dns.Domain{ASCII: "example.com"}); err == nil {
62 t.Fatalf("bad dnsbl is healthy")