Skip to content

Commit

Permalink
client: imp code, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Jul 17, 2023
1 parent b161346 commit 02d64b1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 97 deletions.
11 changes: 1 addition & 10 deletions internal/client/addrproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ type DefaultAddrProc struct {
// isClosed is set to true once the address processor is closed.
isClosed bool

// useRDNS, if true, enables resolving of client IP addresses using reverse
// DNS.
useRDNS bool

// usePrivateRDNS, if true, enables resolving of private client IP addresses
// using reverse DNS.
usePrivateRDNS bool
Expand Down Expand Up @@ -140,7 +136,6 @@ func NewDefaultAddrProc(c *DefaultAddrProcConfig) (p *DefaultAddrProc) {
whois: &whois.Empty{},
privateSubnets: c.PrivateSubnets,
usePrivateRDNS: c.UsePrivateRDNS,
useRDNS: c.UseRDNS,
}

if c.UseRDNS {
Expand Down Expand Up @@ -258,11 +253,7 @@ func (p *DefaultAddrProc) processRDNS(ip netip.Addr) (host string) {
// shouldResolve returns false if ip is a loopback address, or ip is private and
// resolving of private addresses is disabled.
func (p *DefaultAddrProc) shouldResolve(ip netip.Addr) (ok bool) {
if ip.IsLoopback() {
return false
}

return p.useRDNS &&
return !ip.IsLoopback() &&
(p.usePrivateRDNS || !p.privateSubnets.Contains(ip.AsSlice()))
}

Expand Down
68 changes: 0 additions & 68 deletions internal/client/addrproc_internal_test.go

This file was deleted.

69 changes: 50 additions & 19 deletions internal/client/addrproc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,57 @@ func TestEmptyAddrProc(t *testing.T) {
func TestDefaultAddrProc_Process_rDNS(t *testing.T) {
t.Parallel()

privateIP := netip.MustParseAddr("192.168.0.1")

testCases := []struct {
rdnsErr error
name string
host string
wantUpd bool
rdnsErr error
ip netip.Addr
name string
host string
usePrivate bool
wantUpd bool
}{{
rdnsErr: nil,
name: "success",
host: testHost,
wantUpd: true,
rdnsErr: nil,
ip: testIP,
name: "success",
host: testHost,
usePrivate: false,
wantUpd: true,
}, {
rdnsErr: nil,
ip: testIP,
name: "no_host",
host: "",
usePrivate: false,
wantUpd: false,
}, {
rdnsErr: nil,
name: "no_host",
host: "",
wantUpd: false,
rdnsErr: nil,
ip: netip.MustParseAddr("127.0.0.1"),
name: "localhost",
host: "",
usePrivate: false,
wantUpd: false,
}, {
rdnsErr: errors.Error("rdns error"),
name: "rdns_error",
host: "",
wantUpd: false,
rdnsErr: nil,
ip: privateIP,
name: "private_ignored",
host: "",
usePrivate: false,
wantUpd: false,
}, {
rdnsErr: nil,
ip: privateIP,
name: "private_processed",
host: "private.example",
usePrivate: true,
wantUpd: true,
}, {
rdnsErr: errors.Error("rdns error"),
ip: testIP,
name: "rdns_error",
host: "",
usePrivate: false,
wantUpd: false,
}}

for _, tc := range testCases {
Expand All @@ -82,19 +113,19 @@ func TestDefaultAddrProc_Process_rDNS(t *testing.T) {
OnUpdateAddress: newOnUpdateAddress(tc.wantUpd, updIPCh, updHostCh, updInfoCh),
},
UseRDNS: true,
UsePrivateRDNS: true,
UsePrivateRDNS: tc.usePrivate,
UseWHOIS: false,
})
testutil.CleanupAndRequireSuccess(t, p.Close)

p.Process(testIP)
p.Process(tc.ip)

if !tc.wantUpd {
return
}

gotIP, _ := testutil.RequireReceive(t, updIPCh, testTimeout)
assert.Equal(t, testIP, gotIP)
assert.Equal(t, tc.ip, gotIP)

gotHost, _ := testutil.RequireReceive(t, updHostCh, testTimeout)
assert.Equal(t, tc.host, gotHost)
Expand Down

0 comments on commit 02d64b1

Please sign in to comment.