Skip to content

Commit bb14e40

Browse files
committed
pan: add unit tests for DNS resolver
1 parent 0de568a commit bb14e40

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

pkg/pan/dns_txt_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2022 ETH Zurich
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pan
16+
17+
import (
18+
"context"
19+
"net"
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestDNSResolver(t *testing.T) {
26+
cases := []struct {
27+
name string
28+
assertErr assert.ErrorAssertionFunc
29+
expected scionAddr
30+
}{
31+
{"example.com", assert.NoError, mustParse("1-ff00:0:f00,[192.0.2.1]")},
32+
{"example.net", assert.NoError, mustParse("1-ff00:0:ba5,[192.0.2.1]")},
33+
{"dummy4", assertErrHostNotFound, scionAddr{}},
34+
{"barbaz", assertErrHostNotFound, scionAddr{}},
35+
}
36+
var m *mockResolver
37+
resolver := &dnsResolver{res: m}
38+
for _, c := range cases {
39+
actual, err := resolver.Resolve(context.TODO(), c.name)
40+
if !c.assertErr(t, err) {
41+
continue
42+
}
43+
assert.Equal(t, c.expected, actual)
44+
}
45+
}
46+
47+
func TestDNSResolverInvalid(t *testing.T) {
48+
r := &dnsResolver{res: nil}
49+
_, err := r.Resolve(context.TODO(), "example.com")
50+
assert.Error(t, err)
51+
}
52+
53+
type mockResolver struct {
54+
net.Resolver
55+
}
56+
57+
// LookupTXT mocks requesting the DNS TXT records for the given domain name.
58+
func (r *mockResolver) LookupTXT(ctx context.Context, name string) ([]string, error) {
59+
v, ok := map[string][]string{
60+
"example.com.": {
61+
"NS=ns74430548",
62+
"doodle-site-verification=t4SRCkhsSDk_Ec9BPAr4xWQvYqoYJSLuoMmWLBdKqS0",
63+
"scion=1-ff00:0:f00,[192.0.2.1]",
64+
},
65+
"example.net.": {
66+
"scion=1-ff00:0:ba5,[192.0.2.1]",
67+
"BOOM_verify_3ovqPKkST76TzF2c7b13YA",
68+
"v=spf1 include:_id.example.net ip4:192.0.2.38 ip4:192.0.2.197 ~all",
69+
},
70+
}[name]
71+
if !ok {
72+
return nil, &net.DNSError{IsNotFound: true}
73+
}
74+
return v, nil
75+
}

0 commit comments

Comments
 (0)