forked from coredns/coredns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_reverse_test.go
141 lines (119 loc) · 3.18 KB
/
server_reverse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package test
import (
"strings"
"testing"
"github.com/miekg/dns"
)
func TestClasslessReverse(t *testing.T) {
// 25 -> so anything above 1.127 won't be answered, below is OK.
corefile := `192.168.1.0/25:0 {
whoami
}`
s, udp, _, err := CoreDNSServerAndPorts(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer s.Stop()
tests := []struct {
addr string
rcode int
}{
{"192.168.1.0", dns.RcodeSuccess}, // in range
{"192.168.1.1", dns.RcodeSuccess}, // in range
{"192.168.1.127", dns.RcodeSuccess}, // in range
{"192.168.1.128", dns.RcodeRefused}, // out of range
{"192.168.1.129", dns.RcodeRefused}, // out of range
{"192.168.1.255", dns.RcodeRefused}, // out of range
{"192.168.2.0", dns.RcodeRefused}, // different zone
}
m := new(dns.Msg)
for i, tc := range tests {
inaddr, _ := dns.ReverseAddr(tc.addr)
m.SetQuestion(inaddr, dns.TypeA)
r, e := dns.Exchange(m, udp)
if e != nil {
t.Errorf("Test %d, expected no error, got %q", i, e)
}
if r.Rcode != tc.rcode {
t.Errorf("Test %d, expected %d, got %d for %s", i, tc.rcode, r.Rcode, tc.addr)
}
}
}
func TestReverse(t *testing.T) {
corefile := `192.168.1.0/24:0 {
whoami
}`
s, udp, _, err := CoreDNSServerAndPorts(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer s.Stop()
tests := []struct {
addr string
rcode int
}{
{"192.168.1.0", dns.RcodeSuccess},
{"192.168.1.1", dns.RcodeSuccess},
{"192.168.1.127", dns.RcodeSuccess},
{"192.168.1.128", dns.RcodeSuccess},
{"1.168.192.in-addr.arpa.", dns.RcodeSuccess},
{"2.168.192.in-addr.arpa.", dns.RcodeRefused},
}
m := new(dns.Msg)
for i, tc := range tests {
if !strings.HasSuffix(tc.addr, ".arpa.") {
inaddr, err := dns.ReverseAddr(tc.addr)
if err != nil {
t.Fatalf("Test %d, failed to convert %s", i, tc.addr)
}
tc.addr = inaddr
}
m.SetQuestion(tc.addr, dns.TypeA)
r, e := dns.Exchange(m, udp)
if e != nil {
t.Errorf("Test %d, expected no error, got %q", i, e)
}
if r.Rcode != tc.rcode {
t.Errorf("Test %d, expected %d, got %d for %s", i, tc.rcode, r.Rcode, tc.addr)
}
}
}
func TestReverseInAddr(t *testing.T) {
corefile := `1.168.192.in-addr.arpa:0 {
whoami
}`
s, udp, _, err := CoreDNSServerAndPorts(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer s.Stop()
tests := []struct {
addr string
rcode int
}{
{"192.168.1.0", dns.RcodeSuccess},
{"192.168.1.1", dns.RcodeSuccess},
{"192.168.1.127", dns.RcodeSuccess},
{"192.168.1.128", dns.RcodeSuccess},
{"1.168.192.in-addr.arpa.", dns.RcodeSuccess},
{"2.168.192.in-addr.arpa.", dns.RcodeRefused},
}
m := new(dns.Msg)
for i, tc := range tests {
if !strings.HasSuffix(tc.addr, ".arpa.") {
inaddr, err := dns.ReverseAddr(tc.addr)
if err != nil {
t.Fatalf("Test %d, failed to convert %s", i, tc.addr)
}
tc.addr = inaddr
}
m.SetQuestion(tc.addr, dns.TypeA)
r, e := dns.Exchange(m, udp)
if e != nil {
t.Errorf("Test %d, expected no error, got %q", i, e)
}
if r.Rcode != tc.rcode {
t.Errorf("Test %d, expected %d, got %d for %s", i, tc.rcode, r.Rcode, tc.addr)
}
}
}