2019-12-11 09:38:58 +00:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2021-03-31 13:00:47 +01:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2021-01-20 14:27:53 +00:00
|
|
|
"net"
|
2021-03-31 13:00:47 +01:00
|
|
|
"sync"
|
2019-12-11 09:38:58 +00:00
|
|
|
"testing"
|
2021-03-31 13:00:47 +01:00
|
|
|
"time"
|
2019-12-11 09:38:58 +00:00
|
|
|
|
2021-04-20 14:26:19 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghstrings"
|
2021-02-04 17:35:13 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
|
|
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
2021-03-31 13:00:47 +01:00
|
|
|
"github.com/AdguardTeam/golibs/cache"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-04-07 18:16:06 +01:00
|
|
|
"github.com/miekg/dns"
|
2019-12-11 09:38:58 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-03-31 13:00:47 +01:00
|
|
|
func TestRDNS_Begin(t *testing.T) {
|
|
|
|
aghtest.ReplaceLogLevel(t, log.DEBUG)
|
|
|
|
w := &bytes.Buffer{}
|
|
|
|
aghtest.ReplaceLogWriter(t, w)
|
|
|
|
|
|
|
|
ip1234, ip1235 := net.IP{1, 2, 3, 4}, net.IP{1, 2, 3, 5}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
cliIDIndex map[string]*Client
|
|
|
|
customChan chan net.IP
|
|
|
|
name string
|
|
|
|
wantLog string
|
|
|
|
req net.IP
|
|
|
|
wantCacheHit int
|
|
|
|
wantCacheMiss int
|
|
|
|
}{{
|
|
|
|
cliIDIndex: map[string]*Client{},
|
|
|
|
customChan: nil,
|
|
|
|
name: "cached",
|
|
|
|
wantLog: "",
|
|
|
|
req: ip1234,
|
|
|
|
wantCacheHit: 1,
|
|
|
|
wantCacheMiss: 0,
|
|
|
|
}, {
|
|
|
|
cliIDIndex: map[string]*Client{},
|
|
|
|
customChan: nil,
|
|
|
|
name: "not_cached",
|
|
|
|
wantLog: "rdns: queue is full",
|
|
|
|
req: ip1235,
|
|
|
|
wantCacheHit: 0,
|
|
|
|
wantCacheMiss: 1,
|
|
|
|
}, {
|
|
|
|
cliIDIndex: map[string]*Client{"1.2.3.5": {}},
|
|
|
|
customChan: nil,
|
|
|
|
name: "already_in_clients",
|
|
|
|
wantLog: "",
|
|
|
|
req: ip1235,
|
|
|
|
wantCacheHit: 0,
|
|
|
|
wantCacheMiss: 1,
|
|
|
|
}, {
|
|
|
|
cliIDIndex: map[string]*Client{},
|
|
|
|
customChan: make(chan net.IP, 1),
|
|
|
|
name: "add_to_queue",
|
|
|
|
wantLog: `rdns: "1.2.3.5" added to queue`,
|
|
|
|
req: ip1235,
|
|
|
|
wantCacheHit: 0,
|
|
|
|
wantCacheMiss: 1,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
w.Reset()
|
|
|
|
|
|
|
|
ipCache := cache.New(cache.Config{
|
|
|
|
EnableLRU: true,
|
|
|
|
MaxCount: defaultRDNSCacheSize,
|
|
|
|
})
|
|
|
|
ttl := make([]byte, binary.Size(uint64(0)))
|
|
|
|
binary.BigEndian.PutUint64(ttl, uint64(time.Now().Add(100*time.Hour).Unix()))
|
|
|
|
|
|
|
|
rdns := &RDNS{
|
|
|
|
ipCache: ipCache,
|
|
|
|
clients: &clientsContainer{
|
|
|
|
list: map[string]*Client{},
|
|
|
|
idIndex: tc.cliIDIndex,
|
2021-04-02 15:30:39 +01:00
|
|
|
ipToRC: map[string]*RuntimeClient{},
|
2021-04-20 14:26:19 +01:00
|
|
|
allTags: aghstrings.NewSet(),
|
2021-03-31 13:00:47 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
ipCache.Clear()
|
|
|
|
ipCache.Set(net.IP{1, 2, 3, 4}, ttl)
|
|
|
|
|
|
|
|
if tc.customChan != nil {
|
|
|
|
rdns.ipCh = tc.customChan
|
|
|
|
defer close(tc.customChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
rdns.Begin(tc.req)
|
|
|
|
assert.Equal(t, tc.wantCacheHit, ipCache.Stats().Hit)
|
|
|
|
assert.Equal(t, tc.wantCacheMiss, ipCache.Stats().Miss)
|
|
|
|
assert.Contains(t, w.String(), tc.wantLog)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 18:16:06 +01:00
|
|
|
// rDNSExchanger is a mock dnsforward.RDNSExchanger implementation for tests.
|
|
|
|
type rDNSExchanger struct {
|
|
|
|
aghtest.Exchanger
|
|
|
|
}
|
2021-03-31 13:00:47 +01:00
|
|
|
|
2021-04-07 18:16:06 +01:00
|
|
|
// Exchange implements dnsforward.RDNSExchanger interface for *RDNSExchanger.
|
|
|
|
func (e *rDNSExchanger) Exchange(ip net.IP) (host string, err error) {
|
|
|
|
req := &dns.Msg{
|
|
|
|
Question: []dns.Question{{
|
|
|
|
Name: ip.String(),
|
|
|
|
Qtype: dns.TypePTR,
|
|
|
|
}},
|
|
|
|
}
|
2021-03-31 13:00:47 +01:00
|
|
|
|
2021-04-07 18:16:06 +01:00
|
|
|
resp, err := e.Exchanger.Exchange(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-03-31 13:00:47 +01:00
|
|
|
|
2021-04-07 18:16:06 +01:00
|
|
|
if len(resp.Answer) == 0 {
|
|
|
|
return "", nil
|
2021-03-31 13:00:47 +01:00
|
|
|
}
|
2021-04-07 18:16:06 +01:00
|
|
|
|
|
|
|
return resp.Answer[0].Header().Name, nil
|
2021-03-31 13:00:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRDNS_WorkerLoop(t *testing.T) {
|
|
|
|
aghtest.ReplaceLogLevel(t, log.DEBUG)
|
|
|
|
w := &bytes.Buffer{}
|
|
|
|
aghtest.ReplaceLogWriter(t, w)
|
|
|
|
|
|
|
|
locUpstream := &aghtest.TestUpstream{
|
|
|
|
Reverse: map[string][]string{
|
2021-04-13 14:23:36 +01:00
|
|
|
"192.168.1.1": {"local.domain"},
|
|
|
|
"2a00:1450:400c:c06::93": {"ipv6.domain"},
|
2021-03-31 13:00:47 +01:00
|
|
|
},
|
|
|
|
}
|
2021-04-07 18:16:06 +01:00
|
|
|
errUpstream := &aghtest.TestErrUpstream{
|
|
|
|
Err: errors.New("1234"),
|
|
|
|
}
|
2021-03-31 13:00:47 +01:00
|
|
|
|
|
|
|
testCases := []struct {
|
2021-04-07 18:16:06 +01:00
|
|
|
ups upstream.Upstream
|
2021-03-31 13:00:47 +01:00
|
|
|
wantLog string
|
|
|
|
name string
|
|
|
|
cliIP net.IP
|
|
|
|
}{{
|
2021-04-07 18:16:06 +01:00
|
|
|
ups: locUpstream,
|
2021-03-31 13:00:47 +01:00
|
|
|
wantLog: "",
|
|
|
|
name: "all_good",
|
|
|
|
cliIP: net.IP{192, 168, 1, 1},
|
|
|
|
}, {
|
2021-04-07 18:16:06 +01:00
|
|
|
ups: errUpstream,
|
|
|
|
wantLog: `rdns: resolving "192.168.1.2": errupstream: 1234`,
|
|
|
|
name: "resolve_error",
|
|
|
|
cliIP: net.IP{192, 168, 1, 2},
|
2021-04-13 14:23:36 +01:00
|
|
|
}, {
|
|
|
|
ups: locUpstream,
|
|
|
|
wantLog: "",
|
|
|
|
name: "ipv6_good",
|
|
|
|
cliIP: net.ParseIP("2a00:1450:400c:c06::93"),
|
2021-03-31 13:00:47 +01:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
w.Reset()
|
|
|
|
|
|
|
|
cc := &clientsContainer{
|
|
|
|
list: map[string]*Client{},
|
|
|
|
idIndex: map[string]*Client{},
|
2021-04-02 15:30:39 +01:00
|
|
|
ipToRC: map[string]*RuntimeClient{},
|
2021-04-20 14:26:19 +01:00
|
|
|
allTags: aghstrings.NewSet(),
|
2021-03-31 13:00:47 +01:00
|
|
|
}
|
|
|
|
ch := make(chan net.IP)
|
|
|
|
rdns := &RDNS{
|
2021-04-07 18:16:06 +01:00
|
|
|
exchanger: &rDNSExchanger{
|
|
|
|
Exchanger: aghtest.Exchanger{
|
|
|
|
Ups: tc.ups,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
clients: cc,
|
|
|
|
ipCh: ch,
|
2021-03-31 13:00:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
rdns.workerLoop()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
ch <- tc.cliIP
|
|
|
|
close(ch)
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if tc.wantLog != "" {
|
|
|
|
assert.Contains(t, w.String(), tc.wantLog)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, cc.Exists(tc.cliIP.String(), ClientSourceRDNS))
|
|
|
|
})
|
|
|
|
}
|
2019-12-11 09:38:58 +00:00
|
|
|
}
|