8fe4e5410d
* feat(tunnel): introduce persistent tunnel state dir This diff introduces a persistent state directory for tunnels, so that we can bootstrap them more quickly after the first time. Part of https://github.com/ooni/probe/issues/985 * fix: make tunnel dir optional We have many tests where it does not make sense to explicitly provide a tunnel dir because we're not using tunnels. This should simplify setting up a session. * fix(tunnel): repair tests * final changes * more cleanups
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package tunnel
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
|
|
"github.com/ooni/psiphon/oopsi/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary/clientlib"
|
|
)
|
|
|
|
func TestPsiphonFetchPsiphonConfigFailure(t *testing.T) {
|
|
expected := errors.New("mocked error")
|
|
sess := &mockable.Session{
|
|
MockableFetchPsiphonConfigErr: expected,
|
|
}
|
|
tunnel, err := psiphonStart(context.Background(), &Config{
|
|
Session: sess,
|
|
TunnelDir: "testdata",
|
|
})
|
|
if !errors.Is(err, expected) {
|
|
t.Fatal("not the error we expected")
|
|
}
|
|
if tunnel != nil {
|
|
t.Fatal("expected nil tunnel here")
|
|
}
|
|
}
|
|
|
|
func TestPsiphonMkdirAllFailure(t *testing.T) {
|
|
expected := errors.New("mocked error")
|
|
sess := &mockable.Session{
|
|
MockableFetchPsiphonConfigResult: []byte(`{}`),
|
|
}
|
|
tunnel, err := psiphonStart(context.Background(), &Config{
|
|
Session: sess,
|
|
TunnelDir: "testdata",
|
|
testMkdirAll: func(path string, perm os.FileMode) error {
|
|
return expected
|
|
},
|
|
})
|
|
if !errors.Is(err, expected) {
|
|
t.Fatal("not the error we expected")
|
|
}
|
|
if tunnel != nil {
|
|
t.Fatal("expected nil tunnel here")
|
|
}
|
|
}
|
|
|
|
func TestPsiphonStartFailure(t *testing.T) {
|
|
expected := errors.New("mocked error")
|
|
sess := &mockable.Session{
|
|
MockableFetchPsiphonConfigResult: []byte(`{}`),
|
|
}
|
|
tunnel, err := psiphonStart(context.Background(), &Config{
|
|
Session: sess,
|
|
TunnelDir: "testdata",
|
|
testStartPsiphon: func(ctx context.Context, config []byte,
|
|
workdir string) (*clientlib.PsiphonTunnel, error) {
|
|
return nil, expected
|
|
},
|
|
})
|
|
if !errors.Is(err, expected) {
|
|
t.Fatal("not the error we expected")
|
|
}
|
|
if tunnel != nil {
|
|
t.Fatal("expected nil tunnel here")
|
|
}
|
|
}
|
|
|
|
func TestPsiphonNilTunnel(t *testing.T) {
|
|
var tunnel *psiphonTunnel
|
|
if tunnel.BootstrapTime() != 0 {
|
|
t.Fatal("expected zero bootstrap time")
|
|
}
|
|
if tunnel.SOCKS5ProxyURL() != nil {
|
|
t.Fatal("expected nil SOCKS Proxy URL")
|
|
}
|
|
tunnel.Stop() // must not crash
|
|
}
|