2019-08-26 11:54:38 +03:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2019-11-19 14:09:40 +03:00
|
|
|
"os"
|
2019-08-26 11:54:38 +03:00
|
|
|
"testing"
|
2019-09-16 17:07:18 +03:00
|
|
|
"time"
|
2019-08-26 11:54:38 +03:00
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2019-11-19 14:09:40 +03:00
|
|
|
func prepareTestDir() string {
|
|
|
|
const dir = "./agh-test"
|
|
|
|
_ = os.RemoveAll(dir)
|
|
|
|
_ = os.MkdirAll(dir, 0755)
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check adding and loading (with filtering) entries from disk and memory
|
2019-08-26 11:54:38 +03:00
|
|
|
func TestQueryLog(t *testing.T) {
|
|
|
|
conf := Config{
|
2019-09-27 18:58:57 +03:00
|
|
|
Enabled: true,
|
2019-08-26 11:54:38 +03:00
|
|
|
Interval: 1,
|
2019-11-08 12:31:50 +03:00
|
|
|
MemSize: 100,
|
2019-08-26 11:54:38 +03:00
|
|
|
}
|
2019-11-19 14:09:40 +03:00
|
|
|
conf.BaseDir = prepareTestDir()
|
|
|
|
defer func() { _ = os.RemoveAll(conf.BaseDir) }()
|
2019-09-27 18:58:57 +03:00
|
|
|
l := newQueryLog(conf)
|
2019-08-26 11:54:38 +03:00
|
|
|
|
2019-11-19 14:09:40 +03:00
|
|
|
// add disk entries
|
|
|
|
addEntry(l, "example.org", "1.2.3.4", "0.1.2.3")
|
|
|
|
addEntry(l, "example.org", "1.2.3.4", "0.1.2.3")
|
|
|
|
|
|
|
|
// write to disk
|
|
|
|
l.flushLogBuffer(true)
|
|
|
|
|
|
|
|
// add memory entries
|
|
|
|
addEntry(l, "test.example.org", "2.2.3.4", "0.1.2.4")
|
|
|
|
|
|
|
|
// get all entries
|
|
|
|
params := getDataParams{
|
|
|
|
OlderThan: time.Time{},
|
|
|
|
}
|
|
|
|
d := l.getData(params)
|
|
|
|
mdata := d["data"].([]map[string]interface{})
|
|
|
|
assert.True(t, len(mdata) == 2)
|
|
|
|
assert.True(t, checkEntry(t, mdata[0], "test.example.org", "2.2.3.4", "0.1.2.4"))
|
|
|
|
assert.True(t, checkEntry(t, mdata[1], "example.org", "1.2.3.4", "0.1.2.3"))
|
|
|
|
|
|
|
|
// search by domain (strict)
|
|
|
|
params = getDataParams{
|
|
|
|
OlderThan: time.Time{},
|
|
|
|
Domain: "test.example.org",
|
|
|
|
StrictMatchDomain: true,
|
|
|
|
}
|
|
|
|
d = l.getData(params)
|
|
|
|
mdata = d["data"].([]map[string]interface{})
|
|
|
|
assert.True(t, len(mdata) == 1)
|
|
|
|
assert.True(t, checkEntry(t, mdata[0], "test.example.org", "2.2.3.4", "0.1.2.4"))
|
|
|
|
|
|
|
|
// search by domain
|
|
|
|
params = getDataParams{
|
|
|
|
OlderThan: time.Time{},
|
|
|
|
Domain: "example.org",
|
|
|
|
StrictMatchDomain: false,
|
|
|
|
}
|
|
|
|
d = l.getData(params)
|
|
|
|
mdata = d["data"].([]map[string]interface{})
|
|
|
|
assert.True(t, len(mdata) == 2)
|
|
|
|
assert.True(t, checkEntry(t, mdata[0], "test.example.org", "2.2.3.4", "0.1.2.4"))
|
|
|
|
assert.True(t, checkEntry(t, mdata[1], "example.org", "1.2.3.4", "0.1.2.3"))
|
|
|
|
|
|
|
|
// search by client IP (strict)
|
|
|
|
params = getDataParams{
|
|
|
|
OlderThan: time.Time{},
|
|
|
|
Client: "0.1.2.3",
|
|
|
|
StrictMatchClient: true,
|
|
|
|
}
|
|
|
|
d = l.getData(params)
|
|
|
|
mdata = d["data"].([]map[string]interface{})
|
|
|
|
assert.True(t, len(mdata) == 1)
|
|
|
|
assert.True(t, checkEntry(t, mdata[0], "example.org", "1.2.3.4", "0.1.2.3"))
|
|
|
|
|
|
|
|
// search by client IP
|
|
|
|
params = getDataParams{
|
|
|
|
OlderThan: time.Time{},
|
|
|
|
Client: "0.1.2",
|
|
|
|
StrictMatchClient: false,
|
|
|
|
}
|
|
|
|
d = l.getData(params)
|
|
|
|
mdata = d["data"].([]map[string]interface{})
|
|
|
|
assert.True(t, len(mdata) == 2)
|
|
|
|
assert.True(t, checkEntry(t, mdata[0], "test.example.org", "2.2.3.4", "0.1.2.4"))
|
|
|
|
assert.True(t, checkEntry(t, mdata[1], "example.org", "1.2.3.4", "0.1.2.3"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func addEntry(l *queryLog, host, answerStr, client string) {
|
2019-08-26 11:54:38 +03:00
|
|
|
q := dns.Msg{}
|
|
|
|
q.Question = append(q.Question, dns.Question{
|
2019-11-19 14:09:40 +03:00
|
|
|
Name: host + ".",
|
2019-08-26 11:54:38 +03:00
|
|
|
Qtype: dns.TypeA,
|
|
|
|
Qclass: dns.ClassINET,
|
|
|
|
})
|
|
|
|
|
|
|
|
a := dns.Msg{}
|
|
|
|
a.Question = append(a.Question, q.Question[0])
|
|
|
|
answer := new(dns.A)
|
|
|
|
answer.Hdr = dns.RR_Header{
|
|
|
|
Name: q.Question[0].Name,
|
|
|
|
Rrtype: dns.TypeA,
|
|
|
|
Class: dns.ClassINET,
|
|
|
|
}
|
2019-11-19 14:09:40 +03:00
|
|
|
answer.A = net.ParseIP(answerStr)
|
2019-08-26 11:54:38 +03:00
|
|
|
a.Answer = append(a.Answer, answer)
|
|
|
|
res := dnsfilter.Result{}
|
2019-11-19 14:09:40 +03:00
|
|
|
l.Add(&q, &a, &res, 0, net.ParseIP(client), "upstream")
|
|
|
|
}
|
2019-08-26 11:54:38 +03:00
|
|
|
|
2019-11-19 14:09:40 +03:00
|
|
|
func checkEntry(t *testing.T, m map[string]interface{}, host, answer, client string) bool {
|
2019-08-26 11:54:38 +03:00
|
|
|
mq := m["question"].(map[string]interface{})
|
2019-11-19 14:09:40 +03:00
|
|
|
ma := m["answer"].([]map[string]interface{})
|
|
|
|
ma0 := ma[0]
|
|
|
|
if !assert.True(t, mq["host"].(string) == host) ||
|
|
|
|
!assert.True(t, mq["class"].(string) == "IN") ||
|
|
|
|
!assert.True(t, mq["type"].(string) == "A") ||
|
|
|
|
!assert.True(t, ma0["value"].(string) == answer) ||
|
|
|
|
!assert.True(t, m["client"].(string) == client) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2019-08-26 11:54:38 +03:00
|
|
|
}
|
2019-10-24 20:00:58 +03:00
|
|
|
|
|
|
|
func TestJSON(t *testing.T) {
|
|
|
|
s := `
|
|
|
|
{"keystr":"val","obj":{"keybool":true,"keyint":123456}}
|
|
|
|
`
|
|
|
|
k, v, jtype := readJSON(&s)
|
|
|
|
assert.Equal(t, jtype, int32(jsonTStr))
|
|
|
|
assert.Equal(t, "keystr", k)
|
|
|
|
assert.Equal(t, "val", v)
|
|
|
|
|
|
|
|
k, v, jtype = readJSON(&s)
|
|
|
|
assert.Equal(t, jtype, int32(jsonTObj))
|
|
|
|
assert.Equal(t, "obj", k)
|
|
|
|
|
|
|
|
k, v, jtype = readJSON(&s)
|
|
|
|
assert.Equal(t, jtype, int32(jsonTBool))
|
|
|
|
assert.Equal(t, "keybool", k)
|
|
|
|
assert.Equal(t, "true", v)
|
|
|
|
|
|
|
|
k, v, jtype = readJSON(&s)
|
|
|
|
assert.Equal(t, jtype, int32(jsonTNum))
|
|
|
|
assert.Equal(t, "keyint", k)
|
|
|
|
assert.Equal(t, "123456", v)
|
|
|
|
|
|
|
|
k, v, jtype = readJSON(&s)
|
|
|
|
assert.True(t, jtype == jsonTErr)
|
|
|
|
}
|