refactor(netx): move construction logic outside package (#798)
For testability, replace most if-based construction logic with calls to well-tested factories living in other packages. While there, acknowledge that a bunch of types could now be private and make them private, modifying the code to call the public factories allowing to construct said types instead. Part of https://github.com/ooni/probe/issues/2121
This commit is contained in:
@@ -14,6 +14,16 @@ import (
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
// MaybeWrapWithBogonResolver wraps the given resolver with a BogonResolver
|
||||
// iff the provided boolean flag is true. Otherwise, this factory just returns
|
||||
// the provided resolver to the caller without any wrapping.
|
||||
func MaybeWrapWithBogonResolver(enabled bool, reso model.Resolver) model.Resolver {
|
||||
if enabled {
|
||||
reso = &BogonResolver{Resolver: reso}
|
||||
}
|
||||
return reso
|
||||
}
|
||||
|
||||
// BogonResolver is a bogon aware resolver. When a bogon is encountered in
|
||||
// a reply, this resolver will return ErrDNSBogon.
|
||||
//
|
||||
|
||||
@@ -9,6 +9,25 @@ import (
|
||||
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
||||
)
|
||||
|
||||
func TestMaybeWrapWithBogonResolver(t *testing.T) {
|
||||
t.Run("with enabled equal to true", func(t *testing.T) {
|
||||
underlying := &mocks.Resolver{}
|
||||
reso := MaybeWrapWithBogonResolver(true, underlying)
|
||||
bogoreso := reso.(*BogonResolver)
|
||||
if bogoreso.Resolver != underlying {
|
||||
t.Fatal("did not wrap")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with enabled equal to false", func(t *testing.T) {
|
||||
underlying := &mocks.Resolver{}
|
||||
reso := MaybeWrapWithBogonResolver(false, underlying)
|
||||
if reso != underlying {
|
||||
t.Fatal("expected unmodified resolver")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestBogonResolver(t *testing.T) {
|
||||
t.Run("LookupHost", func(t *testing.T) {
|
||||
t.Run("with failure", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user