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-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-11-28 00:36:14 +03:00
|
|
|
const endpointTCP = "localhost:0"
|
|
|
|
|
|
|
|
|
|
func TestTCP(t *testing.T) {
|
|
|
|
|
dio := New()
|
|
|
|
|
|
|
|
|
|
err := dio.Connect(endpointTCP, false)
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("Not listening but no error")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
|
accept(t, l, 1)
|
|
|
|
|
wg.Done()
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
err = dio.Connect(l.Addr().String(), false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Cannot connect to listener: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg := tap.Dnstap_MESSAGE
|
|
|
|
|
dio.Dnstap(tap.Dnstap{Type: &msg})
|
|
|
|
|
|
2017-09-26 17:45:33 +02:00
|
|
|
wg.Wait()
|
2017-11-28 00:36:14 +03:00
|
|
|
|
|
|
|
|
dio.Close()
|
2017-09-26 17:45:33 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-28 00:36:14 +03:00
|
|
|
const endpointSocket = "dnstap.sock"
|
|
|
|
|
|
|
|
|
|
func TestSocket(t *testing.T) {
|
|
|
|
|
dio := New()
|
|
|
|
|
|
|
|
|
|
err := dio.Connect(endpointSocket, true)
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("Not listening but no error")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start Socket listener
|
|
|
|
|
l, err := net.Listen("unix", endpointSocket)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Cannot start listener: %s", err)
|
|
|
|
|
}
|
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
err = dio.Connect(endpointSocket, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Cannot connect to listener: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg := tap.Dnstap_MESSAGE
|
|
|
|
|
dio.Dnstap(tap.Dnstap{Type: &msg})
|
|
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
|
|
dio.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRace(t *testing.T) {
|
|
|
|
|
count := 10
|
|
|
|
|
dio := New()
|
|
|
|
|
|
|
|
|
|
err := dio.Connect(endpointTCP, false)
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("Not listening but no error")
|
2017-09-26 17:45:33 +02:00
|
|
|
}
|
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)
|
|
|
|
|
}
|
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
accept(t, l, count)
|
|
|
|
|
wg.Done()
|
2017-09-26 17:45:33 +02:00
|
|
|
}()
|
2017-11-28 00:36:14 +03:00
|
|
|
|
|
|
|
|
err = dio.Connect(l.Addr().String(), false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Cannot connect to listener: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg := tap.Dnstap_MESSAGE
|
|
|
|
|
wg.Add(count)
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
|
go func(i byte) {
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
dio.Dnstap(tap.Dnstap{Type: &msg, Extra: []byte{i}})
|
|
|
|
|
wg.Done()
|
|
|
|
|
}(byte(i))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
|
|
dio.Close()
|
2017-09-26 17:45:33 +02:00
|
|
|
}
|