2019-08-22 14:34:58 +01:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-04 17:35:13 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
|
2019-08-22 14:34:58 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-02-09 16:38:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-22 14:34:58 +01:00
|
|
|
)
|
|
|
|
|
2020-11-16 12:52:05 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2021-02-04 17:35:13 +00:00
|
|
|
aghtest.DiscardLogOutput(m)
|
2020-11-16 12:52:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 09:15:08 +00:00
|
|
|
func UIntArrayEquals(a, b []uint64) bool {
|
2019-08-22 14:34:58 +01: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 14:14:52 +01:00
|
|
|
conf := Config{
|
|
|
|
Filename: "./stats.db",
|
|
|
|
LimitDays: 1,
|
|
|
|
}
|
2021-02-09 16:38:31 +00:00
|
|
|
|
|
|
|
s, err := createObject(conf)
|
|
|
|
require.Nil(t, err)
|
2021-02-04 12:12:34 +00:00
|
|
|
t.Cleanup(func() {
|
2021-02-09 16:38:31 +00:00
|
|
|
s.clear()
|
|
|
|
s.Close()
|
2021-02-04 12:12:34 +00:00
|
|
|
assert.Nil(t, os.Remove(conf.Filename))
|
|
|
|
})
|
|
|
|
|
2021-02-09 16:38:31 +00: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 14:34:58 +01:00
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
d, ok := s.getData()
|
2021-02-09 16:38:31 +00:00
|
|
|
require.True(t, ok)
|
2021-01-21 16:55:41 +00:00
|
|
|
|
2019-09-10 15:59:10 +01: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 16:55:41 +00:00
|
|
|
assert.True(t, UIntArrayEquals(d.DNSQueries, a))
|
2019-08-22 14:34:58 +01:00
|
|
|
|
2019-09-10 15:59:10 +01: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 16:55:41 +00:00
|
|
|
assert.True(t, UIntArrayEquals(d.BlockedFiltering, a))
|
2019-08-22 14:34:58 +01:00
|
|
|
|
2019-09-10 15:59:10 +01: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 16:55:41 +00:00
|
|
|
assert.True(t, UIntArrayEquals(d.ReplacedSafebrowsing, a))
|
2019-08-22 14:34:58 +01:00
|
|
|
|
2019-09-10 15:59:10 +01: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 16:55:41 +00:00
|
|
|
assert.True(t, UIntArrayEquals(d.ReplacedParental, a))
|
2019-08-22 14:34:58 +01:00
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
m := d.TopQueried
|
2021-02-09 16:38:31 +00:00
|
|
|
require.NotEmpty(t, m)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.EqualValues(t, 1, m[0]["domain"])
|
2019-08-22 14:34:58 +01:00
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
m = d.TopBlocked
|
2021-02-09 16:38:31 +00:00
|
|
|
require.NotEmpty(t, m)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.EqualValues(t, 1, m[0]["domain"])
|
2019-08-22 14:34:58 +01:00
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
m = d.TopClients
|
2021-02-09 16:38:31 +00:00
|
|
|
require.NotEmpty(t, m)
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.EqualValues(t, 2, m[0]["127.0.0.1"])
|
2019-08-22 14:34:58 +01:00
|
|
|
|
2021-01-21 16:55:41 +00: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 14:34:58 +01:00
|
|
|
|
2019-10-07 13:56:33 +01:00
|
|
|
topClients := s.GetTopClientsIP(2)
|
2021-02-09 16:38:31 +00:00
|
|
|
require.NotEmpty(t, topClients)
|
2021-01-20 14:27:53 +00:00
|
|
|
assert.True(t, net.IP{127, 0, 0, 1}.Equal(topClients[0]))
|
2019-08-22 14:34:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLargeNumbers(t *testing.T) {
|
2021-02-09 16:38:31 +00:00
|
|
|
var hour int32 = 0
|
2019-09-10 15:59:10 +01:00
|
|
|
newID := func() uint32 {
|
2021-02-09 16:38:31 +00:00
|
|
|
// Use "atomic" to make go race detector happy.
|
2019-09-10 15:59:10 +01:00
|
|
|
return uint32(atomic.LoadInt32(&hour))
|
2019-08-22 14:34:58 +01:00
|
|
|
}
|
|
|
|
|
2019-09-16 14:14:52 +01:00
|
|
|
conf := Config{
|
|
|
|
Filename: "./stats.db",
|
|
|
|
LimitDays: 1,
|
|
|
|
UnitID: newID,
|
|
|
|
}
|
2021-02-09 16:38:31 +00:00
|
|
|
s, err := createObject(conf)
|
|
|
|
require.Nil(t, err)
|
2021-02-04 12:12:34 +00:00
|
|
|
t.Cleanup(func() {
|
2021-02-09 16:38:31 +00:00
|
|
|
s.Close()
|
2021-02-04 12:12:34 +00:00
|
|
|
assert.Nil(t, os.Remove(conf.Filename))
|
|
|
|
})
|
|
|
|
|
2021-02-09 16:38:31 +00: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 14:34:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
d, ok := s.getData()
|
2021-02-09 16:38:31 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
assert.EqualValues(t, hour*n, d.NumDNSQueries)
|
2019-08-22 14:34:58 +01:00
|
|
|
}
|
2019-10-09 15:17:57 +01:00
|
|
|
|
2021-02-09 16:38:31 +00:00
|
|
|
func TestStatsCollector(t *testing.T) {
|
|
|
|
ng := func(_ *unitDB) uint64 {
|
|
|
|
return 0
|
2019-10-09 15:17:57 +01:00
|
|
|
}
|
2021-02-09 16:38:31 +00:00
|
|
|
units := make([]*unitDB, 720)
|
2020-11-06 09:15:08 +00:00
|
|
|
|
2021-02-09 16:38:31 +00: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 15:17:57 +01:00
|
|
|
}
|