2021-09-28 12:42:01 +02:00
|
|
|
package netxlite
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
2021-09-09 21:24:27 +02:00
|
|
|
"bytes"
|
2021-02-02 12:05:47 +01:00
|
|
|
"context"
|
2021-09-09 21:24:27 +02:00
|
|
|
"crypto/tls"
|
2021-02-02 12:05:47 +01:00
|
|
|
"errors"
|
2021-09-09 21:24:27 +02:00
|
|
|
"io"
|
2022-05-25 17:03:58 +02:00
|
|
|
"math"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net"
|
|
|
|
"testing"
|
2021-09-09 21:24:27 +02:00
|
|
|
"time"
|
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2022-05-14 17:38:31 +02:00
|
|
|
func TestDNSOverTCPTransport(t *testing.T) {
|
2021-09-28 11:26:16 +02:00
|
|
|
t.Run("RoundTrip", func(t *testing.T) {
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("cannot encode query", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
const address = "9.9.9.9:53"
|
2022-06-01 11:10:08 +02:00
|
|
|
txp := NewUnwrappedDNSOverTCPTransport(new(net.Dialer).DialContext, address)
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := txp.RoundTrip(context.Background(), query)
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil response here")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-09-28 11:26:16 +02:00
|
|
|
t.Run("query too large", func(t *testing.T) {
|
|
|
|
const address = "9.9.9.9:53"
|
2022-06-01 11:10:08 +02:00
|
|
|
txp := NewUnwrappedDNSOverTCPTransport(new(net.Dialer).DialContext, address)
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return make([]byte, math.MaxUint16+1), nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := txp.RoundTrip(context.Background(), query)
|
|
|
|
if !errors.Is(err, errQueryTooLarge) {
|
|
|
|
t.Fatal("unexpected err", err)
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil response here")
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2021-09-28 11:26:16 +02:00
|
|
|
t.Run("dial failure", func(t *testing.T) {
|
|
|
|
const address = "9.9.9.9:53"
|
|
|
|
mocked := errors.New("mocked error")
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return make([]byte, 128), nil
|
|
|
|
},
|
|
|
|
}
|
2021-09-28 11:26:16 +02:00
|
|
|
fakedialer := &mocks.Dialer{
|
|
|
|
MockDialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
return nil, mocked
|
2021-09-09 21:24:27 +02:00
|
|
|
},
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
2022-06-01 11:10:08 +02:00
|
|
|
txp := NewUnwrappedDNSOverTCPTransport(fakedialer.DialContext, address)
|
2022-05-25 17:03:58 +02:00
|
|
|
resp, err := txp.RoundTrip(context.Background(), query)
|
2021-09-28 11:26:16 +02:00
|
|
|
if !errors.Is(err, mocked) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil resp here")
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("write failure", func(t *testing.T) {
|
2021-09-28 11:26:16 +02:00
|
|
|
const address = "9.9.9.9:53"
|
|
|
|
mocked := errors.New("mocked error")
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return make([]byte, 128), nil
|
|
|
|
},
|
|
|
|
}
|
2021-09-28 11:26:16 +02:00
|
|
|
fakedialer := &mocks.Dialer{
|
|
|
|
MockDialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
return &mocks.Conn{
|
|
|
|
MockSetDeadline: func(t time.Time) error {
|
2022-05-25 17:03:58 +02:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
MockWrite: func(b []byte) (int, error) {
|
|
|
|
return 0, mocked
|
2021-09-28 11:26:16 +02:00
|
|
|
},
|
|
|
|
MockClose: func() error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, nil
|
2021-09-09 21:24:27 +02:00
|
|
|
},
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
2022-06-01 11:10:08 +02:00
|
|
|
txp := NewUnwrappedDNSOverTCPTransport(fakedialer.DialContext, address)
|
2022-05-25 17:03:58 +02:00
|
|
|
resp, err := txp.RoundTrip(context.Background(), query)
|
2021-09-28 11:26:16 +02:00
|
|
|
if !errors.Is(err, mocked) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil resp here")
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("first read fails", func(t *testing.T) {
|
2021-09-28 11:26:16 +02:00
|
|
|
const address = "9.9.9.9:53"
|
|
|
|
mocked := errors.New("mocked error")
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return make([]byte, 128), nil
|
|
|
|
},
|
|
|
|
}
|
2021-09-28 11:26:16 +02:00
|
|
|
fakedialer := &mocks.Dialer{
|
|
|
|
MockDialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
return &mocks.Conn{
|
|
|
|
MockSetDeadline: func(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
MockWrite: func(b []byte) (int, error) {
|
2022-05-25 17:03:58 +02:00
|
|
|
return len(b), nil
|
|
|
|
},
|
|
|
|
MockRead: func(b []byte) (int, error) {
|
2021-09-28 11:26:16 +02:00
|
|
|
return 0, mocked
|
|
|
|
},
|
|
|
|
MockClose: func() error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, nil
|
2021-09-09 21:24:27 +02:00
|
|
|
},
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
2022-06-01 11:10:08 +02:00
|
|
|
txp := NewUnwrappedDNSOverTCPTransport(fakedialer.DialContext, address)
|
2022-05-25 17:03:58 +02:00
|
|
|
resp, err := txp.RoundTrip(context.Background(), query)
|
2021-09-28 11:26:16 +02:00
|
|
|
if !errors.Is(err, mocked) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil resp here")
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("second read fails", func(t *testing.T) {
|
2021-09-28 11:26:16 +02:00
|
|
|
const address = "9.9.9.9:53"
|
|
|
|
mocked := errors.New("mocked error")
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return make([]byte, 128), nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
input := io.MultiReader(
|
|
|
|
bytes.NewReader([]byte{byte(0), byte(2)}),
|
|
|
|
&mocks.Reader{
|
|
|
|
MockRead: func(b []byte) (int, error) {
|
|
|
|
return 0, mocked
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2021-09-28 11:26:16 +02:00
|
|
|
fakedialer := &mocks.Dialer{
|
|
|
|
MockDialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
return &mocks.Conn{
|
|
|
|
MockSetDeadline: func(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
MockWrite: func(b []byte) (int, error) {
|
|
|
|
return len(b), nil
|
|
|
|
},
|
2022-05-25 17:03:58 +02:00
|
|
|
MockRead: input.Read,
|
2021-09-28 11:26:16 +02:00
|
|
|
MockClose: func() error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, nil
|
2021-09-09 21:24:27 +02:00
|
|
|
},
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
2022-06-01 11:10:08 +02:00
|
|
|
txp := NewUnwrappedDNSOverTCPTransport(fakedialer.DialContext, address)
|
2022-05-25 17:03:58 +02:00
|
|
|
resp, err := txp.RoundTrip(context.Background(), query)
|
2021-09-28 11:26:16 +02:00
|
|
|
if !errors.Is(err, mocked) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil resp here")
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Run("decode failure", func(t *testing.T) {
|
2021-09-28 11:26:16 +02:00
|
|
|
const address = "9.9.9.9:53"
|
|
|
|
mocked := errors.New("mocked error")
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return make([]byte, 128), nil
|
2021-09-09 21:24:27 +02:00
|
|
|
},
|
2022-05-25 17:03:58 +02:00
|
|
|
}
|
|
|
|
input := bytes.NewReader([]byte{byte(0), byte(1), byte(1)})
|
2021-09-28 11:26:16 +02:00
|
|
|
fakedialer := &mocks.Dialer{
|
|
|
|
MockDialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
return &mocks.Conn{
|
|
|
|
MockSetDeadline: func(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
MockWrite: func(b []byte) (int, error) {
|
|
|
|
return len(b), nil
|
|
|
|
},
|
|
|
|
MockRead: input.Read,
|
|
|
|
MockClose: func() error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, nil
|
2021-09-09 21:24:27 +02:00
|
|
|
},
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
2022-06-01 11:10:08 +02:00
|
|
|
txp := NewUnwrappedDNSOverTCPTransport(fakedialer.DialContext, address)
|
2022-05-25 17:03:58 +02:00
|
|
|
txp.decoder = &mocks.DNSDecoder{
|
|
|
|
MockDecodeResponse: func(data []byte, query model.DNSQuery) (model.DNSResponse, error) {
|
|
|
|
return nil, mocked
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := txp.RoundTrip(context.Background(), query)
|
2021-09-28 11:26:16 +02:00
|
|
|
if !errors.Is(err, mocked) {
|
2022-05-25 17:03:58 +02:00
|
|
|
t.Fatal("unexpected err", err)
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil resp here")
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("successful case", func(t *testing.T) {
|
|
|
|
const address = "9.9.9.9:53"
|
2022-05-25 17:03:58 +02:00
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return make([]byte, 128), nil
|
|
|
|
},
|
|
|
|
}
|
2021-09-28 11:26:16 +02:00
|
|
|
input := bytes.NewReader([]byte{byte(0), byte(1), byte(1)})
|
|
|
|
fakedialer := &mocks.Dialer{
|
|
|
|
MockDialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
return &mocks.Conn{
|
|
|
|
MockSetDeadline: func(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
MockWrite: func(b []byte) (int, error) {
|
|
|
|
return len(b), nil
|
|
|
|
},
|
|
|
|
MockRead: input.Read,
|
|
|
|
MockClose: func() error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}, nil
|
2021-09-09 21:24:27 +02:00
|
|
|
},
|
2021-09-28 11:26:16 +02:00
|
|
|
}
|
2022-06-01 11:10:08 +02:00
|
|
|
txp := NewUnwrappedDNSOverTCPTransport(fakedialer.DialContext, address)
|
2022-05-25 17:03:58 +02:00
|
|
|
expectedResp := &mocks.DNSResponse{}
|
|
|
|
txp.decoder = &mocks.DNSDecoder{
|
|
|
|
MockDecodeResponse: func(data []byte, query model.DNSQuery) (model.DNSResponse, error) {
|
|
|
|
return expectedResp, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := txp.RoundTrip(context.Background(), query)
|
2021-09-28 11:26:16 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if resp != expectedResp {
|
2021-09-28 11:26:16 +02:00
|
|
|
t.Fatal("not the response we expected")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2021-09-28 11:26:16 +02:00
|
|
|
t.Run("other functions okay with TCP", func(t *testing.T) {
|
|
|
|
const address = "9.9.9.9:53"
|
2022-06-01 11:10:08 +02:00
|
|
|
txp := NewUnwrappedDNSOverTCPTransport(new(net.Dialer).DialContext, address)
|
2021-09-28 11:26:16 +02:00
|
|
|
if txp.RequiresPadding() != false {
|
|
|
|
t.Fatal("invalid RequiresPadding")
|
|
|
|
}
|
|
|
|
if txp.Network() != "tcp" {
|
|
|
|
t.Fatal("invalid Network")
|
|
|
|
}
|
|
|
|
if txp.Address() != address {
|
|
|
|
t.Fatal("invalid Address")
|
|
|
|
}
|
2022-01-26 12:18:36 +01:00
|
|
|
txp.CloseIdleConnections()
|
2021-09-28 11:26:16 +02:00
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2021-09-28 11:26:16 +02:00
|
|
|
t.Run("other functions okay with TLS", func(t *testing.T) {
|
|
|
|
const address = "9.9.9.9:853"
|
2022-06-01 11:10:08 +02:00
|
|
|
txp := NewUnwrappedDNSOverTLSTransport((&tls.Dialer{}).DialContext, address)
|
2021-09-28 11:26:16 +02:00
|
|
|
if txp.RequiresPadding() != true {
|
|
|
|
t.Fatal("invalid RequiresPadding")
|
|
|
|
}
|
|
|
|
if txp.Network() != "dot" {
|
|
|
|
t.Fatal("invalid Network")
|
|
|
|
}
|
|
|
|
if txp.Address() != address {
|
|
|
|
t.Fatal("invalid Address")
|
|
|
|
}
|
2022-01-26 12:18:36 +01:00
|
|
|
txp.CloseIdleConnections()
|
2021-09-28 11:26:16 +02:00
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|