summaryrefslogtreecommitdiffstats
path: root/tests/dnstap
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-08 20:37:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-08 20:37:50 +0000
commitc1f743ab2e4a7046d5500875a47d1f62c8624603 (patch)
tree709946d52f5f3bbaeb38be9e3f1d56d11f058237 /tests/dnstap
parentInitial commit. (diff)
downloadknot-resolver-upstream/5.7.1.tar.xz
knot-resolver-upstream/5.7.1.zip
Adding upstream version 5.7.1.upstream/5.7.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--tests/dnstap/meson.build11
-rw-r--r--tests/dnstap/src/dnstap-test/config14
-rw-r--r--tests/dnstap/src/dnstap-test/go.mod9
-rw-r--r--tests/dnstap/src/dnstap-test/go.sum44
-rw-r--r--tests/dnstap/src/dnstap-test/main.go247
-rwxr-xr-xtests/dnstap/src/dnstap-test/run.sh34
6 files changed, 359 insertions, 0 deletions
diff --git a/tests/dnstap/meson.build b/tests/dnstap/meson.build
new file mode 100644
index 0000000..1dfd87e
--- /dev/null
+++ b/tests/dnstap/meson.build
@@ -0,0 +1,11 @@
+
+# note: it will be skipped if 'go' is missing (and marked so)
+test('dnstap',
+ find_program('./src/dnstap-test/run.sh'),
+ args: [ sbin_dir / 'kresd' ],
+ suite: [ 'postinstall', 'dnstap' ],
+ timeout: 120, # it may need to fetch go packages, etc.
+ # it takes relatively long time
+ kwargs: meson.version().version_compare('<0.52') ? {} : { 'priority': 5 },
+)
+
diff --git a/tests/dnstap/src/dnstap-test/config b/tests/dnstap/src/dnstap-test/config
new file mode 100644
index 0000000..5f15308
--- /dev/null
+++ b/tests/dnstap/src/dnstap-test/config
@@ -0,0 +1,14 @@
+-- SPDX-License-Identifier: GPL-3.0-or-later
+modules = {
+ 'hints',
+ dnstap = {
+ socket_path = "dnstap.sock",
+ client = {
+ log_queries = true,
+ log_responses = true,
+ }
+ }
+}
+hints['fake1.localdomain'] = '1.2.3.4'
+hints['fake2.localdomain'] = '1.2.3.5'
+hints['fake3.localdomain'] = '1.2.3.6'
diff --git a/tests/dnstap/src/dnstap-test/go.mod b/tests/dnstap/src/dnstap-test/go.mod
new file mode 100644
index 0000000..6b65088
--- /dev/null
+++ b/tests/dnstap/src/dnstap-test/go.mod
@@ -0,0 +1,9 @@
+module gitlab.nic.cz/knot/knot-resolver/tests/dnstap-test
+
+go 1.17
+
+require (
+ github.com/cloudflare/dns v0.0.0-20151007113418-e20ffa3da443
+ github.com/dnstap/golang-dnstap v0.4.0
+ google.golang.org/protobuf v1.30.0
+)
diff --git a/tests/dnstap/src/dnstap-test/go.sum b/tests/dnstap/src/dnstap-test/go.sum
new file mode 100644
index 0000000..1860f9e
--- /dev/null
+++ b/tests/dnstap/src/dnstap-test/go.sum
@@ -0,0 +1,44 @@
+github.com/cloudflare/dns v0.0.0-20151007113418-e20ffa3da443 h1:dYR6/V5rx/uaHsy4m1JuWfKYZO0r+G89BLD+XN7s9AI=
+github.com/cloudflare/dns v0.0.0-20151007113418-e20ffa3da443/go.mod h1:pa4p3oKOxzbXjrV5AGD1v5xjL7skv9BvO4J0Llo3P+s=
+github.com/dnstap/golang-dnstap v0.4.0 h1:KRHBoURygdGtBjDI2w4HifJfMAhhOqDuktAokaSa234=
+github.com/dnstap/golang-dnstap v0.4.0/go.mod h1:FqsSdH58NAmkAvKcpyxht7i4FoBjKu8E4JUPt8ipSUs=
+github.com/farsightsec/golang-framestream v0.3.0 h1:/spFQHucTle/ZIPkYqrfshQqPe2VQEzesH243TjIwqA=
+github.com/farsightsec/golang-framestream v0.3.0/go.mod h1:eNde4IQyEiA5br02AouhEHCu3p3UzrCdFR4LuQHklMI=
+github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
+github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
+github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
+github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
+github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
+github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
+github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
+github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
+github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
+github.com/miekg/dns v1.1.31 h1:sJFOl9BgwbYAWOGEwr61FU28pqsBNdpRBnhGXtO06Oo=
+github.com/miekg/dns v1.1.31/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
+golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
+golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g=
+golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe h1:6fAMxZRR6sl1Uq8U61gxU+kPTs2tR8uOySCbBP7BN/M=
+golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
+golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
+google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
+google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
+google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
+google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
+google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
+google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
+google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
+google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
diff --git a/tests/dnstap/src/dnstap-test/main.go b/tests/dnstap/src/dnstap-test/main.go
new file mode 100644
index 0000000..a9d6635
--- /dev/null
+++ b/tests/dnstap/src/dnstap-test/main.go
@@ -0,0 +1,247 @@
+package main
+
+import (
+ "context"
+ "flag"
+ "fmt"
+ "github.com/cloudflare/dns"
+ dnstap "github.com/dnstap/golang-dnstap"
+ "google.golang.org/protobuf/proto"
+ "io"
+ "io/ioutil"
+ "log"
+ "net"
+ "os"
+ "os/exec"
+ "strings"
+ "time"
+)
+
+var (
+ kresdArgs = []string{
+ "-n",
+ "-q",
+ }
+)
+
+func qnameFromFrame(b []byte) (string, error) {
+ dt := &dnstap.Dnstap{}
+ var name string
+ if err := proto.Unmarshal(b, dt); err != nil {
+ return name, err
+ }
+
+ var msg_raw []byte
+ m := dt.Message
+ if *m.Type == dnstap.Message_CLIENT_QUERY {
+ msg_raw = m.QueryMessage
+ } else if *m.Type == dnstap.Message_CLIENT_RESPONSE {
+ msg_raw = m.ResponseMessage
+ } else {
+ return name, fmt.Errorf("incorrect message type: %v", *m.Type)
+ }
+
+ if msg_raw == nil {
+ return name, fmt.Errorf("no message payload")
+ }
+ if err := dns.IsMsg(msg_raw); err != nil {
+ return name, err
+ }
+ var msg dns.Msg
+ if err := msg.Unpack(msg_raw); err != nil {
+ return name, err
+ }
+ if len(msg.Question) < 1 {
+ return name, fmt.Errorf("question empty")
+ }
+ return msg.Question[0].Name, nil
+}
+
+func listenOn() (net.Addr, *os.File, error) {
+ udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
+ IP: net.ParseIP("127.0.0.1"),
+ Port: 0,
+ })
+ if err != nil {
+ return nil, nil, err
+ }
+
+ file, err := udpConn.File()
+ if err != nil {
+ return nil, nil, err
+ }
+ return udpConn.LocalAddr(), file, nil
+}
+
+func runKresd(ctx context.Context, path, configFile string, grace time.Duration) (chan bool, error) {
+ ch := make(chan bool)
+ kresdArgs = append(kresdArgs, "-c"+configFile)
+ // we have 1 object in ExtraFiles with index 0
+ // child fd will be 3 + i = 3
+ kresdArgs = append(kresdArgs, "-S3")
+
+ file := ctx.Value("file").(*os.File)
+ debug := ctx.Value("debug").(bool)
+
+ cmd := exec.CommandContext(ctx, path, kresdArgs...)
+ cmd.ExtraFiles = []*os.File{file}
+
+ var stdout, stderr io.ReadCloser
+ var err error
+ if debug {
+ stdout, err = cmd.StdoutPipe()
+ if err != nil {
+ log.Printf("stdoutpipe: %v\n", err)
+ return ch, err
+ }
+
+ stderr, err = cmd.StderrPipe()
+ if err != nil {
+ log.Printf("stderrpipe: %v\n", err)
+ return ch, err
+ }
+ }
+
+ go func() {
+ status := false
+ defer func() {
+ ch <- status // kresd done
+ }()
+ if err := cmd.Start(); err != nil {
+ log.Printf("start: %v\n", err)
+ return
+ }
+ time.Sleep(grace)
+ ch <- true // Started kresd
+
+ if debug {
+ s, err := ioutil.ReadAll(stdout)
+ if err != nil {
+ log.Printf("readall: %v\n", err)
+ return
+ }
+ if len(s) > 0 {
+ fmt.Printf("stdout:\n%s\n", s)
+ }
+
+ s, err = ioutil.ReadAll(stderr)
+ if err != nil {
+ log.Printf("readall: %v\n", err)
+ return
+ }
+ if len(s) > 0 {
+ fmt.Printf("stderr:\n%s\n", s)
+ }
+ }
+
+ if err := cmd.Wait(); err != nil && err.Error() != "signal: killed" {
+ log.Printf("wait: %v\n", err)
+ return
+ }
+ status = true
+ }()
+ return ch, nil
+}
+
+func main() {
+ var (
+ unixSocket = flag.String("u", "dnstap.sock", "dnstap socket")
+ kresdPath = flag.String("cmd", "kresd", "kresd path")
+ configFile = flag.String("c", "config", "config file")
+ qnames = flag.String("q", ".", "list of comma separated zones")
+ grace = flag.String("g", "1s", "Time to wait for daemon start")
+ timeout = flag.String("t", "60s", "Test Timeout")
+ debug = flag.Bool("d", false, "Debug")
+ )
+
+ flag.Parse()
+
+ kresdStartGracePeriod, err := time.ParseDuration(*grace)
+ if err != nil {
+ panic(err)
+ }
+
+ testTimeout, err := time.ParseDuration(*timeout)
+ if err != nil {
+ panic(err)
+ }
+
+ input, err := dnstap.NewFrameStreamSockInputFromPath(*unixSocket)
+ if err != nil {
+ panic(err)
+ }
+
+ output := make(chan []byte)
+ go input.ReadInto(output)
+
+ ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
+
+ // Create a UDP listening socket on random port
+ // FD will be passed on to kresd
+ addr, file, err := listenOn()
+ if err != nil {
+ panic(err)
+ }
+ if *debug {
+ log.Printf("listen addr:%v", addr)
+ }
+ ctx = context.WithValue(ctx, "file", file)
+ ctx = context.WithValue(ctx, "debug", *debug)
+
+ ch, err := runKresd(ctx, *kresdPath, *configFile, kresdStartGracePeriod)
+ if err != nil {
+ panic(err)
+ }
+
+ log.Printf("Waiting for kresd to start\n")
+ status := <-ch
+ if !status {
+ os.Exit(1) // error starting
+ }
+
+ go func() {
+ parts := strings.Split(*qnames, ",")
+
+ if len(parts) == 0 {
+ log.Printf("qname count is 0")
+ }
+ for _, name := range parts {
+ m := new(dns.Msg)
+ fqdn := dns.Fqdn(name)
+ m.SetQuestion(fqdn, dns.TypeA)
+ c := new(dns.Client)
+ resp, _, err := c.Exchange(m, fmt.Sprintf("%v", addr))
+ if err != nil {
+ log.Printf("%v\n", err)
+ os.Exit(1) // Test Failed
+ }
+ if *debug {
+ log.Printf("Response: %v", resp)
+ }
+
+ for range "QR" { // Checking Query and Response is the same ATM
+ o := <-output
+ if *debug {
+ log.Printf("raw dnstap:%v", o)
+ }
+ dtName, err := qnameFromFrame(o)
+ if err != nil {
+ log.Printf("%v\n", err)
+ os.Exit(1)
+ }
+ if fqdn != dtName {
+ log.Printf("expected %v got %v", fqdn, dtName)
+ os.Exit(1) // Test failed
+ }
+ log.Printf("matched qname: %v", dtName)
+ }
+ }
+ cancel() // Send signal to close daemon
+ }()
+
+ status = <-ch
+ if !status {
+ os.Exit(1) // error in wait
+ }
+ log.Printf("Tested OK\n")
+}
diff --git a/tests/dnstap/src/dnstap-test/run.sh b/tests/dnstap/src/dnstap-test/run.sh
new file mode 100755
index 0000000..37822b7
--- /dev/null
+++ b/tests/dnstap/src/dnstap-test/run.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+set -e
+KRESD_CMD=$1
+MESON_BUILD_ROOT=$(pwd)
+mkdir -p tests/dnstap
+export GOPATH=$MESON_BUILD_ROOT/tests/dnstap
+echo "$GOPATH"
+cd "$(dirname $0)"
+DNSTAP_TEST=dnstap-test
+
+if [ -z "$GITLAB_CI" ]; then
+ type -P go >/dev/null || exit 77
+ echo "Building the dnstap test and its dependencies..."
+ # some packages may be missing on the system right now
+ go get .
+else
+ # In CI we've prebuilt dependencies into the default GOPATH.
+ # We're in a scratch container, so we just add the dnstap test inside.
+ export GOPATH=/root/go
+fi
+DTAP_DIR="$GOPATH/src"
+DTAP="$DTAP_DIR/$DNSTAP_TEST"
+mkdir -p "$DTAP_DIR"
+rm -f $DTAP && ln -s $(realpath ..)/$DNSTAP_TEST $DTAP
+go install .
+
+
+CONFIG=$(realpath ./config)
+ZONES="fake1.localdomain,fake2.localdomain,fake3.localdomain"
+TIMEOUT=60s
+GRACE=5s
+cd $MESON_BUILD_ROOT/tests/dnstap # don't leave stuff like *.mdb in ./.
+$GOPATH/bin/$DNSTAP_TEST -c $CONFIG -cmd $KRESD_CMD -q $ZONES -t $TIMEOUT -g $GRACE -d
+