mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-18 05:41:55 +03:00
f9fe3172c4
Merge in DNS/adguard-home from 4299-querylog-stats-clients to master Squashed commit of the following: commit 33b80b67224f7c1a15bee8e6a23d9d5bab6ac629 Merge: 61964fdd5d5a7295
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 12:43:22 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit 61964fdd02221abbddedf2d6d02bb0bce6845362 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 12:42:01 2023 +0300 dnsforward: imp code commit 7382168500bab6ca7494d39aabfc2d7bfceb5d24 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 7 11:13:07 2023 +0300 all: imp code, chlog commit c7852902f635af6c296dcb6735f7b0bfb83f4e87 Merge: aa4dc0a5a55cbbe7
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 6 14:34:24 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit aa4dc0a54e95bc5b24718ec158340b631a822801 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 6 12:54:02 2023 +0300 all: imp code commit dd541f0cd7ecbf0afcf10ccbd130fd1d1fa4c1c4 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 13:01:53 2023 +0300 querylog: fix typo commit d2c8fdb35b04d27c8957fa027882fde704cc07be Merge: 83d0baa12eb3bf6e
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 12:36:49 2023 +0300 Merge branch 'master' into 4299-querylog-stats-clients commit 83d0baa1f1202f9c62d4be2041d7aed12ee9ab2c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Mar 31 12:35:15 2023 +0300 all: add tests commit a459f19f25cf9646d145813fe7834b2d9979c516 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 29 16:51:53 2023 +0300 all: add clients querylog stats ignore
261 lines
6.3 KiB
Go
261 lines
6.3 KiB
Go
package stats_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/stats"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
|
"github.com/AdguardTeam/golibs/testutil"
|
|
"github.com/AdguardTeam/golibs/timeutil"
|
|
"github.com/miekg/dns"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testutil.DiscardLogOutput(m)
|
|
}
|
|
|
|
// constUnitID is the UnitIDGenFunc which always return 0.
|
|
func constUnitID() (id uint32) { return 0 }
|
|
|
|
func assertSuccessAndUnmarshal(t *testing.T, to any, handler http.Handler, req *http.Request) {
|
|
t.Helper()
|
|
|
|
require.NotNil(t, handler)
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
require.Equal(t, http.StatusOK, rw.Code)
|
|
|
|
data := rw.Body.Bytes()
|
|
if to == nil {
|
|
assert.Empty(t, data)
|
|
|
|
return
|
|
}
|
|
|
|
err := json.Unmarshal(data, to)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestStats(t *testing.T) {
|
|
cliIP := netutil.IPv4Localhost()
|
|
cliIPStr := cliIP.String()
|
|
|
|
handlers := map[string]http.Handler{}
|
|
conf := stats.Config{
|
|
ShouldCountClient: func([]string) bool { return true },
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
Limit: timeutil.Day,
|
|
Enabled: true,
|
|
UnitID: constUnitID,
|
|
HTTPRegister: func(_, url string, handler http.HandlerFunc) {
|
|
handlers[url] = handler
|
|
},
|
|
}
|
|
|
|
s, err := stats.New(conf)
|
|
require.NoError(t, err)
|
|
|
|
s.Start()
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
t.Run("data", func(t *testing.T) {
|
|
const reqDomain = "domain"
|
|
|
|
entries := []stats.Entry{{
|
|
Domain: reqDomain,
|
|
Client: cliIPStr,
|
|
Result: stats.RFiltered,
|
|
Time: 123456,
|
|
}, {
|
|
Domain: reqDomain,
|
|
Client: cliIPStr,
|
|
Result: stats.RNotFiltered,
|
|
Time: 123456,
|
|
}}
|
|
|
|
wantData := &stats.StatsResp{
|
|
TimeUnits: "hours",
|
|
TopQueried: []map[string]uint64{0: {reqDomain: 1}},
|
|
TopClients: []map[string]uint64{0: {cliIPStr: 2}},
|
|
TopBlocked: []map[string]uint64{0: {reqDomain: 1}},
|
|
DNSQueries: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
|
|
},
|
|
BlockedFiltering: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
|
},
|
|
ReplacedSafebrowsing: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
},
|
|
ReplacedParental: []uint64{
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
},
|
|
NumDNSQueries: 2,
|
|
NumBlockedFiltering: 1,
|
|
NumReplacedSafebrowsing: 0,
|
|
NumReplacedSafesearch: 0,
|
|
NumReplacedParental: 0,
|
|
AvgProcessingTime: 0.123456,
|
|
}
|
|
|
|
for _, e := range entries {
|
|
s.Update(e)
|
|
}
|
|
|
|
data := &stats.StatsResp{}
|
|
req := httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
|
|
|
assert.Equal(t, wantData, data)
|
|
})
|
|
|
|
t.Run("tops", func(t *testing.T) {
|
|
topClients := s.TopClientsIP(2)
|
|
require.NotEmpty(t, topClients)
|
|
|
|
assert.Equal(t, cliIP, topClients[0])
|
|
})
|
|
|
|
t.Run("reset", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodPost, "/control/stats_reset", nil)
|
|
assertSuccessAndUnmarshal(t, nil, handlers["/control/stats_reset"], req)
|
|
|
|
_24zeroes := [24]uint64{}
|
|
emptyData := &stats.StatsResp{
|
|
TimeUnits: "hours",
|
|
TopQueried: []map[string]uint64{},
|
|
TopClients: []map[string]uint64{},
|
|
TopBlocked: []map[string]uint64{},
|
|
DNSQueries: _24zeroes[:],
|
|
BlockedFiltering: _24zeroes[:],
|
|
ReplacedSafebrowsing: _24zeroes[:],
|
|
ReplacedParental: _24zeroes[:],
|
|
}
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
|
data := &stats.StatsResp{}
|
|
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
|
assert.Equal(t, emptyData, data)
|
|
})
|
|
}
|
|
|
|
func TestLargeNumbers(t *testing.T) {
|
|
var curHour uint32 = 1
|
|
handlers := map[string]http.Handler{}
|
|
|
|
conf := stats.Config{
|
|
ShouldCountClient: func([]string) bool { return true },
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
Limit: timeutil.Day,
|
|
Enabled: true,
|
|
UnitID: func() (id uint32) { return atomic.LoadUint32(&curHour) },
|
|
HTTPRegister: func(_, url string, handler http.HandlerFunc) { handlers[url] = handler },
|
|
}
|
|
|
|
s, err := stats.New(conf)
|
|
require.NoError(t, err)
|
|
|
|
s.Start()
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
const (
|
|
hoursNum = 12
|
|
cliNumPerHour = 1000
|
|
)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/control/stats", nil)
|
|
|
|
for h := 0; h < hoursNum; h++ {
|
|
atomic.AddUint32(&curHour, 1)
|
|
|
|
for i := 0; i < cliNumPerHour; i++ {
|
|
ip := net.IP{127, 0, byte((i & 0xff00) >> 8), byte(i & 0xff)}
|
|
e := stats.Entry{
|
|
Domain: fmt.Sprintf("domain%d.hour%d", i, h),
|
|
Client: ip.String(),
|
|
Result: stats.RNotFiltered,
|
|
Time: 123456,
|
|
}
|
|
s.Update(e)
|
|
}
|
|
}
|
|
|
|
data := &stats.StatsResp{}
|
|
assertSuccessAndUnmarshal(t, data, handlers["/control/stats"], req)
|
|
assert.Equal(t, hoursNum*cliNumPerHour, int(data.NumDNSQueries))
|
|
}
|
|
|
|
func TestShouldCount(t *testing.T) {
|
|
const (
|
|
ignored1 = "ignor.ed"
|
|
ignored2 = "ignored.to"
|
|
)
|
|
set := stringutil.NewSet(ignored1, ignored2)
|
|
|
|
s, err := stats.New(stats.Config{
|
|
Enabled: true,
|
|
Filename: filepath.Join(t.TempDir(), "stats.db"),
|
|
Limit: timeutil.Day,
|
|
Ignored: set,
|
|
ShouldCountClient: func(ids []string) (a bool) {
|
|
return ids[0] != "no_count"
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
s.Start()
|
|
testutil.CleanupAndRequireSuccess(t, s.Close)
|
|
|
|
testCases := []struct {
|
|
wantCount assert.BoolAssertionFunc
|
|
name string
|
|
host string
|
|
ids []string
|
|
}{{
|
|
name: "count",
|
|
host: "example.com",
|
|
ids: []string{"whatever"},
|
|
wantCount: assert.True,
|
|
}, {
|
|
name: "no_count_ignored_1",
|
|
host: ignored1,
|
|
ids: []string{"whatever"},
|
|
wantCount: assert.False,
|
|
}, {
|
|
name: "no_count_ignored_2",
|
|
host: ignored2,
|
|
ids: []string{"whatever"},
|
|
wantCount: assert.False,
|
|
}, {
|
|
name: "no_count_client_ignore",
|
|
host: "example.com",
|
|
ids: []string{"no_count"},
|
|
wantCount: assert.False,
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
res := s.ShouldCount(tc.host, dns.TypeA, dns.ClassINET, tc.ids)
|
|
|
|
tc.wantCount(t, res)
|
|
})
|
|
}
|
|
}
|