2021-04-03 19:57:21 +02:00
|
|
|
package tunnel
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-04-05 19:18:00 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net/url"
|
2021-04-05 19:18:00 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2021-05-04 08:14:25 +02:00
|
|
|
"syscall"
|
2021-02-02 12:05:47 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cretz/bine/control"
|
|
|
|
"github.com/cretz/bine/tor"
|
|
|
|
)
|
|
|
|
|
2021-04-05 11:27:41 +02:00
|
|
|
// torCloser is used to mock a running tor process, which
|
|
|
|
// we abstract as a io.Closer in tor.go.
|
|
|
|
type torCloser struct {
|
2021-02-02 12:05:47 +01:00
|
|
|
counter int
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:27:41 +02:00
|
|
|
// Close implements io.Closer.Close.
|
|
|
|
func (c *torCloser) Close() error {
|
2021-02-02 12:05:47 +01:00
|
|
|
c.counter++
|
|
|
|
return errors.New("mocked mocked mocked")
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
func TestTorTunnelNonNil(t *testing.T) {
|
2021-04-05 11:27:41 +02:00
|
|
|
closer := new(torCloser)
|
2021-02-02 12:05:47 +01:00
|
|
|
proxy := &url.URL{Scheme: "x", Host: "10.0.0.1:443"}
|
2021-04-03 21:25:08 +02:00
|
|
|
tun := &torTunnel{
|
|
|
|
bootstrapTime: 128,
|
|
|
|
instance: closer,
|
|
|
|
proxy: 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-05 16:38:25 +02:00
|
|
|
func TestTorWithCancelledContext(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
|
|
|
|
tun, err := torStart(ctx, &Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Session: &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")
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 16:38:25 +02:00
|
|
|
func TestTorWithEmptyTunnelDir(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tun, err := torStart(ctx, &Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Session: &MockableSession{},
|
2021-04-05 16:38:25 +02:00
|
|
|
TunnelDir: "",
|
|
|
|
})
|
|
|
|
if !errors.Is(err, ErrEmptyTunnelDir) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 08:14:25 +02:00
|
|
|
func TestTorBinaryNotFoundFailure(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tun, err := torStart(ctx, &Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Session: &MockableSession{},
|
2021-05-04 08:14:25 +02:00
|
|
|
TorBinary: "/nonexistent/directory/tor",
|
|
|
|
TunnelDir: "testdata",
|
|
|
|
})
|
|
|
|
if !errors.Is(err, syscall.ENOENT) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 16:38:25 +02:00
|
|
|
func TestTorStartFailure(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
expected := errors.New("mocked error")
|
|
|
|
ctx := context.Background()
|
2021-04-03 21:25:08 +02:00
|
|
|
tun, err := torStart(ctx, &Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Session: &MockableSession{},
|
2021-04-05 11:27:41 +02:00
|
|
|
TunnelDir: "testdata",
|
2021-05-04 08:14:25 +02:00
|
|
|
testExecabsLookPath: func(name string) (string, error) {
|
|
|
|
return "/usr/local/bin/tor", nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
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-05 16:38:25 +02:00
|
|
|
func TestTorEnableNetworkFailure(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
expected := errors.New("mocked error")
|
|
|
|
ctx := context.Background()
|
2021-04-03 21:25:08 +02:00
|
|
|
tun, err := torStart(ctx, &Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Session: &MockableSession{},
|
2021-04-05 11:27:41 +02:00
|
|
|
TunnelDir: "testdata",
|
2021-05-04 08:14:25 +02:00
|
|
|
testExecabsLookPath: func(name string) (string, error) {
|
|
|
|
return "/usr/local/bin/tor", nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorEnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
2021-02-02 12:05:47 +01:00
|
|
|
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-05 16:38:25 +02:00
|
|
|
func TestTorGetInfoFailure(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
expected := errors.New("mocked error")
|
|
|
|
ctx := context.Background()
|
2021-04-03 21:25:08 +02:00
|
|
|
tun, err := torStart(ctx, &Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Session: &MockableSession{},
|
2021-04-05 11:27:41 +02:00
|
|
|
TunnelDir: "testdata",
|
2021-05-04 08:14:25 +02:00
|
|
|
testExecabsLookPath: func(name string) (string, error) {
|
|
|
|
return "/usr/local/bin/tor", nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorEnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
2021-02-02 12:05:47 +01:00
|
|
|
return nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorGetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
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-05 16:38:25 +02:00
|
|
|
func TestTorGetInfoInvalidNumberOfKeys(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx := context.Background()
|
2021-04-03 21:25:08 +02:00
|
|
|
tun, err := torStart(ctx, &Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Session: &MockableSession{},
|
2021-04-05 11:27:41 +02:00
|
|
|
TunnelDir: "testdata",
|
2021-05-04 08:14:25 +02:00
|
|
|
testExecabsLookPath: func(name string) (string, error) {
|
|
|
|
return "/usr/local/bin/tor", nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorEnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
2021-02-02 12:05:47 +01:00
|
|
|
return nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorGetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
})
|
2021-04-05 16:38:25 +02:00
|
|
|
if !errors.Is(err, ErrTorUnableToGetSOCKSProxyAddress) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 16:38:25 +02:00
|
|
|
func TestTorGetInfoInvalidKey(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx := context.Background()
|
2021-04-03 21:25:08 +02:00
|
|
|
tun, err := torStart(ctx, &Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Session: &MockableSession{},
|
2021-04-05 11:27:41 +02:00
|
|
|
TunnelDir: "testdata",
|
2021-05-04 08:14:25 +02:00
|
|
|
testExecabsLookPath: func(name string) (string, error) {
|
|
|
|
return "/usr/local/bin/tor", nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorEnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
2021-02-02 12:05:47 +01:00
|
|
|
return nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorGetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return []*control.KeyVal{{}}, nil
|
|
|
|
},
|
|
|
|
})
|
2021-04-05 16:38:25 +02:00
|
|
|
if !errors.Is(err, ErrTorUnableToGetSOCKSProxyAddress) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 16:38:25 +02:00
|
|
|
func TestTorGetInfoInvalidProxyType(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx := context.Background()
|
2021-04-03 21:25:08 +02:00
|
|
|
tun, err := torStart(ctx, &Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Session: &MockableSession{},
|
2021-04-05 11:27:41 +02:00
|
|
|
TunnelDir: "testdata",
|
2021-05-04 08:14:25 +02:00
|
|
|
testExecabsLookPath: func(name string) (string, error) {
|
|
|
|
return "/usr/local/bin/tor", nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorEnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
2021-02-02 12:05:47 +01:00
|
|
|
return nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorGetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
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-05 16:38:25 +02:00
|
|
|
func TestTorUnsupportedProxy(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx := context.Background()
|
2021-04-03 21:25:08 +02:00
|
|
|
tun, err := torStart(ctx, &Config{
|
2021-06-04 15:15:41 +02:00
|
|
|
Session: &MockableSession{},
|
2021-04-05 11:27:41 +02:00
|
|
|
TunnelDir: "testdata",
|
2021-05-04 08:14:25 +02:00
|
|
|
testExecabsLookPath: func(name string) (string, error) {
|
|
|
|
return "/usr/local/bin/tor", nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return &tor.Tor{}, nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorEnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
2021-02-02 12:05:47 +01:00
|
|
|
return nil
|
|
|
|
},
|
2021-04-03 21:25:08 +02:00
|
|
|
testTorGetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return []*control.KeyVal{{Key: "net/listeners/socks", Val: "unix:/foo/bar"}}, nil
|
|
|
|
},
|
|
|
|
})
|
2021-04-05 16:38:25 +02:00
|
|
|
if !errors.Is(err, ErrTorReturnedUnsupportedProxy) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if tun != nil {
|
|
|
|
t.Fatal("expected nil tunnel here")
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 19:18:00 +02:00
|
|
|
|
|
|
|
func TestMaybeCleanupTunnelDir(t *testing.T) {
|
|
|
|
fakeTunDir := filepath.Join("testdata", "fake-tun-dir")
|
|
|
|
if err := os.RemoveAll(fakeTunDir); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(fakeTunDir, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fakeData := []byte("deadbeef\n")
|
|
|
|
logfile := filepath.Join(fakeTunDir, "tor.log")
|
|
|
|
if err := ioutil.WriteFile(logfile, fakeData, 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for idx := 0; idx < 3; idx++ {
|
|
|
|
filename := filepath.Join(fakeTunDir, fmt.Sprintf("torrc-%d", idx))
|
|
|
|
if err := ioutil.WriteFile(filename, fakeData, 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
filename = filepath.Join(fakeTunDir, fmt.Sprintf("control-port-%d", idx))
|
|
|
|
if err := ioutil.WriteFile(filename, fakeData, 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
filename = filepath.Join(fakeTunDir, fmt.Sprintf("antani-%d", idx))
|
|
|
|
if err := ioutil.WriteFile(filename, fakeData, 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
files, err := filepath.Glob(filepath.Join(fakeTunDir, "*"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) != 10 {
|
|
|
|
t.Fatal("unexpected number of files")
|
|
|
|
}
|
|
|
|
maybeCleanupTunnelDir(fakeTunDir, logfile)
|
|
|
|
files, err = filepath.Glob(filepath.Join(fakeTunDir, "*"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) != 3 {
|
|
|
|
t.Fatal("unexpected number of files")
|
|
|
|
}
|
|
|
|
expectPrefix := filepath.Join(fakeTunDir, "antani-")
|
|
|
|
for _, file := range files {
|
|
|
|
if !strings.HasPrefix(file, expectPrefix) {
|
|
|
|
t.Fatal("unexpected file name: ", file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|