2019-08-22 13:34:58 +00:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
|
2020-11-16 12:52:05 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/testutil"
|
2019-08-22 13:34:58 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-11-16 12:52:05 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
testutil.DiscardLogOutput(m)
|
|
|
|
}
|
|
|
|
|
2020-11-06 09:15:08 +00:00
|
|
|
func UIntArrayEquals(a, b []uint64) bool {
|
2019-08-22 13:34:58 +00: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 13:14:52 +00:00
|
|
|
conf := Config{
|
|
|
|
Filename: "./stats.db",
|
|
|
|
LimitDays: 1,
|
|
|
|
}
|
2021-02-04 12:12:34 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
assert.Nil(t, os.Remove(conf.Filename))
|
|
|
|
})
|
|
|
|
|
2019-09-25 12:36:09 +00:00
|
|
|
s, _ := createObject(conf)
|
2019-08-22 13:34:58 +00:00
|
|
|
|
|
|
|
e := Entry{}
|
|
|
|
|
|
|
|
e.Domain = "domain"
|
2021-01-27 15:32:13 +00:00
|
|
|
e.Client = "127.0.0.1"
|
2019-08-22 13:34:58 +00:00
|
|
|
e.Result = RFiltered
|
|
|
|
e.Time = 123456
|
|
|
|
s.Update(e)
|
|
|
|
|
|
|
|
e.Domain = "domain"
|
2021-01-27 15:32:13 +00:00
|
|
|
e.Client = "127.0.0.1"
|
2019-08-22 13:34:58 +00:00
|
|
|
e.Result = RNotFiltered
|
|
|
|
e.Time = 123456
|
|
|
|
s.Update(e)
|
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
d, ok := s.getData()
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
2019-09-10 14:59:10 +00: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 13:34:58 +00:00
|
|
|
|
2019-09-10 14:59:10 +00: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 13:34:58 +00:00
|
|
|
|
2019-09-10 14:59:10 +00: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 13:34:58 +00:00
|
|
|
|
2019-09-10 14:59:10 +00: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 13:34:58 +00:00
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
m := d.TopQueried
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.EqualValues(t, 1, m[0]["domain"])
|
2019-08-22 13:34:58 +00:00
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
m = d.TopBlocked
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.EqualValues(t, 1, m[0]["domain"])
|
2019-08-22 13:34:58 +00:00
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
m = d.TopClients
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.EqualValues(t, 2, m[0]["127.0.0.1"])
|
2019-08-22 13:34:58 +00: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 13:34:58 +00:00
|
|
|
|
2019-10-07 12:56:33 +00:00
|
|
|
topClients := s.GetTopClientsIP(2)
|
2021-01-20 14:27:53 +00:00
|
|
|
assert.True(t, net.IP{127, 0, 0, 1}.Equal(topClients[0]))
|
2019-10-07 12:56:33 +00:00
|
|
|
|
2019-09-25 12:36:09 +00:00
|
|
|
s.clear()
|
2019-08-22 13:34:58 +00:00
|
|
|
s.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLargeNumbers(t *testing.T) {
|
2020-11-06 09:15:08 +00:00
|
|
|
var hour int32 = 1
|
2019-09-10 14:59:10 +00:00
|
|
|
newID := func() uint32 {
|
2019-08-22 13:34:58 +00:00
|
|
|
// use "atomic" to make Go race detector happy
|
2019-09-10 14:59:10 +00:00
|
|
|
return uint32(atomic.LoadInt32(&hour))
|
2019-08-22 13:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// log.SetLevel(log.DEBUG)
|
2019-09-16 13:14:52 +00:00
|
|
|
conf := Config{
|
|
|
|
Filename: "./stats.db",
|
|
|
|
LimitDays: 1,
|
|
|
|
UnitID: newID,
|
|
|
|
}
|
2021-02-04 12:12:34 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
assert.Nil(t, os.Remove(conf.Filename))
|
|
|
|
})
|
|
|
|
|
2019-09-25 12:36:09 +00:00
|
|
|
s, _ := createObject(conf)
|
2019-08-22 13:34:58 +00:00
|
|
|
e := Entry{}
|
|
|
|
|
|
|
|
n := 1000 // number of distinct clients and domains every hour
|
|
|
|
for h := 0; h != 12; h++ {
|
|
|
|
if h != 0 {
|
|
|
|
atomic.AddInt32(&hour, 1)
|
|
|
|
}
|
|
|
|
for i := 0; i != n; i++ {
|
|
|
|
e.Domain = fmt.Sprintf("domain%d", i)
|
2021-01-27 15:32:13 +00:00
|
|
|
ip := net.IP{127, 0, 0, 1}
|
|
|
|
ip[2] = byte((i & 0xff00) >> 8)
|
|
|
|
ip[3] = byte(i & 0xff)
|
|
|
|
e.Client = ip.String()
|
2019-08-22 13:34:58 +00:00
|
|
|
e.Result = RNotFiltered
|
|
|
|
e.Time = 123456
|
|
|
|
s.Update(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:55:41 +00:00
|
|
|
d, ok := s.getData()
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.EqualValues(t, int(hour)*n, d.NumDNSQueries)
|
2019-08-22 13:34:58 +00:00
|
|
|
|
|
|
|
s.Close()
|
|
|
|
}
|
2019-10-09 14:17:57 +00:00
|
|
|
|
|
|
|
// this code is a chunk copied from getData() that generates aggregate data per day
|
|
|
|
func aggregateDataPerDay(firstID uint32) int {
|
|
|
|
firstDayID := (firstID + 24 - 1) / 24 * 24 // align_ceil(24)
|
|
|
|
a := []uint64{}
|
|
|
|
var sum uint64
|
|
|
|
id := firstDayID
|
|
|
|
nextDayID := firstDayID + 24
|
|
|
|
for i := firstDayID - firstID; int(i) != 720; i++ {
|
|
|
|
sum++
|
|
|
|
if id == nextDayID {
|
|
|
|
a = append(a, sum)
|
|
|
|
sum = 0
|
|
|
|
nextDayID += 24
|
|
|
|
}
|
|
|
|
id++
|
|
|
|
}
|
|
|
|
if id <= nextDayID {
|
|
|
|
a = append(a, sum)
|
|
|
|
}
|
|
|
|
return len(a)
|
|
|
|
}
|
2020-11-06 09:15:08 +00:00
|
|
|
|
2019-10-09 14:17:57 +00:00
|
|
|
func TestAggregateDataPerTimeUnit(t *testing.T) {
|
|
|
|
for i := 0; i != 25; i++ {
|
|
|
|
alen := aggregateDataPerDay(uint32(i))
|
2021-01-13 13:56:05 +00:00
|
|
|
assert.Equalf(t, 30, alen, "i=%d", i)
|
2019-10-09 14:17:57 +00:00
|
|
|
}
|
|
|
|
}
|