ooni-probe-cli/internal/tunnel/psiphon_test.go
Simone Basso 97d2b5a0e3
chore: upgrade psiphon and go-cmp (#669)
I have experimented with a new approach for embedding psiphon in
7fc0bcd97c.

It seems the build is still building and the experiment is still
running. With the new approach, we're now vendoring less dependencies,
which hopefully puts us in the right track to, one day, import
Psiphon as a normal Go dependency.

I'll make sure to report to the Psiphon team what is currently
preventing us from importing their ClientLibrary directly.

This work is part of https://github.com/ooni/probe/issues/1894.

As part of running the update, I run `go get -u -v ./...`, which
led to go-cmp also being updated in the process.
2022-01-21 11:54:48 +01:00

100 lines
2.2 KiB
Go

package tunnel
import (
"context"
"errors"
"os"
"testing"
"github.com/ooni/psiphon/tunnel-core/ClientLibrary/clientlib"
)
func TestPsiphonWithCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // immediately fail
sess := &MockableSession{}
tunnel, err := psiphonStart(ctx, &Config{
Session: sess,
TunnelDir: "testdata",
})
if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestPsiphonWithEmptyTunnelDir(t *testing.T) {
ctx := context.Background()
sess := &MockableSession{}
tunnel, err := psiphonStart(ctx, &Config{
Session: sess,
TunnelDir: "",
})
if !errors.Is(err, ErrEmptyTunnelDir) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestPsiphonFetchPsiphonConfigFailure(t *testing.T) {
expected := errors.New("mocked error")
sess := &MockableSession{
Err: 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 := &MockableSession{
Result: []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 := &MockableSession{
Result: []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")
}
}