Skip to content

Commit

Permalink
Chore: improve error handling and improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
till committed Nov 14, 2023
1 parent f0908a3 commit 99e4cd1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
31 changes: 21 additions & 10 deletions pkg/rbl/rbl.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rbl

import (
"fmt"
"net"
"sync"

Expand Down Expand Up @@ -70,23 +69,35 @@ func (rbl *Rbl) query(ip string, blacklist string, result *Rblresult) {

rbl.logger.Debug("About to query RBL", slog.String("rbl", blacklist), slog.String("ip", ip))

lookup := fmt.Sprintf("%s.%s", ip, blacklist)
lookup := ip + "." + blacklist
rbl.logger.Debug("Built lookup", slog.String("lookup", lookup))

res, err := rbl.util.GetARecords(lookup)
if len(res) > 0 {
result.Listed = true
if err != nil {
rbl.logger.Error("error occurred fetching A record", slog.String("msg", err.Error()))
result.Error = true
result.ErrorType = err
return
}

txt, _ := net.LookupTXT(lookup)
if len(txt) > 0 {
result.Text = txt[0]
}
if len(res) == 0 {
// ip is not listed
return
}

rbl.logger.Debug("ip is listed", slog.String("ip", ip))
result.Listed = true

// fetch (potential) reason
txt, err := net.LookupTXT(lookup)
if err != nil {
result.Error = true
result.ErrorType = err
rbl.logger.Error("error occurred fetching TXT record", slog.String("msg", err.Error()))
return
}

if len(txt) > 0 {
result.Text = txt[0]
}
}

func (rbl *Rbl) lookup(rblList string, targetHost string) []Rblresult {
Expand Down
41 changes: 33 additions & 8 deletions pkg/rbl/rbl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,43 @@ import (
"github.com/Luzilla/dnsbl_exporter/pkg/dns"
"github.com/Luzilla/dnsbl_exporter/pkg/rbl"
x "github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/slog"
)

func TestUpdate(t *testing.T) {
logger := slog.New(slog.NewTextHandler(os.Stderr))
func TestRblSuite(t *testing.T) {
t.Run("run=invalid_ip", func(t *testing.T) {
logger := slog.New(slog.NewTextHandler(os.Stderr))

d := dns.New(new(x.Client), "0.0.0.0:53", logger)
d := dns.New(new(x.Client), "0.0.0.0:53", logger)

r := rbl.New(d, logger)
r.Update("this.is.not.an.ip", []string{"cbl.abuseat.org"})
r := rbl.New(d, logger)
r.Update("this.is.not.an.ip", []string{"cbl.abuseat.org"})

if len(r.Results) > 0 {
t.Errorf("Got a result, but shouldn't have: %v", r.Results)
}
assert.Equal(t, 0, len(r.Results), "Got a result, but shouldn't have: %v", r.Results)
})

t.Run("run=error_result", func(t *testing.T) {
logger := slog.New(slog.NewTextHandler(os.Stderr))

d := dns.New(new(x.Client), "0.0.0.0:53", logger)

r := rbl.New(d, logger)
r.Update("79.214.198.85", []string{"zen.spamhaus.org"})

assert.Equal(t, 1, len(r.Results))

result := r.Results[0]

// assert the right RBL is in there
assert.Equal(t, "zen.spamhaus.org", result.Rbl)

// this is not an error as in transport/dialer
assert.False(t, result.Error)
assert.NoError(t, result.ErrorType)

// but the IP is listed
assert.True(t, result.Listed)
assert.Equal(t, "https://www.spamhaus.org/query/ip/79.214.198.85", result.Text)
})
}

0 comments on commit 99e4cd1

Please sign in to comment.