refactor: move tunnel pkg down one level (#358)
* refactor: move tunnel pkg down one level While there, reduce unnecessary dependency on external packages. * file I forgot to commit
This commit is contained in:
parent
39aec6677d
commit
3cb6c7c6fb
|
@ -17,12 +17,12 @@ import (
|
|||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
|
||||
"github.com/ooni/probe-cli/v3/internal/randx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/selfcensor"
|
||||
"github.com/ooni/probe-cli/v3/internal/randx"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
|
@ -11,11 +11,11 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/randx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/randx"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/tunnel"
|
||||
"github.com/ooni/probe-cli/v3/internal/tunnel"
|
||||
)
|
||||
|
||||
// The Getter gets the specified target in the context of the
|
||||
|
|
|
@ -6,8 +6,8 @@ import (
|
|||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
|
||||
"github.com/ooni/probe-cli/v3/internal/randx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
|
||||
"github.com/ooni/probe-cli/v3/internal/randx"
|
||||
)
|
||||
|
||||
func TestHTTPBodyLengthChecks(t *testing.T) {
|
||||
|
|
|
@ -9,8 +9,8 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/multierror"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/multierror"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
|
@ -17,9 +17,9 @@ import (
|
|||
"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/probeservices"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/tunnel"
|
||||
"github.com/ooni/probe-cli/v3/internal/kvstore"
|
||||
"github.com/ooni/probe-cli/v3/internal/platform"
|
||||
"github.com/ooni/probe-cli/v3/internal/tunnel"
|
||||
"github.com/ooni/probe-cli/v3/internal/version"
|
||||
)
|
||||
|
||||
|
|
|
@ -23,21 +23,26 @@ type Logger interface {
|
|||
// to fill all the mandatory fields. You SHOULD NOT modify the content of this
|
||||
// structure while in use, because that may lead to data races.
|
||||
type Config struct {
|
||||
// Logger is the logger to use. If empty we use a default
|
||||
// implementation that does not emit any output.
|
||||
Logger Logger
|
||||
|
||||
// Name is the mandatory name of the tunnel. We support
|
||||
// Name is the MANDATORY name of the tunnel. We support
|
||||
// "tor", "psiphon", and "fake" tunnels. You SHOULD
|
||||
// use "fake" tunnels only for testing: they don't provide
|
||||
// any real tunneling, just a socks5 proxy.
|
||||
Name string
|
||||
|
||||
// Session is the mandatory measurement session, or a suitable
|
||||
// Session is the MANDATORY measurement session, or a suitable
|
||||
// mock of the required functionality. That is, the possibility
|
||||
// of obtaining a valid psiphon configuration.
|
||||
Session Session
|
||||
|
||||
// TunnelDir is the MANDATORY directory in which the tunnel SHOULD
|
||||
// store its state, if any. If this field is empty, the
|
||||
// Start function fails with ErrEmptyTunnelDir.
|
||||
TunnelDir string
|
||||
|
||||
// Logger is the optional logger to use. If empty we use a default
|
||||
// implementation that does not emit any output.
|
||||
Logger Logger
|
||||
|
||||
// TorArgs contains the optional arguments that you want us to pass
|
||||
// to the tor binary when invoking it. By default we do not
|
||||
// pass any extra argument. This flag might be useful to
|
||||
|
@ -48,11 +53,6 @@ type Config struct {
|
|||
// executing. When not set, we execute `tor`.
|
||||
TorBinary string
|
||||
|
||||
// TunnelDir is the mandatory directory in which the tunnel SHOULD
|
||||
// store its state, if any. If this field is empty, the
|
||||
// Start function fails with ErrEmptyTunnelDir.
|
||||
TunnelDir string
|
||||
|
||||
// testExecabsLookPath allows us to mock exeabs.LookPath
|
||||
testExecabsLookPath func(name string) (string, error)
|
||||
|
|
@ -7,7 +7,7 @@ import (
|
|||
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/tunnel"
|
||||
"github.com/ooni/probe-cli/v3/internal/tunnel"
|
||||
)
|
||||
|
||||
func TestFakeStartStop(t *testing.T) {
|
|
@ -8,13 +8,12 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/armon/go-socks5"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
|
||||
)
|
||||
|
||||
func TestFakeWithCancelledContext(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // immediately fail
|
||||
sess := &mockable.Session{}
|
||||
sess := &MockableSession{}
|
||||
tunnel, err := fakeStart(ctx, &Config{
|
||||
Session: sess,
|
||||
TunnelDir: "testdata",
|
||||
|
@ -29,7 +28,7 @@ func TestFakeWithCancelledContext(t *testing.T) {
|
|||
|
||||
func TestFakeWithEmptyTunnelDir(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
sess := &mockable.Session{}
|
||||
sess := &MockableSession{}
|
||||
tunnel, err := fakeStart(ctx, &Config{
|
||||
Session: sess,
|
||||
TunnelDir: "",
|
||||
|
@ -45,7 +44,7 @@ func TestFakeWithEmptyTunnelDir(t *testing.T) {
|
|||
func TestFakeWithFailingMkdirAll(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
ctx := context.Background()
|
||||
sess := &mockable.Session{}
|
||||
sess := &MockableSession{}
|
||||
tunnel, err := fakeStart(ctx, &Config{
|
||||
Session: sess,
|
||||
TunnelDir: "testdata",
|
||||
|
@ -64,7 +63,7 @@ func TestFakeWithFailingMkdirAll(t *testing.T) {
|
|||
func TestFakeSocks5NewFails(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
ctx := context.Background()
|
||||
sess := &mockable.Session{}
|
||||
sess := &MockableSession{}
|
||||
tunnel, err := fakeStart(ctx, &Config{
|
||||
Session: sess,
|
||||
TunnelDir: "testdata",
|
||||
|
@ -83,7 +82,7 @@ func TestFakeSocks5NewFails(t *testing.T) {
|
|||
func TestFakeNetListenFails(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
ctx := context.Background()
|
||||
sess := &mockable.Session{}
|
||||
sess := &MockableSession{}
|
||||
tunnel, err := fakeStart(ctx, &Config{
|
||||
Session: sess,
|
||||
TunnelDir: "testdata",
|
|
@ -7,7 +7,7 @@ import (
|
|||
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/tunnel"
|
||||
"github.com/ooni/probe-cli/v3/internal/tunnel"
|
||||
)
|
||||
|
||||
func TestPsiphonStartStop(t *testing.T) {
|
|
@ -6,14 +6,13 @@ import (
|
|||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
|
||||
"github.com/ooni/psiphon/oopsi/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary/clientlib"
|
||||
)
|
||||
|
||||
func TestPsiphonWithCancelledContext(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // immediately fail
|
||||
sess := &mockable.Session{}
|
||||
sess := &MockableSession{}
|
||||
tunnel, err := psiphonStart(ctx, &Config{
|
||||
Session: sess,
|
||||
TunnelDir: "testdata",
|
||||
|
@ -28,7 +27,7 @@ func TestPsiphonWithCancelledContext(t *testing.T) {
|
|||
|
||||
func TestPsiphonWithEmptyTunnelDir(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
sess := &mockable.Session{}
|
||||
sess := &MockableSession{}
|
||||
tunnel, err := psiphonStart(ctx, &Config{
|
||||
Session: sess,
|
||||
TunnelDir: "",
|
||||
|
@ -43,8 +42,8 @@ func TestPsiphonWithEmptyTunnelDir(t *testing.T) {
|
|||
|
||||
func TestPsiphonFetchPsiphonConfigFailure(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
sess := &mockable.Session{
|
||||
MockableFetchPsiphonConfigErr: expected,
|
||||
sess := &MockableSession{
|
||||
Err: expected,
|
||||
}
|
||||
tunnel, err := psiphonStart(context.Background(), &Config{
|
||||
Session: sess,
|
||||
|
@ -60,8 +59,8 @@ func TestPsiphonFetchPsiphonConfigFailure(t *testing.T) {
|
|||
|
||||
func TestPsiphonMkdirAllFailure(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
sess := &mockable.Session{
|
||||
MockableFetchPsiphonConfigResult: []byte(`{}`),
|
||||
sess := &MockableSession{
|
||||
Result: []byte(`{}`),
|
||||
}
|
||||
tunnel, err := psiphonStart(context.Background(), &Config{
|
||||
Session: sess,
|
||||
|
@ -80,8 +79,8 @@ func TestPsiphonMkdirAllFailure(t *testing.T) {
|
|||
|
||||
func TestPsiphonStartFailure(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
sess := &mockable.Session{
|
||||
MockableFetchPsiphonConfigResult: []byte(`{}`),
|
||||
sess := &MockableSession{
|
||||
Result: []byte(`{}`),
|
||||
}
|
||||
tunnel, err := psiphonStart(context.Background(), &Config{
|
||||
Session: sess,
|
17
internal/tunnel/session_test.go
Normal file
17
internal/tunnel/session_test.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package tunnel
|
||||
|
||||
import "context"
|
||||
|
||||
// MockableSession is a mockable session.
|
||||
type MockableSession struct {
|
||||
// Result contains the bytes of the psiphon config.
|
||||
Result []byte
|
||||
|
||||
// Err is the error, if any.
|
||||
Err error
|
||||
}
|
||||
|
||||
// FetchPsiphonConfig implements ExperimentSession.FetchPsiphonConfig
|
||||
func (sess *MockableSession) FetchPsiphonConfig(ctx context.Context) ([]byte, error) {
|
||||
return sess.Result, sess.Err
|
||||
}
|
|
@ -7,7 +7,7 @@ import (
|
|||
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/tunnel"
|
||||
"github.com/ooni/probe-cli/v3/internal/tunnel"
|
||||
"golang.org/x/sys/execabs"
|
||||
)
|
||||
|
|
@ -14,7 +14,6 @@ import (
|
|||
|
||||
"github.com/cretz/bine/control"
|
||||
"github.com/cretz/bine/tor"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
|
||||
)
|
||||
|
||||
// torCloser is used to mock a running tor process, which
|
||||
|
@ -53,7 +52,7 @@ func TestTorWithCancelledContext(t *testing.T) {
|
|||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // fail immediately
|
||||
tun, err := torStart(ctx, &Config{
|
||||
Session: &mockable.Session{},
|
||||
Session: &MockableSession{},
|
||||
TunnelDir: "testdata",
|
||||
})
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
|
@ -67,7 +66,7 @@ func TestTorWithCancelledContext(t *testing.T) {
|
|||
func TestTorWithEmptyTunnelDir(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tun, err := torStart(ctx, &Config{
|
||||
Session: &mockable.Session{},
|
||||
Session: &MockableSession{},
|
||||
TunnelDir: "",
|
||||
})
|
||||
if !errors.Is(err, ErrEmptyTunnelDir) {
|
||||
|
@ -81,7 +80,7 @@ func TestTorWithEmptyTunnelDir(t *testing.T) {
|
|||
func TestTorBinaryNotFoundFailure(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tun, err := torStart(ctx, &Config{
|
||||
Session: &mockable.Session{},
|
||||
Session: &MockableSession{},
|
||||
TorBinary: "/nonexistent/directory/tor",
|
||||
TunnelDir: "testdata",
|
||||
})
|
||||
|
@ -97,7 +96,7 @@ func TestTorStartFailure(t *testing.T) {
|
|||
expected := errors.New("mocked error")
|
||||
ctx := context.Background()
|
||||
tun, err := torStart(ctx, &Config{
|
||||
Session: &mockable.Session{},
|
||||
Session: &MockableSession{},
|
||||
TunnelDir: "testdata",
|
||||
testExecabsLookPath: func(name string) (string, error) {
|
||||
return "/usr/local/bin/tor", nil
|
||||
|
@ -118,7 +117,7 @@ func TestTorEnableNetworkFailure(t *testing.T) {
|
|||
expected := errors.New("mocked error")
|
||||
ctx := context.Background()
|
||||
tun, err := torStart(ctx, &Config{
|
||||
Session: &mockable.Session{},
|
||||
Session: &MockableSession{},
|
||||
TunnelDir: "testdata",
|
||||
testExecabsLookPath: func(name string) (string, error) {
|
||||
return "/usr/local/bin/tor", nil
|
||||
|
@ -142,7 +141,7 @@ func TestTorGetInfoFailure(t *testing.T) {
|
|||
expected := errors.New("mocked error")
|
||||
ctx := context.Background()
|
||||
tun, err := torStart(ctx, &Config{
|
||||
Session: &mockable.Session{},
|
||||
Session: &MockableSession{},
|
||||
TunnelDir: "testdata",
|
||||
testExecabsLookPath: func(name string) (string, error) {
|
||||
return "/usr/local/bin/tor", nil
|
||||
|
@ -168,7 +167,7 @@ func TestTorGetInfoFailure(t *testing.T) {
|
|||
func TestTorGetInfoInvalidNumberOfKeys(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tun, err := torStart(ctx, &Config{
|
||||
Session: &mockable.Session{},
|
||||
Session: &MockableSession{},
|
||||
TunnelDir: "testdata",
|
||||
testExecabsLookPath: func(name string) (string, error) {
|
||||
return "/usr/local/bin/tor", nil
|
||||
|
@ -194,7 +193,7 @@ func TestTorGetInfoInvalidNumberOfKeys(t *testing.T) {
|
|||
func TestTorGetInfoInvalidKey(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tun, err := torStart(ctx, &Config{
|
||||
Session: &mockable.Session{},
|
||||
Session: &MockableSession{},
|
||||
TunnelDir: "testdata",
|
||||
testExecabsLookPath: func(name string) (string, error) {
|
||||
return "/usr/local/bin/tor", nil
|
||||
|
@ -220,7 +219,7 @@ func TestTorGetInfoInvalidKey(t *testing.T) {
|
|||
func TestTorGetInfoInvalidProxyType(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tun, err := torStart(ctx, &Config{
|
||||
Session: &mockable.Session{},
|
||||
Session: &MockableSession{},
|
||||
TunnelDir: "testdata",
|
||||
testExecabsLookPath: func(name string) (string, error) {
|
||||
return "/usr/local/bin/tor", nil
|
||||
|
@ -246,7 +245,7 @@ func TestTorGetInfoInvalidProxyType(t *testing.T) {
|
|||
func TestTorUnsupportedProxy(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tun, err := torStart(ctx, &Config{
|
||||
Session: &mockable.Session{},
|
||||
Session: &MockableSession{},
|
||||
TunnelDir: "testdata",
|
||||
testExecabsLookPath: func(name string) (string, error) {
|
||||
return "/usr/local/bin/tor", nil
|
|
@ -5,18 +5,14 @@ import (
|
|||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/tunnel"
|
||||
"github.com/ooni/probe-cli/v3/internal/tunnel"
|
||||
)
|
||||
|
||||
func TestStartNoTunnel(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tun, err := tunnel.Start(ctx, &tunnel.Config{
|
||||
Name: "",
|
||||
Session: &mockable.Session{
|
||||
MockableLogger: log.Log,
|
||||
},
|
||||
Session: &tunnel.MockableSession{},
|
||||
})
|
||||
if !errors.Is(err, tunnel.ErrUnsupportedTunnelName) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
|
@ -31,9 +27,7 @@ func TestStartPsiphonWithCancelledContext(t *testing.T) {
|
|||
cancel() // fail immediately
|
||||
tun, err := tunnel.Start(ctx, &tunnel.Config{
|
||||
Name: "psiphon",
|
||||
Session: &mockable.Session{
|
||||
MockableLogger: log.Log,
|
||||
},
|
||||
Session: &tunnel.MockableSession{},
|
||||
TunnelDir: "testdata",
|
||||
})
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
|
@ -49,9 +43,7 @@ func TestStartTorWithCancelledContext(t *testing.T) {
|
|||
cancel() // fail immediately
|
||||
tun, err := tunnel.Start(ctx, &tunnel.Config{
|
||||
Name: "tor",
|
||||
Session: &mockable.Session{
|
||||
MockableLogger: log.Log,
|
||||
},
|
||||
Session: &tunnel.MockableSession{},
|
||||
TunnelDir: "testdata",
|
||||
})
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
|
@ -66,9 +58,7 @@ func TestStartInvalidTunnel(t *testing.T) {
|
|||
ctx := context.Background()
|
||||
tun, err := tunnel.Start(ctx, &tunnel.Config{
|
||||
Name: "antani",
|
||||
Session: &mockable.Session{
|
||||
MockableLogger: log.Log,
|
||||
},
|
||||
Session: &tunnel.MockableSession{},
|
||||
TunnelDir: "testdata",
|
||||
})
|
||||
if !errors.Is(err, tunnel.ErrUnsupportedTunnelName) {
|
Loading…
Reference in New Issue
Block a user