feat(tunnel): introduce persistent tunnel state dir (#294)

* feat(tunnel): introduce persistent tunnel state dir

This diff introduces a persistent state directory for tunnels, so that
we can bootstrap them more quickly after the first time.

Part of https://github.com/ooni/probe/issues/985

* fix: make tunnel dir optional

We have many tests where it does not make sense to explicitly
provide a tunnel dir because we're not using tunnels.

This should simplify setting up a session.

* fix(tunnel): repair tests

* final changes

* more cleanups
This commit is contained in:
Simone Basso 2021-04-05 11:27:41 +02:00 committed by GitHub
commit 8fe4e5410d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 166 additions and 105 deletions

View file

@ -11,17 +11,20 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
)
type Closer struct {
// torCloser is used to mock a running tor process, which
// we abstract as a io.Closer in tor.go.
type torCloser struct {
counter int
}
func (c *Closer) Close() error {
// Close implements io.Closer.Close.
func (c *torCloser) Close() error {
c.counter++
return errors.New("mocked mocked mocked")
}
func TestTorTunnelNonNil(t *testing.T) {
closer := new(Closer)
closer := new(torCloser)
proxy := &url.URL{Scheme: "x", Host: "10.0.0.1:443"}
tun := &torTunnel{
bootstrapTime: 128,
@ -53,8 +56,11 @@ func TestTorTunnelNil(t *testing.T) {
func TestTorStartWithCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
tun, err := torStart(ctx, &Config{Session: &mockable.Session{}})
cancel() // fail immediately
tun, err := torStart(ctx, &Config{
Session: &mockable.Session{},
TunnelDir: "testdata",
})
if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected")
}
@ -67,7 +73,8 @@ func TestTorStartStartFailure(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
tun, err := torStart(ctx, &Config{
Session: &mockable.Session{},
Session: &mockable.Session{},
TunnelDir: "testdata",
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return nil, expected
},
@ -84,7 +91,8 @@ func TestTorStartEnableNetworkFailure(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
tun, err := torStart(ctx, &Config{
Session: &mockable.Session{},
Session: &mockable.Session{},
TunnelDir: "testdata",
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},
@ -104,7 +112,8 @@ func TestTorStartGetInfoFailure(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
tun, err := torStart(ctx, &Config{
Session: &mockable.Session{},
Session: &mockable.Session{},
TunnelDir: "testdata",
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},
@ -126,7 +135,8 @@ func TestTorStartGetInfoFailure(t *testing.T) {
func TestTorStartGetInfoInvalidNumberOfKeys(t *testing.T) {
ctx := context.Background()
tun, err := torStart(ctx, &Config{
Session: &mockable.Session{},
Session: &mockable.Session{},
TunnelDir: "testdata",
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},
@ -148,7 +158,8 @@ func TestTorStartGetInfoInvalidNumberOfKeys(t *testing.T) {
func TestTorStartGetInfoInvalidKey(t *testing.T) {
ctx := context.Background()
tun, err := torStart(ctx, &Config{
Session: &mockable.Session{},
Session: &mockable.Session{},
TunnelDir: "testdata",
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},
@ -170,7 +181,8 @@ func TestTorStartGetInfoInvalidKey(t *testing.T) {
func TestTorStartGetInfoInvalidProxyType(t *testing.T) {
ctx := context.Background()
tun, err := torStart(ctx, &Config{
Session: &mockable.Session{},
Session: &mockable.Session{},
TunnelDir: "testdata",
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},
@ -192,7 +204,8 @@ func TestTorStartGetInfoInvalidProxyType(t *testing.T) {
func TestTorStartUnsupportedProxy(t *testing.T) {
ctx := context.Background()
tun, err := torStart(ctx, &Config{
Session: &mockable.Session{},
Session: &mockable.Session{},
TunnelDir: "testdata",
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},