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-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"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
2022-01-05 17:17:20 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewResolverVanilla(t *testing.T) {
|
|
|
|
r := netx.NewResolver(netx.Config{})
|
2022-01-07 18:33:37 +01:00
|
|
|
ir, ok := r.(*netxlite.ResolverIDNA)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ewr, ok := ir.Resolver.(*netxlite.ErrorWrapperResolver)
|
2021-09-05 18:03:50 +02:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ar, ok := ewr.Resolver.(*netxlite.AddressResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01: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
|
|
|
|
},
|
|
|
|
})
|
2022-01-07 18:33:37 +01:00
|
|
|
ir, ok := r.(*netxlite.ResolverIDNA)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ewr, ok := ir.Resolver.(*netxlite.ErrorWrapperResolver)
|
2021-09-05 18:03:50 +02:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ar, ok := ewr.Resolver.(*netxlite.AddressResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +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,
|
|
|
|
})
|
2022-01-07 18:33:37 +01:00
|
|
|
ir, ok := r.(*netxlite.ResolverIDNA)
|
2021-09-05 18:03:50 +02:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ewr, ok := ir.Resolver.(*netxlite.ErrorWrapperResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +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")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ar, ok := br.Resolver.(*netxlite.AddressResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01: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,
|
|
|
|
})
|
2022-01-07 18:33:37 +01:00
|
|
|
ir, ok := r.(*netxlite.ResolverIDNA)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
lr, ok := ir.Resolver.(*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")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ewr, ok := lr.Resolver.(*netxlite.ErrorWrapperResolver)
|
2021-09-05 18:03:50 +02:00
|
|
|
if !ok {
|
2022-01-07 18:33:37 +01:00
|
|
|
t.Fatalf("not the resolver we expected")
|
2021-09-05 18:03:50 +02:00
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ar, ok := ewr.Resolver.(*netxlite.AddressResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01: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,
|
|
|
|
})
|
2022-01-07 18:33:37 +01:00
|
|
|
ir, ok := r.(*netxlite.ResolverIDNA)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
sr, ok := ir.Resolver.(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")
|
|
|
|
}
|
2022-01-07 17:31:21 +01:00
|
|
|
ewr, ok := sr.Resolver.(*netxlite.ErrorWrapperResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ar, ok := ewr.Resolver.(*netxlite.AddressResolver)
|
2021-09-09 20:21:43 +02:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01: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,
|
|
|
|
})
|
2022-01-07 18:33:37 +01:00
|
|
|
ir, ok := r.(*netxlite.ResolverIDNA)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ewr, ok := ir.Resolver.(*netxlite.ErrorWrapperResolver)
|
2021-09-05 18:03:50 +02:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
cr, ok := ewr.Resolver.(*resolver.CacheResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
if cr.ReadOnly != false {
|
|
|
|
t.Fatal("expected readwrite cache here")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ar, ok := cr.Resolver.(*netxlite.AddressResolver)
|
2021-03-08 12:05:43 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01: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"},
|
|
|
|
},
|
|
|
|
})
|
2022-01-07 18:33:37 +01:00
|
|
|
ir, ok := r.(*netxlite.ResolverIDNA)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ewr, ok := ir.Resolver.(*netxlite.ErrorWrapperResolver)
|
2021-09-05 18:03:50 +02:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
cr, ok := ewr.Resolver.(*resolver.CacheResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
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")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
ar, ok := cr.Resolver.(*netxlite.AddressResolver)
|
2021-03-08 12:05:43 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01: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")
|
|
|
|
}
|
2022-01-07 17:31:21 +01:00
|
|
|
ewth, ok := rtd.TLSHandshaker.(*netxlite.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")
|
|
|
|
}
|
2022-01-07 17:31:21 +01:00
|
|
|
ewth, ok := rtd.TLSHandshaker.(*netxlite.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")
|
|
|
|
}
|
2022-01-03 13:53:23 +01:00
|
|
|
if lth.DebugLogger != log.Log {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the Logger we expected")
|
|
|
|
}
|
2022-01-07 17:31:21 +01:00
|
|
|
ewth, ok := lth.TLSHandshaker.(*netxlite.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")
|
|
|
|
}
|
2022-01-07 17:31:21 +01:00
|
|
|
ewth, ok := sth.TLSHandshaker.(*netxlite.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")
|
|
|
|
}
|
2022-01-07 17:31:21 +01:00
|
|
|
ewth, ok := rtd.TLSHandshaker.(*netxlite.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")
|
|
|
|
}
|
2022-01-07 17:31:21 +01:00
|
|
|
ewth, ok := rtd.TLSHandshaker.(*netxlite.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{})
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
if _, ok := txp.(*httptransport.SystemTransportWrapper); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
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,
|
|
|
|
})
|
fix(netxlite): do not mutate outgoing requests (#508)
I have recently seen a data race related our way of
mutating the outgoing request to set the host header.
Unfortunately, I've lost track of the race output,
because I rebooted my Linux box before saving it.
Though, after inspecting why and and where we're mutating
outgoing requets, I've found that:
1. we add the host header when logging to have it logged,
which is not a big deal since we already emit the URL
rather than just the URL path when logging a request, and
so we can safely zap this piece of code;
2. as a result, in measurements we may omit the host header
but again this is pretty much obvious from the URL itself
and so it should not be very important (nonetheless,
avoid surprises and keep the existing behavior);
3. when the User-Agent header is not set, we default to
a `miniooni/0.1.0-dev` user agent, which is probably not
very useful anyway, so we can actually remove it.
Part of https://github.com/ooni/probe/issues/1733 (this diff
has been extracted from https://github.com/ooni/probe-cli/pull/506).
2021-09-27 13:35:47 +02:00
|
|
|
bctxp, ok := txp.(httptransport.ByteCountingTransport)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if bctxp.Counter != counter {
|
|
|
|
t.Fatal("not the byte counter we expected")
|
|
|
|
}
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
if _, ok := bctxp.HTTPTransport.(*httptransport.SystemTransportWrapper); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewWithLogger(t *testing.T) {
|
|
|
|
txp := netx.NewHTTPTransport(netx.Config{
|
|
|
|
Logger: log.Log,
|
|
|
|
})
|
fix(netxlite): do not mutate outgoing requests (#508)
I have recently seen a data race related our way of
mutating the outgoing request to set the host header.
Unfortunately, I've lost track of the race output,
because I rebooted my Linux box before saving it.
Though, after inspecting why and and where we're mutating
outgoing requets, I've found that:
1. we add the host header when logging to have it logged,
which is not a big deal since we already emit the URL
rather than just the URL path when logging a request, and
so we can safely zap this piece of code;
2. as a result, in measurements we may omit the host header
but again this is pretty much obvious from the URL itself
and so it should not be very important (nonetheless,
avoid surprises and keep the existing behavior);
3. when the User-Agent header is not set, we default to
a `miniooni/0.1.0-dev` user agent, which is probably not
very useful anyway, so we can actually remove it.
Part of https://github.com/ooni/probe/issues/1733 (this diff
has been extracted from https://github.com/ooni/probe-cli/pull/506).
2021-09-27 13:35:47 +02:00
|
|
|
ltxp, ok := txp.(*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")
|
|
|
|
}
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
if _, ok := ltxp.HTTPTransport.(*httptransport.SystemTransportWrapper); !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,
|
|
|
|
})
|
fix(netxlite): do not mutate outgoing requests (#508)
I have recently seen a data race related our way of
mutating the outgoing request to set the host header.
Unfortunately, I've lost track of the race output,
because I rebooted my Linux box before saving it.
Though, after inspecting why and and where we're mutating
outgoing requets, I've found that:
1. we add the host header when logging to have it logged,
which is not a big deal since we already emit the URL
rather than just the URL path when logging a request, and
so we can safely zap this piece of code;
2. as a result, in measurements we may omit the host header
but again this is pretty much obvious from the URL itself
and so it should not be very important (nonetheless,
avoid surprises and keep the existing behavior);
3. when the User-Agent header is not set, we default to
a `miniooni/0.1.0-dev` user agent, which is probably not
very useful anyway, so we can actually remove it.
Part of https://github.com/ooni/probe/issues/1733 (this diff
has been extracted from https://github.com/ooni/probe-cli/pull/506).
2021-09-27 13:35:47 +02:00
|
|
|
stxptxp, ok := txp.(httptransport.SaverTransactionHTTPTransport)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if stxptxp.Saver != saver {
|
|
|
|
t.Fatal("not the logger we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
sptxp, ok := stxptxp.HTTPTransport.(httptransport.SaverPerformanceHTTPTransport)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if sptxp.Saver != saver {
|
|
|
|
t.Fatal("not the logger we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
sbtxp, ok := sptxp.HTTPTransport.(httptransport.SaverBodyHTTPTransport)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if sbtxp.Saver != saver {
|
|
|
|
t.Fatal("not the logger we expected")
|
|
|
|
}
|
2022-01-07 18:33:37 +01:00
|
|
|
smtxp, ok := sbtxp.HTTPTransport.(httptransport.SaverMetadataHTTPTransport)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
|
|
|
if smtxp.Saver != saver {
|
|
|
|
t.Fatal("not the logger we expected")
|
|
|
|
}
|
feature: merge measurex and netx archival layer (1/N) (#663)
This diff introduces a new package called `./internal/archival`. This package collects data from `./internal/model` network interfaces (e.g., `Dialer`, `QUICDialer`, `HTTPTransport`), saves such data into an internal tabular data format suitable for on-line processing and analysis, and allows exporting data into the OONI data format.
The code for collecting and the internal tabular data formats are adapted from `measurex`. The code for formatting and exporting OONI data-format-compliant structures is adapted from `netx/archival`.
My original objective was to _also_ (1) fully replace `netx/archival` with this package and (2) adapt `measurex` to use this package rather than its own code. Both operations seem easily feasible because: (a) this code is `measurex` code without extensions that are `measurex` related, which will need to be added back as part of the process; (b) the API provided by this code allows for trivially converting from using `netx/archival` to using this code.
Yet, both changes should not be taken lightly. After implementing them, there's need to spend some time doing QA and ensuring all nettests work as intended. However, I am planning a release in the next two weeks, and this QA task is likely going to defer the release. For this reason, I have chosen to commit the work done so far into the tree and defer the second part of this refactoring for a later moment in time. (This explains why the title mentions "1/N").
On a more high-level perspective, it would also be beneficial, I guess, to explain _why_ I am doing these changes. There are two intertwined reasons. The first reason is that `netx/archival` has shortcomings deriving from its original https://github.com/ooni/netx legacy. The most relevant shortcoming is that it saves all kind of data into the same tabular structure named `Event`. This design choice is unfortunate because it does not allow one to apply data-type specific logic when processing the results. In turn, this choice results in complex processing code. Therefore, I believe that replacing the code with event-specific data structures is clearly an improvement in terms of code maintainability and would quite likely lead us to more confidently change and evolve the codebase.
The second reason why I would like to move forward these changes is to unify the codepaths used for measuring. At this point in time, we basically have two codepaths: `./internal/engine/netx` and `./internal/measurex`. They both have pros and cons and I don't think we want to rewrite whole experiments using `netx`. Rather, what we probably want is to gradually merge these two codepaths such that `netx` is a set of abstractions on top of `measurex` (which is more low-level and has a more-easily-testable design). Because saving events and generating an archival data format out of them consists of at least 50% of the complexity of both `netx` and `measurex`, it seems reasonable to unify this archival-related part of the two codebases as the first step.
At the highest level of abstraction, these changes are part of the train of changes which will eventually lead us to bless `websteps` as a first class citizen in OONI land. Because `websteps` requires different underlying primitives, I chose to develop these primitives from scratch rather than wrestling with `netx`, which used another model. The model used by `websteps` is that we perform each operation in isolation and immediately we save the results, while `netx` creates whole data structures and collects all the events happening via tracing. We believe the model used by `websteps` to be better because it does not require your code to figure out everything that happened after the measurement, which is a source of subtle bugs in the current implementation. So, when I started implementing websteps I extracted the bits of `netx` that could also be beneficial to `websteps` into a separate library, thus `netxlite` was born.
The reference issue describing merging the archival of `netx` and `measurex` is https://github.com/ooni/probe/issues/1957. As of this writing the issue still references the original plan, which I could not complete by the end of this Sprint, so I am going to adapt the text of the issue to only refer to what was done in here next. Of course, I also need follow-up issues.
2022-01-14 12:13:10 +01:00
|
|
|
if _, ok := smtxp.HTTPTransport.(*httptransport.SystemTransportWrapper); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
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")
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
if dnsclient != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("expected nil resolver here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
if dnsclient != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("expected nil resolver here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSClientSystemResolver(t *testing.T) {
|
|
|
|
dnsclient, err := netx.NewDNSClient(
|
|
|
|
netx.Config{}, "system:///")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
if _, ok := dnsclient.(*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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
if _, ok := dnsclient.(*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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
r, ok := dnsclient.(*netxlite.SerialResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
if _, ok := r.Transport().(*netxlite.DNSOverHTTPS); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
r, ok := dnsclient.(*netxlite.SerialResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
if _, ok := r.Transport().(*netxlite.DNSOverHTTPS); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
r, ok := dnsclient.(*netxlite.SerialResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
if _, ok := r.Transport().(*netxlite.DNSOverHTTPS); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
r, ok := dnsclient.(*netxlite.SerialResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
txp, ok := r.Transport().(resolver.SaverDNSTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
if _, ok := txp.DNSTransport.(*netxlite.DNSOverHTTPS); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
r, ok := dnsclient.(*netxlite.SerialResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
if _, ok := r.Transport().(*netxlite.DNSOverUDP); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
r, ok := dnsclient.(*netxlite.SerialResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
txp, ok := r.Transport().(resolver.SaverDNSTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
if _, ok := txp.DNSTransport.(*netxlite.DNSOverUDP); !ok {
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
r, ok := dnsclient.(*netxlite.SerialResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
txp, ok := r.Transport().(*netxlite.DNSOverTCP)
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
r, ok := dnsclient.(*netxlite.SerialResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
txp, ok := r.Transport().(resolver.SaverDNSTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
dotcp, ok := txp.DNSTransport.(*netxlite.DNSOverTCP)
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
r, ok := dnsclient.(*netxlite.SerialResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
txp, ok := r.Transport().(*netxlite.DNSOverTCP)
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
r, ok := dnsclient.(*netxlite.SerialResolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the resolver we expected")
|
|
|
|
}
|
|
|
|
txp, ok := r.Transport().(resolver.SaverDNSTransport)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("not the transport we expected")
|
|
|
|
}
|
2022-01-07 20:02:19 +01:00
|
|
|
dotls, ok := txp.DNSTransport.(*netxlite.DNSOverTCP)
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
if c.Address() != "8.8.8.8:853" {
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
if c.Address() != "8.8.8.8:53" {
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-01-10 11:53:06 +01:00
|
|
|
if c.Address() != "8.8.8.8:53" {
|
2021-02-02 12:05:47 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|