Skip to content

Commit 6334048

Browse files
committed
search domain discovery: add tests
1 parent 8ae3fd0 commit 6334048

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

hinting/dns_search_domain_fallbacks.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ func domainsFromHostnames(hostnames []string) (domains []string) {
2222
continue
2323
}
2424
domainString = strings.Join([]string{label, domainString}, ".")
25+
// Filter out Effective ccTLDs (of the form "co.uk"), as we are only interested in the ETLD+1 domains
26+
if 5 == len(domainString) && // TODO: complete TLD specific exceptions, or directly use PSL
27+
(label == "co" ||
28+
label == "ac" ||
29+
label == "re" ||
30+
label == "ne") {
31+
// do not add country-code second-level ETLD domains to domains candidate list
32+
continue
33+
}
2534
if !slices.Contains(domains, domainString) {
2635
domains = append(domains, domainString)
2736
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package hinting
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"net/netip"
6+
"testing"
7+
)
8+
9+
func TestReverseLookupDomains(t *testing.T) {
10+
11+
testCases := []struct {
12+
name string
13+
values []struct {
14+
ip netip.Addr
15+
domain string
16+
}
17+
}{
18+
{
19+
name: "ETHZ",
20+
values: []struct {
21+
ip netip.Addr
22+
domain string
23+
}{
24+
{ip: netip.MustParseAddr("129.132.19.216"), domain: "ethz.ch"},
25+
{ip: netip.MustParseAddr("82.130.64.0"), domain: "ethz.ch"},
26+
{ip: netip.MustParseAddr("148.187.192.0"), domain: "ethz.ch"},
27+
},
28+
},
29+
{
30+
name: "PU",
31+
values: []struct {
32+
ip netip.Addr
33+
domain string
34+
}{
35+
{ip: netip.MustParseAddr("66.180.178.131"), domain: "princeton.edu"},
36+
},
37+
},
38+
{
39+
name: "VU",
40+
values: []struct {
41+
ip netip.Addr
42+
domain string
43+
}{
44+
{ip: netip.MustParseAddr("128.143.33.137"), domain: "virginia.edu"},
45+
{ip: netip.MustParseAddr("128.143.33.144"), domain: "virginia.edu"},
46+
},
47+
},
48+
{
49+
name: "SWITCH",
50+
values: []struct {
51+
ip netip.Addr
52+
domain string
53+
}{
54+
{ip: netip.MustParseAddr("130.59.31.80"), domain: "switch.ch"},
55+
},
56+
},
57+
{
58+
name: "KREONET",
59+
values: []struct {
60+
ip netip.Addr
61+
domain string
62+
}{
63+
{ip: netip.MustParseAddr("134.75.254.11"), domain: "kreonet.net"},
64+
{ip: netip.MustParseAddr("134.75.254.12"), domain: "kreonet.net"},
65+
},
66+
},
67+
{
68+
name: "KU",
69+
values: []struct {
70+
ip netip.Addr
71+
domain string
72+
}{
73+
{ip: netip.MustParseAddr("163.152.6.10"), domain: "korea.ac.kr"},
74+
},
75+
},
76+
}
77+
for _, tc := range testCases {
78+
t.Log(tc.name)
79+
for _, v := range tc.values {
80+
t.Log(v.ip)
81+
res := reverseLookupDomains(v.ip)
82+
t.Log(res)
83+
assert.Subset(t, res, []string{v.domain}, "")
84+
}
85+
}
86+
}
87+
88+
func TestDomainsFromHostnamesDerivation(t *testing.T) {
89+
90+
testCases := []struct {
91+
name string
92+
values []struct {
93+
hostnames []string
94+
domains []string
95+
}
96+
}{
97+
{
98+
name: "ETHZ",
99+
values: []struct {
100+
hostnames []string
101+
domains []string
102+
}{
103+
{hostnames: []string{"82-130-64-0.net4.ethz.ch."}, domains: []string{"net4.ethz.ch", "ethz.ch"}},
104+
{hostnames: []string{"service-id-api-cd-dcz1-server-4-b.ethz.ch."}, domains: []string{"ethz.ch"}},
105+
},
106+
},
107+
{
108+
name: "ETHZ",
109+
values: []struct {
110+
hostnames []string
111+
domains []string
112+
}{
113+
{hostnames: []string{"60.korea.ac.kr.", "sub.korea.ac.kr."}, domains: []string{"korea.ac.kr"}},
114+
},
115+
},
116+
}
117+
118+
for _, tc := range testCases {
119+
t.Log(tc.name)
120+
for _, v := range tc.values {
121+
t.Log(v.hostnames)
122+
res := domainsFromHostnames(v.hostnames)
123+
t.Log(res)
124+
assert.EqualValues(t, v.domains, res, "")
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)