"Fossies" - the Fresh Open Source Software Archive

Member "AdGuardHome-0.104.3/internal/querylog/decode_test.go" (19 Nov 2020, 2800 Bytes) of package /linux/misc/dns/AdGuardHome-0.104.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "decode_test.go": 0.104.1_vs_0.104.3.

    1 package querylog
    2 
    3 import (
    4     "bytes"
    5     "strings"
    6     "testing"
    7 
    8     "github.com/AdguardTeam/AdGuardHome/internal/testutil"
    9     "github.com/AdguardTeam/golibs/log"
   10     "github.com/stretchr/testify/assert"
   11 )
   12 
   13 func TestDecode_decodeQueryLog(t *testing.T) {
   14     logOutput := &bytes.Buffer{}
   15 
   16     testutil.ReplaceLogWriter(t, logOutput)
   17     testutil.ReplaceLogLevel(t, log.DEBUG)
   18 
   19     testCases := []struct {
   20         name string
   21         log  string
   22         want string
   23     }{{
   24         name: "back_compatibility_all_right",
   25         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"}`,
   26         want: "default",
   27     }, {
   28         name: "back_compatibility_bad_msg",
   29         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"}`,
   30         want: "decodeLogEntry err: dns: overflow unpacking uint16\n",
   31     }, {
   32         name: "back_compatibility_bad_decoding",
   33         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"}`,
   34         want: "decodeLogEntry err: illegal base64 data at input byte 48\n",
   35     }}
   36 
   37     for _, tc := range testCases {
   38         t.Run(tc.name, func(t *testing.T) {
   39             _, err := logOutput.Write([]byte("default"))
   40             assert.Nil(t, err)
   41 
   42             l := &logEntry{}
   43             decodeLogEntry(l, tc.log)
   44 
   45             assert.True(t, strings.HasSuffix(logOutput.String(), tc.want), logOutput.String())
   46 
   47             logOutput.Reset()
   48         })
   49     }
   50 }
   51 
   52 func TestJSON(t *testing.T) {
   53     s := `
   54     {"keystr":"val","obj":{"keybool":true,"keyint":123456}}
   55     `
   56     k, v, jtype := readJSON(&s)
   57     assert.Equal(t, jtype, int32(jsonTStr))
   58     assert.Equal(t, "keystr", k)
   59     assert.Equal(t, "val", v)
   60 
   61     k, _, jtype = readJSON(&s)
   62     assert.Equal(t, jtype, int32(jsonTObj))
   63     assert.Equal(t, "obj", k)
   64 
   65     k, v, jtype = readJSON(&s)
   66     assert.Equal(t, jtype, int32(jsonTBool))
   67     assert.Equal(t, "keybool", k)
   68     assert.Equal(t, "true", v)
   69 
   70     k, v, jtype = readJSON(&s)
   71     assert.Equal(t, jtype, int32(jsonTNum))
   72     assert.Equal(t, "keyint", k)
   73     assert.Equal(t, "123456", v)
   74 
   75     _, _, jtype = readJSON(&s)
   76     assert.True(t, jtype == jsonTErr)
   77 }