refactor(netx/dialer): hide implementation complexity (#372)

* refactor(netx/dialer): hide implementation complexity

This follows the blueprint of `module.Config` and `nodule.New`
described at https://github.com/ooni/probe/issues/1591.

* fix: ndt7 bug where we were not using the right resolver

* fix(legacy/netx): clarify irrelevant implementation change

* fix: improve comments

* fix(hhfm): do not use dialer.New b/c it breaks it

Unclear to me why this is happening. Still, improve upon the
previous situation by adding a timeout.

It does not seem a priority to look into this issue now.
This commit is contained in:
Simone Basso 2021-06-09 09:42:31 +02:00 committed by GitHub
commit 06ee0e55a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 312 additions and 517 deletions

View file

@ -10,7 +10,6 @@ import (
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/netx"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
@ -210,257 +209,6 @@ func TestNewResolverWithPrefilledReadonlyCache(t *testing.T) {
}
}
func TestNewDialerVanilla(t *testing.T) {
d := netx.NewDialer(netx.Config{})
sd, ok := d.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
pd, ok := sd.Dialer.(dialer.ProxyDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if pd.ProxyURL != nil {
t.Fatal("not the proxy URL we expected")
}
dnsd, ok := pd.Dialer.(dialer.DNSDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if dnsd.Resolver == nil {
t.Fatal("not the resolver we expected")
}
ir, ok := dnsd.Resolver.(resolver.IDNAResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
if _, ok := ir.Resolver.(resolver.ErrorWrapperResolver); !ok {
t.Fatal("not the resolver we expected")
}
ewd, ok := dnsd.Dialer.(dialer.ErrorWrapperDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := ewd.Dialer.(dialer.SystemDialer); !ok {
t.Fatal("not the dialer we expected")
}
}
func TestNewDialerWithResolver(t *testing.T) {
d := netx.NewDialer(netx.Config{
FullResolver: resolver.BogonResolver{
// not initialized because it doesn't matter in this context
},
})
sd, ok := d.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
pd, ok := sd.Dialer.(dialer.ProxyDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if pd.ProxyURL != nil {
t.Fatal("not the proxy URL we expected")
}
dnsd, ok := pd.Dialer.(dialer.DNSDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if dnsd.Resolver == nil {
t.Fatal("not the resolver we expected")
}
if _, ok := dnsd.Resolver.(resolver.BogonResolver); !ok {
t.Fatal("not the resolver we expected")
}
ewd, ok := dnsd.Dialer.(dialer.ErrorWrapperDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := ewd.Dialer.(dialer.SystemDialer); !ok {
t.Fatal("not the dialer we expected")
}
}
func TestNewDialerWithLogger(t *testing.T) {
d := netx.NewDialer(netx.Config{
Logger: log.Log,
})
sd, ok := d.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
pd, ok := sd.Dialer.(dialer.ProxyDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if pd.ProxyURL != nil {
t.Fatal("not the proxy URL we expected")
}
dnsd, ok := pd.Dialer.(dialer.DNSDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if dnsd.Resolver == nil {
t.Fatal("not the resolver we expected")
}
ir, ok := dnsd.Resolver.(resolver.IDNAResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
if _, ok := ir.Resolver.(resolver.LoggingResolver); !ok {
t.Fatal("not the resolver we expected")
}
ld, ok := dnsd.Dialer.(dialer.LoggingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if ld.Logger != log.Log {
t.Fatal("not the logger we expected")
}
ewd, ok := ld.Dialer.(dialer.ErrorWrapperDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := ewd.Dialer.(dialer.SystemDialer); !ok {
t.Fatal("not the dialer we expected")
}
}
func TestNewDialerWithDialSaver(t *testing.T) {
saver := new(trace.Saver)
d := netx.NewDialer(netx.Config{
DialSaver: saver,
})
sd, ok := d.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
pd, ok := sd.Dialer.(dialer.ProxyDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if pd.ProxyURL != nil {
t.Fatal("not the proxy URL we expected")
}
dnsd, ok := pd.Dialer.(dialer.DNSDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if dnsd.Resolver == nil {
t.Fatal("not the resolver we expected")
}
ir, ok := dnsd.Resolver.(resolver.IDNAResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
if _, ok := ir.Resolver.(resolver.ErrorWrapperResolver); !ok {
t.Fatal("not the resolver we expected")
}
sad, ok := dnsd.Dialer.(dialer.SaverDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if sad.Saver != saver {
t.Fatal("not the logger we expected")
}
ewd, ok := sad.Dialer.(dialer.ErrorWrapperDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := ewd.Dialer.(dialer.SystemDialer); !ok {
t.Fatal("not the dialer we expected")
}
}
func TestNewDialerWithReadWriteSaver(t *testing.T) {
saver := new(trace.Saver)
d := netx.NewDialer(netx.Config{
ReadWriteSaver: saver,
})
sd, ok := d.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
pd, ok := sd.Dialer.(dialer.ProxyDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if pd.ProxyURL != nil {
t.Fatal("not the proxy URL we expected")
}
dnsd, ok := pd.Dialer.(dialer.DNSDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if dnsd.Resolver == nil {
t.Fatal("not the resolver we expected")
}
ir, ok := dnsd.Resolver.(resolver.IDNAResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
if _, ok := ir.Resolver.(resolver.ErrorWrapperResolver); !ok {
t.Fatal("not the resolver we expected")
}
scd, ok := dnsd.Dialer.(dialer.SaverConnDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if scd.Saver != saver {
t.Fatal("not the logger we expected")
}
ewd, ok := scd.Dialer.(dialer.ErrorWrapperDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := ewd.Dialer.(dialer.SystemDialer); !ok {
t.Fatal("not the dialer we expected")
}
}
func TestNewDialerWithContextByteCounting(t *testing.T) {
d := netx.NewDialer(netx.Config{
ContextByteCounting: true,
})
sd, ok := d.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
bcd, ok := sd.Dialer.(dialer.ByteCounterDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
pd, ok := bcd.Dialer.(dialer.ProxyDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if pd.ProxyURL != nil {
t.Fatal("not the proxy URL we expected")
}
dnsd, ok := pd.Dialer.(dialer.DNSDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if dnsd.Resolver == nil {
t.Fatal("not the resolver we expected")
}
ir, ok := dnsd.Resolver.(resolver.IDNAResolver)
if !ok {
t.Fatal("not the resolver we expected")
}
if _, ok := ir.Resolver.(resolver.ErrorWrapperResolver); !ok {
t.Fatal("not the resolver we expected")
}
ewd, ok := dnsd.Dialer.(dialer.ErrorWrapperDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := ewd.Dialer.(dialer.SystemDialer); !ok {
t.Fatal("not the dialer we expected")
}
}
func TestNewTLSDialerVanilla(t *testing.T) {
td := netx.NewTLSDialer(netx.Config{})
rtd, ok := td.(tlsdialer.TLSDialer)
@ -479,13 +227,6 @@ func TestNewTLSDialerVanilla(t *testing.T) {
if rtd.Dialer == nil {
t.Fatal("invalid Dialer")
}
sd, ok := rtd.Dialer.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := sd.Dialer.(dialer.ProxyDialer); !ok {
t.Fatal("not the Dialer we expected")
}
if rtd.TLSHandshaker == nil {
t.Fatal("invalid TLSHandshaker")
}
@ -519,13 +260,6 @@ func TestNewTLSDialerWithConfig(t *testing.T) {
if rtd.Dialer == nil {
t.Fatal("invalid Dialer")
}
sd, ok := rtd.Dialer.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := sd.Dialer.(dialer.ProxyDialer); !ok {
t.Fatal("not the Dialer we expected")
}
if rtd.TLSHandshaker == nil {
t.Fatal("invalid TLSHandshaker")
}
@ -562,13 +296,6 @@ func TestNewTLSDialerWithLogging(t *testing.T) {
if rtd.Dialer == nil {
t.Fatal("invalid Dialer")
}
sd, ok := rtd.Dialer.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := sd.Dialer.(dialer.ProxyDialer); !ok {
t.Fatal("not the Dialer we expected")
}
if rtd.TLSHandshaker == nil {
t.Fatal("invalid TLSHandshaker")
}
@ -613,13 +340,6 @@ func TestNewTLSDialerWithSaver(t *testing.T) {
if rtd.Dialer == nil {
t.Fatal("invalid Dialer")
}
sd, ok := rtd.Dialer.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := sd.Dialer.(dialer.ProxyDialer); !ok {
t.Fatal("not the Dialer we expected")
}
if rtd.TLSHandshaker == nil {
t.Fatal("invalid TLSHandshaker")
}
@ -664,13 +384,6 @@ func TestNewTLSDialerWithNoTLSVerifyAndConfig(t *testing.T) {
if rtd.Dialer == nil {
t.Fatal("invalid Dialer")
}
sd, ok := rtd.Dialer.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := sd.Dialer.(dialer.ProxyDialer); !ok {
t.Fatal("not the Dialer we expected")
}
if rtd.TLSHandshaker == nil {
t.Fatal("invalid TLSHandshaker")
}
@ -710,13 +423,6 @@ func TestNewTLSDialerWithNoTLSVerifyAndNoConfig(t *testing.T) {
if rtd.Dialer == nil {
t.Fatal("invalid Dialer")
}
sd, ok := rtd.Dialer.(dialer.ShapingDialer)
if !ok {
t.Fatal("not the dialer we expected")
}
if _, ok := sd.Dialer.(dialer.ProxyDialer); !ok {
t.Fatal("not the Dialer we expected")
}
if rtd.TLSHandshaker == nil {
t.Fatal("invalid TLSHandshaker")
}