Skip to content

Commit ab22e90

Browse files
committed
Add coredns tests
1 parent b22c301 commit ab22e90

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed

test/coredns/coredns.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package coredns
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net"
7+
)
8+
9+
func DefaultEnv(ctx context.Context, state *Request) map[string]interface{} {
10+
return map[string]interface{}{
11+
"incidr": func(ipStr, cidrStr string) (bool, error) {
12+
ip := net.ParseIP(ipStr)
13+
if ip == nil {
14+
return false, errors.New("first argument is not an IP address")
15+
}
16+
_, cidr, err := net.ParseCIDR(cidrStr)
17+
if err != nil {
18+
return false, err
19+
}
20+
return cidr.Contains(ip), nil
21+
},
22+
"metadata": func(label string) string {
23+
return ""
24+
},
25+
"type": state.Type,
26+
"name": state.Name,
27+
"class": state.Class,
28+
"proto": state.Proto,
29+
"size": state.Len,
30+
"client_ip": state.IP,
31+
"port": state.Port,
32+
"id": func() int { return int(state.Req.Id) },
33+
"opcode": func() int { return state.Req.Opcode },
34+
"do": state.Do,
35+
"bufsize": state.Size,
36+
"server_ip": state.LocalIP,
37+
"server_port": state.LocalPort,
38+
}
39+
}
40+
41+
type Request struct {
42+
Req *Msg
43+
W ResponseWriter
44+
Zone string
45+
}
46+
47+
func (r *Request) NewWithQuestion(name string, typ uint16) Request {
48+
return Request{}
49+
}
50+
51+
func (r *Request) IP() string {
52+
return ""
53+
}
54+
55+
func (r *Request) LocalIP() string {
56+
return ""
57+
}
58+
59+
func (r *Request) Port() string {
60+
return ""
61+
}
62+
63+
func (r *Request) LocalPort() string {
64+
return ""
65+
}
66+
67+
func (r *Request) RemoteAddr() string { return r.W.RemoteAddr().String() }
68+
69+
func (r *Request) LocalAddr() string { return r.W.LocalAddr().String() }
70+
71+
func (r *Request) Proto() string {
72+
return "udp"
73+
}
74+
75+
func (r *Request) Family() int {
76+
return 2
77+
}
78+
79+
func (r *Request) Do() bool {
80+
return true
81+
}
82+
83+
func (r *Request) Len() int { return 0 }
84+
85+
func (r *Request) Size() int {
86+
return 0
87+
}
88+
89+
func (r *Request) SizeAndDo(m *Msg) bool {
90+
return true
91+
}
92+
93+
func (r *Request) Scrub(reply *Msg) *Msg {
94+
return reply
95+
}
96+
97+
func (r *Request) Type() string {
98+
return ""
99+
}
100+
101+
func (r *Request) QType() uint16 {
102+
return 0
103+
}
104+
105+
func (r *Request) Name() string {
106+
return "."
107+
}
108+
109+
func (r *Request) QName() string {
110+
return "."
111+
}
112+
113+
func (r *Request) Class() string {
114+
return ""
115+
}
116+
117+
func (r *Request) QClass() uint16 {
118+
return 0
119+
}
120+
121+
func (r *Request) Clear() {
122+
}
123+
124+
func (r *Request) Match(reply *Msg) bool {
125+
return true
126+
}
127+
128+
type Msg struct {
129+
MsgHdr
130+
Compress bool `json:"-"`
131+
Question []Question
132+
Answer []RR
133+
Ns []RR
134+
Extra []RR
135+
}
136+
137+
type MsgHdr struct {
138+
Id uint16
139+
Response bool
140+
Opcode int
141+
Authoritative bool
142+
Truncated bool
143+
RecursionDesired bool
144+
RecursionAvailable bool
145+
Zero bool
146+
AuthenticatedData bool
147+
CheckingDisabled bool
148+
Rcode int
149+
}
150+
151+
type Question struct {
152+
Name string `dns:"cdomain-name"`
153+
Qtype uint16
154+
Qclass uint16
155+
}
156+
157+
type RR interface {
158+
Header() *RR_Header
159+
String() string
160+
}
161+
162+
type RR_Header struct {
163+
Name string `dns:"cdomain-name"`
164+
Rrtype uint16
165+
Class uint16
166+
Ttl uint32
167+
Rdlength uint16
168+
}
169+
170+
type ResponseWriter interface {
171+
LocalAddr() net.Addr
172+
RemoteAddr() net.Addr
173+
WriteMsg(*Msg) error
174+
Write([]byte) (int, error)
175+
Close() error
176+
TsigStatus() error
177+
TsigTimersOnly(bool)
178+
Hijack()
179+
}

test/coredns/coredns_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package coredns_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/antonmedv/expr"
8+
"github.com/antonmedv/expr/test/coredns"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestCoreDNS(t *testing.T) {
13+
env := coredns.DefaultEnv(context.Background(), &coredns.Request{})
14+
15+
tests := []struct {
16+
input string
17+
}{
18+
{`metadata('geoip/city/name') == 'Exampleshire'`},
19+
{`(type() == 'A' && name() == 'example.com') || client_ip() == '1.2.3.4'`},
20+
{`name() matches '^abc\\..*\\.example\\.com\\.$'`},
21+
{`type() in ['A', 'AAAA']`},
22+
{`incidr(client_ip(), '192.168.0.0/16')`},
23+
{`incidr(client_ip(), '127.0.0.0/24')`},
24+
}
25+
26+
for _, test := range tests {
27+
t.Run(test.input, func(t *testing.T) {
28+
_, err := expr.Compile(test.input, expr.Env(env))
29+
assert.NoError(t, err)
30+
})
31+
}
32+
}

0 commit comments

Comments
 (0)