diff --git a/internal/engine/experiment/hhfm/hhfm.go b/internal/engine/experiment/hhfm/hhfm.go index 2015a9f..10eef48 100644 --- a/internal/engine/experiment/hhfm/hhfm.go +++ b/internal/engine/experiment/hhfm/hhfm.go @@ -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 ( diff --git a/internal/engine/experiment/hirl/hirl.go b/internal/engine/experiment/hirl/hirl.go index 8ec1134..5d7c47e 100644 --- a/internal/engine/experiment/hirl/hirl.go +++ b/internal/engine/experiment/hirl/hirl.go @@ -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 ( diff --git a/internal/engine/experiment/urlgetter/getter.go b/internal/engine/experiment/urlgetter/getter.go index 5c55063..d1225ec 100644 --- a/internal/engine/experiment/urlgetter/getter.go +++ b/internal/engine/experiment/urlgetter/getter.go @@ -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 diff --git a/internal/engine/experiment/webconnectivity/httpanalysis_test.go b/internal/engine/experiment/webconnectivity/httpanalysis_test.go index 4c0c0f0..54264e9 100644 --- a/internal/engine/experiment/webconnectivity/httpanalysis_test.go +++ b/internal/engine/experiment/webconnectivity/httpanalysis_test.go @@ -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) { diff --git a/internal/engine/geolocate/iplookup.go b/internal/engine/geolocate/iplookup.go index 994055a..d9db4fe 100644 --- a/internal/engine/geolocate/iplookup.go +++ b/internal/engine/geolocate/iplookup.go @@ -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 ( diff --git a/internal/engine/session.go b/internal/engine/session.go index 6db35c2..8e93af1 100644 --- a/internal/engine/session.go +++ b/internal/engine/session.go @@ -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" ) diff --git a/internal/engine/tunnel/config.go b/internal/tunnel/config.go similarity index 95% rename from internal/engine/tunnel/config.go rename to internal/tunnel/config.go index a85056f..b44983b 100644 --- a/internal/engine/tunnel/config.go +++ b/internal/tunnel/config.go @@ -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) diff --git a/internal/engine/tunnel/config_test.go b/internal/tunnel/config_test.go similarity index 100% rename from internal/engine/tunnel/config_test.go rename to internal/tunnel/config_test.go diff --git a/internal/engine/tunnel/fake.go b/internal/tunnel/fake.go similarity index 100% rename from internal/engine/tunnel/fake.go rename to internal/tunnel/fake.go diff --git a/internal/engine/tunnel/fake_integration_test.go b/internal/tunnel/fake_integration_test.go similarity index 94% rename from internal/engine/tunnel/fake_integration_test.go rename to internal/tunnel/fake_integration_test.go index 921c8d6..366c3e3 100644 --- a/internal/engine/tunnel/fake_integration_test.go +++ b/internal/tunnel/fake_integration_test.go @@ -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) { diff --git a/internal/engine/tunnel/fake_test.go b/internal/tunnel/fake_test.go similarity index 90% rename from internal/engine/tunnel/fake_test.go rename to internal/tunnel/fake_test.go index 9da1d20..e2b0abf 100644 --- a/internal/engine/tunnel/fake_test.go +++ b/internal/tunnel/fake_test.go @@ -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", diff --git a/internal/engine/tunnel/psiphon.go b/internal/tunnel/psiphon.go similarity index 100% rename from internal/engine/tunnel/psiphon.go rename to internal/tunnel/psiphon.go diff --git a/internal/engine/tunnel/psiphon_integration_test.go b/internal/tunnel/psiphon_integration_test.go similarity index 94% rename from internal/engine/tunnel/psiphon_integration_test.go rename to internal/tunnel/psiphon_integration_test.go index c49c383..2e67a7d 100644 --- a/internal/engine/tunnel/psiphon_integration_test.go +++ b/internal/tunnel/psiphon_integration_test.go @@ -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) { diff --git a/internal/engine/tunnel/psiphon_test.go b/internal/tunnel/psiphon_test.go similarity index 85% rename from internal/engine/tunnel/psiphon_test.go rename to internal/tunnel/psiphon_test.go index 044ea58..d6c0cc9 100644 --- a/internal/engine/tunnel/psiphon_test.go +++ b/internal/tunnel/psiphon_test.go @@ -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, diff --git a/internal/tunnel/session_test.go b/internal/tunnel/session_test.go new file mode 100644 index 0000000..5235c50 --- /dev/null +++ b/internal/tunnel/session_test.go @@ -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 +} diff --git a/internal/engine/tunnel/testdata/.gitignore b/internal/tunnel/testdata/.gitignore similarity index 100% rename from internal/engine/tunnel/testdata/.gitignore rename to internal/tunnel/testdata/.gitignore diff --git a/internal/engine/tunnel/tor.go b/internal/tunnel/tor.go similarity index 100% rename from internal/engine/tunnel/tor.go rename to internal/tunnel/tor.go diff --git a/internal/engine/tunnel/tor_integration_test.go b/internal/tunnel/tor_integration_test.go similarity index 95% rename from internal/engine/tunnel/tor_integration_test.go rename to internal/tunnel/tor_integration_test.go index f52702c..0918d2f 100644 --- a/internal/engine/tunnel/tor_integration_test.go +++ b/internal/tunnel/tor_integration_test.go @@ -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" ) diff --git a/internal/engine/tunnel/tor_test.go b/internal/tunnel/tor_test.go similarity index 95% rename from internal/engine/tunnel/tor_test.go rename to internal/tunnel/tor_test.go index 35dc6be..fea674b 100644 --- a/internal/engine/tunnel/tor_test.go +++ b/internal/tunnel/tor_test.go @@ -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 diff --git a/internal/engine/tunnel/tunnel.go b/internal/tunnel/tunnel.go similarity index 100% rename from internal/engine/tunnel/tunnel.go rename to internal/tunnel/tunnel.go diff --git a/internal/engine/tunnel/tunnel_test.go b/internal/tunnel/tunnel_test.go similarity index 74% rename from internal/engine/tunnel/tunnel_test.go rename to internal/tunnel/tunnel_test.go index cdce387..0bbc57d 100644 --- a/internal/engine/tunnel/tunnel_test.go +++ b/internal/tunnel/tunnel_test.go @@ -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, - }, + Name: "", + Session: &tunnel.MockableSession{}, }) if !errors.Is(err, tunnel.ErrUnsupportedTunnelName) { t.Fatal("not the error we expected", err) @@ -30,10 +26,8 @@ func TestStartPsiphonWithCancelledContext(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() // fail immediately tun, err := tunnel.Start(ctx, &tunnel.Config{ - Name: "psiphon", - Session: &mockable.Session{ - MockableLogger: log.Log, - }, + Name: "psiphon", + Session: &tunnel.MockableSession{}, TunnelDir: "testdata", }) if !errors.Is(err, context.Canceled) { @@ -48,10 +42,8 @@ func TestStartTorWithCancelledContext(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() // fail immediately tun, err := tunnel.Start(ctx, &tunnel.Config{ - Name: "tor", - Session: &mockable.Session{ - MockableLogger: log.Log, - }, + Name: "tor", + Session: &tunnel.MockableSession{}, TunnelDir: "testdata", }) if !errors.Is(err, context.Canceled) { @@ -65,10 +57,8 @@ func TestStartTorWithCancelledContext(t *testing.T) { func TestStartInvalidTunnel(t *testing.T) { ctx := context.Background() tun, err := tunnel.Start(ctx, &tunnel.Config{ - Name: "antani", - Session: &mockable.Session{ - MockableLogger: log.Log, - }, + Name: "antani", + Session: &tunnel.MockableSession{}, TunnelDir: "testdata", }) if !errors.Is(err, tunnel.ErrUnsupportedTunnelName) {