mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-17 10:58:29 -07:00
e8c1f5c8d3
Merge in DNS/adguard-home from 2508-ip-conversion to master Updates #2508. Squashed commit of the following: commit 3f64709fbc73ef74c11b910997be1e9bc337193c Merge: 5ac7faaaa0d67aa251
Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 16:21:34 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit 5ac7faaaa9dda570fdb872acad5d13d078f46b64 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 12:00:11 2021 +0300 all: replace conditions with appropriate functions in tests commit 9e3fa9a115ed23024c57dd5192d5173477ddbf71 Merge: db992a42abba74859e
Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Jan 13 10:47:10 2021 +0300 Merge branch 'master' into 2508-ip-conversion commit db992a42a2c6f315421e78a6a0492e2bfb3ce89d Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:55:53 2021 +0300 sysutil: fix linux tests commit f629b15d62349323ce2da05e68dc9cc0b5f6e194 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 18:41:20 2021 +0300 all: improve code quality commit 3bf03a75524040738562298bd1de6db536af130f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 17:33:26 2021 +0300 sysutil: fix linux net.IP conversion commit 5d5b6994916923636e635588631b63b7e7b74e5f Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Jan 12 14:57:26 2021 +0300 dnsforward: remove redundant net.IP <-> string conversion commit 0b955d99b7fad40942f21d1dd8734adb99126195 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Jan 11 18:04:25 2021 +0300 dhcpd: remove net.IP <-> string conversion
158 lines
3.6 KiB
Go
158 lines
3.6 KiB
Go
package stats
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testutil.DiscardLogOutput(m)
|
|
}
|
|
|
|
func UIntArrayEquals(a, b []uint64) bool {
|
|
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) {
|
|
conf := Config{
|
|
Filename: "./stats.db",
|
|
LimitDays: 1,
|
|
}
|
|
s, _ := createObject(conf)
|
|
|
|
e := Entry{}
|
|
|
|
e.Domain = "domain"
|
|
e.Client = net.IP{127, 0, 0, 1}
|
|
e.Result = RFiltered
|
|
e.Time = 123456
|
|
s.Update(e)
|
|
|
|
e.Domain = "domain"
|
|
e.Client = net.IP{127, 0, 0, 1}
|
|
e.Result = RNotFiltered
|
|
e.Time = 123456
|
|
s.Update(e)
|
|
|
|
d := s.getData()
|
|
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}
|
|
assert.True(t, UIntArrayEquals(d["dns_queries"].([]uint64), a))
|
|
|
|
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}
|
|
assert.True(t, UIntArrayEquals(d["blocked_filtering"].([]uint64), a))
|
|
|
|
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}
|
|
assert.True(t, UIntArrayEquals(d["replaced_safebrowsing"].([]uint64), a))
|
|
|
|
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}
|
|
assert.True(t, UIntArrayEquals(d["replaced_parental"].([]uint64), a))
|
|
|
|
m := d["top_queried_domains"].([]map[string]uint64)
|
|
assert.EqualValues(t, 1, m[0]["domain"])
|
|
|
|
m = d["top_blocked_domains"].([]map[string]uint64)
|
|
assert.EqualValues(t, 1, m[0]["domain"])
|
|
|
|
m = d["top_clients"].([]map[string]uint64)
|
|
assert.EqualValues(t, 2, m[0]["127.0.0.1"])
|
|
|
|
assert.EqualValues(t, 2, d["num_dns_queries"].(uint64))
|
|
assert.EqualValues(t, 1, d["num_blocked_filtering"].(uint64))
|
|
assert.EqualValues(t, 0, d["num_replaced_safebrowsing"].(uint64))
|
|
assert.EqualValues(t, 0, d["num_replaced_safesearch"].(uint64))
|
|
assert.EqualValues(t, 0, d["num_replaced_parental"].(uint64))
|
|
assert.EqualValues(t, 0.123456, d["avg_processing_time"].(float64))
|
|
|
|
topClients := s.GetTopClientsIP(2)
|
|
assert.Equal(t, "127.0.0.1", topClients[0])
|
|
|
|
s.clear()
|
|
s.Close()
|
|
os.Remove(conf.Filename)
|
|
}
|
|
|
|
func TestLargeNumbers(t *testing.T) {
|
|
var hour int32 = 1
|
|
newID := func() uint32 {
|
|
// use "atomic" to make Go race detector happy
|
|
return uint32(atomic.LoadInt32(&hour))
|
|
}
|
|
|
|
// log.SetLevel(log.DEBUG)
|
|
conf := Config{
|
|
Filename: "./stats.db",
|
|
LimitDays: 1,
|
|
UnitID: newID,
|
|
}
|
|
os.Remove(conf.Filename)
|
|
s, _ := createObject(conf)
|
|
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)
|
|
e.Client = net.IP{127, 0, 0, 1}
|
|
e.Client[2] = byte((i & 0xff00) >> 8)
|
|
e.Client[3] = byte(i & 0xff)
|
|
e.Result = RNotFiltered
|
|
e.Time = 123456
|
|
s.Update(e)
|
|
}
|
|
}
|
|
|
|
d := s.getData()
|
|
assert.EqualValues(t, int(hour)*n, d["num_dns_queries"])
|
|
|
|
s.Close()
|
|
os.Remove(conf.Filename)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
func TestAggregateDataPerTimeUnit(t *testing.T) {
|
|
for i := 0; i != 25; i++ {
|
|
alen := aggregateDataPerDay(uint32(i))
|
|
assert.Equalf(t, 30, alen, "i=%d", i)
|
|
}
|
|
}
|