2020-05-26 06:24:31 -07:00
package querylog
import (
2020-11-16 09:45:31 -07:00
"bytes"
2020-12-21 07:48:07 -07:00
"encoding/base64"
"net"
2020-11-16 09:45:31 -07:00
"strings"
2020-05-26 06:24:31 -07:00
"testing"
2020-12-21 07:48:07 -07:00
"time"
2020-05-26 06:24:31 -07:00
2021-02-04 10:35:13 -07:00
"github.com/AdguardTeam/AdGuardHome/internal/aghtest"
2020-12-21 07:48:07 -07:00
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
2020-11-16 09:45:31 -07:00
"github.com/AdguardTeam/golibs/log"
2020-12-21 07:48:07 -07:00
"github.com/AdguardTeam/urlfilter/rules"
"github.com/miekg/dns"
2020-05-26 06:24:31 -07:00
"github.com/stretchr/testify/assert"
)
2020-12-21 07:48:07 -07:00
func TestDecodeLogEntry ( t * testing . T ) {
2020-11-16 09:45:31 -07:00
logOutput := & bytes . Buffer { }
2021-02-04 10:35:13 -07:00
aghtest . ReplaceLogWriter ( t , logOutput )
aghtest . ReplaceLogLevel ( t , log . DEBUG )
2020-11-16 09:45:31 -07:00
2020-12-21 07:48:07 -07:00
t . Run ( "success" , func ( t * testing . T ) {
const ansStr = ` Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA== `
const data = ` { "IP":"127.0.0.1", ` +
2021-01-27 08:32:13 -07:00
` "CID":"cli42", ` +
2020-12-21 07:48:07 -07:00
` "T":"2020-11-25T18:55:56.519796+03:00", ` +
` "QH":"an.yandex.ru", ` +
` "QT":"A", ` +
` "QC":"IN", ` +
` "CP":"", ` +
` "Answer":" ` + ansStr + ` ", ` +
` "Result": { ` +
` "IsFiltered":true, ` +
` "Reason":3, ` +
` "ReverseHosts":["example.net"], ` +
` "IPList":["127.0.0.2"], ` +
` "Rules":[ { "FilterListID":42,"Text":"||an.yandex.ru","IP":"127.0.0.2"}, ` +
` { "FilterListID":43,"Text":"||an2.yandex.ru","IP":"127.0.0.3"}], ` +
` "CanonName":"example.com", ` +
` "ServiceName":"example.org", ` +
` "DNSRewriteResult": { "RCode":0,"Response": { "1":["127.0.0.2"]}}}, ` +
` "Elapsed":837429} `
ans , err := base64 . StdEncoding . DecodeString ( ansStr )
assert . Nil ( t , err )
want := & logEntry {
2021-01-20 07:27:53 -07:00
IP : net . IPv4 ( 127 , 0 , 0 , 1 ) ,
2020-12-21 07:48:07 -07:00
Time : time . Date ( 2020 , 11 , 25 , 15 , 55 , 56 , 519796000 , time . UTC ) ,
QHost : "an.yandex.ru" ,
QType : "A" ,
QClass : "IN" ,
2021-01-27 08:32:13 -07:00
ClientID : "cli42" ,
2020-12-21 07:48:07 -07:00
ClientProto : "" ,
Answer : ans ,
Result : dnsfilter . Result {
IsFiltered : true ,
Reason : dnsfilter . FilteredBlockList ,
ReverseHosts : [ ] string { "example.net" } ,
IPList : [ ] net . IP { net . IPv4 ( 127 , 0 , 0 , 2 ) } ,
Rules : [ ] * dnsfilter . ResultRule { {
FilterListID : 42 ,
Text : "||an.yandex.ru" ,
IP : net . IPv4 ( 127 , 0 , 0 , 2 ) ,
} , {
FilterListID : 43 ,
Text : "||an2.yandex.ru" ,
IP : net . IPv4 ( 127 , 0 , 0 , 3 ) ,
} } ,
CanonName : "example.com" ,
ServiceName : "example.org" ,
DNSRewriteResult : & dnsfilter . DNSRewriteResult {
RCode : dns . RcodeSuccess ,
Response : dnsfilter . DNSRewriteResultResponse {
dns . TypeA : [ ] rules . RRValue { net . IPv4 ( 127 , 0 , 0 , 2 ) } ,
} ,
} ,
} ,
Elapsed : 837429 ,
}
got := & logEntry { }
decodeLogEntry ( got , data )
s := logOutput . String ( )
2021-01-13 06:56:05 -07:00
assert . Empty ( t , s )
2020-12-21 07:48:07 -07:00
// Correct for time zones.
got . Time = got . Time . UTC ( )
assert . Equal ( t , want , got )
} )
2020-11-16 09:45:31 -07:00
testCases := [ ] struct {
name string
log string
want string
} { {
2020-12-21 07:48:07 -07:00
name : "all_right_old_rule" ,
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3,"Rule":"||an.yandex.","FilterID":1,"ReverseHosts":["example.com"],"IPList":["127.0.0.1"]},"Elapsed":837429} ` ,
want : "" ,
2020-11-27 02:33:25 -07:00
} , {
2020-12-21 07:48:07 -07:00
name : "bad_filter_id_old_rule" ,
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3,"FilterID":1.5},"Elapsed":837429} ` ,
2020-12-17 03:32:46 -07:00
want : "decodeResult handler err: strconv.ParseInt: parsing \"1.5\": invalid syntax\n" ,
2020-11-27 02:33:25 -07:00
} , {
name : "bad_is_filtered" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":trooe,"Reason":3},"Elapsed":837429} ` ,
2020-12-17 03:32:46 -07:00
want : "decodeLogEntry err: invalid character 'o' in literal true (expecting 'u')\n" ,
2020-11-27 02:33:25 -07:00
} , {
name : "bad_elapsed" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3},"Elapsed":-1} ` ,
want : "" ,
2020-11-27 02:33:25 -07:00
} , {
name : "bad_ip" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":127001,"T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3},"Elapsed":837429} ` ,
want : "" ,
2020-11-27 02:33:25 -07:00
} , {
name : "bad_time" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"12/09/1998T15:00:00.000000+05:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3},"Elapsed":837429} ` ,
2020-12-17 03:32:46 -07:00
want : "decodeLogEntry handler err: parsing time \"12/09/1998T15:00:00.000000+05:00\" as \"2006-01-02T15:04:05Z07:00\": cannot parse \"9/1998T15:00:00.000000+05:00\" as \"2006\"\n" ,
2020-11-27 02:33:25 -07:00
} , {
name : "bad_host" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":6,"QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3},"Elapsed":837429} ` ,
want : "" ,
2020-11-27 02:33:25 -07:00
} , {
name : "bad_type" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":true,"QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3},"Elapsed":837429} ` ,
want : "" ,
2020-11-27 02:33:25 -07:00
} , {
name : "bad_class" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":false,"CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3},"Elapsed":837429} ` ,
want : "" ,
2020-11-27 02:33:25 -07:00
} , {
name : "bad_client_proto" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":8,"Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3},"Elapsed":837429} ` ,
want : "" ,
2020-11-27 02:33:25 -07:00
} , {
name : "very_bad_client_proto" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"dog","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3},"Elapsed":837429} ` ,
2020-12-17 03:32:46 -07:00
want : "decodeLogEntry handler err: invalid client proto: \"dog\"\n" ,
2020-11-27 02:33:25 -07:00
} , {
name : "bad_answer" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":0.9,"Result": { "IsFiltered":true,"Reason":3},"Elapsed":837429} ` ,
want : "" ,
2020-11-27 02:33:25 -07:00
} , {
name : "very_bad_answer" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3},"Elapsed":837429} ` ,
2020-12-17 03:32:46 -07:00
want : "decodeLogEntry handler err: illegal base64 data at input byte 61\n" ,
2020-11-27 02:33:25 -07:00
} , {
name : "bad_rule" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3,"Rule":false},"Elapsed":837429} ` ,
want : "" ,
2020-11-27 02:33:25 -07:00
} , {
name : "bad_reason" ,
2020-12-21 07:48:07 -07:00
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":true},"Elapsed":837429} ` ,
want : "" ,
} , {
name : "bad_reverse_hosts" ,
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3,"ReverseHosts":[ { }]},"Elapsed":837429} ` ,
want : "decodeResultReverseHosts: unexpected delim \"{\"\n" ,
} , {
name : "bad_ip_list" ,
log : ` { "IP":"127.0.0.1","T":"2020-11-25T18:55:56.519796+03:00","QH":"an.yandex.ru","QT":"A","QC":"IN","CP":"","Answer":"Qz+BgAABAAEAAAAAAmFuBnlhbmRleAJydQAAAQABwAwAAQABAAAACgAEAAAAAA==","Result": { "IsFiltered":true,"Reason":3,"ReverseHosts":["example.net"],"IPList":[ { }]},"Elapsed":837429} ` ,
want : "decodeResultIPList: unexpected delim \"{\"\n" ,
2020-11-16 09:45:31 -07:00
} }
for _ , tc := range testCases {
t . Run ( tc . name , func ( t * testing . T ) {
l := & logEntry { }
decodeLogEntry ( l , tc . log )
2020-12-21 07:48:07 -07:00
s := logOutput . String ( )
if tc . want == "" {
2021-01-13 06:56:05 -07:00
assert . Empty ( t , s )
2020-12-21 07:48:07 -07:00
} else {
assert . True ( t , strings . HasSuffix ( s , tc . want ) ,
"got %q" , s )
}
2020-11-16 09:45:31 -07:00
logOutput . Reset ( )
} )
}
}