2019-08-22 16:34:58 +03:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-04 20:35:13 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
2021-10-22 11:58:18 +03:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2019-08-22 16:34:58 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-09 19:38:31 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-22 16:34:58 +03:00
|
|
|
)
|
|
|
|
|
2020-11-16 15:52:05 +03:00
|
|
|
func TestMain(m *testing.M) {
|
2021-02-04 20:35:13 +03:00
|
|
|
aghtest.DiscardLogOutput(m)
|
2020-11-16 15:52:05 +03:00
|
|
|
}
|
|
|
|
|
2020-11-06 12:15:08 +03:00
|
|
|
func UIntArrayEquals(a, b []uint64) bool {
|
2019-08-22 16:34:58 +03:00
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range a {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStats(t *testing.T) {
|
2019-09-16 16:14:52 +03:00
|
|
|
conf := Config{
|
|
|
|
Filename: "./stats.db",
|
|
|
|
LimitDays: 1,
|
|
|
|
}
|
2021-02-09 19:38:31 +03:00
|
|
|
|
|
|
|
s, err := createObject(conf)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, func() (err error) {
|
2021-02-09 19:38:31 +03:00
|
|
|
s.clear()
|
|
|
|
s.Close()
|
2021-10-22 11:58:18 +03:00
|
|
|
|
|
|
|
return os.Remove(conf.Filename)
|
2021-02-04 15:12:34 +03:00
|
|
|
})
|
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
s.Update(Entry{
|
|
|
|
Domain: "domain",
|
|
|
|
Client: "127.0.0.1",
|
|
|
|
Result: RFiltered,
|
|
|
|
Time: 123456,
|
|
|
|
})
|
|
|
|
s.Update(Entry{
|
|
|
|
Domain: "domain",
|
|
|
|
Client: "127.0.0.1",
|
|
|
|
Result: RNotFiltered,
|
|
|
|
Time: 123456,
|
|
|
|
})
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2021-01-21 19:55:41 +03:00
|
|
|
d, ok := s.getData()
|
2021-02-09 19:38:31 +03:00
|
|
|
require.True(t, ok)
|
2021-01-21 19:55:41 +03:00
|
|
|
|
2019-09-10 17:59:10 +03:00
|
|
|
a := []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}
|
2021-01-21 19:55:41 +03:00
|
|
|
assert.True(t, UIntArrayEquals(d.DNSQueries, a))
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2019-09-10 17:59:10 +03:00
|
|
|
a = []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}
|
2021-01-21 19:55:41 +03:00
|
|
|
assert.True(t, UIntArrayEquals(d.BlockedFiltering, a))
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2019-09-10 17:59:10 +03:00
|
|
|
a = []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}
|
2021-01-21 19:55:41 +03:00
|
|
|
assert.True(t, UIntArrayEquals(d.ReplacedSafebrowsing, a))
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2019-09-10 17:59:10 +03:00
|
|
|
a = []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}
|
2021-01-21 19:55:41 +03:00
|
|
|
assert.True(t, UIntArrayEquals(d.ReplacedParental, a))
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2021-01-21 19:55:41 +03:00
|
|
|
m := d.TopQueried
|
2021-02-09 19:38:31 +03:00
|
|
|
require.NotEmpty(t, m)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.EqualValues(t, 1, m[0]["domain"])
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2021-01-21 19:55:41 +03:00
|
|
|
m = d.TopBlocked
|
2021-02-09 19:38:31 +03:00
|
|
|
require.NotEmpty(t, m)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.EqualValues(t, 1, m[0]["domain"])
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2021-01-21 19:55:41 +03:00
|
|
|
m = d.TopClients
|
2021-02-09 19:38:31 +03:00
|
|
|
require.NotEmpty(t, m)
|
2021-01-13 16:56:05 +03:00
|
|
|
assert.EqualValues(t, 2, m[0]["127.0.0.1"])
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2021-01-21 19:55:41 +03:00
|
|
|
assert.EqualValues(t, 2, d.NumDNSQueries)
|
|
|
|
assert.EqualValues(t, 1, d.NumBlockedFiltering)
|
|
|
|
assert.EqualValues(t, 0, d.NumReplacedSafebrowsing)
|
|
|
|
assert.EqualValues(t, 0, d.NumReplacedSafesearch)
|
|
|
|
assert.EqualValues(t, 0, d.NumReplacedParental)
|
|
|
|
assert.EqualValues(t, 0.123456, d.AvgProcessingTime)
|
2019-08-22 16:34:58 +03:00
|
|
|
|
2019-10-07 15:56:33 +03:00
|
|
|
topClients := s.GetTopClientsIP(2)
|
2021-02-09 19:38:31 +03:00
|
|
|
require.NotEmpty(t, topClients)
|
2021-01-20 17:27:53 +03:00
|
|
|
assert.True(t, net.IP{127, 0, 0, 1}.Equal(topClients[0]))
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLargeNumbers(t *testing.T) {
|
2021-02-09 19:38:31 +03:00
|
|
|
var hour int32 = 0
|
2019-09-10 17:59:10 +03:00
|
|
|
newID := func() uint32 {
|
2021-02-09 19:38:31 +03:00
|
|
|
// Use "atomic" to make go race detector happy.
|
2019-09-10 17:59:10 +03:00
|
|
|
return uint32(atomic.LoadInt32(&hour))
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:14:52 +03:00
|
|
|
conf := Config{
|
|
|
|
Filename: "./stats.db",
|
|
|
|
LimitDays: 1,
|
|
|
|
UnitID: newID,
|
|
|
|
}
|
2021-02-09 19:38:31 +03:00
|
|
|
s, err := createObject(conf)
|
2021-10-22 11:58:18 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
testutil.CleanupAndRequireSuccess(t, func() (err error) {
|
2021-02-09 19:38:31 +03:00
|
|
|
s.Close()
|
2021-10-22 11:58:18 +03:00
|
|
|
|
|
|
|
return os.Remove(conf.Filename)
|
2021-02-04 15:12:34 +03:00
|
|
|
})
|
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
// Number of distinct clients and domains every hour.
|
|
|
|
const n = 1000
|
|
|
|
|
|
|
|
for h := 0; h < 12; h++ {
|
|
|
|
atomic.AddInt32(&hour, 1)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
s.Update(Entry{
|
|
|
|
Domain: fmt.Sprintf("domain%d", i),
|
|
|
|
Client: net.IP{
|
|
|
|
127,
|
|
|
|
0,
|
|
|
|
byte((i & 0xff00) >> 8),
|
|
|
|
byte(i & 0xff),
|
|
|
|
}.String(),
|
|
|
|
Result: RNotFiltered,
|
|
|
|
Time: 123456,
|
|
|
|
})
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 19:55:41 +03:00
|
|
|
d, ok := s.getData()
|
2021-02-09 19:38:31 +03:00
|
|
|
require.True(t, ok)
|
|
|
|
assert.EqualValues(t, hour*n, d.NumDNSQueries)
|
2019-08-22 16:34:58 +03:00
|
|
|
}
|
2019-10-09 17:17:57 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
func TestStatsCollector(t *testing.T) {
|
|
|
|
ng := func(_ *unitDB) uint64 {
|
|
|
|
return 0
|
2019-10-09 17:17:57 +03:00
|
|
|
}
|
2021-02-09 19:38:31 +03:00
|
|
|
units := make([]*unitDB, 720)
|
2020-11-06 12:15:08 +03:00
|
|
|
|
2021-02-09 19:38:31 +03:00
|
|
|
t.Run("hours", func(t *testing.T) {
|
|
|
|
statsData := statsCollector(units, 0, Hours, ng)
|
|
|
|
assert.Len(t, statsData, 720)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("days", func(t *testing.T) {
|
|
|
|
for i := 0; i != 25; i++ {
|
|
|
|
statsData := statsCollector(units, uint32(i), Days, ng)
|
|
|
|
require.Lenf(t, statsData, 30, "i=%d", i)
|
|
|
|
}
|
|
|
|
})
|
2019-10-09 17:17:57 +03:00
|
|
|
}
|