feat: create tunnel inside NewSession (#286)
* feat: create tunnel inside NewSession We want to create the tunnel when we create the session. This change allows us to nicely ignore the problem of creating a tunnel when we already have a proxy, as well as the problem of locking. Everything is happening, in fact, inside of the NewSession factory. Modify miniooni such that --tunnel is just syntactic sugar for --proxy, at least for now. We want, in the future, to teach the tunnel to possibly use a socks5 proxy. Because starting a tunnel is a slow operation, we need a context in NewSession. This causes a bunch of places to change. Not really a big deal except we need to propagate the changes. Make sure that the mobile code can create a new session using a proxy for all the APIs we support. Make sure all tests are still green and we don't loose coverage of the various ways in which this code could be used. This change is part of https://github.com/ooni/probe/issues/985. * changes after merge * fix: only keep tests that can hopefully work While there, identify other places where we should add more tests or fix integration tests. Part of https://github.com/ooni/probe/issues/985
This commit is contained in:
parent
a849213b59
commit
c5ad5eedeb
18 changed files with 135 additions and 223 deletions
|
|
@ -75,7 +75,7 @@ func TestNewSessionBuilderGood(t *testing.T) {
|
|||
}
|
||||
|
||||
func newSessionMustFail(t *testing.T, config SessionConfig) {
|
||||
sess, err := NewSession(config)
|
||||
sess, err := NewSession(context.Background(), config)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error here")
|
||||
}
|
||||
|
|
@ -88,7 +88,7 @@ func TestSessionTorArgsTorBinary(t *testing.T) {
|
|||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess, err := NewSession(SessionConfig{
|
||||
sess, err := NewSession(context.Background(), SessionConfig{
|
||||
AvailableProbeServices: []model.Service{{
|
||||
Address: "https://ams-pg-test.ooni.org",
|
||||
Type: "https",
|
||||
|
|
@ -120,7 +120,7 @@ func TestSessionTorArgsTorBinary(t *testing.T) {
|
|||
}
|
||||
|
||||
func newSessionForTestingNoLookupsWithProxyURL(t *testing.T, URL *url.URL) *Session {
|
||||
sess, err := NewSession(SessionConfig{
|
||||
sess, err := NewSession(context.Background(), SessionConfig{
|
||||
AvailableProbeServices: []model.Service{{
|
||||
Address: "https://ams-pg-test.ooni.org",
|
||||
Type: "https",
|
||||
|
|
@ -336,7 +336,7 @@ func TestGetAvailableProbeServices(t *testing.T) {
|
|||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess, err := NewSession(SessionConfig{
|
||||
sess, err := NewSession(context.Background(), SessionConfig{
|
||||
Logger: model.DiscardLogger,
|
||||
SoftwareName: "ooniprobe-engine",
|
||||
SoftwareVersion: "0.0.1",
|
||||
|
|
@ -356,7 +356,7 @@ func TestMaybeLookupBackendsFailure(t *testing.T) {
|
|||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess, err := NewSession(SessionConfig{
|
||||
sess, err := NewSession(context.Background(), SessionConfig{
|
||||
Logger: model.DiscardLogger,
|
||||
SoftwareName: "ooniprobe-engine",
|
||||
SoftwareVersion: "0.0.1",
|
||||
|
|
@ -377,7 +377,7 @@ func TestMaybeLookupTestHelpersIdempotent(t *testing.T) {
|
|||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess, err := NewSession(SessionConfig{
|
||||
sess, err := NewSession(context.Background(), SessionConfig{
|
||||
Logger: model.DiscardLogger,
|
||||
SoftwareName: "ooniprobe-engine",
|
||||
SoftwareVersion: "0.0.1",
|
||||
|
|
@ -402,7 +402,7 @@ func TestAllProbeServicesUnsupported(t *testing.T) {
|
|||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess, err := NewSession(SessionConfig{
|
||||
sess, err := NewSession(context.Background(), SessionConfig{
|
||||
Logger: model.DiscardLogger,
|
||||
SoftwareName: "ooniprobe-engine",
|
||||
SoftwareVersion: "0.0.1",
|
||||
|
|
@ -421,127 +421,8 @@ func TestAllProbeServicesUnsupported(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestStartTunnelGood(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess := newSessionForTestingNoLookups(t)
|
||||
defer sess.Close()
|
||||
ctx := context.Background()
|
||||
if err := sess.MaybeStartTunnel(ctx, "psiphon"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := sess.MaybeStartTunnel(ctx, "psiphon"); err != nil {
|
||||
t.Fatal(err) // check twice, must be idempotent
|
||||
}
|
||||
if sess.ProxyURL() == nil {
|
||||
t.Fatal("expected non-nil ProxyURL")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartTunnelNonexistent(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess := newSessionForTestingNoLookups(t)
|
||||
defer sess.Close()
|
||||
ctx := context.Background()
|
||||
if err := sess.MaybeStartTunnel(ctx, "antani"); err.Error() != "unsupported tunnel" {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if sess.ProxyURL() != nil {
|
||||
t.Fatal("expected nil ProxyURL")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartTunnelEmptyString(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess := newSessionForTestingNoLookups(t)
|
||||
defer sess.Close()
|
||||
ctx := context.Background()
|
||||
if sess.MaybeStartTunnel(ctx, "") != nil {
|
||||
t.Fatal("expected no error here")
|
||||
}
|
||||
if sess.ProxyURL() != nil {
|
||||
t.Fatal("expected nil ProxyURL")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartTunnelEmptyStringWithProxy(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
proxyURL := &url.URL{Scheme: "socks5", Host: "127.0.0.1:9050"}
|
||||
sess := newSessionForTestingNoLookups(t)
|
||||
sess.proxyURL = proxyURL
|
||||
defer sess.Close()
|
||||
ctx := context.Background()
|
||||
if sess.MaybeStartTunnel(ctx, "") != nil {
|
||||
t.Fatal("expected no error here")
|
||||
}
|
||||
diff := cmp.Diff(proxyURL, sess.ProxyURL())
|
||||
if diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartTunnelWithAlreadyExistingTunnel(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess := newSessionForTestingNoLookups(t)
|
||||
defer sess.Close()
|
||||
ctx := context.Background()
|
||||
if sess.MaybeStartTunnel(ctx, "psiphon") != nil {
|
||||
t.Fatal("expected no error here")
|
||||
}
|
||||
prev := sess.ProxyURL()
|
||||
err := sess.MaybeStartTunnel(ctx, "tor")
|
||||
if !errors.Is(err, ErrAlreadyUsingProxy) {
|
||||
t.Fatal("expected another error here")
|
||||
}
|
||||
cur := sess.ProxyURL()
|
||||
diff := cmp.Diff(prev, cur)
|
||||
if diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartTunnelWithAlreadyExistingProxy(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess := newSessionForTestingNoLookups(t)
|
||||
defer sess.Close()
|
||||
ctx := context.Background()
|
||||
orig := &url.URL{Scheme: "socks5", Host: "[::1]:9050"}
|
||||
sess.proxyURL = orig
|
||||
err := sess.MaybeStartTunnel(ctx, "psiphon")
|
||||
if !errors.Is(err, ErrAlreadyUsingProxy) {
|
||||
t.Fatal("expected another error here")
|
||||
}
|
||||
cur := sess.ProxyURL()
|
||||
diff := cmp.Diff(orig, cur)
|
||||
if diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartTunnelCanceledContext(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
sess := newSessionForTestingNoLookups(t)
|
||||
defer sess.Close()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // immediately cancel
|
||||
err := sess.MaybeStartTunnel(ctx, "psiphon")
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
}
|
||||
// TODO(bassosimone): we should write unit/integration tests
|
||||
// for the new way in which tunnels work.
|
||||
|
||||
func TestUserAgentNoProxy(t *testing.T) {
|
||||
if testing.Short() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue