fix(session): make sure tunnel code is tested (#301)
* fix(session): make sure tunnel code is tested Part of https://github.com/ooni/probe/issues/985 * fix: add missing TunnelDir for correctness
This commit is contained in:
parent
8b92037ae3
commit
7172e750dd
|
@ -421,9 +421,6 @@ func TestAllProbeServicesUnsupported(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO(bassosimone): we should write unit/integration tests
|
||||
// for the new way in which tunnels work.
|
||||
|
||||
func TestUserAgentNoProxy(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
|
|
|
@ -3,9 +3,11 @@ package engine
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
|
@ -247,3 +249,45 @@ func TestSessionFetchPsiphonConfigWithCancelledContext(t *testing.T) {
|
|||
t.Fatal("expected nil response here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewSessionWithFakeTunnel(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
sess, err := NewSession(ctx, SessionConfig{
|
||||
Logger: log.Log,
|
||||
ProxyURL: &url.URL{Scheme: "fake"},
|
||||
SoftwareName: "miniooni",
|
||||
SoftwareVersion: "0.1.0-dev",
|
||||
TunnelDir: "testdata",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sess == nil {
|
||||
t.Fatal("expected non-nil session here")
|
||||
}
|
||||
if sess.ProxyURL() == nil {
|
||||
t.Fatal("expected non-nil proxyURL here")
|
||||
}
|
||||
if sess.tunnel == nil {
|
||||
t.Fatal("expected non-nil tunnel here")
|
||||
}
|
||||
sess.Close() // ensure we don't crash
|
||||
}
|
||||
|
||||
func TestNewSessionWithFakeTunnelAndCancelledContext(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // fail immediately
|
||||
sess, err := NewSession(ctx, SessionConfig{
|
||||
Logger: log.Log,
|
||||
ProxyURL: &url.URL{Scheme: "fake"},
|
||||
SoftwareName: "miniooni",
|
||||
SoftwareVersion: "0.1.0-dev",
|
||||
TunnelDir: "testdata",
|
||||
})
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if sess != nil {
|
||||
t.Fatal("expected nil session here")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,11 @@ func (s *Session) FetchPsiphonConfig(ctx context.Context) ([]byte, error) {
|
|||
// to tunnel.Start to fetch the Psiphon configuration.
|
||||
type sessionTunnelEarlySession struct{}
|
||||
|
||||
// errPsiphonNoEmbeddedConfig indicates that there is no
|
||||
// embedded psiphong config file in this binary.
|
||||
var errPsiphonNoEmbeddedConfig = errors.New("no embedded configuration file")
|
||||
|
||||
// FetchPsiphonConfig implements tunnel.Session.FetchPsiphonConfig.
|
||||
func (s *sessionTunnelEarlySession) FetchPsiphonConfig(ctx context.Context) ([]byte, error) {
|
||||
return nil, errors.New("no embedded configuration file")
|
||||
return nil, errPsiphonNoEmbeddedConfig
|
||||
}
|
||||
|
|
20
internal/engine/session_nopsiphon_test.go
Normal file
20
internal/engine/session_nopsiphon_test.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
// +build !ooni_psiphon_config
|
||||
|
||||
package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEarlySessionNoPsiphonFetchPsiphonConfig(t *testing.T) {
|
||||
s := &sessionTunnelEarlySession{}
|
||||
out, err := s.FetchPsiphonConfig(context.Background())
|
||||
if !errors.Is(err, errPsiphonNoEmbeddedConfig) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if out != nil {
|
||||
t.Fatal("expected nil here")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user