package tunnel 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" ) // torCloser is used to mock a running tor process, which // we abstract as a io.Closer in tor.go. type torCloser struct { counter int } // 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(torCloser) proxy := &url.URL{Scheme: "x", Host: "10.0.0.1:443"} tun := &torTunnel{ bootstrapTime: 128, instance: closer, proxy: proxy, } 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") } } func TestTorWithCancelledContext(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) 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") } if tun != nil { t.Fatal("expected nil tunnel here") } } func TestTorWithEmptyTunnelDir(t *testing.T) { ctx := context.Background() tun, err := torStart(ctx, &Config{ Session: &mockable.Session{}, TunnelDir: "", }) if !errors.Is(err, ErrEmptyTunnelDir) { t.Fatal("not the error we expected") } if tun != nil { t.Fatal("expected nil tunnel here") } } func TestTorStartFailure(t *testing.T) { expected := errors.New("mocked error") ctx := context.Background() tun, err := torStart(ctx, &Config{ Session: &mockable.Session{}, TunnelDir: "testdata", testTorStart: 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") } } func TestTorEnableNetworkFailure(t *testing.T) { expected := errors.New("mocked error") ctx := context.Background() tun, err := torStart(ctx, &Config{ Session: &mockable.Session{}, TunnelDir: "testdata", testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { return &tor.Tor{}, nil }, testTorEnableNetwork: 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") } } func TestTorGetInfoFailure(t *testing.T) { expected := errors.New("mocked error") ctx := context.Background() tun, err := torStart(ctx, &Config{ Session: &mockable.Session{}, TunnelDir: "testdata", testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { return &tor.Tor{}, nil }, testTorEnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error { return nil }, testTorGetInfo: 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") } } func TestTorGetInfoInvalidNumberOfKeys(t *testing.T) { ctx := context.Background() tun, err := torStart(ctx, &Config{ Session: &mockable.Session{}, TunnelDir: "testdata", testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { return &tor.Tor{}, nil }, testTorEnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error { return nil }, testTorGetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) { return nil, nil }, }) if !errors.Is(err, ErrTorUnableToGetSOCKSProxyAddress) { t.Fatal("not the error we expected", err) } if tun != nil { t.Fatal("expected nil tunnel here") } } func TestTorGetInfoInvalidKey(t *testing.T) { ctx := context.Background() tun, err := torStart(ctx, &Config{ Session: &mockable.Session{}, TunnelDir: "testdata", testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { return &tor.Tor{}, nil }, testTorEnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error { return nil }, testTorGetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) { return []*control.KeyVal{{}}, nil }, }) if !errors.Is(err, ErrTorUnableToGetSOCKSProxyAddress) { t.Fatal("not the error we expected") } if tun != nil { t.Fatal("expected nil tunnel here") } } func TestTorGetInfoInvalidProxyType(t *testing.T) { ctx := context.Background() tun, err := torStart(ctx, &Config{ Session: &mockable.Session{}, TunnelDir: "testdata", testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { return &tor.Tor{}, nil }, testTorEnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error { return nil }, testTorGetInfo: 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") } } func TestTorUnsupportedProxy(t *testing.T) { ctx := context.Background() tun, err := torStart(ctx, &Config{ Session: &mockable.Session{}, TunnelDir: "testdata", testTorStart: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { return &tor.Tor{}, nil }, testTorEnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error { return nil }, testTorGetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) { return []*control.KeyVal{{Key: "net/listeners/socks", Val: "unix:/foo/bar"}}, nil }, }) if !errors.Is(err, ErrTorReturnedUnsupportedProxy) { t.Fatal("not the error we expected") } if tun != nil { t.Fatal("expected nil tunnel here") } }