-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce model.MeasuringNetwork (#1258)
This diff introduces the model.MeasuringNetwork interface that defines the factories required for implementing experiments. By defining this interface, we can make measuring code depend on it rather than on the netxlite package. While there, create specific files for `mocks.UDPListener`. Reference issue: ooni/probe#2531
- Loading branch information
1 parent
3fcd5b0
commit 8bf9d88
Showing
13 changed files
with
314 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package mocks | ||
|
||
import ( | ||
"github.com/ooni/probe-cli/v3/internal/model" | ||
utls "gitlab.com/yawning/utls.git" | ||
) | ||
|
||
// MeasuringNetwork allows mocking [model.MeasuringNetwork]. | ||
type MeasuringNetwork struct { | ||
MockNewDialerWithResolver func(dl model.DebugLogger, r model.Resolver, w ...model.DialerWrapper) model.Dialer | ||
|
||
MockNewParallelDNSOverHTTPSResolver func(logger model.DebugLogger, URL string) model.Resolver | ||
|
||
MockNewParallelUDPResolver func(logger model.DebugLogger, dialer model.Dialer, address string) model.Resolver | ||
|
||
MockNewQUICDialerWithResolver func(listener model.UDPListener, logger model.DebugLogger, resolver model.Resolver, w ...model.QUICDialerWrapper) model.QUICDialer | ||
|
||
MockNewStdlibResolver func(logger model.DebugLogger) model.Resolver | ||
|
||
MockNewTLSHandshakerStdlib func(logger model.DebugLogger) model.TLSHandshaker | ||
|
||
MockNewTLSHandshakerUTLS func(logger model.DebugLogger, id *utls.ClientHelloID) model.TLSHandshaker | ||
|
||
MockNewUDPListener func() model.UDPListener | ||
} | ||
|
||
var _ model.MeasuringNetwork = &MeasuringNetwork{} | ||
|
||
// NewDialerWithResolver implements model.MeasuringNetwork. | ||
func (mn *MeasuringNetwork) NewDialerWithResolver(dl model.DebugLogger, r model.Resolver, w ...model.DialerWrapper) model.Dialer { | ||
return mn.MockNewDialerWithResolver(dl, r, w...) | ||
} | ||
|
||
// NewParallelDNSOverHTTPSResolver implements model.MeasuringNetwork. | ||
func (mn *MeasuringNetwork) NewParallelDNSOverHTTPSResolver(logger model.DebugLogger, URL string) model.Resolver { | ||
return mn.MockNewParallelDNSOverHTTPSResolver(logger, URL) | ||
} | ||
|
||
// NewParallelUDPResolver implements model.MeasuringNetwork. | ||
func (mn *MeasuringNetwork) NewParallelUDPResolver(logger model.DebugLogger, dialer model.Dialer, address string) model.Resolver { | ||
return mn.MockNewParallelUDPResolver(logger, dialer, address) | ||
} | ||
|
||
// NewQUICDialerWithResolver implements model.MeasuringNetwork. | ||
func (mn *MeasuringNetwork) NewQUICDialerWithResolver(listener model.UDPListener, logger model.DebugLogger, resolver model.Resolver, w ...model.QUICDialerWrapper) model.QUICDialer { | ||
return mn.MockNewQUICDialerWithResolver(listener, logger, resolver, w...) | ||
} | ||
|
||
// NewStdlibResolver implements model.MeasuringNetwork. | ||
func (mn *MeasuringNetwork) NewStdlibResolver(logger model.DebugLogger) model.Resolver { | ||
return mn.MockNewStdlibResolver(logger) | ||
} | ||
|
||
// NewTLSHandshakerStdlib implements model.MeasuringNetwork. | ||
func (mn *MeasuringNetwork) NewTLSHandshakerStdlib(logger model.DebugLogger) model.TLSHandshaker { | ||
return mn.MockNewTLSHandshakerStdlib(logger) | ||
} | ||
|
||
// NewTLSHandshakerUTLS implements model.MeasuringNetwork. | ||
func (mn *MeasuringNetwork) NewTLSHandshakerUTLS(logger model.DebugLogger, id *utls.ClientHelloID) model.TLSHandshaker { | ||
return mn.MockNewTLSHandshakerUTLS(logger, id) | ||
} | ||
|
||
// NewUDPListener implements model.MeasuringNetwork. | ||
func (mn *MeasuringNetwork) NewUDPListener() model.UDPListener { | ||
return mn.MockNewUDPListener() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package mocks | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ooni/probe-cli/v3/internal/model" | ||
utls "gitlab.com/yawning/utls.git" | ||
) | ||
|
||
func TestMeasuringN(t *testing.T) { | ||
t.Run("MockNewDialerWithResolver", func(t *testing.T) { | ||
expected := &Dialer{} | ||
mn := &MeasuringNetwork{ | ||
MockNewDialerWithResolver: func(dl model.DebugLogger, r model.Resolver, w ...model.DialerWrapper) model.Dialer { | ||
return expected | ||
}, | ||
} | ||
got := mn.NewDialerWithResolver(nil, nil) | ||
if expected != got { | ||
t.Fatal("unexpected result") | ||
} | ||
}) | ||
|
||
t.Run("MockNewParallelDNSOverHTTPSResolver", func(t *testing.T) { | ||
expected := &Resolver{} | ||
mn := &MeasuringNetwork{ | ||
MockNewParallelDNSOverHTTPSResolver: func(logger model.DebugLogger, URL string) model.Resolver { | ||
return expected | ||
}, | ||
} | ||
got := mn.NewParallelDNSOverHTTPSResolver(nil, "") | ||
if expected != got { | ||
t.Fatal("unexpected result") | ||
} | ||
}) | ||
|
||
t.Run("MockNewParallelUDPResolver", func(t *testing.T) { | ||
expected := &Resolver{} | ||
mn := &MeasuringNetwork{ | ||
MockNewParallelUDPResolver: func(logger model.DebugLogger, dialer model.Dialer, address string) model.Resolver { | ||
return expected | ||
}, | ||
} | ||
got := mn.NewParallelUDPResolver(nil, nil, "") | ||
if expected != got { | ||
t.Fatal("unexpected result") | ||
} | ||
}) | ||
|
||
t.Run("MockNewQUICDialerWithResolver", func(t *testing.T) { | ||
expected := &QUICDialer{} | ||
mn := &MeasuringNetwork{ | ||
MockNewQUICDialerWithResolver: func(listener model.UDPListener, logger model.DebugLogger, resolver model.Resolver, w ...model.QUICDialerWrapper) model.QUICDialer { | ||
return expected | ||
}, | ||
} | ||
got := mn.NewQUICDialerWithResolver(nil, nil, nil) | ||
if expected != got { | ||
t.Fatal("unexpected result") | ||
} | ||
}) | ||
|
||
t.Run("MockNewStdlibResolver", func(t *testing.T) { | ||
expected := &Resolver{} | ||
mn := &MeasuringNetwork{ | ||
MockNewStdlibResolver: func(logger model.DebugLogger) model.Resolver { | ||
return expected | ||
}, | ||
} | ||
got := mn.NewStdlibResolver(nil) | ||
if expected != got { | ||
t.Fatal("unexpected result") | ||
} | ||
}) | ||
|
||
t.Run("MockNewTLSHandshakerStdlib", func(t *testing.T) { | ||
expected := &TLSHandshaker{} | ||
mn := &MeasuringNetwork{ | ||
MockNewTLSHandshakerStdlib: func(logger model.DebugLogger) model.TLSHandshaker { | ||
return expected | ||
}, | ||
} | ||
got := mn.NewTLSHandshakerStdlib(nil) | ||
if expected != got { | ||
t.Fatal("unexpected result") | ||
} | ||
}) | ||
|
||
t.Run("MockNewTLSHandshakerUTLS", func(t *testing.T) { | ||
expected := &TLSHandshaker{} | ||
mn := &MeasuringNetwork{ | ||
MockNewTLSHandshakerUTLS: func(logger model.DebugLogger, id *utls.ClientHelloID) model.TLSHandshaker { | ||
return expected | ||
}, | ||
} | ||
got := mn.NewTLSHandshakerUTLS(nil, nil) | ||
if expected != got { | ||
t.Fatal("unexpected result") | ||
} | ||
}) | ||
|
||
t.Run("MockNewUDPListener", func(t *testing.T) { | ||
expected := &UDPListener{} | ||
mn := &MeasuringNetwork{ | ||
MockNewUDPListener: func() model.UDPListener { | ||
return expected | ||
}, | ||
} | ||
got := mn.NewUDPListener() | ||
if expected != got { | ||
t.Fatal("unexpected result") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package mocks | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/ooni/probe-cli/v3/internal/model" | ||
) | ||
|
||
// UDPListener is a mockable netxlite.UDPListener. | ||
type UDPListener struct { | ||
MockListen func(addr *net.UDPAddr) (model.UDPLikeConn, error) | ||
} | ||
|
||
// Listen calls MockListen. | ||
func (ql *UDPListener) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) { | ||
return ql.MockListen(addr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package mocks | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"testing" | ||
|
||
"github.com/ooni/probe-cli/v3/internal/model" | ||
) | ||
|
||
func TestUDPListener(t *testing.T) { | ||
t.Run("Listen", func(t *testing.T) { | ||
expected := errors.New("mocked error") | ||
ql := &UDPListener{ | ||
MockListen: func(addr *net.UDPAddr) (model.UDPLikeConn, error) { | ||
return nil, expected | ||
}, | ||
} | ||
pconn, err := ql.Listen(&net.UDPAddr{}) | ||
if !errors.Is(err, expected) { | ||
t.Fatal("not the error we expected", expected) | ||
} | ||
if pconn != nil { | ||
t.Fatal("expected nil conn here") | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.