7c35d208b1
Updates #1273. Squashed commit of the following: commit 55b78153b1b775c855e759011141bbbe6d4b962c Author: Artem Baskal <a.baskal@adguard.com> Date: Fri Apr 2 16:55:39 2021 +0300 Update client_info in case of null commit 5c80c1438ed9d961af11617831b704d6ae15cc34 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Apr 2 16:24:14 2021 +0300 querylog: always set client_info commit b48efd64d757cc0bcf5b34de22fdd0b0464d98a6 Merge: 4ed7eab523c9f528
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Apr 2 16:22:08 2021 +0300 Merge branch 'master' into 1273-querylog-client-name commit 4ed7eab52b6b5b0c0ddb5aa5a3225a62d1f9265b Merge: dbf990eb70d4c70e
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Apr 2 12:57:17 2021 +0300 Merge branch 'master' into 1273-querylog-client-name commit dbf990eb881116754554270e7b691b5db8e9ee34 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Apr 2 12:56:13 2021 +0300 home: imp names commit c2cfdef494ca26fff62b9fa008f1b389d9d4d46b Author: Artem Baskal <a.baskal@adguard.com> Date: Thu Apr 1 19:26:04 2021 +0300 Rename to whois commit e3cc4a68ee576770b1922680155308e33bed31e8 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Apr 1 19:03:42 2021 +0300 home: imp whois more commit 3b8ef8691c298aff35946b35923ef2e5b1f9bbbe Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Apr 1 18:51:14 2021 +0300 home: imp whois resp commit fb97e0d74976723a512d6ff4c69e830fe59c8df8 Author: Artem Baskal <a.baskal@adguard.com> Date: Thu Apr 1 18:00:03 2021 +0300 Fix client_info ids prop types commit 298005189e372651ceff453e88aca19ee925a138 Author: Artem Baskal <a.baskal@adguard.com> Date: Thu Apr 1 17:58:14 2021 +0300 Adapt changes on client commit aa1769f64197d865478a66271da483babfc5dfd0 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Apr 1 17:18:36 2021 +0300 all: add more fields to querylog client commit 4b2a2dbd380ec410f3068d15ea16430912e03e33 Merge: cda92c3f2e4e2f62
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Apr 1 16:57:26 2021 +0300 Merge branch 'master' into 1273-querylog-client-name commit cda92c3f0331cbac252f3163d31457f716bc7f2c Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Mar 29 18:03:51 2021 +0300 querylog: fix windows tests commit 5a56f0a32608869ed93a38f18f63ea3a20f7bde2 Merge: 627e4958e710ce11
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Mar 29 17:45:53 2021 +0300 Merge branch 'master' into 1273-querylog-client-name commit 627e495828e82d44cc77aa393536479f23cc68b7 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Mar 29 17:44:49 2021 +0300 querylog: add tests, imp code, docs commit 6dec468a2f0c29357875ff99458e0e8f8e580e6d Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Mar 26 16:10:47 2021 +0300 querylog: search clients by name, enrich http resp
376 lines
9.6 KiB
Go
376 lines
9.6 KiB
Go
package querylog
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"net"
|
|
"sort"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
|
"github.com/AdguardTeam/dnsproxy/proxyutil"
|
|
"github.com/miekg/dns"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
aghtest.DiscardLogOutput(m)
|
|
}
|
|
|
|
// TestQueryLog tests adding and loading (with filtering) entries from disk and
|
|
// memory.
|
|
func TestQueryLog(t *testing.T) {
|
|
l := newQueryLog(Config{
|
|
Enabled: true,
|
|
FileEnabled: true,
|
|
RotationIvl: 1,
|
|
MemSize: 100,
|
|
BaseDir: t.TempDir(),
|
|
})
|
|
|
|
// Add disk entries.
|
|
addEntry(l, "example.org", net.IPv4(1, 1, 1, 1), net.IPv4(2, 2, 2, 1))
|
|
// Write to disk (first file).
|
|
require.Nil(t, l.flushLogBuffer(true))
|
|
// Start writing to the second file.
|
|
require.Nil(t, l.rotate())
|
|
// Add disk entries.
|
|
addEntry(l, "example.org", net.IPv4(1, 1, 1, 2), net.IPv4(2, 2, 2, 2))
|
|
// Write to disk.
|
|
require.Nil(t, l.flushLogBuffer(true))
|
|
// Add memory entries.
|
|
addEntry(l, "test.example.org", net.IPv4(1, 1, 1, 3), net.IPv4(2, 2, 2, 3))
|
|
addEntry(l, "example.com", net.IPv4(1, 1, 1, 4), net.IPv4(2, 2, 2, 4))
|
|
|
|
type tcAssertion struct {
|
|
num int
|
|
host string
|
|
answer, client net.IP
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
sCr []searchCriteria
|
|
want []tcAssertion
|
|
}{{
|
|
name: "all",
|
|
sCr: []searchCriteria{},
|
|
want: []tcAssertion{
|
|
{num: 0, host: "example.com", answer: net.IPv4(1, 1, 1, 4), client: net.IPv4(2, 2, 2, 4)},
|
|
{num: 1, host: "test.example.org", answer: net.IPv4(1, 1, 1, 3), client: net.IPv4(2, 2, 2, 3)},
|
|
{num: 2, host: "example.org", answer: net.IPv4(1, 1, 1, 2), client: net.IPv4(2, 2, 2, 2)},
|
|
{num: 3, host: "example.org", answer: net.IPv4(1, 1, 1, 1), client: net.IPv4(2, 2, 2, 1)},
|
|
},
|
|
}, {
|
|
name: "by_domain_strict",
|
|
sCr: []searchCriteria{{
|
|
criteriaType: ctDomainOrClient,
|
|
strict: true,
|
|
value: "TEST.example.org",
|
|
}},
|
|
want: []tcAssertion{{
|
|
num: 0, host: "test.example.org", answer: net.IPv4(1, 1, 1, 3), client: net.IPv4(2, 2, 2, 3),
|
|
}},
|
|
}, {
|
|
name: "by_domain_non-strict",
|
|
sCr: []searchCriteria{{
|
|
criteriaType: ctDomainOrClient,
|
|
strict: false,
|
|
value: "example.ORG",
|
|
}},
|
|
want: []tcAssertion{
|
|
{num: 0, host: "test.example.org", answer: net.IPv4(1, 1, 1, 3), client: net.IPv4(2, 2, 2, 3)},
|
|
{num: 1, host: "example.org", answer: net.IPv4(1, 1, 1, 2), client: net.IPv4(2, 2, 2, 2)},
|
|
{num: 2, host: "example.org", answer: net.IPv4(1, 1, 1, 1), client: net.IPv4(2, 2, 2, 1)},
|
|
},
|
|
}, {
|
|
name: "by_client_ip_strict",
|
|
sCr: []searchCriteria{{
|
|
criteriaType: ctDomainOrClient,
|
|
strict: true,
|
|
value: "2.2.2.2",
|
|
}},
|
|
want: []tcAssertion{{
|
|
num: 0, host: "example.org", answer: net.IPv4(1, 1, 1, 2), client: net.IPv4(2, 2, 2, 2),
|
|
}},
|
|
}, {
|
|
name: "by_client_ip_non-strict",
|
|
sCr: []searchCriteria{{
|
|
criteriaType: ctDomainOrClient,
|
|
strict: false,
|
|
value: "2.2.2",
|
|
}},
|
|
want: []tcAssertion{
|
|
{num: 0, host: "example.com", answer: net.IPv4(1, 1, 1, 4), client: net.IPv4(2, 2, 2, 4)},
|
|
{num: 1, host: "test.example.org", answer: net.IPv4(1, 1, 1, 3), client: net.IPv4(2, 2, 2, 3)},
|
|
{num: 2, host: "example.org", answer: net.IPv4(1, 1, 1, 2), client: net.IPv4(2, 2, 2, 2)},
|
|
{num: 3, host: "example.org", answer: net.IPv4(1, 1, 1, 1), client: net.IPv4(2, 2, 2, 1)},
|
|
},
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
params := newSearchParams()
|
|
params.searchCriteria = tc.sCr
|
|
|
|
entries, _ := l.search(params)
|
|
require.Len(t, entries, len(tc.want))
|
|
for _, want := range tc.want {
|
|
assertLogEntry(t, entries[want.num], want.host, want.answer, want.client)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryLogOffsetLimit(t *testing.T) {
|
|
l := newQueryLog(Config{
|
|
Enabled: true,
|
|
RotationIvl: 1,
|
|
MemSize: 100,
|
|
BaseDir: t.TempDir(),
|
|
})
|
|
|
|
const (
|
|
entNum = 10
|
|
firstPageDomain = "first.example.org"
|
|
secondPageDomain = "second.example.org"
|
|
)
|
|
// Add entries to the log.
|
|
for i := 0; i < entNum; i++ {
|
|
addEntry(l, secondPageDomain, net.IPv4(1, 1, 1, 1), net.IPv4(2, 2, 2, 1))
|
|
}
|
|
// Write them to the first file.
|
|
require.Nil(t, l.flushLogBuffer(true))
|
|
// Add more to the in-memory part of log.
|
|
for i := 0; i < entNum; i++ {
|
|
addEntry(l, firstPageDomain, net.IPv4(1, 1, 1, 1), net.IPv4(2, 2, 2, 1))
|
|
}
|
|
|
|
params := newSearchParams()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
offset int
|
|
limit int
|
|
wantLen int
|
|
want string
|
|
}{{
|
|
name: "page_1",
|
|
offset: 0,
|
|
limit: 10,
|
|
wantLen: 10,
|
|
want: firstPageDomain,
|
|
}, {
|
|
name: "page_2",
|
|
offset: 10,
|
|
limit: 10,
|
|
wantLen: 10,
|
|
want: secondPageDomain,
|
|
}, {
|
|
name: "page_2.5",
|
|
offset: 15,
|
|
limit: 10,
|
|
wantLen: 5,
|
|
want: secondPageDomain,
|
|
}, {
|
|
name: "page_3",
|
|
offset: 20,
|
|
limit: 10,
|
|
wantLen: 0,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
params.offset = tc.offset
|
|
params.limit = tc.limit
|
|
entries, _ := l.search(params)
|
|
|
|
require.Len(t, entries, tc.wantLen)
|
|
|
|
if tc.wantLen > 0 {
|
|
assert.Equal(t, entries[0].QHost, tc.want)
|
|
assert.Equal(t, entries[tc.wantLen-1].QHost, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryLogMaxFileScanEntries(t *testing.T) {
|
|
l := newQueryLog(Config{
|
|
Enabled: true,
|
|
FileEnabled: true,
|
|
RotationIvl: 1,
|
|
MemSize: 100,
|
|
BaseDir: t.TempDir(),
|
|
})
|
|
|
|
const entNum = 10
|
|
// Add entries to the log.
|
|
for i := 0; i < entNum; i++ {
|
|
addEntry(l, "example.org", net.IPv4(1, 1, 1, 1), net.IPv4(2, 2, 2, 1))
|
|
}
|
|
// Write them to disk.
|
|
require.Nil(t, l.flushLogBuffer(true))
|
|
|
|
params := newSearchParams()
|
|
|
|
for _, maxFileScanEntries := range []int{5, 0} {
|
|
t.Run(fmt.Sprintf("limit_%d", maxFileScanEntries), func(t *testing.T) {
|
|
params.maxFileScanEntries = maxFileScanEntries
|
|
entries, _ := l.search(params)
|
|
assert.Len(t, entries, entNum-maxFileScanEntries)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQueryLogFileDisabled(t *testing.T) {
|
|
l := newQueryLog(Config{
|
|
Enabled: true,
|
|
FileEnabled: false,
|
|
RotationIvl: 1,
|
|
MemSize: 2,
|
|
BaseDir: t.TempDir(),
|
|
})
|
|
|
|
addEntry(l, "example1.org", net.IPv4(1, 1, 1, 1), net.IPv4(2, 2, 2, 1))
|
|
addEntry(l, "example2.org", net.IPv4(1, 1, 1, 1), net.IPv4(2, 2, 2, 1))
|
|
// The oldest entry is going to be removed from memory buffer.
|
|
addEntry(l, "example3.org", net.IPv4(1, 1, 1, 1), net.IPv4(2, 2, 2, 1))
|
|
|
|
params := newSearchParams()
|
|
ll, _ := l.search(params)
|
|
require.Len(t, ll, 2)
|
|
assert.Equal(t, "example3.org", ll[0].QHost)
|
|
assert.Equal(t, "example2.org", ll[1].QHost)
|
|
}
|
|
|
|
func addEntry(l *queryLog, host string, answerStr, client net.IP) {
|
|
q := dns.Msg{
|
|
Question: []dns.Question{{
|
|
Name: host + ".",
|
|
Qtype: dns.TypeA,
|
|
Qclass: dns.ClassINET,
|
|
}},
|
|
}
|
|
|
|
a := dns.Msg{
|
|
Question: q.Question,
|
|
Answer: []dns.RR{&dns.A{
|
|
Hdr: dns.RR_Header{
|
|
Name: q.Question[0].Name,
|
|
Rrtype: dns.TypeA,
|
|
Class: dns.ClassINET,
|
|
},
|
|
A: answerStr,
|
|
}},
|
|
}
|
|
res := dnsfilter.Result{
|
|
IsFiltered: true,
|
|
Reason: dnsfilter.Rewritten,
|
|
ServiceName: "SomeService",
|
|
Rules: []*dnsfilter.ResultRule{{
|
|
FilterListID: 1,
|
|
Text: "SomeRule",
|
|
}},
|
|
}
|
|
params := AddParams{
|
|
Question: &q,
|
|
Answer: &a,
|
|
OrigAnswer: &a,
|
|
Result: &res,
|
|
ClientIP: client,
|
|
Upstream: "upstream",
|
|
}
|
|
l.Add(params)
|
|
}
|
|
|
|
func assertLogEntry(t *testing.T, entry *logEntry, host string, answer, client net.IP) {
|
|
t.Helper()
|
|
|
|
require.NotNil(t, entry)
|
|
|
|
assert.Equal(t, host, entry.QHost)
|
|
assert.Equal(t, client, entry.IP)
|
|
assert.Equal(t, "A", entry.QType)
|
|
assert.Equal(t, "IN", entry.QClass)
|
|
|
|
msg := &dns.Msg{}
|
|
require.Nil(t, msg.Unpack(entry.Answer))
|
|
require.Len(t, msg.Answer, 1)
|
|
|
|
ip := proxyutil.GetIPFromDNSRecord(msg.Answer[0]).To16()
|
|
assert.Equal(t, answer, ip)
|
|
}
|
|
|
|
func testEntries() (entries []*logEntry) {
|
|
rsrc := rand.NewSource(time.Now().UnixNano())
|
|
rgen := rand.New(rsrc)
|
|
|
|
entries = make([]*logEntry, 1000)
|
|
for i := range entries {
|
|
min := rgen.Intn(60)
|
|
sec := rgen.Intn(60)
|
|
entries[i] = &logEntry{
|
|
Time: time.Date(2020, 1, 1, 0, min, sec, 0, time.UTC),
|
|
}
|
|
}
|
|
|
|
return entries
|
|
}
|
|
|
|
// logEntriesByTimeDesc is a wrapper over []*logEntry for sorting.
|
|
//
|
|
// NOTE(a.garipov): Weirdly enough, on my machine this gets consistently
|
|
// outperformed by sort.Slice, see the benchmark below. I'm leaving this
|
|
// implementation here, in tests, in case we want to make sure it outperforms on
|
|
// most machines, but for now this is unused in the actual code.
|
|
type logEntriesByTimeDesc []*logEntry
|
|
|
|
// Len implements the sort.Interface interface for logEntriesByTimeDesc.
|
|
func (les logEntriesByTimeDesc) Len() (n int) { return len(les) }
|
|
|
|
// Less implements the sort.Interface interface for logEntriesByTimeDesc.
|
|
func (les logEntriesByTimeDesc) Less(i, j int) (less bool) {
|
|
return les[i].Time.After(les[j].Time)
|
|
}
|
|
|
|
// Swap implements the sort.Interface interface for logEntriesByTimeDesc.
|
|
func (les logEntriesByTimeDesc) Swap(i, j int) { les[i], les[j] = les[j], les[i] }
|
|
|
|
func BenchmarkLogEntry_sort(b *testing.B) {
|
|
b.Run("methods", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
b.StopTimer()
|
|
entries := testEntries()
|
|
b.StartTimer()
|
|
|
|
sort.Stable(logEntriesByTimeDesc(entries))
|
|
}
|
|
})
|
|
|
|
b.Run("reflect", func(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
b.StopTimer()
|
|
entries := testEntries()
|
|
b.StartTimer()
|
|
|
|
sort.SliceStable(entries, func(i, j int) (less bool) {
|
|
return entries[i].Time.After(entries[j].Time)
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestLogEntriesByTime_sort(t *testing.T) {
|
|
entries := testEntries()
|
|
sort.Sort(logEntriesByTimeDesc(entries))
|
|
|
|
for i := range entries[1:] {
|
|
assert.False(t, entries[i+1].Time.After(entries[i].Time),
|
|
"%s %s", entries[i+1].Time, entries[i].Time)
|
|
}
|
|
}
|