Skip to content

Commit 88e8553

Browse files
committed
DNS search domain fallbacks: increase test coverage
Increase test coverage and cleanup
1 parent 10d43ba commit 88e8553

File tree

4 files changed

+70
-18
lines changed

4 files changed

+70
-18
lines changed

hinting/dns_search_domain_fallbacks.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Fallback:
4747
// if all configured IPs are private.
4848
ip, err := queryExternalIP()
4949
if err == nil {
50-
ips = append(ips, *ip)
50+
ips = append(ips, ip)
5151
}
5252
}
5353

@@ -96,16 +96,17 @@ func getPublicAddresses() (ips []netip.Addr) {
9696
return
9797
}
9898
for _, iface := range ifaces {
99-
addrs, err := iface.Addrs()
99+
ifaddrs, err := iface.Addrs()
100100
if err != nil {
101101
continue
102102
}
103-
for _, addr := range addrs {
104-
ip, err := netip.ParseAddr(addr.String())
105-
if err != nil {
103+
for _, ifaddr := range ifaddrs {
104+
ifaddr, ok := ifaddr.(*net.IPNet)
105+
if !ok {
106106
continue
107107
}
108-
if !ip.IsPrivate() {
108+
ip, ok := netip.AddrFromSlice(ifaddr.IP)
109+
if ok && !ip.IsPrivate() {
109110
ips = append(ips, ip)
110111
}
111112
}

hinting/dns_search_domain_fallbacks_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"strings"
1010
"testing"
11+
"time"
1112

1213
"github.com/stretchr/testify/assert"
1314
)
@@ -244,6 +245,45 @@ func TestDomainsFromHostnamesDerivation(t *testing.T) {
244245
}
245246
}
246247

248+
func TestSearchDomainFallbacks(t *testing.T) {
249+
dnsChanReadable := dispatcher.getDNSConfig()
250+
var regular []DNSInfo
251+
var fallback []DNSInfo
252+
253+
// get info from happy path
254+
r := <-dnsChanReadable
255+
regular = append(regular, r)
256+
// wait for dnsInfoDone to be closed to move to fallback
257+
time.Sleep(DNSInfoTimeout)
258+
259+
// get info from fallback
260+
select {
261+
case <-dnsInfoFallbackDone:
262+
// Fallback timed out
263+
case r, ok := <-dnsChanReadable:
264+
if ok {
265+
fallback = append(fallback, r)
266+
}
267+
}
268+
269+
t.Log(regular)
270+
t.Log(fallback)
271+
assert.Condition(t, func() bool { return len(regular) != 0 || len(fallback) != 0 },
272+
"Both regular host system and DNS based DNS Search Domain discovery failed.")
273+
}
274+
275+
func TestDNSLookupExternalIP(t *testing.T) {
276+
ip, err := queryExternalIP()
277+
assert.NoError(t, err, "queryExternalIP() failed with error %s.", err)
278+
assert.NotEmpty(t, ip, "queryExternalIP() did not return any external IP.")
279+
}
280+
281+
func TestDNSLookupAkaNS(t *testing.T) {
282+
ns, err := getAkaNS()
283+
assert.NoError(t, err, "getAkaNS() failed with error %s.", err)
284+
assert.NotEmpty(t, ns, "getAkaNS() did not return the nameserver IP.")
285+
}
286+
247287
// randIPFromCIDR returns a random host IP in the subnet specified by the CIDR
248288
func randIPFromCIDR(cidr string) (ip netip.Addr) {
249289
_, ipNet, err := net.ParseCIDR(cidr)

hinting/dns_search_domain_fallbacks_whois.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,13 @@ func extractEmailDomains(response string) (domains []string) {
111111
}
112112
}
113113
// filter out RIR domains
114-
return domainsFromHostnames(hostnames)
114+
filteredHostnames := hostnames[:0] // in place slice filter: https://go.dev/wiki/SliceTricks
115+
for _, hostname := range hostnames {
116+
for _, rir := range rirWHOIS {
117+
if !strings.HasSuffix(hostname, strings.TrimPrefix(rir, "whois.")) {
118+
filteredHostnames = append(filteredHostnames, hostname)
119+
}
120+
}
121+
}
122+
return domainsFromHostnames(filteredHostnames)
115123
}

hinting/hinting.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,20 @@ func initDispatcher() (dnsChan <-chan DNSInfo) {
172172
// Signal dnsInfoChan fallback senders after timeout
173173
dnsInfoTimeoutFallback := time.After(DNSInfoTimeoutFallback)
174174
go func() {
175-
select {
176-
case <-dnsInfoTimeout:
177-
// Signal senders about timeout
178-
close(dnsInfoDone)
179-
case <-dnsInfoTimeoutFallback:
180-
// Signal fallback about timeout
181-
close(dnsInfoFallbackDone)
182-
// Wait for remaining senders
183-
dnsInfoWriters.Wait()
184-
// Stop publishing new DNSInfo
185-
close(dnsInfoChan)
175+
for {
176+
select {
177+
case <-dnsInfoTimeout:
178+
// Signal senders about timeout
179+
close(dnsInfoDone)
180+
case <-dnsInfoTimeoutFallback:
181+
// Signal fallback about timeout
182+
close(dnsInfoFallbackDone)
183+
// Wait for remaining senders
184+
dnsInfoWriters.Wait()
185+
// Stop publishing new DNSInfo
186+
close(dnsInfoChan)
187+
return
188+
}
186189
}
187190
}()
188191
return

0 commit comments

Comments
 (0)