2021-04-03 19:57:21 +02:00
|
|
|
package tunnel
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cretz/bine/control"
|
|
|
|
"github.com/cretz/bine/tor"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Closer struct {
|
|
|
|
counter int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Closer) Close() error {
|
|
|
|
c.counter++
|
|
|
|
return errors.New("mocked mocked mocked")
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestTorTunnelNonNil(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
closer := new(Closer)
|
|
|
|
proxy := &url.URL{Scheme: "x", Host: "10.0.0.1:443"}
|
2021-04-03 19:57:21 +02:00
|
|
|
tun := newTorTunnel(128, closer, proxy)
|
2021-02-02 12:05:47 +01:00
|
|
|
if tun.BootstrapTime() != 128 {
|
|
|
|
t.Fatal("not the bootstrap time we expected")
|
|
|
|
}
|
|
|
|
if tun.SOCKS5ProxyURL() != proxy {
|
|
|
|
t.Fatal("not the url we expected")
|
|
|
|
}
|
|
|
|
tun.Stop()
|
|
|
|
if closer.counter != 1 {
|
|
|
|
t.Fatal("something went wrong while stopping the tunnel")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestTorTunnelNil(t *testing.T) {
|
|
|
|
var tun *torTunnel
|
2021-02-02 12:05:47 +01:00
|
|
|
if tun.BootstrapTime() != 0 {
|
|
|
|
t.Fatal("not the bootstrap time we expected")
|
|
|
|
}
|
|
|
|
if tun.SOCKS5ProxyURL() != nil {
|
|
|
|
t.Fatal("not the url we expected")
|
|
|
|
}
|
|
|
|
tun.Stop() // ensure we don't crash
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestTorStartWithCancelledContext(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
2021-04-03 19:57:21 +02:00
|
|
|
tun, err := torStart(ctx, &mockable.Session{})
|
2021-02-02 12:05:47 +01:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestTorStartWithConfigStartFailure(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
expected := errors.New("mocked error")
|
|
|
|
ctx := context.Background()
|
2021-04-03 19:57:21 +02:00
|
|
|
tun, err := torStartWithConfig(ctx, torStartConfig{
|
2021-02-02 12:05:47 +01:00
|
|
|
Sess: &mockable.Session{},
|
|
|
|
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestTorStartWithConfigEnableNetworkFailure(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
expected := errors.New("mocked error")
|
|
|
|
ctx := context.Background()
|
2021-04-03 19:57:21 +02:00
|
|
|
tun, err := torStartWithConfig(ctx, torStartConfig{
|
2021-02-02 12:05:47 +01:00
|
|
|
Sess: &mockable.Session{},
|
|
|
|
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
|
|
|
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
|
|
|
return expected
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestTorStartWithConfigGetInfoFailure(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
expected := errors.New("mocked error")
|
|
|
|
ctx := context.Background()
|
2021-04-03 19:57:21 +02:00
|
|
|
tun, err := torStartWithConfig(ctx, torStartConfig{
|
2021-02-02 12:05:47 +01:00
|
|
|
Sess: &mockable.Session{},
|
|
|
|
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
|
|
|
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestTorStartWithConfigGetInfoInvalidNumberOfKeys(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx := context.Background()
|
2021-04-03 19:57:21 +02:00
|
|
|
tun, err := torStartWithConfig(ctx, torStartConfig{
|
2021-02-02 12:05:47 +01:00
|
|
|
Sess: &mockable.Session{},
|
|
|
|
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
|
|
|
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
|
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err.Error() != "unable to get socks proxy address" {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestTorStartWithConfigGetInfoInvalidKey(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx := context.Background()
|
2021-04-03 19:57:21 +02:00
|
|
|
tun, err := torStartWithConfig(ctx, torStartConfig{
|
2021-02-02 12:05:47 +01:00
|
|
|
Sess: &mockable.Session{},
|
|
|
|
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
|
|
|
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
|
|
|
|
return []*control.KeyVal{{}}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err.Error() != "unable to get socks proxy address" {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestTorStartWithConfigGetInfoInvalidProxyType(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx := context.Background()
|
2021-04-03 19:57:21 +02:00
|
|
|
tun, err := torStartWithConfig(ctx, torStartConfig{
|
2021-02-02 12:05:47 +01:00
|
|
|
Sess: &mockable.Session{},
|
|
|
|
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
|
|
|
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
|
|
|
|
return []*control.KeyVal{{Key: "net/listeners/socks", Val: "127.0.0.1:9050"}}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if tun == nil {
|
|
|
|
t.Fatal("expected non-nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestTorStartWithConfigSuccess(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx := context.Background()
|
2021-04-03 19:57:21 +02:00
|
|
|
tun, err := torStartWithConfig(ctx, torStartConfig{
|
2021-02-02 12:05:47 +01:00
|
|
|
Sess: &mockable.Session{},
|
|
|
|
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
|
|
|
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
|
|
|
|
return []*control.KeyVal{{Key: "net/listeners/socks", Val: "unix:/foo/bar"}}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err.Error() != "tor returned unsupported proxy" {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|