querylog: imp code

This commit is contained in:
Dimitry Kolyshev 2023-06-13 13:08:15 +04:00
parent fd7260f6de
commit 86e25944b3
2 changed files with 75 additions and 2 deletions

View file

@ -36,7 +36,7 @@ var csvHeaderRow = csvRow{
// toCSV returns a slice of strings with entry fields according to the // toCSV returns a slice of strings with entry fields according to the
// csvHeaderRow slice. // csvHeaderRow slice.
func (e *logEntry) toCSV() (out csvRow) { func (e *logEntry) toCSV() (out *csvRow) {
var filterID, filterRule string var filterID, filterRule string
if e.Result.IsFiltered && len(e.Result.Rules) > 0 { if e.Result.IsFiltered && len(e.Result.Rules) > 0 {
@ -47,7 +47,7 @@ func (e *logEntry) toCSV() (out csvRow) {
aData := ansData(e) aData := ansData(e)
return csvRow{ return &csvRow{
strconv.FormatBool(e.AuthenticatedData), strconv.FormatBool(e.AuthenticatedData),
aData.rCode, aData.rCode,
aData.typ, aData.typ,

View file

@ -0,0 +1,73 @@
package querylog
import (
"net"
"testing"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var testDate = time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
func TestLogEntry_toCSV(t *testing.T) {
ans, err := dns.NewRR("www.example.org. IN A 127.0.0.1")
require.NoError(t, err)
ansBytes, err := (&dns.Msg{Answer: []dns.RR{ans}}).Pack()
require.NoError(t, err)
testCases := []struct {
entry *logEntry
want *csvRow
name string
}{{
name: "simple",
entry: &logEntry{
Time: testDate,
QHost: "test.host",
QType: "A",
QClass: "IN",
ReqECS: "",
ClientID: "test-client-id",
ClientProto: ClientProtoDoH,
Upstream: "https://test.upstream:443/dns-query",
Answer: ansBytes,
OrigAnswer: nil,
IP: net.IP{1, 2, 3, 4},
Result: filtering.Result{},
Elapsed: 500 * time.Millisecond,
Cached: false,
AuthenticatedData: false,
},
want: &[18]string{
"false",
"NOERROR",
"A",
"127.0.0.1",
"false",
"1.2.3.4",
"test-client-id",
"",
"500",
"",
"",
"doh",
"IN",
"test.host",
"A",
"NotFilteredNotFound",
"2022-01-01T00:00:00Z",
"https://test.upstream:443/dns-query",
},
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, tc.entry.toCSV())
})
}
}