1package dnsbl
2
3import (
4 "context"
5 "net"
6 "testing"
7
8 "github.com/mjl-/mox/dns"
9)
10
11func TestDNSBL(t *testing.T) {
12 ctx := context.Background()
13
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"},
19 },
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!"},
23 },
24 }
25
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)
32 }
33
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)
40 }
41
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)
46 }
47
48 // ../rfc/5782:357
49 if err := CheckHealth(ctx, resolver, dns.Domain{ASCII: "example.com"}); err != nil {
50 t.Fatalf("dnsbl not healthy: %v", err)
51 }
52 if err := CheckHealth(ctx, resolver, dns.Domain{ASCII: "example.org"}); err == nil {
53 t.Fatalf("bad dnsbl is healthy")
54 }
55
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.
59 },
60 }
61 if err := CheckHealth(ctx, unhealthyResolver, dns.Domain{ASCII: "example.com"}); err == nil {
62 t.Fatalf("bad dnsbl is healthy")
63 }
64}
65