refactor: interfaces and data types into the model package (#642)

## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885
- [x] related ooni/spec pull request: N/A

Location of the issue tracker: https://github.com/ooni/probe

## Description

This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package.

The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity.

An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other.

This is what I want to move in `internal/model`:

- [x] most important interfaces from `internal/netxlite`
- [x] everything that was previously part of `internal/engine/model`
- [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
This commit is contained in:
Simone Basso
2022-01-03 13:53:23 +01:00
committed by GitHub
parent 69aca619ea
commit 273b70bacc
275 changed files with 1281 additions and 1375 deletions
+1 -1
View File
@@ -18,8 +18,8 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
errorsxlegacy "github.com/ooni/probe-cli/v3/internal/engine/legacy/errorsx"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
@@ -12,9 +12,9 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/gorilla/websocket"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
@@ -10,7 +10,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
)
func dorequest(ctx context.Context, url string) error {
+4 -12
View File
@@ -7,6 +7,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/legacy/errorsx"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
@@ -22,15 +23,6 @@ type Resolver interface {
LookupHost(ctx context.Context, hostname string) (addrs []string, err error)
}
// Logger is the interface we expect from a logger.
type Logger interface {
// Debugf formats and emits a debug message.
Debugf(format string, v ...interface{})
// Debug emits a debug message.
Debug(msg string)
}
// Config contains the settings for New.
type Config struct {
// ContextByteCounting optionally configures context-based
@@ -58,7 +50,7 @@ type Config struct {
// Logger is the optional logger. If not set, there
// will be no logging from the new dialer.
Logger Logger
Logger model.DebugLogger
// ProxyURL is the optional proxy URL.
ProxyURL *url.URL
@@ -73,8 +65,8 @@ func New(config *Config, resolver Resolver) Dialer {
d = &errorsx.ErrorWrapperDialer{Dialer: d}
if config.Logger != nil {
d = &netxlite.DialerLogger{
Dialer: netxlite.NewDialerLegacyAdapter(d),
Logger: config.Logger,
Dialer: netxlite.NewDialerLegacyAdapter(d),
DebugLogger: config.Logger,
}
}
if config.DialSaver != nil {
+1 -1
View File
@@ -8,7 +8,7 @@ import (
"net/url"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
)
func TestProxyDialerDialContextNoProxyURL(t *testing.T) {
+1 -1
View File
@@ -10,7 +10,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
)
func TestSaverDialerFailure(t *testing.T) {
+3 -8
View File
@@ -39,15 +39,10 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// Logger is the logger assumed by this package
type Logger interface {
Debugf(format string, v ...interface{})
Debug(message string)
}
// Dialer is the definition of dialer assumed by this package.
type Dialer interface {
DialContext(ctx context.Context, network, address string) (net.Conn, error)
@@ -95,7 +90,7 @@ type Config struct {
QUICDialer QUICDialer // default: quicdialer.DNSDialer
HTTP3Enabled bool // default: disabled
HTTPSaver *trace.Saver // default: not saving HTTP
Logger Logger // default: no logging
Logger model.DebugLogger // default: no logging
NoTLSVerify bool // default: perform TLS verify
ProxyURL *url.URL // default: no proxy
ReadWriteSaver *trace.Saver // default: not saving read/write
@@ -196,7 +191,7 @@ func NewTLSDialer(config Config) TLSDialer {
var h tlsHandshaker = &netxlite.TLSHandshakerConfigurable{}
h = &errorsx.ErrorWrapperTLSHandshaker{TLSHandshaker: h}
if config.Logger != nil {
h = &netxlite.TLSHandshakerLogger{Logger: config.Logger, TLSHandshaker: h}
h = &netxlite.TLSHandshakerLogger{DebugLogger: config.Logger, TLSHandshaker: h}
}
if config.TLSSaver != nil {
h = tlsdialer.SaverTLSHandshaker{TLSHandshaker: h, Saver: config.TLSSaver}
+2 -2
View File
@@ -18,7 +18,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
)
func TestNewResolverVanilla(t *testing.T) {
@@ -367,7 +367,7 @@ func TestNewTLSDialerWithLogging(t *testing.T) {
if !ok {
t.Fatal("not the TLSHandshaker we expected")
}
if lth.Logger != log.Log {
if lth.DebugLogger != log.Log {
t.Fatal("not the Logger we expected")
}
ewth, ok := lth.TLSHandshaker.(*errorsx.ErrorWrapperTLSHandshaker)
+5 -5
View File
@@ -5,14 +5,14 @@ import (
"time"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/quicx"
)
// QUICListener listens for QUIC connections.
type QUICListener interface {
// Listen creates a new listening UDPConn.
Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error)
Listen(addr *net.UDPAddr) (model.UDPLikeConn, error)
}
// QUICListenerSaver is a QUICListener that also implements saving events.
@@ -25,7 +25,7 @@ type QUICListenerSaver struct {
}
// Listen implements QUICListener.Listen.
func (qls *QUICListenerSaver) Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
func (qls *QUICListenerSaver) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) {
pconn, err := qls.QUICListener.Listen(addr)
if err != nil {
return nil, err
@@ -37,11 +37,11 @@ func (qls *QUICListenerSaver) Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, erro
}
type saverUDPConn struct {
quicx.UDPLikeConn
model.UDPLikeConn
saver *trace.Saver
}
var _ quicx.UDPLikeConn = &saverUDPConn{}
var _ model.UDPLikeConn = &saverUDPConn{}
func (c *saverUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
start := time.Now()
@@ -10,17 +10,17 @@ import (
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/engine/netx/quicdialer"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
"github.com/ooni/probe-cli/v3/internal/netxlite/quictesting"
"github.com/ooni/probe-cli/v3/internal/netxlite/quicx"
)
func TestQUICListenerSaverCannotListen(t *testing.T) {
expected := errors.New("mocked error")
qls := &quicdialer.QUICListenerSaver{
QUICListener: &mocks.QUICListener{
MockListen: func(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
MockListen: func(addr *net.UDPAddr) (model.UDPLikeConn, error) {
return nil, expected
},
},
@@ -13,7 +13,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/handlers"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
)
func TestEmitterTransportSuccess(t *testing.T) {
+8 -5
View File
@@ -1,9 +1,12 @@
package resolver
import "github.com/ooni/probe-cli/v3/internal/netxlite"
import (
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// Variables that other packages expect to find here but have been
// moved into the internal/netxlite/dnsx package.
// moved into the internal/netxlite package.
var (
NewSerialResolver = netxlite.NewSerialResolver
NewDNSOverUDP = netxlite.NewDNSOverUDP
@@ -14,15 +17,15 @@ var (
)
// Types that other packages expect to find here but have been
// moved into the internal/netxlite/dnsx package.
// moved into the internal/netxlite package.
type (
DNSOverHTTPS = netxlite.DNSOverHTTPS
DNSOverTCP = netxlite.DNSOverTCP
DNSOverUDP = netxlite.DNSOverUDP
MiekgEncoder = netxlite.DNSEncoderMiekg
MiekgDecoder = netxlite.DNSDecoderMiekg
RoundTripper = netxlite.DNSTransport
RoundTripper = model.DNSTransport
SerialResolver = netxlite.SerialResolver
Dialer = netxlite.Dialer
Dialer = model.Dialer
DialContextFunc = netxlite.DialContextFunc
)
@@ -16,7 +16,7 @@ func TestTLSDialerSuccess(t *testing.T) {
dialer := &netxlite.TLSDialerLegacy{Dialer: netxlite.DefaultDialer,
TLSHandshaker: &netxlite.TLSHandshakerLogger{
TLSHandshaker: &netxlite.TLSHandshakerConfigurable{},
Logger: log.Log,
DebugLogger: log.Log,
},
}
txp := &http.Transport{
@@ -1,7 +0,0 @@
package tlsdialer
// Logger is the logger assumed by this package
type Logger interface {
Debugf(format string, v ...interface{})
Debug(message string)
}