2020-05-26 13:24:31 +00:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
2020-11-16 16:45:31 +00:00
|
|
|
"bytes"
|
|
|
|
"strings"
|
2020-05-26 13:24:31 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-11-16 16:45:31 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/testutil"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-05-26 13:24:31 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-11-16 16:45:31 +00:00
|
|
|
func TestDecode_decodeQueryLog(t *testing.T) {
|
|
|
|
logOutput := &bytes.Buffer{}
|
|
|
|
|
|
|
|
testutil.ReplaceLogWriter(t, logOutput)
|
|
|
|
testutil.ReplaceLogLevel(t, log.DEBUG)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
log string
|
|
|
|
want string
|
|
|
|
}{{
|
|
|
|
name: "back_compatibility_all_right",
|
|
|
|
log: `{"Question":"ULgBAAABAAAAAAAAC2FkZ3VhcmR0ZWFtBmdpdGh1YgJpbwAAHAAB","Answer":"ULiBgAABAAAAAQAAC2FkZ3VhcmR0ZWFtBmdpdGh1YgJpbwAAHAABwBgABgABAAADQgBLB25zLTE2MjIJYXdzZG5zLTEwAmNvAnVrABFhd3NkbnMtaG9zdG1hc3RlcgZhbWF6b24DY29tAAAAAAEAABwgAAADhAASdQAAAVGA","Result":{},"Time":"2020-11-13T12:41:25.970861+03:00","Elapsed":244066501,"IP":"127.0.0.1","Upstream":"https://1.1.1.1:443/dns-query"}`,
|
|
|
|
want: "default",
|
|
|
|
}, {
|
|
|
|
name: "back_compatibility_bad_msg",
|
|
|
|
log: `{"Question":"","Answer":"ULiBgAABAAAAAQAAC2FkZ3VhcmR0ZWFtBmdpdGh1YgJpbwAAHAABwBgABgABAAADQgBLB25zLTE2MjIJYXdzZG5zLTEwAmNvAnVrABFhd3NkbnMtaG9zdG1hc3RlcgZhbWF6b24DY29tAAAAAAEAABwgAAADhAASdQAAAVGA","Result":{},"Time":"2020-11-13T12:41:25.970861+03:00","Elapsed":244066501,"IP":"127.0.0.1","Upstream":"https://1.1.1.1:443/dns-query"}`,
|
|
|
|
want: "decodeLogEntry err: dns: overflow unpacking uint16\n",
|
|
|
|
}, {
|
|
|
|
name: "back_compatibility_bad_decoding",
|
|
|
|
log: `{"Question":"LgBAAABAAAAAAAAC2FkZ3VhcmR0ZWFtBmdpdGh1YgJpbwAAHAAB","Answer":"ULiBgAABAAAAAQAAC2FkZ3VhcmR0ZWFtBmdpdGh1YgJpbwAAHAABwBgABgABAAADQgBLB25zLTE2MjIJYXdzZG5zLTEwAmNvAnVrABFhd3NkbnMtaG9zdG1hc3RlcgZhbWF6b24DY29tAAAAAAEAABwgAAADhAASdQAAAVGA","Result":{},"Time":"2020-11-13T12:41:25.970861+03:00","Elapsed":244066501,"IP":"127.0.0.1","Upstream":"https://1.1.1.1:443/dns-query"}`,
|
|
|
|
want: "decodeLogEntry err: illegal base64 data at input byte 48\n",
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
_, err := logOutput.Write([]byte("default"))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
l := &logEntry{}
|
|
|
|
decodeLogEntry(l, tc.log)
|
|
|
|
|
|
|
|
assert.True(t, strings.HasSuffix(logOutput.String(), tc.want), logOutput.String())
|
|
|
|
|
|
|
|
logOutput.Reset()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 13:24:31 +00: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)
|
|
|
|
|
2020-10-30 16:18:51 +00:00
|
|
|
k, _, jtype = readJSON(&s)
|
2020-05-26 13:24:31 +00:00
|
|
|
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)
|
|
|
|
|
2020-10-30 16:18:51 +00:00
|
|
|
_, _, jtype = readJSON(&s)
|
2020-05-26 13:24:31 +00:00
|
|
|
assert.True(t, jtype == jsonTErr)
|
|
|
|
}
|