change resolvedFromSearch content (#1629)

This commit is contained in:
Gerard Nguyen
2024-09-30 11:22:10 +10:00
committed by GitHub
parent 668b7ed0b2
commit 196ebcaccc
2 changed files with 37 additions and 1 deletions
+11 -1
View File
@@ -177,7 +177,7 @@ func queryDNS(name, query, server string) DNSEntry {
// remember the search domain that resolved the query
// e.g. foo.test.com -> test.com
entry.Search = strings.Replace(query, name, "", 1)
entry.Search = extractSearchFromFQDN(query, name)
// populate record detail
switch rec {
@@ -207,3 +207,13 @@ func queryDNS(name, query, server string) DNSEntry {
func (c *CollectHostDNS) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}
func extractSearchFromFQDN(fqdn, name string) string {
// no search domain
if fqdn == name {
return ""
}
search := strings.TrimPrefix(fqdn, name+".") // remove name
search = strings.TrimSuffix(search, ".") // remove root dot
return search
}
+26
View File
@@ -0,0 +1,26 @@
package collect
import (
"testing"
)
func TestExtractSearchFromFQDN(t *testing.T) {
tests := []struct {
fqdn string
name string
expected string
}{
{"foo.com.", "foo.com", ""},
{"bar.com", "bar.com", ""},
{"*.foo.testcluster.net.", "*", "foo.testcluster.net"},
}
for _, test := range tests {
t.Run(test.fqdn, func(t *testing.T) {
result := extractSearchFromFQDN(test.fqdn, test.name)
if result != test.expected {
t.Errorf("extractSearchFromFQDN(%q, %q) = %q; want %q", test.fqdn, test.name, result, test.expected)
}
})
}
}