1package dnsbl
2
3import (
4 "context"
5 "net"
6 "testing"
7
8 "github.com/mjl-/mox/dns"
9 "github.com/mjl-/mox/mlog"
10)
11
12func TestDNSBL(t *testing.T) {
13 ctx := context.Background()
14 log := mlog.New("dnsbl", nil)
15
16 resolver := dns.MockResolver{
17 A: map[string][]string{
18 "2.0.0.127.example.com.": {"127.0.0.2"}, // required for health
19 "1.0.0.10.example.com.": {"127.0.0.2"},
20 "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"},
21 },
22 TXT: map[string][]string{
23 "1.0.0.10.example.com.": {"listed!"},
24 "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!"},
25 },
26 }
27
28 if status, expl, err := Lookup(ctx, log.Logger, resolver, dns.Domain{ASCII: "example.com"}, net.ParseIP("10.0.0.1")); err != nil {
29 t.Fatalf("lookup: %v", err)
30 } else if status != StatusFail {
31 t.Fatalf("lookup, got status %v, expected fail", status)
32 } else if expl != "listed!" {
33 t.Fatalf("lookup, got explanation %q", expl)
34 }
35
36 if status, expl, err := Lookup(ctx, log.Logger, resolver, dns.Domain{ASCII: "example.com"}, net.ParseIP("2001:db8:1:2:3:4:567:89ab")); err != nil {
37 t.Fatalf("lookup: %v", err)
38 } else if status != StatusFail {
39 t.Fatalf("lookup, got status %v, expected fail", status)
40 } else if expl != "listed!" {
41 t.Fatalf("lookup, got explanation %q", expl)
42 }
43
44 if status, _, err := Lookup(ctx, log.Logger, resolver, dns.Domain{ASCII: "example.com"}, net.ParseIP("10.0.0.2")); err != nil {
45 t.Fatalf("lookup: %v", err)
46 } else if status != StatusPass {
47 t.Fatalf("lookup, got status %v, expected pass", status)
48 }
49
50 // ../rfc/5782:357
51 if err := CheckHealth(ctx, log.Logger, resolver, dns.Domain{ASCII: "example.com"}); err != nil {
52 t.Fatalf("dnsbl not healthy: %v", err)
53 }
54 if err := CheckHealth(ctx, log.Logger, resolver, dns.Domain{ASCII: "example.org"}); err == nil {
55 t.Fatalf("bad dnsbl is healthy")
56 }
57
58 unhealthyResolver := dns.MockResolver{
59 A: map[string][]string{
60 "1.0.0.127.example.com.": {"127.0.0.2"}, // Should not be present in healthy dnsbl.
61 },
62 }
63 if err := CheckHealth(ctx, log.Logger, unhealthyResolver, dns.Domain{ASCII: "example.com"}); err == nil {
64 t.Fatalf("bad dnsbl is healthy")
65 }
66}
67