ooni-probe-cli/internal/tunnel/config_test.go
Simone Basso 85664f1e31
feat(torsf): collect tor logs, select rendezvous method, count bytes (#683)
This diff contains significant improvements over the previous
implementation of the torsf experiment.

We add support for configuring different rendezvous methods after
the convo at https://github.com/ooni/probe/issues/2004. In doing
that, I've tried to use a terminology that is consistent with the
names being actually used by tor developers.

In terms of what to do next, this diff basically instruments
torsf to always rendezvous using domain fronting. Yet, it's also
possible to change the rendezvous method from the command line,
when using miniooni, which allows to experiment a bit more. In the
same vein, by default we use a persistent tor datadir, but it's
also possible to use a temporary datadir using the cmdline.

Here's how a generic invocation of `torsf` looks like:

```bash
./miniooni -O DisablePersistentDatadir=true \
           -O RendezvousMethod=amp \
           -O DisableProgress=true \
           torsf
```

(The default is `DisablePersistentDatadir=false` and
`RendezvousMethod=domain_fronting`.)

With this implementation, we can start measuring whether snowflake
and tor together can boostrap, which seems the most important thing
to focus on at the beginning. Understanding why the bootstrap most
often does not converge with a temporary datadir on Android devices
remains instead an open problem for now. (I'll also update the
relevant issues or create new issues after commit this.)

We also address some methodology improvements that were proposed
in https://github.com/ooni/probe/issues/1686. Namely:

1. we record the tor version;

2. we include the bootstrap percentage by reading the logs;

3. we set the anomaly key correctly;

4. we measure the bytes send and received (by `tor` not by `snowflake`, since
doing it for snowflake seems more complex at this stage).

What remains to be done is the possibility of including Snowflake
events into the measurement, which is not possible until the new
improvements at common/event in snowflake.git are included into a
tagged version of snowflake itself. (I'll make sure to mention
this aspect to @cohosh in https://github.com/ooni/probe/issues/2004.)
2022-02-07 17:05:36 +01:00

125 lines
3.5 KiB
Go

package tunnel
import (
"errors"
"os"
"testing"
"github.com/apex/log"
"github.com/cretz/bine/tor"
"github.com/ooni/probe-cli/v3/internal/model"
)
func TestConfigLoggerDefault(t *testing.T) {
config := &Config{}
if config.logger() != model.DiscardLogger {
t.Fatal("not the logger we expected")
}
}
func TestConfigLoggerCustom(t *testing.T) {
config := &Config{Logger: log.Log}
if config.logger() != log.Log {
t.Fatal("not the logger 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
},
}
}
// 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)
})
}
func TestConfigTorProtocolInfo(t *testing.T) {
t.Run("with nil Control field", func(t *testing.T) {
config := &Config{}
protocolInfo, err := config.torProtocolInfo(&tor.Tor{})
if !errors.Is(err, errNoTorControl) {
t.Fatal("unexpected error", err)
}
if protocolInfo != nil {
t.Fatal("expected nil protocol info")
}
})
}