feature: use go-libtor on mobile, OONI_TOR_BINARY env on desktop (#614)
This branch adds support for running: 1. `go-libtor` on mobile. 2. the tor provided by the desktop app via the `OONI_TOR_BINARY` environment variable. See https://github.com/ooni/ooni.org/issues/761. Co-authored-by: Simone Basso <bassosimone@gmail.com>
This commit is contained in:
parent
09e300ed26
commit
41cf4a8671
8 changed files with 170 additions and 28 deletions
|
|
@ -1,6 +1,8 @@
|
|||
package tunnel
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/apex/log"
|
||||
|
|
@ -20,17 +22,88 @@ func TestConfigLoggerCustom(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestTorBinaryNotSet(t *testing.T) {
|
||||
config := &Config{}
|
||||
if config.torBinary() != "tor" {
|
||||
t.Fatal("not the result we expected")
|
||||
func TestConfigTorBinary(t *testing.T) {
|
||||
// newConfig is a factory for creating a new config
|
||||
//
|
||||
// Arguments:
|
||||
//
|
||||
// - torBinaryPath is the possibly-empty config.TorBinary to use;
|
||||
//
|
||||
// - realBinaryPath is the possibly-empty path execabs.LookupPath should return;
|
||||
//
|
||||
// - err is the possbly-nil error that execabs.LookupPath should return.
|
||||
//
|
||||
// Returns a new *Config.
|
||||
newConfig := func(binaryPath, realBinaryPath string, err error) *Config {
|
||||
return &Config{
|
||||
TorBinary: binaryPath,
|
||||
testExecabsLookPath: func(name string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return realBinaryPath, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTorBinarySet(t *testing.T) {
|
||||
path := "/usr/local/bin/tor"
|
||||
config := &Config{TorBinary: path}
|
||||
if config.torBinary() != path {
|
||||
t.Fatal("not the result we expected")
|
||||
// verifyExpectations ensures that config.torBinary() produces in
|
||||
// output the expectPath and expectErr result.
|
||||
verifyExpectations := func(
|
||||
t *testing.T, config *Config, expectPath string, expectErr error) {
|
||||
path, err := config.torBinary()
|
||||
if !errors.Is(err, expectErr) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if path != expectPath {
|
||||
t.Fatal("not the path we expected", path)
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("with empty TorBinary and no tor in PATH", func(t *testing.T) {
|
||||
expected := errors.New("no such binary in PATH")
|
||||
config := newConfig("", "", expected)
|
||||
verifyExpectations(t, config, "", expected)
|
||||
})
|
||||
|
||||
t.Run("with empty TorBinary and tor in PATH", func(t *testing.T) {
|
||||
expected := "/usr/bin/tor"
|
||||
config := newConfig("", expected, nil)
|
||||
verifyExpectations(t, config, expected, nil)
|
||||
})
|
||||
|
||||
t.Run("with TorBinary and no such binary in PATH", func(t *testing.T) {
|
||||
expected := errors.New("no such binary in PATH")
|
||||
config := newConfig("tor-real", "", expected)
|
||||
verifyExpectations(t, config, "", expected)
|
||||
})
|
||||
|
||||
t.Run("with TorBinary and the binary is in PATH", func(t *testing.T) {
|
||||
expected := "/usr/bin/tor-real"
|
||||
config := newConfig("tor-real", expected, nil)
|
||||
verifyExpectations(t, config, expected, nil)
|
||||
})
|
||||
|
||||
t.Run("with OONI_TOR_BINARY and empty TorBinary", func(t *testing.T) {
|
||||
expected := "./tor.exe"
|
||||
os.Setenv(ooniTorBinaryEnv, expected)
|
||||
defer os.Unsetenv(ooniTorBinaryEnv)
|
||||
config := newConfig("", expected, errors.New("should not be seen"))
|
||||
verifyExpectations(t, config, expected, nil)
|
||||
})
|
||||
|
||||
t.Run("with OONI_TOR_BINARY and TorBinary not in PATH", func(t *testing.T) {
|
||||
expected := errors.New("no such binary in PATH")
|
||||
os.Setenv(ooniTorBinaryEnv, "./tor.exe")
|
||||
defer os.Unsetenv(ooniTorBinaryEnv)
|
||||
config := newConfig("tor-real", "", expected)
|
||||
verifyExpectations(t, config, "", expected)
|
||||
})
|
||||
|
||||
t.Run("with OONI_TOR_BINARY and TorBinary in PATH", func(t *testing.T) {
|
||||
expected := "/usr/bin/tor-real"
|
||||
os.Setenv(ooniTorBinaryEnv, "./tor.exe")
|
||||
defer os.Unsetenv(ooniTorBinaryEnv)
|
||||
config := newConfig("tor-real", expected, nil)
|
||||
verifyExpectations(t, config, expected, nil)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue