1package iprev
2
3import (
4 "context"
5 "errors"
6 "net"
7 "strings"
8 "testing"
9
10 "github.com/mjl-/mox/dns"
11)
12
13func TestIPRev(t *testing.T) {
14 resolver := dns.MockResolver{
15 PTR: map[string][]string{
16 "10.0.0.1": {"basic.example."},
17 "10.0.0.4": {"absent.example.", "b.example."},
18 "10.0.0.5": {"other.example.", "c.example."},
19 "10.0.0.6": {"temperror.example.", "d.example."},
20 "10.0.0.7": {"temperror.example.", "temperror2.example."},
21 "10.0.0.8": {"other.example."},
22 "2001:db8::1": {"basic6.example."},
23 },
24 A: map[string][]string{
25 "basic.example.": {"10.0.0.1"},
26 "b.example.": {"10.0.0.4"},
27 "c.example.": {"10.0.0.5"},
28 "d.example.": {"10.0.0.6"},
29 "other.example.": {"10.9.9.9"},
30 "temperror.example.": {"10.0.0.99"},
31 "temperror2.example.": {"10.0.0.99"},
32 },
33 AAAA: map[string][]string{
34 "basic6.example.": {"2001:db8::1"},
35 },
36 Fail: []string{
37 "ptr 10.0.0.3",
38 "ptr 2001:db8::3",
39 "ip temperror.example.",
40 "ip temperror2.example.",
41 },
42 Authentic: []string{
43 "ptr 10.0.0.1",
44 "ptr 10.0.0.5", // Only IP to name authentic, not name to IP.
45 "ip basic.example.",
46 "ip d.example.", // Only name to IP authentic, not IP to name.
47 },
48 }
49
50 test := func(ip string, expStatus Status, expName string, expNames string, expAuth bool, expErr error) {
51 t.Helper()
52
53 status, name, names, auth, err := Lookup(context.Background(), resolver, net.ParseIP(ip))
54 if (err == nil) != (expErr == nil) || err != nil && !errors.Is(err, expErr) {
55 t.Fatalf("got err %v, expected err %v", err, expErr)
56 } else if err != nil {
57 return
58 } else if status != expStatus || name != expName || strings.Join(names, ",") != expNames || auth != expAuth {
59 t.Fatalf("got status %q, name %q, names %v, auth %v, expected %q %q %v %v", status, name, names, auth, expStatus, expName, expNames, expAuth)
60 }
61 }
62
63 test("10.0.0.1", StatusPass, "basic.example.", "basic.example.", true, nil)
64 test("10.0.0.2", StatusPermerror, "", "", false, ErrNoRecord)
65 test("10.0.0.3", StatusTemperror, "", "", false, ErrDNS)
66 test("10.0.0.4", StatusPass, "b.example.", "absent.example.,b.example.", false, nil)
67 test("10.0.0.5", StatusPass, "c.example.", "other.example.,c.example.", false, nil)
68 test("10.0.0.6", StatusPass, "d.example.", "temperror.example.,d.example.", false, nil)
69 test("10.0.0.7", StatusTemperror, "", "temperror.example.,temperror2.example.", false, ErrDNS)
70 test("10.0.0.8", StatusFail, "", "other.example.", false, nil)
71 test("2001:db8::1", StatusPass, "basic6.example.", "basic6.example.", false, nil)
72 test("2001:db8::2", StatusPermerror, "", "", false, ErrNoRecord)
73 test("2001:db8::3", StatusTemperror, "", "", false, ErrDNS)
74}
75