2021-04-05 16:38:25 +02:00
|
|
|
package tunnel_test
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2021-06-04 15:15:41 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/tunnel"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestStartNoTunnel(t *testing.T) {
|
2021-04-05 11:27:41 +02:00
|
|
|
ctx := context.Background()
|
2021-04-05 16:38:25 +02:00
|
|
|
tun, err := tunnel.Start(ctx, &tunnel.Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Name: "",
|
|
|
|
Session: &tunnel.MockableSession{},
|
2021-02-02 12:05:47 +01:00
|
|
|
})
|
2021-04-05 16:38:25 +02:00
|
|
|
if !errors.Is(err, tunnel.ErrUnsupportedTunnelName) {
|
2021-04-05 16:08:16 +02:00
|
|
|
t.Fatal("not the error we expected", err)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-04-05 16:38:25 +02:00
|
|
|
if tun != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:27:41 +02:00
|
|
|
func TestStartPsiphonWithCancelledContext(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2021-04-05 11:27:41 +02:00
|
|
|
cancel() // fail immediately
|
2021-04-05 16:38:25 +02:00
|
|
|
tun, err := tunnel.Start(ctx, &tunnel.Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Name: "psiphon",
|
|
|
|
Session: &tunnel.MockableSession{},
|
2021-04-05 11:27:41 +02:00
|
|
|
TunnelDir: "testdata",
|
2021-02-02 12:05:47 +01:00
|
|
|
})
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
2021-04-05 16:38:25 +02:00
|
|
|
if tun != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:27:41 +02:00
|
|
|
func TestStartTorWithCancelledContext(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2021-04-05 11:27:41 +02:00
|
|
|
cancel() // fail immediately
|
2021-04-05 16:38:25 +02:00
|
|
|
tun, err := tunnel.Start(ctx, &tunnel.Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Name: "tor",
|
|
|
|
Session: &tunnel.MockableSession{},
|
2021-04-05 11:27:41 +02:00
|
|
|
TunnelDir: "testdata",
|
2021-02-02 12:05:47 +01:00
|
|
|
})
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
2021-04-05 16:38:25 +02:00
|
|
|
if tun != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestStartInvalidTunnel(t *testing.T) {
|
2021-04-05 11:27:41 +02:00
|
|
|
ctx := context.Background()
|
2021-04-05 16:38:25 +02:00
|
|
|
tun, err := tunnel.Start(ctx, &tunnel.Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Name: "antani",
|
|
|
|
Session: &tunnel.MockableSession{},
|
2021-04-05 11:27:41 +02:00
|
|
|
TunnelDir: "testdata",
|
2021-02-02 12:05:47 +01:00
|
|
|
})
|
2021-04-05 16:38:25 +02:00
|
|
|
if !errors.Is(err, tunnel.ErrUnsupportedTunnelName) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
2021-04-05 16:38:25 +02:00
|
|
|
if tun != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|