aboutsummaryrefslogtreecommitdiff
path: root/plugin/dnstap/dnstapio/dnstap_encoder_test.go
blob: 7ddb0877630a04959e9d48989a49663314f4e8f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package dnstapio

import (
	"bytes"
	"testing"

	tap "github.com/dnstap/golang-dnstap"
	fs "github.com/farsightsec/golang-framestream"
	"github.com/golang/protobuf/proto"
)

func dnstapMsg() *tap.Dnstap {
	t := tap.Dnstap_MESSAGE
	mt := tap.Message_CLIENT_RESPONSE
	msg := &tap.Message{Type: &mt}
	return &tap.Dnstap{Type: &t, Message: msg}
}

func TestEncoderCompatibility(t *testing.T) {
	opts := &fs.EncoderOptions{
		ContentType:   []byte("protobuf:dnstap.DnstapTest"),
		Bidirectional: false,
	}
	msg := dnstapMsg()

	//framestream encoder
	fsW := new(bytes.Buffer)
	fsEnc, err := fs.NewEncoder(fsW, opts)
	if err != nil {
		t.Fatal(err)
	}
	data, err := proto.Marshal(msg)
	if err != nil {
		t.Fatal(err)
	}
	fsEnc.Write(data)
	fsEnc.Close()

	//dnstap encoder
	dnstapW := new(bytes.Buffer)
	dnstapEnc := newDnstapEncoder(opts)
	if err := dnstapEnc.resetWriter(dnstapW); err != nil {
		t.Fatal(err)
	}
	dnstapEnc.writeMsg(msg)
	dnstapEnc.flushBuffer()
	dnstapEnc.close()

	//compare results
	if !bytes.Equal(fsW.Bytes(), dnstapW.Bytes()) {
		t.Fatal("dnstapEncoder is not compatible with framestream Encoder")
	}
}