2021-02-02 12:05:47 +01:00
|
|
|
package netx_test
|
|
|
|
|
|
|
|
import (
|
2021-09-05 19:55:28 +02:00
|
|
|
"context"
|
2021-02-02 12:05:47 +01:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2021-09-05 19:55:28 +02:00
|
|
|
"net"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
2021-06-22 13:00:29 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/bytecounter"
|
2021-09-07 17:52:42 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/legacy/errorsx"
|
2021-02-02 12:05:47 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
|
2021-06-08 11:24:13 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
|
2021-02-02 12:05:47 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
refactor: start pivoting netx (#396)
What do I mean by pivoting? Netx is currently organized by row:
```
| dialer | quicdialer | resolver | ...
saving | | | | ...
errorwrapping | | | | ...
logging | | | | ...
mocking/sys | | | | ...
```
Every row needs to implement saving, errorwrapping, logging, mocking (or
adapting to the system or to some underlying library).
This causes cross package dependencies and, in turn, complexity. For
example, we need the `trace` package for supporting saving.
And `dialer`, `quickdialer`, et al. need to depend on such a package.
The same goes for errorwrapping.
This arrangement further complicates testing. For example, I am
currently working on https://github.com/ooni/probe/issues/1505 and
I realize it need to repeat integration tests in multiple places.
Let's say instead we pivot the above matrix as follows:
```
| saving | errorwrapping | logging | ...
dialer | | | | ...
quicdialer | | | | ...
logging | | | | ...
mocking/sys | | | | ...
...
```
In this way, now every row contains everything related to a specific
action to perform. We can now share code without relying on extra
support packages. What's more, we can write tests and, judding from
the way in which things are made, it seems we only need integration
testing in `errorwrapping` because it's where data quality matters
whereas, in all other cases, unit testing is fine.
I am going, therefore, to proceed with these changes and "pivot"
`netx`. Hopefully, it won't be too painful.
2021-06-23 15:53:12 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-09-05 19:55:28 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewResolverVanilla(t *testing.T) {
|
|
|
|
r := netx.NewResolver(netx.Config{})
|
2021-08-17 11:02:12 +02:00
|
|
|
ir, ok := r.(*resolver.IDNAResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-09-05 18:03:50 +02:00
|
|
|
rla, ok := ir.Resolver.(*netxlite.ResolverLegacyAdapter)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
ewr, ok := rla.ResolverLegacy.(*errorsx.ErrorWrapperResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-03-08 12:05:43 +01:00
|
|
|
ar, ok := ewr.Resolver.(resolver.AddressResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-06-25 11:07:26 +02:00
|
|
|
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewResolverSpecificResolver(t *testing.T) {
|
|
|
|
r := netx.NewResolver(netx.Config{
|
|
|
|
BaseResolver: resolver.BogonResolver{
|
|
|
|
// not initialized because it doesn't matter in this context
|
|
|
|
},
|
|
|
|
})
|
2021-08-17 11:02:12 +02:00
|
|
|
ir, ok := r.(*resolver.IDNAResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-09-05 18:03:50 +02:00
|
|
|
rla, ok := ir.Resolver.(*netxlite.ResolverLegacyAdapter)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
ewr, ok := rla.ResolverLegacy.(*errorsx.ErrorWrapperResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-03-08 12:05:43 +01:00
|
|
|
ar, ok := ewr.Resolver.(resolver.AddressResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-03-08 12:05:43 +01:00
|
|
|
_, ok = ar.Resolver.(resolver.BogonResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewResolverWithBogonFilter(t *testing.T) {
|
|
|
|
r := netx.NewResolver(netx.Config{
|
|
|
|
BogonIsError: true,
|
|
|
|
})
|
2021-08-17 11:02:12 +02:00
|
|
|
ir, ok := r.(*resolver.IDNAResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-09-05 18:03:50 +02:00
|
|
|
rla, ok := ir.Resolver.(*netxlite.ResolverLegacyAdapter)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
ewr, ok := rla.ResolverLegacy.(*errorsx.ErrorWrapperResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-03-08 12:05:43 +01:00
|
|
|
br, ok := ewr.Resolver.(resolver.BogonResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-03-08 12:05:43 +01:00
|
|
|
ar, ok := br.Resolver.(resolver.AddressResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-06-25 11:07:26 +02:00
|
|
|
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewResolverWithLogging(t *testing.T) {
|
|
|
|
r := netx.NewResolver(netx.Config{
|
|
|
|
Logger: log.Log,
|
|
|
|
})
|
2021-08-17 11:02:12 +02:00
|
|
|
ir, ok := r.(*resolver.IDNAResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-09-05 18:03:50 +02:00
|
|
|
rla, ok := ir.Resolver.(*netxlite.ResolverLegacyAdapter)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
lr, ok := rla.ResolverLegacy.(*netxlite.ResolverLogger)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
if lr.Logger != log.Log {
|
|
|
|
t.Fatal("not the logger we expected")
|
|
|
|
}
|
2021-09-05 18:03:50 +02:00
|
|
|
rla, ok = ir.Resolver.(*netxlite.ResolverLegacyAdapter)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
lr, ok = rla.ResolverLegacy.(*netxlite.ResolverLogger)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-09-05 18:03:50 +02:00
|
|
|
rla, ok = lr.Resolver.(*netxlite.ResolverLegacyAdapter)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
ewr, ok := rla.ResolverLegacy.(*errorsx.ErrorWrapperResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("not the resolver we expected %T", rla.ResolverLegacy)
|
|
|
|
}
|
2021-03-08 12:05:43 +01:00
|
|
|
ar, ok := ewr.Resolver.(resolver.AddressResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-06-25 11:07:26 +02:00
|
|
|
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewResolverWithSaver(t *testing.T) {
|
|
|
|
saver := new(trace.Saver)
|
|
|
|
r := netx.NewResolver(netx.Config{
|
|
|
|
ResolveSaver: saver,
|
|
|
|
})
|
2021-08-17 11:02:12 +02:00
|
|
|
ir, ok := r.(*resolver.IDNAResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-09-05 18:03:50 +02:00
|
|
|
rla, ok := ir.Resolver.(*netxlite.ResolverLegacyAdapter)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
sr, ok := rla.ResolverLegacy.(resolver.SaverResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
if sr.Saver != saver {
|
|
|
|
t.Fatal("not the saver we expected")
|
|
|
|
}
|
2021-07-01 18:51:40 +02:00
|
|
|
ewr, ok := sr.Resolver.(*errorsx.ErrorWrapperResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-03-08 12:05:43 +01:00
|
|
|
ar, ok := ewr.Resolver.(resolver.AddressResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-06-25 11:07:26 +02:00
|
|
|
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewResolverWithReadWriteCache(t *testing.T) {
|
|
|
|
r := netx.NewResolver(netx.Config{
|
|
|
|
CacheResolutions: true,
|
|
|
|
})
|
2021-08-17 11:02:12 +02:00
|
|
|
ir, ok := r.(*resolver.IDNAResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-09-05 18:03:50 +02:00
|
|
|
rla, ok := ir.Resolver.(*netxlite.ResolverLegacyAdapter)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
ewr, ok := rla.ResolverLegacy.(*errorsx.ErrorWrapperResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
cr, ok := ewr.Resolver.(*resolver.CacheResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
if cr.ReadOnly != false {
|
|
|
|
t.Fatal("expected readwrite cache here")
|
|
|
|
}
|
2021-03-08 12:05:43 +01:00
|
|
|
ar, ok := cr.Resolver.(resolver.AddressResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-06-25 11:07:26 +02:00
|
|
|
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewResolverWithPrefilledReadonlyCache(t *testing.T) {
|
|
|
|
r := netx.NewResolver(netx.Config{
|
|
|
|
DNSCache: map[string][]string{
|
|
|
|
"dns.google.com": {"8.8.8.8"},
|
|
|
|
},
|
|
|
|
})
|
2021-08-17 11:02:12 +02:00
|
|
|
ir, ok := r.(*resolver.IDNAResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-09-05 18:03:50 +02:00
|
|
|
rla, ok := ir.Resolver.(*netxlite.ResolverLegacyAdapter)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
ewr, ok := rla.ResolverLegacy.(*errorsx.ErrorWrapperResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
cr, ok := ewr.Resolver.(*resolver.CacheResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
if cr.ReadOnly != true {
|
|
|
|
t.Fatal("expected readonly cache here")
|
|
|
|
}
|
|
|
|
if cr.Get("dns.google.com")[0] != "8.8.8.8" {
|
|
|
|
t.Fatal("cache not correctly prefilled")
|
|
|
|
}
|
2021-03-08 12:05:43 +01:00
|
|
|
ar, ok := cr.Resolver.(resolver.AddressResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2021-06-25 11:07:26 +02:00
|
|
|
_, ok = ar.Resolver.(*netxlite.ResolverSystem)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewTLSDialerVanilla(t *testing.T) {
|
|
|
|
td := netx.NewTLSDialer(netx.Config{})
|
2021-09-06 14:12:30 +02:00
|
|
|
rtd, ok := td.(*netxlite.TLSDialerLegacy)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSDialer we expected")
|
|
|
|
}
|
|
|
|
if len(rtd.Config.NextProtos) != 2 {
|
|
|
|
t.Fatal("invalid len(config.NextProtos)")
|
|
|
|
}
|
|
|
|
if rtd.Config.NextProtos[0] != "h2" || rtd.Config.NextProtos[1] != "http/1.1" {
|
|
|
|
t.Fatal("invalid Config.NextProtos")
|
|
|
|
}
|
|
|
|
if rtd.Config.RootCAs != netx.DefaultCertPool() {
|
|
|
|
t.Fatal("invalid Config.RootCAs")
|
|
|
|
}
|
|
|
|
if rtd.Dialer == nil {
|
|
|
|
t.Fatal("invalid Dialer")
|
|
|
|
}
|
|
|
|
if rtd.TLSHandshaker == nil {
|
|
|
|
t.Fatal("invalid TLSHandshaker")
|
|
|
|
}
|
2021-07-01 18:00:09 +02:00
|
|
|
ewth, ok := rtd.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
2021-06-25 20:51:59 +02:00
|
|
|
if _, ok := ewth.TLSHandshaker.(*netxlite.TLSHandshakerConfigurable); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewTLSDialerWithConfig(t *testing.T) {
|
|
|
|
td := netx.NewTLSDialer(netx.Config{
|
|
|
|
TLSConfig: new(tls.Config),
|
|
|
|
})
|
2021-09-06 14:12:30 +02:00
|
|
|
rtd, ok := td.(*netxlite.TLSDialerLegacy)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSDialer we expected")
|
|
|
|
}
|
|
|
|
if len(rtd.Config.NextProtos) != 0 {
|
|
|
|
t.Fatal("invalid len(config.NextProtos)")
|
|
|
|
}
|
|
|
|
if rtd.Config.RootCAs != netx.DefaultCertPool() {
|
|
|
|
t.Fatal("invalid Config.RootCAs")
|
|
|
|
}
|
|
|
|
if rtd.Dialer == nil {
|
|
|
|
t.Fatal("invalid Dialer")
|
|
|
|
}
|
|
|
|
if rtd.TLSHandshaker == nil {
|
|
|
|
t.Fatal("invalid TLSHandshaker")
|
|
|
|
}
|
2021-07-01 18:00:09 +02:00
|
|
|
ewth, ok := rtd.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
2021-06-25 20:51:59 +02:00
|
|
|
if _, ok := ewth.TLSHandshaker.(*netxlite.TLSHandshakerConfigurable); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewTLSDialerWithLogging(t *testing.T) {
|
|
|
|
td := netx.NewTLSDialer(netx.Config{
|
|
|
|
Logger: log.Log,
|
|
|
|
})
|
2021-09-06 14:12:30 +02:00
|
|
|
rtd, ok := td.(*netxlite.TLSDialerLegacy)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSDialer we expected")
|
|
|
|
}
|
|
|
|
if len(rtd.Config.NextProtos) != 2 {
|
|
|
|
t.Fatal("invalid len(config.NextProtos)")
|
|
|
|
}
|
|
|
|
if rtd.Config.NextProtos[0] != "h2" || rtd.Config.NextProtos[1] != "http/1.1" {
|
|
|
|
t.Fatal("invalid Config.NextProtos")
|
|
|
|
}
|
|
|
|
if rtd.Config.RootCAs != netx.DefaultCertPool() {
|
|
|
|
t.Fatal("invalid Config.RootCAs")
|
|
|
|
}
|
|
|
|
if rtd.Dialer == nil {
|
|
|
|
t.Fatal("invalid Dialer")
|
|
|
|
}
|
|
|
|
if rtd.TLSHandshaker == nil {
|
|
|
|
t.Fatal("invalid TLSHandshaker")
|
|
|
|
}
|
2021-06-25 12:21:34 +02:00
|
|
|
lth, ok := rtd.TLSHandshaker.(*netxlite.TLSHandshakerLogger)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
|
|
|
if lth.Logger != log.Log {
|
|
|
|
t.Fatal("not the Logger we expected")
|
|
|
|
}
|
2021-07-01 18:00:09 +02:00
|
|
|
ewth, ok := lth.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
2021-06-25 20:51:59 +02:00
|
|
|
if _, ok := ewth.TLSHandshaker.(*netxlite.TLSHandshakerConfigurable); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewTLSDialerWithSaver(t *testing.T) {
|
|
|
|
saver := new(trace.Saver)
|
|
|
|
td := netx.NewTLSDialer(netx.Config{
|
|
|
|
TLSSaver: saver,
|
|
|
|
})
|
2021-09-06 14:12:30 +02:00
|
|
|
rtd, ok := td.(*netxlite.TLSDialerLegacy)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSDialer we expected")
|
|
|
|
}
|
|
|
|
if len(rtd.Config.NextProtos) != 2 {
|
|
|
|
t.Fatal("invalid len(config.NextProtos)")
|
|
|
|
}
|
|
|
|
if rtd.Config.NextProtos[0] != "h2" || rtd.Config.NextProtos[1] != "http/1.1" {
|
|
|
|
t.Fatal("invalid Config.NextProtos")
|
|
|
|
}
|
|
|
|
if rtd.Config.RootCAs != netx.DefaultCertPool() {
|
|
|
|
t.Fatal("invalid Config.RootCAs")
|
|
|
|
}
|
|
|
|
if rtd.Dialer == nil {
|
|
|
|
t.Fatal("invalid Dialer")
|
|
|
|
}
|
|
|
|
if rtd.TLSHandshaker == nil {
|
|
|
|
t.Fatal("invalid TLSHandshaker")
|
|
|
|
}
|
2021-06-08 11:24:13 +02:00
|
|
|
sth, ok := rtd.TLSHandshaker.(tlsdialer.SaverTLSHandshaker)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
|
|
|
if sth.Saver != saver {
|
|
|
|
t.Fatal("not the Logger we expected")
|
|
|
|
}
|
2021-07-01 18:00:09 +02:00
|
|
|
ewth, ok := sth.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
2021-06-25 20:51:59 +02:00
|
|
|
if _, ok := ewth.TLSHandshaker.(*netxlite.TLSHandshakerConfigurable); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewTLSDialerWithNoTLSVerifyAndConfig(t *testing.T) {
|
|
|
|
td := netx.NewTLSDialer(netx.Config{
|
|
|
|
TLSConfig: new(tls.Config),
|
|
|
|
NoTLSVerify: true,
|
|
|
|
})
|
2021-09-06 14:12:30 +02:00
|
|
|
rtd, ok := td.(*netxlite.TLSDialerLegacy)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSDialer we expected")
|
|
|
|
}
|
|
|
|
if len(rtd.Config.NextProtos) != 0 {
|
|
|
|
t.Fatal("invalid len(config.NextProtos)")
|
|
|
|
}
|
|
|
|
if rtd.Config.InsecureSkipVerify != true {
|
|
|
|
t.Fatal("expected true InsecureSkipVerify")
|
|
|
|
}
|
|
|
|
if rtd.Config.RootCAs != netx.DefaultCertPool() {
|
|
|
|
t.Fatal("invalid Config.RootCAs")
|
|
|
|
}
|
|
|
|
if rtd.Dialer == nil {
|
|
|
|
t.Fatal("invalid Dialer")
|
|
|
|
}
|
|
|
|
if rtd.TLSHandshaker == nil {
|
|
|
|
t.Fatal("invalid TLSHandshaker")
|
|
|
|
}
|
2021-07-01 18:00:09 +02:00
|
|
|
ewth, ok := rtd.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
2021-06-25 20:51:59 +02:00
|
|
|
if _, ok := ewth.TLSHandshaker.(*netxlite.TLSHandshakerConfigurable); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewTLSDialerWithNoTLSVerifyAndNoConfig(t *testing.T) {
|
|
|
|
td := netx.NewTLSDialer(netx.Config{
|
|
|
|
NoTLSVerify: true,
|
|
|
|
})
|
2021-09-06 14:12:30 +02:00
|
|
|
rtd, ok := td.(*netxlite.TLSDialerLegacy)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSDialer we expected")
|
|
|
|
}
|
|
|
|
if len(rtd.Config.NextProtos) != 2 {
|
|
|
|
t.Fatal("invalid len(config.NextProtos)")
|
|
|
|
}
|
|
|
|
if rtd.Config.NextProtos[0] != "h2" || rtd.Config.NextProtos[1] != "http/1.1" {
|
|
|
|
t.Fatal("invalid Config.NextProtos")
|
|
|
|
}
|
|
|
|
if rtd.Config.InsecureSkipVerify != true {
|
|
|
|
t.Fatal("expected true InsecureSkipVerify")
|
|
|
|
}
|
|
|
|
if rtd.Config.RootCAs != netx.DefaultCertPool() {
|
|
|
|
t.Fatal("invalid Config.RootCAs")
|
|
|
|
}
|
|
|
|
if rtd.Dialer == nil {
|
|
|
|
t.Fatal("invalid Dialer")
|
|
|
|
}
|
|
|
|
if rtd.TLSHandshaker == nil {
|
|
|
|
t.Fatal("invalid TLSHandshaker")
|
|
|
|
}
|
2021-07-01 18:00:09 +02:00
|
|
|
ewth, ok := rtd.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
2021-06-25 20:51:59 +02:00
|
|
|
if _, ok := ewth.TLSHandshaker.(*netxlite.TLSHandshakerConfigurable); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the TLSHandshaker we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewVanilla(t *testing.T) {
|
|
|
|
txp := netx.NewHTTPTransport(netx.Config{})
|
|
|
|
uatxp, ok := txp.(httptransport.UserAgentTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if _, ok := uatxp.RoundTripper.(*http.Transport); !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewWithDialer(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
dialer := netx.FakeDialer{Err: expected}
|
|
|
|
txp := netx.NewHTTPTransport(netx.Config{
|
|
|
|
Dialer: dialer,
|
|
|
|
})
|
|
|
|
client := &http.Client{Transport: txp}
|
|
|
|
resp, err := client.Get("http://www.google.com")
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("not the response we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewWithTLSDialer(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
2021-09-06 14:12:30 +02:00
|
|
|
tlsDialer := &netxlite.TLSDialerLegacy{
|
2021-09-05 19:55:28 +02:00
|
|
|
Config: new(tls.Config),
|
|
|
|
Dialer: &mocks.Dialer{
|
|
|
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
// nothing
|
|
|
|
},
|
|
|
|
},
|
2021-06-25 20:51:59 +02:00
|
|
|
TLSHandshaker: &netxlite.TLSHandshakerConfigurable{},
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
txp := netx.NewHTTPTransport(netx.Config{
|
|
|
|
TLSDialer: tlsDialer,
|
|
|
|
})
|
|
|
|
client := &http.Client{Transport: txp}
|
|
|
|
resp, err := client.Get("https://www.google.com")
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("not the response we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewWithByteCounter(t *testing.T) {
|
|
|
|
counter := bytecounter.New()
|
|
|
|
txp := netx.NewHTTPTransport(netx.Config{
|
|
|
|
ByteCounter: counter,
|
|
|
|
})
|
|
|
|
uatxp, ok := txp.(httptransport.UserAgentTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
bctxp, ok := uatxp.RoundTripper.(httptransport.ByteCountingTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if bctxp.Counter != counter {
|
|
|
|
t.Fatal("not the byte counter we expected")
|
|
|
|
}
|
|
|
|
if _, ok := bctxp.RoundTripper.(*http.Transport); !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewWithLogger(t *testing.T) {
|
|
|
|
txp := netx.NewHTTPTransport(netx.Config{
|
|
|
|
Logger: log.Log,
|
|
|
|
})
|
|
|
|
uatxp, ok := txp.(httptransport.UserAgentTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
2021-06-26 18:11:47 +02:00
|
|
|
ltxp, ok := uatxp.RoundTripper.(*netxlite.HTTPTransportLogger)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if ltxp.Logger != log.Log {
|
|
|
|
t.Fatal("not the logger we expected")
|
|
|
|
}
|
2021-06-26 18:11:47 +02:00
|
|
|
if _, ok := ltxp.HTTPTransport.(*http.Transport); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewWithSaver(t *testing.T) {
|
|
|
|
saver := new(trace.Saver)
|
|
|
|
txp := netx.NewHTTPTransport(netx.Config{
|
|
|
|
HTTPSaver: saver,
|
|
|
|
})
|
|
|
|
uatxp, ok := txp.(httptransport.UserAgentTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
stxptxp, ok := uatxp.RoundTripper.(httptransport.SaverTransactionHTTPTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if stxptxp.Saver != saver {
|
|
|
|
t.Fatal("not the logger we expected")
|
|
|
|
}
|
|
|
|
sptxp, ok := stxptxp.RoundTripper.(httptransport.SaverPerformanceHTTPTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if sptxp.Saver != saver {
|
|
|
|
t.Fatal("not the logger we expected")
|
|
|
|
}
|
|
|
|
sbtxp, ok := sptxp.RoundTripper.(httptransport.SaverBodyHTTPTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if sbtxp.Saver != saver {
|
|
|
|
t.Fatal("not the logger we expected")
|
|
|
|
}
|
|
|
|
smtxp, ok := sbtxp.RoundTripper.(httptransport.SaverMetadataHTTPTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if smtxp.Saver != saver {
|
|
|
|
t.Fatal("not the logger we expected")
|
|
|
|
}
|
|
|
|
if _, ok := smtxp.RoundTripper.(*http.Transport); !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientInvalidURL(t *testing.T) {
|
|
|
|
dnsclient, err := netx.NewDNSClient(netx.Config{}, "\t\t\t")
|
|
|
|
if err == nil || !strings.HasSuffix(err.Error(), "invalid control character in URL") {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if dnsclient.Resolver != nil {
|
|
|
|
t.Fatal("expected nil resolver here")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientUnsupportedScheme(t *testing.T) {
|
|
|
|
dnsclient, err := netx.NewDNSClient(netx.Config{}, "antani:///")
|
|
|
|
if err == nil || err.Error() != "unsupported resolver scheme" {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if dnsclient.Resolver != nil {
|
|
|
|
t.Fatal("expected nil resolver here")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientSystemResolver(t *testing.T) {
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "system:///")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-06-25 11:07:26 +02:00
|
|
|
if _, ok := dnsclient.Resolver.(*netxlite.ResolverSystem); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientEmpty(t *testing.T) {
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-06-25 11:07:26 +02:00
|
|
|
if _, ok := dnsclient.Resolver.(*netxlite.ResolverSystem); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientPowerdnsDoH(t *testing.T) {
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "doh://powerdns")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, ok := dnsclient.Resolver.(resolver.SerialResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
if _, ok := r.Transport().(resolver.DNSOverHTTPS); !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientGoogleDoH(t *testing.T) {
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "doh://google")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, ok := dnsclient.Resolver.(resolver.SerialResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
if _, ok := r.Transport().(resolver.DNSOverHTTPS); !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientCloudflareDoH(t *testing.T) {
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "doh://cloudflare")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, ok := dnsclient.Resolver.(resolver.SerialResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
if _, ok := r.Transport().(resolver.DNSOverHTTPS); !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientCloudflareDoHSaver(t *testing.T) {
|
|
|
|
saver := new(trace.Saver)
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{ResolveSaver: saver}, "doh://cloudflare")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, ok := dnsclient.Resolver.(resolver.SerialResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
txp, ok := r.Transport().(resolver.SaverDNSTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if _, ok := txp.RoundTripper.(resolver.DNSOverHTTPS); !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientUDP(t *testing.T) {
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "udp://8.8.8.8:53")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, ok := dnsclient.Resolver.(resolver.SerialResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
if _, ok := r.Transport().(resolver.DNSOverUDP); !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientUDPDNSSaver(t *testing.T) {
|
|
|
|
saver := new(trace.Saver)
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{ResolveSaver: saver}, "udp://8.8.8.8:53")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, ok := dnsclient.Resolver.(resolver.SerialResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
txp, ok := r.Transport().(resolver.SaverDNSTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if _, ok := txp.RoundTripper.(resolver.DNSOverUDP); !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientTCP(t *testing.T) {
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "tcp://8.8.8.8:53")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, ok := dnsclient.Resolver.(resolver.SerialResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
txp, ok := r.Transport().(resolver.DNSOverTCP)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if txp.Network() != "tcp" {
|
|
|
|
t.Fatal("not the Network we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientTCPDNSSaver(t *testing.T) {
|
|
|
|
saver := new(trace.Saver)
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{ResolveSaver: saver}, "tcp://8.8.8.8:53")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, ok := dnsclient.Resolver.(resolver.SerialResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
txp, ok := r.Transport().(resolver.SaverDNSTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
dotcp, ok := txp.RoundTripper.(resolver.DNSOverTCP)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if dotcp.Network() != "tcp" {
|
|
|
|
t.Fatal("not the Network we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientDoT(t *testing.T) {
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "dot://8.8.8.8:53")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, ok := dnsclient.Resolver.(resolver.SerialResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
txp, ok := r.Transport().(resolver.DNSOverTCP)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if txp.Network() != "dot" {
|
|
|
|
t.Fatal("not the Network we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientDoTDNSSaver(t *testing.T) {
|
|
|
|
saver := new(trace.Saver)
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{ResolveSaver: saver}, "dot://8.8.8.8:53")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r, ok := dnsclient.Resolver.(resolver.SerialResolver)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
txp, ok := r.Transport().(resolver.SaverDNSTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
dotls, ok := txp.RoundTripper.(resolver.DNSOverTCP)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if dotls.Network() != "dot" {
|
|
|
|
t.Fatal("not the Network we expected")
|
|
|
|
}
|
|
|
|
dnsclient.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSCLientDoTWithoutPort(t *testing.T) {
|
|
|
|
c, err := netx.NewDNSClientWithOverrides(
|
|
|
|
netx.Config{}, "dot://8.8.8.8", "", "8.8.8.8", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if c.Resolver.Address() != "8.8.8.8:853" {
|
|
|
|
t.Fatal("expected default port to be added")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSCLientTCPWithoutPort(t *testing.T) {
|
|
|
|
c, err := netx.NewDNSClientWithOverrides(
|
|
|
|
netx.Config{}, "tcp://8.8.8.8", "", "8.8.8.8", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if c.Resolver.Address() != "8.8.8.8:53" {
|
|
|
|
t.Fatal("expected default port to be added")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSCLientUDPWithoutPort(t *testing.T) {
|
|
|
|
c, err := netx.NewDNSClientWithOverrides(
|
|
|
|
netx.Config{}, "udp://8.8.8.8", "", "8.8.8.8", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if c.Resolver.Address() != "8.8.8.8:53" {
|
|
|
|
t.Fatal("expected default port to be added")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientBadDoTEndpoint(t *testing.T) {
|
|
|
|
_, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "dot://bad:endpoint:53")
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "too many colons in address") {
|
|
|
|
t.Fatal("expected error with bad endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientBadTCPEndpoint(t *testing.T) {
|
|
|
|
_, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "tcp://bad:endpoint:853")
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "too many colons in address") {
|
|
|
|
t.Fatal("expected error with bad endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientBadUDPEndpoint(t *testing.T) {
|
|
|
|
_, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "udp://bad:endpoint:853")
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "too many colons in address") {
|
|
|
|
t.Fatal("expected error with bad endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSCLientWithInvalidTLSVersion(t *testing.T) {
|
|
|
|
_, err := netx.NewDNSClientWithOverrides(
|
|
|
|
netx.Config{}, "dot://8.8.8.8", "", "", "TLSv999")
|
2021-06-25 12:39:45 +02:00
|
|
|
if !errors.Is(err, netxlite.ErrInvalidTLSVersion) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatalf("not the error we expected: %+v", err)
|
|
|
|
}
|
|
|
|
}
|