Debian Codegraph @

cmd/atomdns/handlers/acl/acl_test.go

raw
1 package acl_test
2
3 import (
4 "context"
5 "testing"
6
7 "codeberg.org/miekg/dns"
8 "codeberg.org/miekg/dns/cmd/atomdns/atomtest"
9 "codeberg.org/miekg/dns/cmd/atomdns/handlers/acl"
10 "codeberg.org/miekg/dns/cmd/atomdns/internal/dnsserver"
11 "codeberg.org/miekg/dns/dnstest"
12 "codeberg.org/miekg/dns/dnsutil"
13 )
14
15 var testcases = []struct {
16 name string
17 config string
18 qtype uint16
19 setup func() context.Context
20
21 rcode int
22 extendedError uint16
23 noResponse bool
24 }{
25 {
26 name: "block all",
27 config: `acl {
28 block
29 }`,
30 rcode: dns.RcodeRefused,
31 },
32 {
33 name: "blocklist block",
34 config: `acl {
35 block A 198.51.100.0/16
36 }`,
37 rcode: dns.RcodeRefused,
38 extendedError: dns.ExtendedErrorBlocked,
39 },
40 {
41 name: "blocklist allowed",
42 config: `acl {
43 block A 192.168.0.0/16
44 }`,
45 },
46 {
47 name: "blocklist all blocked",
48 config: `acl {
49 block 198.51.100.0/16
50 }`,
51 qtype: dns.TypeAAAA,
52 rcode: dns.RcodeRefused,
53 extendedError: dns.ExtendedErrorBlocked,
54 },
55 {
56 name: "block A and allow AAAA",
57 config: `acl {
58 block A 198.51.100.0/16
59 allow AAAA 198.51.100.0/16
60 allow TXT
61 }`,
62 rcode: dns.RcodeRefused,
63 extendedError: dns.ExtendedErrorBlocked,
64 },
65 {
66 name: "block A and allow AAAA",
67 config: `acl {
68 block A 198.51.100.0/16
69 allow AAAA 198.51.100.0/16
70 allow TXT
71 }`,
72 qtype: dns.TypeAAAA,
73 },
74 {
75 name: "block A, not TXT",
76 config: `acl {
77 block A
78 allow TXT
79 }`,
80 rcode: dns.RcodeRefused,
81 extendedError: dns.ExtendedErrorBlocked,
82 },
83 {
84 name: "block A, not TXT",
85 config: `acl {
86 block A
87 allow TXT
88 }`,
89 qtype: dns.TypeTXT,
90 },
91 {
92 name: "ctx: block Cambridge",
93 setup: func() context.Context { return context.WithValue(context.TODO(), "geoip/city", "Cambridge") },
94 config: `acl {
95 block geoip/city Cambridge
96 }`,
97 rcode: dns.RcodeRefused,
98 },
99 {
100 name: "ctx: block Cambridge",
101 setup: func() context.Context {
102 return context.WithValue(context.WithValue(context.TODO(), "geoip/country/eu", true), "geoip/city", "Amsterdam")
103 },
104 config: `acl {
105 block geoip/city Cambridge
106 allow geoip/country/eu true
107 }`,
108 },
109 {
110 name: "block opcode QUERY",
111 config: `acl {
112 block QUERY
113 }`,
114 rcode: dns.RcodeRefused,
115 },
116 {
117 name: "block opcode UPDATE", // we don't send that, so this is allowed
118 config: `acl {
119 block UPDATE
120 }`,
121 },
122 }
123
124 func TestAcl(t *testing.T) {
125 for _, tc := range testcases {
126 t.Run(tc.name, func(t *testing.T) {
127 a := new(acl.Acl)
128 co := dnsserver.NewTestController(tc.config)
129 a.Setup(co)
130
131 tw := dnstest.NewTestRecorder()
132 m := new(dns.Msg)
133 if tc.qtype == 0 {
134 tc.qtype = dns.TypeA
135 }
136 dnsutil.SetQuestion(m, "www.example.org.", tc.qtype)
137
138 ctx := context.TODO()
139 if tc.setup != nil {
140 ctx = tc.setup()
141 }
142 a.HandlerFunc(atomtest.Echo).ServeDNS(ctx, tw, m)
143
144 tw.Msg.Unpack()
145 if tw.Msg.Rcode != uint16(tc.rcode) {
146 t.Errorf("rcode mismatch want %d, got %d", tc.rcode, tw.Msg.Rcode)
147 }
148 if tc.noResponse && tw.Msg != nil {
149 t.Errorf("responded to client when not expected")
150 }
151 if tc.extendedError != 0 {
152 for _, p := range tw.Msg.Pseudo {
153 if ede, ok := p.(*dns.EDE); ok {
154 if ede.InfoCode != tc.extendedError {
155 t.Errorf("expected extended error %d, got %d", ede.InfoCode, tc.extendedError)
156 }
157 }
158 }
159 }
160 })
161 }
162 }