2017-09-26 17:45:33 +02:00
|
|
|
package dnstapio
|
|
|
|
|
|
|
|
|
|
import (
|
2017-11-28 00:36:14 +03:00
|
|
|
"net"
|
2017-09-26 17:45:33 +02:00
|
|
|
"sync"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
tap "github.com/dnstap/golang-dnstap"
|
2017-11-28 00:36:14 +03:00
|
|
|
fs "github.com/farsightsec/golang-framestream"
|
2017-09-26 17:45:33 +02:00
|
|
|
)
|
|
|
|
|
|
2017-12-01 13:16:14 +02:00
|
|
|
const (
|
|
|
|
|
endpointTCP = "localhost:0"
|
|
|
|
|
endpointSocket = "dnstap.sock"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
msgType = tap.Dnstap_MESSAGE
|
|
|
|
|
msg = tap.Dnstap{Type: &msgType}
|
|
|
|
|
)
|
|
|
|
|
|
2017-11-28 00:36:14 +03:00
|
|
|
func accept(t *testing.T, l net.Listener, count int) {
|
|
|
|
|
server, err := l.Accept()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("server accept: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-09-29 21:29:33 +02:00
|
|
|
|
2017-11-28 00:36:14 +03:00
|
|
|
dec, err := fs.NewDecoder(server, &fs.DecoderOptions{
|
|
|
|
|
ContentType: []byte("protobuf:dnstap.Dnstap"),
|
|
|
|
|
Bidirectional: true,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("server decoder: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-09-26 17:45:33 +02:00
|
|
|
|
2017-11-28 00:36:14 +03:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
|
if _, err := dec.Decode(); err != nil {
|
|
|
|
|
t.Errorf("server decode: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-26 17:45:33 +02:00
|
|
|
|
2017-11-28 00:36:14 +03:00
|
|
|
if err := server.Close(); err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
2017-09-26 17:45:33 +02:00
|
|
|
}
|
|
|
|
|
|
2017-12-01 13:16:14 +02:00
|
|
|
func TestTransport(t *testing.T) {
|
|
|
|
|
transport := [2][3]string{
|
|
|
|
|
{"tcp", endpointTCP, "false"},
|
|
|
|
|
{"unix", endpointSocket, "true"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, param := range transport {
|
|
|
|
|
// Start TCP listener
|
|
|
|
|
l, err := net.Listen(param[0], param[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Cannot start listener: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
accept(t, l, 1)
|
|
|
|
|
wg.Done()
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
dio := New(l.Addr().String(), param[2] == "true")
|
|
|
|
|
dio.Connect()
|
2017-11-28 00:36:14 +03:00
|
|
|
|
2017-12-01 13:16:14 +02:00
|
|
|
dio.Dnstap(msg)
|
2017-11-28 00:36:14 +03:00
|
|
|
|
2017-12-01 13:16:14 +02:00
|
|
|
wg.Wait()
|
|
|
|
|
l.Close()
|
|
|
|
|
dio.Close()
|
2017-11-28 00:36:14 +03:00
|
|
|
}
|
2017-12-01 13:16:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRace(t *testing.T) {
|
|
|
|
|
count := 10
|
2017-11-28 00:36:14 +03:00
|
|
|
|
|
|
|
|
// Start TCP listener
|
|
|
|
|
l, err := net.Listen("tcp", endpointTCP)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Cannot start listener: %s", err)
|
2017-09-26 17:45:33 +02:00
|
|
|
}
|
2017-11-28 00:36:14 +03:00
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func() {
|
2017-12-01 13:16:14 +02:00
|
|
|
accept(t, l, count)
|
2017-11-28 00:36:14 +03:00
|
|
|
wg.Done()
|
|
|
|
|
}()
|
|
|
|
|
|
2017-12-01 13:16:14 +02:00
|
|
|
dio := New(l.Addr().String(), false)
|
|
|
|
|
dio.Connect()
|
|
|
|
|
defer dio.Close()
|
2017-11-28 00:36:14 +03:00
|
|
|
|
2017-12-01 13:16:14 +02:00
|
|
|
wg.Add(count)
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
|
go func() {
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
dio.Dnstap(msg)
|
|
|
|
|
wg.Done()
|
|
|
|
|
}()
|
|
|
|
|
}
|
2017-11-28 00:36:14 +03:00
|
|
|
|
2017-09-26 17:45:33 +02:00
|
|
|
wg.Wait()
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 13:16:14 +02:00
|
|
|
func TestReconnect(t *testing.T) {
|
|
|
|
|
count := 5
|
2017-11-28 00:36:14 +03:00
|
|
|
|
2017-12-01 13:16:14 +02:00
|
|
|
// Start TCP listener
|
|
|
|
|
l, err := net.Listen("tcp", endpointTCP)
|
2017-11-28 00:36:14 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Cannot start listener: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(1)
|
2017-09-26 17:45:33 +02:00
|
|
|
go func() {
|
2017-11-28 00:36:14 +03:00
|
|
|
accept(t, l, 1)
|
|
|
|
|
wg.Done()
|
2017-09-26 17:45:33 +02:00
|
|
|
}()
|
2017-11-28 00:36:14 +03:00
|
|
|
|
2017-12-01 13:16:14 +02:00
|
|
|
addr := l.Addr().String()
|
|
|
|
|
dio := New(addr, false)
|
|
|
|
|
dio.Connect()
|
|
|
|
|
defer dio.Close()
|
2017-11-28 00:36:14 +03:00
|
|
|
|
|
|
|
|
msg := tap.Dnstap_MESSAGE
|
|
|
|
|
dio.Dnstap(tap.Dnstap{Type: &msg})
|
|
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
2017-12-01 13:16:14 +02:00
|
|
|
// Close listener
|
|
|
|
|
l.Close()
|
2017-11-28 00:36:14 +03:00
|
|
|
|
2017-12-01 13:16:14 +02:00
|
|
|
// And start TCP listener again on the same port
|
|
|
|
|
l, err = net.Listen("tcp", addr)
|
2017-11-28 00:36:14 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Cannot start listener: %s", err)
|
|
|
|
|
}
|
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func() {
|
2017-12-01 13:16:14 +02:00
|
|
|
accept(t, l, 1)
|
2017-11-28 00:36:14 +03:00
|
|
|
wg.Done()
|
2017-09-26 17:45:33 +02:00
|
|
|
}()
|
2017-11-28 00:36:14 +03:00
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
2017-12-01 13:16:14 +02:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
dio.Dnstap(tap.Dnstap{Type: &msg})
|
2017-11-28 00:36:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wg.Wait()
|
2017-09-26 17:45:33 +02:00
|
|
|
}
|