ooni-probe-cli/internal/engine/tunnel/tunnel_test.go
Simone Basso 8fe4e5410d
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
2021-04-05 11:27:41 +02:00

80 lines
1.6 KiB
Go

package tunnel
import (
"context"
"errors"
"testing"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
)
func TestStartNoTunnel(t *testing.T) {
ctx := context.Background()
tunnel, err := Start(ctx, &Config{
Name: "",
Session: &mockable.Session{
MockableLogger: log.Log,
},
})
if err != nil {
t.Fatal(err)
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestStartPsiphonWithCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // fail immediately
tunnel, err := Start(ctx, &Config{
Name: "psiphon",
Session: &mockable.Session{
MockableLogger: log.Log,
},
TunnelDir: "testdata",
})
if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestStartTorWithCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // fail immediately
tunnel, err := Start(ctx, &Config{
Name: "tor",
Session: &mockable.Session{
MockableLogger: log.Log,
},
TunnelDir: "testdata",
})
if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestStartInvalidTunnel(t *testing.T) {
ctx := context.Background()
tunnel, err := Start(ctx, &Config{
Name: "antani",
Session: &mockable.Session{
MockableLogger: log.Log,
},
TunnelDir: "testdata",
})
if !errors.Is(err, ErrUnsupportedTunnelName) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}