decode_test.go (AdGuardHome-0.104.1) | : | decode_test.go (AdGuardHome-0.104.3) | ||
---|---|---|---|---|
package querylog | package querylog | |||
import ( | import ( | |||
"bytes" | ||||
"strings" | ||||
"testing" | "testing" | |||
"github.com/AdguardTeam/AdGuardHome/internal/testutil" | ||||
"github.com/AdguardTeam/golibs/log" | ||||
"github.com/stretchr/testify/assert" | "github.com/stretchr/testify/assert" | |||
) | ) | |||
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":"ULgBAAABAAAAAAAAC2FkZ3VhcmR0ZWFtBmdpdGh1YgJpb | ||||
wAAHAAB","Answer":"ULiBgAABAAAAAQAAC2FkZ3VhcmR0ZWFtBmdpdGh1YgJpbwAAHAABwBgABgABA | ||||
AADQgBLB25zLTE2MjIJYXdzZG5zLTEwAmNvAnVrABFhd3NkbnMtaG9zdG1hc3RlcgZhbWF6b24DY29tA | ||||
AAAAAEAABwgAAADhAASdQAAAVGA","Result":{},"Time":"2020-11-13T12:41:25.970861+03:0 | ||||
0","Elapsed":244066501,"IP":"127.0.0.1","Upstream":"https://1.1.1.1:443/dns-quer | ||||
y"}`, | ||||
want: "default", | ||||
}, { | ||||
name: "back_compatibility_bad_msg", | ||||
log: `{"Question":"","Answer":"ULiBgAABAAAAAQAAC2FkZ3VhcmR0ZWFtB | ||||
mdpdGh1YgJpbwAAHAABwBgABgABAAADQgBLB25zLTE2MjIJYXdzZG5zLTEwAmNvAnVrABFhd3NkbnMta | ||||
G9zdG1hc3RlcgZhbWF6b24DY29tAAAAAAEAABwgAAADhAASdQAAAVGA","Result":{},"Time":"202 | ||||
0-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":"LgBAAABAAAAAAAAC2FkZ3VhcmR0ZWFtBmdpdGh1YgJpbw | ||||
AAHAAB","Answer":"ULiBgAABAAAAAQAAC2FkZ3VhcmR0ZWFtBmdpdGh1YgJpbwAAHAABwBgABgABAA | ||||
ADQgBLB25zLTE2MjIJYXdzZG5zLTEwAmNvAnVrABFhd3NkbnMtaG9zdG1hc3RlcgZhbWF6b24DY29tAA | ||||
AAAAEAABwgAAADhAASdQAAAVGA","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.w | ||||
ant), logOutput.String()) | ||||
logOutput.Reset() | ||||
}) | ||||
} | ||||
} | ||||
func TestJSON(t *testing.T) { | func TestJSON(t *testing.T) { | |||
s := ` | s := ` | |||
{"keystr":"val","obj":{"keybool":true,"keyint":123456}} | {"keystr":"val","obj":{"keybool":true,"keyint":123456}} | |||
` | ` | |||
k, v, jtype := readJSON(&s) | k, v, jtype := readJSON(&s) | |||
assert.Equal(t, jtype, int32(jsonTStr)) | assert.Equal(t, jtype, int32(jsonTStr)) | |||
assert.Equal(t, "keystr", k) | assert.Equal(t, "keystr", k) | |||
assert.Equal(t, "val", v) | assert.Equal(t, "val", v) | |||
k, _, jtype = readJSON(&s) | k, _, jtype = readJSON(&s) | |||
End of changes. 3 change blocks. | ||||
0 lines changed or deleted | 59 lines changed or added |