ooni-probe-cli/pkg/oonimkall/webconnectivity_test.go
Simone Basso 273b70bacc
refactor: interfaces and data types into the model package (#642)
## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885
- [x] related ooni/spec pull request: N/A

Location of the issue tracker: https://github.com/ooni/probe

## Description

This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package.

The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity.

An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other.

This is what I want to move in `internal/model`:

- [x] most important interfaces from `internal/netxlite`
- [x] everything that was previously part of `internal/engine/model`
- [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
2022-01-03 13:53:23 +01:00

178 lines
5.0 KiB
Go

package oonimkall
import (
"context"
"encoding/json"
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/model"
)
func TestWebConnectivityRunnerWithMaybeLookupBackendsFailure(t *testing.T) {
errMocked := errors.New("mocked error")
sess := &FakeExperimentSession{
LockCount: &atomicx.Int64{},
LookupBackendsErr: errMocked,
UnlockCount: &atomicx.Int64{},
}
runner := &webConnectivityRunner{sess: sess}
ctx := context.Background()
config := &WebConnectivityConfig{Input: "https://ooni.org"}
out, err := runner.run(ctx, config)
if !errors.Is(err, errMocked) {
t.Fatal("not the error we expected", err)
}
if out != nil {
t.Fatal("expected nil here")
}
if sess.LockCount.Load() != 1 || sess.UnlockCount.Load() != 1 {
t.Fatal("invalid locking pattern")
}
}
func TestWebConnectivityRunnerWithMaybeLookupLocationFailure(t *testing.T) {
errMocked := errors.New("mocked error")
sess := &FakeExperimentSession{
LockCount: &atomicx.Int64{},
LookupLocationErr: errMocked,
UnlockCount: &atomicx.Int64{},
}
runner := &webConnectivityRunner{sess: sess}
ctx := context.Background()
config := &WebConnectivityConfig{Input: "https://ooni.org"}
out, err := runner.run(ctx, config)
if !errors.Is(err, errMocked) {
t.Fatal("not the error we expected", err)
}
if out != nil {
t.Fatal("expected nil here")
}
if sess.LockCount.Load() != 1 || sess.UnlockCount.Load() != 1 {
t.Fatal("invalid locking pattern")
}
}
func TestWebConnectivityRunnerWithNewExperimentBuilderFailure(t *testing.T) {
errMocked := errors.New("mocked error")
sess := &FakeExperimentSession{
LockCount: &atomicx.Int64{},
NewExperimentBuilderErr: errMocked,
UnlockCount: &atomicx.Int64{},
}
runner := &webConnectivityRunner{sess: sess}
ctx := context.Background()
config := &WebConnectivityConfig{Input: "https://ooni.org"}
out, err := runner.run(ctx, config)
if !errors.Is(err, errMocked) {
t.Fatal("not the error we expected", err)
}
if out != nil {
t.Fatal("expected nil here")
}
if sess.LockCount.Load() != 1 || sess.UnlockCount.Load() != 1 {
t.Fatal("invalid locking pattern")
}
}
func TestWebConnectivityRunnerWithMeasureFailure(t *testing.T) {
errMocked := errors.New("mocked error")
cbs := &FakeExperimentCallbacks{}
e := &FakeExperiment{Err: errMocked}
eb := &FakeExperimentBuilder{Experiment: e}
sess := &FakeExperimentSession{
LockCount: &atomicx.Int64{},
ExperimentBuilder: eb,
UnlockCount: &atomicx.Int64{},
}
runner := &webConnectivityRunner{sess: sess}
ctx := context.Background()
config := &WebConnectivityConfig{
Callbacks: cbs,
Input: "https://ooni.org",
}
out, err := runner.run(ctx, config)
if !errors.Is(err, errMocked) {
t.Fatal("not the error we expected", err)
}
if out != nil {
t.Fatal("expected nil here")
}
if sess.LockCount.Load() != 1 || sess.UnlockCount.Load() != 1 {
t.Fatal("invalid locking pattern")
}
if eb.Callbacks != cbs {
t.Fatal("unexpected callbacks")
}
}
func TestWebConnectivityRunnerWithNoError(t *testing.T) {
// We create a measurement with non default fields. One of them is
// enough to check that we are getting in output the non default
// data structure that was preconfigured in the mocks.
m := &model.Measurement{Input: "https://ooni.org"}
cbs := &FakeExperimentCallbacks{}
e := &FakeExperiment{Measurement: m, Sent: 10, Received: 128}
eb := &FakeExperimentBuilder{Experiment: e}
sess := &FakeExperimentSession{
LockCount: &atomicx.Int64{},
ExperimentBuilder: eb,
UnlockCount: &atomicx.Int64{},
}
runner := &webConnectivityRunner{sess: sess}
ctx := context.Background()
config := &WebConnectivityConfig{
Callbacks: cbs,
Input: "https://ooni.org",
}
out, err := runner.run(ctx, config)
if err != nil {
t.Fatal(err)
}
if out == nil {
t.Fatal("expected non-nil here")
}
if sess.LockCount.Load() != 1 || sess.UnlockCount.Load() != 1 {
t.Fatal("invalid locking pattern")
}
if eb.Callbacks != cbs {
t.Fatal("unexpected callbacks")
}
if out.KibiBytesSent != 10 || out.KibiBytesReceived != 128 {
t.Fatal("invalid bytes sent or received")
}
var mm *model.Measurement
mdata := []byte(out.Measurement)
if err := json.Unmarshal(mdata, &mm); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(m, mm); diff != "" {
t.Fatal(diff)
}
}
func TestWebConnectivityRunWithCancelledContext(t *testing.T) {
sess, err := NewSession(&SessionConfig{
AssetsDir: "../testdata/oonimkall/assets",
ProbeServicesURL: "https://ams-pg-test.ooni.org/",
SoftwareName: "oonimkall-test",
SoftwareVersion: "0.1.0",
StateDir: "../testdata/oonimkall/state",
TempDir: "../testdata/",
})
if err != nil {
t.Fatal(err)
}
ctx := sess.NewContext()
ctx.Cancel() // kill it immediately
out, err := sess.WebConnectivity(ctx, &WebConnectivityConfig{})
if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected", err)
}
if out != nil {
t.Fatal("expected nil output here")
}
}