2021-02-02 12:05:47 +01:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
"context"
|
|
|
|
"errors"
|
2021-04-05 19:51:41 +02:00
|
|
|
"net/url"
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-05 19:51:41 +02:00
|
|
|
"github.com/apex/log"
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
func (s *Session) GetAvailableProbeServices() []model.OOAPIService {
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
return s.getAvailableProbeServicesUnlocked()
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-01-03 13:53:23 +01:00
|
|
|
func (s *Session) AppendAvailableProbeService(svc model.OOAPIService) {
|
2021-02-02 12:05:47 +01:00
|
|
|
s.availableProbeServices = append(s.availableProbeServices, svc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) QueryProbeServicesCount() int64 {
|
|
|
|
return s.queryProbeServicesCount.Load()
|
|
|
|
}
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
|
|
|
|
// mockableProbeServicesClientForCheckIn allows us to mock the
|
|
|
|
// probeservices.Client used by Session.CheckIn.
|
|
|
|
type mockableProbeServicesClientForCheckIn struct {
|
|
|
|
// Config is the config passed to the call.
|
2022-01-03 13:53:23 +01:00
|
|
|
Config *model.OOAPICheckInConfig
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
|
|
|
|
// Results contains the results of the call. This field MUST be
|
|
|
|
// non-nil if and only if Error is nil.
|
2022-01-03 13:53:23 +01:00
|
|
|
Results *model.OOAPICheckInInfo
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
|
|
|
|
// Error indicates whether the call failed. This field MUST be
|
|
|
|
// non-nil if and only if Error is nil.
|
|
|
|
Error error
|
|
|
|
|
|
|
|
// mu provides mutual exclusion.
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckIn implements sessionProbeServicesClientForCheckIn.CheckIn.
|
|
|
|
func (c *mockableProbeServicesClientForCheckIn) CheckIn(
|
2022-01-03 13:53:23 +01:00
|
|
|
ctx context.Context, config model.OOAPICheckInConfig) (*model.OOAPICheckInInfo, error) {
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
defer c.mu.Unlock()
|
|
|
|
c.mu.Lock()
|
|
|
|
if c.Config != nil {
|
|
|
|
return nil, errors.New("called more than once")
|
|
|
|
}
|
|
|
|
c.Config = &config
|
|
|
|
if c.Results == nil && c.Error == nil {
|
|
|
|
return nil, errors.New("misconfigured mockableProbeServicesClientForCheckIn")
|
|
|
|
}
|
|
|
|
return c.Results, c.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionCheckInSuccessful(t *testing.T) {
|
2022-01-03 13:53:23 +01:00
|
|
|
results := &model.OOAPICheckInInfo{
|
|
|
|
WebConnectivity: &model.OOAPICheckInInfoWebConnectivity{
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
ReportID: "xxx-x-xx",
|
2022-01-03 13:53:23 +01:00
|
|
|
URLs: []model.OOAPIURLInfo{{
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
CategoryCode: "NEWS",
|
|
|
|
CountryCode: "IT",
|
|
|
|
URL: "https://www.repubblica.it/",
|
|
|
|
}, {
|
|
|
|
CategoryCode: "NEWS",
|
|
|
|
CountryCode: "IT",
|
|
|
|
URL: "https://www.unita.it/",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
mockedClnt := &mockableProbeServicesClientForCheckIn{
|
|
|
|
Results: results,
|
|
|
|
}
|
|
|
|
s := &Session{
|
|
|
|
location: &geolocate.Results{
|
|
|
|
ASN: 137,
|
|
|
|
CountryCode: "IT",
|
|
|
|
},
|
|
|
|
softwareName: "miniooni",
|
|
|
|
softwareVersion: "0.1.0-dev",
|
|
|
|
testMaybeLookupLocationContext: func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
testNewProbeServicesClientForCheckIn: func(
|
|
|
|
ctx context.Context) (sessionProbeServicesClientForCheckIn, error) {
|
|
|
|
return mockedClnt, nil
|
|
|
|
},
|
|
|
|
}
|
2022-01-03 13:53:23 +01:00
|
|
|
out, err := s.CheckIn(context.Background(), &model.OOAPICheckInConfig{})
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(results, out); diff != "" {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
if mockedClnt.Config.Platform != s.Platform() {
|
|
|
|
t.Fatal("invalid Config.Platform")
|
|
|
|
}
|
|
|
|
if mockedClnt.Config.ProbeASN != "AS137" {
|
|
|
|
t.Fatal("invalid Config.ProbeASN")
|
|
|
|
}
|
|
|
|
if mockedClnt.Config.ProbeCC != "IT" {
|
|
|
|
t.Fatal("invalid Config.ProbeCC")
|
|
|
|
}
|
2022-04-29 13:41:09 +02:00
|
|
|
if mockedClnt.Config.RunType != model.RunTypeTimed {
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
t.Fatal("invalid Config.RunType")
|
|
|
|
}
|
|
|
|
if mockedClnt.Config.SoftwareName != "miniooni" {
|
|
|
|
t.Fatal("invalid Config.SoftwareName")
|
|
|
|
}
|
|
|
|
if mockedClnt.Config.SoftwareVersion != "0.1.0-dev" {
|
|
|
|
t.Fatal("invalid Config.SoftwareVersion")
|
|
|
|
}
|
|
|
|
if mockedClnt.Config.WebConnectivity.CategoryCodes == nil {
|
|
|
|
t.Fatal("invalid ...CategoryCodes")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionCheckInCannotLookupLocation(t *testing.T) {
|
|
|
|
errMocked := errors.New("mocked error")
|
|
|
|
s := &Session{
|
|
|
|
testMaybeLookupLocationContext: func(ctx context.Context) error {
|
|
|
|
return errMocked
|
|
|
|
},
|
|
|
|
}
|
2022-01-03 13:53:23 +01:00
|
|
|
out, err := s.CheckIn(context.Background(), &model.OOAPICheckInConfig{})
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
if !errors.Is(err, errMocked) {
|
|
|
|
t.Fatal("no the error we expected", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("expected nil result here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionCheckInCannotCreateProbeServicesClient(t *testing.T) {
|
|
|
|
errMocked := errors.New("mocked error")
|
|
|
|
s := &Session{
|
|
|
|
location: &geolocate.Results{
|
|
|
|
ASN: 137,
|
|
|
|
CountryCode: "IT",
|
|
|
|
},
|
|
|
|
softwareName: "miniooni",
|
|
|
|
softwareVersion: "0.1.0-dev",
|
|
|
|
testMaybeLookupLocationContext: func(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
testNewProbeServicesClientForCheckIn: func(
|
|
|
|
ctx context.Context) (sessionProbeServicesClientForCheckIn, error) {
|
|
|
|
return nil, errMocked
|
|
|
|
},
|
|
|
|
}
|
2022-01-03 13:53:23 +01:00
|
|
|
out, err := s.CheckIn(context.Background(), &model.OOAPICheckInConfig{})
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
if !errors.Is(err, errMocked) {
|
|
|
|
t.Fatal("no the error we expected", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatal("expected nil result here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLowercaseMaybeLookupLocationContextWithCancelledContext(t *testing.T) {
|
|
|
|
s := &Session{}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // immediately kill the context
|
|
|
|
err := s.maybeLookupLocationContext(ctx)
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewProbeServicesClientForCheckIn(t *testing.T) {
|
|
|
|
s := &Session{}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // immediately kill the context
|
|
|
|
clnt, err := s.newProbeServicesClientForCheckIn(ctx)
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if clnt != nil {
|
|
|
|
t.Fatal("expected nil client here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionNewSubmitterWithCancelledContext(t *testing.T) {
|
|
|
|
sess := newSessionForTesting(t)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // fail immediately
|
|
|
|
subm, err := sess.NewSubmitter(ctx)
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if subm != nil {
|
|
|
|
t.Fatal("expected nil submitter here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionMaybeLookupLocationContextLookupLocationContextFailure(t *testing.T) {
|
|
|
|
errMocked := errors.New("mocked error")
|
|
|
|
sess := newSessionForTestingNoLookups(t)
|
|
|
|
sess.testLookupLocationContext = func(ctx context.Context) (*geolocate.Results, error) {
|
|
|
|
return nil, errMocked
|
|
|
|
}
|
|
|
|
err := sess.MaybeLookupLocationContext(context.Background())
|
|
|
|
if !errors.Is(err, errMocked) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
}
|
2021-04-02 12:03:18 +02:00
|
|
|
|
|
|
|
func TestSessionFetchURLListWithCancelledContext(t *testing.T) {
|
|
|
|
sess := &Session{}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // cause failure
|
2022-01-03 13:53:23 +01:00
|
|
|
resp, err := sess.FetchURLList(ctx, model.OOAPIURLListConfig{})
|
2021-04-02 12:03:18 +02:00
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil response here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionFetchTorTargetsWithCancelledContext(t *testing.T) {
|
|
|
|
sess := &Session{}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // cause failure
|
|
|
|
resp, err := sess.FetchTorTargets(ctx, "IT")
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil response here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSessionFetchPsiphonConfigWithCancelledContext(t *testing.T) {
|
|
|
|
sess := &Session{}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel() // cause failure
|
|
|
|
resp, err := sess.FetchPsiphonConfig(ctx)
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil response here")
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 19:51:41 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|