refactor: move tunnel pkg down one level (#358)
* refactor: move tunnel pkg down one level While there, reduce unnecessary dependency on external packages. * file I forgot to commit
This commit is contained in:
parent
39aec6677d
commit
3cb6c7c6fb
21 changed files with 69 additions and 65 deletions
99
internal/tunnel/psiphon_test.go
Normal file
99
internal/tunnel/psiphon_test.go
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
package tunnel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/psiphon/oopsi/github.com/Psiphon-Labs/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")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue