Skip to content

Commit

Permalink
Chore: add basic test for dns utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
till committed Nov 14, 2023
1 parent 9353834 commit f0908a3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
19 changes: 11 additions & 8 deletions pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,25 @@ func (d *DNSUtil) GetARecords(target string) ([]string, error) {
return list, err
}

func (d *DNSUtil) GetTxtRecords(target string) ([]string, error) {
func (d *DNSUtil) GetTxtRecords(target string) (list []string, err error) {
msg := createQuestion(target, x.TypeTXT)

result, err := d.makeQuery(msg)
if err != nil {
return
}

var list []string
if len(result.Answer) == 0 {
return
}

if len(result.Answer) > 0 {
for _, ans := range result.Answer {
if t, ok := ans.(*x.TXT); ok {
list = append(list, t.Txt...)
}
for _, ans := range result.Answer {
if t, ok := ans.(*x.TXT); ok {
list = append(list, t.Txt...)
}
}

return list, err
return
}

func (d *DNSUtil) makeQuery(msg *x.Msg) (*x.Msg, error) {
Expand Down
30 changes: 30 additions & 0 deletions pkg/dns/dns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dns_test

import (
"testing"

"github.com/Luzilla/dnsbl_exporter/pkg/dns"
x "github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/slog"
)

func TestDNSSuite(t *testing.T) {
t.Run("test=a", func(t *testing.T) {
d := dns.New(new(x.Client), "0.0.0.0:53", slog.Default())

aRecords, err := d.GetARecords("relay.heise.de")

assert.NoError(t, err)
assert.Greater(t, len(aRecords), 0)
})

t.Run("test=txt", func(t *testing.T) {
d := dns.New(new(x.Client), "0.0.0.0:53", slog.Default())

// DTAG dial-up
txtRecords, err := d.GetTxtRecords("85.198.214.79.zen.spamhaus.org")
assert.NoError(t, err)
assert.Greater(t, len(txtRecords), 0)
})
}

0 comments on commit f0908a3

Please sign in to comment.