refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
# Package github.com/ooni/probe-engine/atomicx
|
||||
|
||||
Atomic int64/float64 that works also on 32 bit platforms.
|
||||
@@ -1,68 +0,0 @@
|
||||
// Package atomicx contains atomic int64/float64 that work also on 32 bit
|
||||
// platforms. The main reason for rolling out this package is to avoid potential
|
||||
// crashes when using 32 bit devices where we are atomically accessing a 64 bit
|
||||
// variable that is not aligned. The solution to this issue is rather crude: use
|
||||
// a normal variable and protect it using a normal mutex. While this could be
|
||||
// disappointing in general, it seems fine to be done in our context where
|
||||
// we mainly use atomic semantics for counting.
|
||||
package atomicx
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Int64 is an int64 with atomic semantics.
|
||||
type Int64 struct {
|
||||
mu sync.Mutex
|
||||
v int64
|
||||
}
|
||||
|
||||
// NewInt64 creates a new int64 with atomic semantics.
|
||||
func NewInt64() *Int64 {
|
||||
return new(Int64)
|
||||
}
|
||||
|
||||
// Add behaves like atomic.AddInt64
|
||||
func (i64 *Int64) Add(delta int64) (newvalue int64) {
|
||||
i64.mu.Lock()
|
||||
i64.v += delta
|
||||
newvalue = i64.v
|
||||
i64.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Load behaves like atomic.LoadInt64
|
||||
func (i64 *Int64) Load() (v int64) {
|
||||
i64.mu.Lock()
|
||||
v = i64.v
|
||||
i64.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Float64 is an float64 with atomic semantics.
|
||||
type Float64 struct {
|
||||
mu sync.Mutex
|
||||
v float64
|
||||
}
|
||||
|
||||
// NewFloat64 creates a new float64 with atomic semantics.
|
||||
func NewFloat64() *Float64 {
|
||||
return new(Float64)
|
||||
}
|
||||
|
||||
// Add behaves like AtomicInt64.Add but for float64
|
||||
func (f64 *Float64) Add(delta float64) (newvalue float64) {
|
||||
f64.mu.Lock()
|
||||
f64.v += delta
|
||||
newvalue = f64.v
|
||||
f64.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Load behaves like LoadInt64.Load buf for float64
|
||||
func (f64 *Float64) Load() (v float64) {
|
||||
f64.mu.Lock()
|
||||
v = f64.v
|
||||
f64.mu.Unlock()
|
||||
return
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package atomicx_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
)
|
||||
|
||||
func TestInt64(t *testing.T) {
|
||||
// TODO(bassosimone): how to write tests with race conditions
|
||||
// and be confident that they're WAI? Here I hope this test is
|
||||
// run with `-race` and I'm doing something that AFAICT will
|
||||
// be flagged as race if we were not be using mutexes.
|
||||
v := atomicx.NewInt64()
|
||||
go func() {
|
||||
v.Add(17)
|
||||
}()
|
||||
go func() {
|
||||
v.Add(14)
|
||||
}()
|
||||
time.Sleep(1 * time.Second)
|
||||
if v.Add(3) != 34 {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if v.Load() != 34 {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloat64(t *testing.T) {
|
||||
// TODO(bassosimone): how to write tests with race conditions
|
||||
// and be confident that they're WAI? Here I hope this test is
|
||||
// run with `-race` and I'm doing something that AFAICT will
|
||||
// be flagged as race if we were not be using mutexes.
|
||||
v := atomicx.NewFloat64()
|
||||
go func() {
|
||||
v.Add(17.0)
|
||||
}()
|
||||
go func() {
|
||||
v.Add(14.0)
|
||||
}()
|
||||
time.Sleep(1 * time.Second)
|
||||
if r := v.Add(3); r < 33.9 && r > 34.1 {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if v.Load() < 33.9 && v.Load() > 34.1 {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/platform"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
|
||||
@@ -168,7 +167,7 @@ func (e *Experiment) newMeasurement(input string) *model.Measurement {
|
||||
}
|
||||
m.AddAnnotation("engine_name", "ooniprobe-engine")
|
||||
m.AddAnnotation("engine_version", version.Version)
|
||||
m.AddAnnotation("platform", platform.Name())
|
||||
m.AddAnnotation("platform", e.session.Platform())
|
||||
return m
|
||||
}
|
||||
|
||||
|
||||
@@ -15,11 +15,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/montanaflynn/stats"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/humanizex"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
||||
"github.com/ooni/probe-cli/v3/internal/humanize"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -181,7 +181,7 @@ func (r runner) measure(
|
||||
total += current.Received
|
||||
avgspeed := 8 * float64(total) / time.Now().Sub(begin).Seconds()
|
||||
percentage := float64(current.Iteration) / float64(numIterations)
|
||||
message := fmt.Sprintf("streaming: speed: %s", humanizex.SI(avgspeed, "bit/s"))
|
||||
message := fmt.Sprintf("streaming: speed: %s", humanize.SI(avgspeed, "bit/s"))
|
||||
r.callbacks.OnProgress(percentage, message)
|
||||
current.Iteration++
|
||||
speed := float64(current.Received) / float64(current.Elapsed)
|
||||
|
||||
@@ -11,15 +11,15 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -31,7 +31,7 @@ const (
|
||||
// Endpoints keeps track of repeatedly measured endpoints.
|
||||
type Endpoints struct {
|
||||
WaitTime time.Duration
|
||||
count uint32
|
||||
count *atomicx.Int64
|
||||
nextVisit map[string]time.Time
|
||||
mu sync.Mutex
|
||||
}
|
||||
@@ -48,7 +48,10 @@ func (e *Endpoints) maybeSleep(resolverURL string, logger model.Logger) {
|
||||
return
|
||||
}
|
||||
sleepTime := nextTime.Sub(now)
|
||||
atomic.AddUint32(&e.count, 1)
|
||||
if e.count == nil {
|
||||
e.count = &atomicx.Int64{}
|
||||
}
|
||||
e.count.Add(1)
|
||||
logger.Infof("waiting %v before testing %s again", sleepTime, resolverURL)
|
||||
time.Sleep(sleepTime)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -221,7 +220,7 @@ func TestDNSCheckWait(t *testing.T) {
|
||||
}
|
||||
run("dot://one.one.one.one")
|
||||
run("dot://1dot1dot1dot1.cloudflare-dns.com")
|
||||
if atomic.LoadUint32(&endpoints.count) < 1 {
|
||||
if endpoints.count.Load() < 1 {
|
||||
t.Fatal("did not sleep")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/randx"
|
||||
"github.com/ooni/probe-cli/v3/internal/randx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/randx"
|
||||
"github.com/ooni/probe-cli/v3/internal/randx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
|
||||
|
||||
@@ -11,10 +11,10 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/humanizex"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/mlablocatev2"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/humanize"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -127,7 +127,7 @@ func (m *Measurer) doDownload(
|
||||
// 50% of the whole experiment, hence the `/2.0`.
|
||||
percentage := elapsed / paramMaxRuntimeUpperBound / 2.0
|
||||
speed := float64(count) * 8.0 / elapsed
|
||||
message := fmt.Sprintf(" download: speed %s", humanizex.SI(
|
||||
message := fmt.Sprintf(" download: speed %s", humanize.SI(
|
||||
float64(speed), "bit/s"))
|
||||
tk.Summary.Download = speed / 1e03 /* bit/s => kbit/s */
|
||||
callbacks.OnProgress(percentage, message)
|
||||
@@ -197,7 +197,7 @@ func (m *Measurer) doUpload(
|
||||
// the whole experiment, hence `0.5 +` and `/2.0`.
|
||||
percentage := 0.5 + elapsed/paramMaxRuntimeUpperBound/2.0
|
||||
speed := float64(count) * 8.0 / elapsed
|
||||
message := fmt.Sprintf(" upload: speed %s", humanizex.SI(
|
||||
message := fmt.Sprintf(" upload: speed %s", humanize.SI(
|
||||
float64(speed), "bit/s"))
|
||||
tk.Summary.Upload = speed / 1e03 /* bit/s => kbit/s */
|
||||
callbacks.OnProgress(percentage, message)
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/psiphon"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
|
||||
@@ -83,7 +83,7 @@ func TestRunWillPrintSomethingWithCancelledContext(t *testing.T) {
|
||||
time.Sleep(2 * time.Second)
|
||||
cancel() // fail after we've given the printer a chance to run
|
||||
}
|
||||
observer := observerCallbacks{progress: atomicx.NewInt64()}
|
||||
observer := observerCallbacks{progress: &atomicx.Int64{}}
|
||||
err := measurer.Run(ctx, newfakesession(), measurement, observer)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("expected another error here")
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/telegram"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
|
||||
@@ -271,9 +271,9 @@ func TestUpdateWebWithMixedResults(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWeConfigureWebChecksToFailOnHTTPError(t *testing.T) {
|
||||
called := atomicx.NewInt64()
|
||||
failOnErrorHTTPS := atomicx.NewInt64()
|
||||
failOnErrorHTTP := atomicx.NewInt64()
|
||||
called := &atomicx.Int64{}
|
||||
failOnErrorHTTPS := &atomicx.Int64{}
|
||||
failOnErrorHTTP := &atomicx.Int64{}
|
||||
measurer := telegram.Measurer{
|
||||
Config: telegram.Config{},
|
||||
Getter: func(ctx context.Context, g urlgetter.Getter) (urlgetter.TestKeys, error) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/tlstool/internal"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/randx"
|
||||
"github.com/ooni/probe-cli/v3/internal/randx"
|
||||
)
|
||||
|
||||
func TestSplitter84restSmall(t *testing.T) {
|
||||
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -12,14 +12,14 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netxlogger"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/oonidatamodel"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/oonitemplates"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -264,7 +264,7 @@ func newResultsCollector(
|
||||
) *resultsCollector {
|
||||
rc := &resultsCollector{
|
||||
callbacks: callbacks,
|
||||
completed: atomicx.NewInt64(),
|
||||
completed: &atomicx.Int64{},
|
||||
measurement: measurement,
|
||||
sess: sess,
|
||||
targetresults: make(map[string]TargetResults),
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
const httpRequestFailed = "http_request_failed"
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
|
||||
)
|
||||
@@ -235,7 +235,7 @@ func TestRunnerHTTPCannotReadBodyWinsOver400(t *testing.T) {
|
||||
|
||||
func TestRunnerWeCanForceUserAgent(t *testing.T) {
|
||||
expected := "antani/1.23.4-dev"
|
||||
found := atomicx.NewInt64()
|
||||
found := &atomicx.Int64{}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("User-Agent") == expected {
|
||||
found.Add(1)
|
||||
@@ -262,7 +262,7 @@ func TestRunnerWeCanForceUserAgent(t *testing.T) {
|
||||
|
||||
func TestRunnerDefaultUserAgent(t *testing.T) {
|
||||
expected := httpheader.UserAgent()
|
||||
found := atomicx.NewInt64()
|
||||
found := &atomicx.Int64{}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("User-Agent") == expected {
|
||||
found.Add(1)
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"net"
|
||||
"net/url"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
// EndpointInfo describes a TCP/TLS endpoint.
|
||||
|
||||
@@ -6,12 +6,12 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
|
||||
)
|
||||
|
||||
func TestNewEndpointPortPanicsWithInvalidScheme(t *testing.T) {
|
||||
counter := atomicx.NewInt64()
|
||||
counter := &atomicx.Int64{}
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@@ -30,7 +30,7 @@ func TestNewEndpointPortPanicsWithInvalidScheme(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewEndpointPortPanicsWithInvalidHost(t *testing.T) {
|
||||
counter := atomicx.NewInt64()
|
||||
counter := &atomicx.Int64{}
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/randx"
|
||||
"github.com/ooni/probe-cli/v3/internal/randx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
|
||||
)
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/httpfailure"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/experiment/whatsapp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/httpfailure"
|
||||
@@ -555,7 +555,7 @@ func TestTestKeysOnlyWebHTTPFailureTooManyURLs(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestWeConfigureWebChecksCorrectly(t *testing.T) {
|
||||
called := atomicx.NewInt64()
|
||||
called := &atomicx.Int64{}
|
||||
emptyConfig := urlgetter.Config{}
|
||||
configWithFailOnHTTPError := urlgetter.Config{FailOnHTTPError: true}
|
||||
configWithNoFollowRedirects := urlgetter.Config{NoFollowRedirects: true}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/version"
|
||||
)
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/multierror"
|
||||
"github.com/ooni/probe-cli/v3/internal/multierror"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
// Package humanizex is like dustin/go-humanize
|
||||
package humanizex
|
||||
|
||||
import "fmt"
|
||||
|
||||
// SI is like dustin/go-humanize.SI
|
||||
func SI(value float64, unit string) string {
|
||||
value, prefix := reduce(value)
|
||||
return fmt.Sprintf("%3.0f %s%s", value, prefix, unit)
|
||||
}
|
||||
|
||||
func reduce(value float64) (float64, string) {
|
||||
if value < 1e03 {
|
||||
return value, " "
|
||||
}
|
||||
value /= 1e03
|
||||
if value < 1e03 {
|
||||
return value, "k"
|
||||
}
|
||||
value /= 1e03
|
||||
if value < 1e03 {
|
||||
return value, "M"
|
||||
}
|
||||
value /= 1e03
|
||||
return value, "G"
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package humanizex_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/humanizex"
|
||||
)
|
||||
|
||||
func TestGood(t *testing.T) {
|
||||
if humanizex.SI(128, "bit/s") != "128 bit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(1280, "bit/s") != " 1 kbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(12800, "bit/s") != " 13 kbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(128000, "bit/s") != "128 kbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(1280000, "bit/s") != " 1 Mbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(12800000, "bit/s") != " 13 Mbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(128000000, "bit/s") != "128 Mbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(1280000000, "bit/s") != " 1 Gbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
"io/fs"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/fsx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/fsx"
|
||||
)
|
||||
|
||||
// These errors are returned by the InputLoader.
|
||||
@@ -154,7 +154,7 @@ func (il *InputLoader) loadLocal() ([]model.URLInfo, error) {
|
||||
inputs = append(inputs, model.URLInfo{URL: input})
|
||||
}
|
||||
for _, filepath := range il.SourceFiles {
|
||||
extra, err := il.readfile(filepath, fsx.Open)
|
||||
extra, err := il.readfile(filepath, fsx.OpenFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import (
|
||||
|
||||
"github.com/apex/log"
|
||||
engine "github.com/ooni/probe-cli/v3/internal/engine"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/kvstore"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/kvstore"
|
||||
)
|
||||
|
||||
func TestInputLoaderInputOrQueryBackendWithNoInput(t *testing.T) {
|
||||
@@ -19,7 +19,7 @@ func TestInputLoaderInputOrQueryBackendWithNoInput(t *testing.T) {
|
||||
Address: "https://ams-pg-test.ooni.org/",
|
||||
Type: "https",
|
||||
}},
|
||||
KVStore: kvstore.NewMemoryKeyValueStore(),
|
||||
KVStore: &kvstore.Memory{},
|
||||
Logger: log.Log,
|
||||
SoftwareName: "miniooni",
|
||||
SoftwareVersion: "0.1.0-dev",
|
||||
|
||||
@@ -11,8 +11,8 @@ import (
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/kvstore"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/kvstore"
|
||||
)
|
||||
|
||||
func TestInputLoaderInputNoneWithStaticInputs(t *testing.T) {
|
||||
@@ -237,7 +237,7 @@ func TestInputLoaderInputOrQueryBackendWithInput(t *testing.T) {
|
||||
|
||||
func TestInputLoaderInputOrQueryBackendWithNoInputAndCancelledContext(t *testing.T) {
|
||||
sess, err := NewSession(context.Background(), SessionConfig{
|
||||
KVStore: kvstore.NewMemoryKeyValueStore(),
|
||||
KVStore: &kvstore.Memory{},
|
||||
Logger: log.Log,
|
||||
SoftwareName: "miniooni",
|
||||
SoftwareVersion: "0.1.0-dev",
|
||||
|
||||
@@ -2,7 +2,6 @@ package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
@@ -68,11 +67,6 @@ type InputProcessor struct {
|
||||
// Submitter is the code that will submit measurements
|
||||
// to the OONI collector.
|
||||
Submitter InputProcessorSubmitterWrapper
|
||||
|
||||
// terminatedByMaxRuntime is an internal atomic variabile
|
||||
// incremented when we're terminated by MaxRuntime. We
|
||||
// only use this variable when testing.
|
||||
terminatedByMaxRuntime int32
|
||||
}
|
||||
|
||||
// InputProcessorSaverWrapper is InputProcessor's
|
||||
@@ -129,29 +123,41 @@ func (ipsw inputProcessorSubmitterWrapper) Submit(
|
||||
// though is free to choose different policies by configuring
|
||||
// the Experiment, Submitter, and Saver fields properly.
|
||||
func (ip *InputProcessor) Run(ctx context.Context) error {
|
||||
_, err := ip.run(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
// These are the reasons why run could stop.
|
||||
const (
|
||||
stopNormal = (1 << iota)
|
||||
stopMaxRuntime
|
||||
)
|
||||
|
||||
// run is like Run but, in addition to returning an error, it
|
||||
// also returns the reason why we stopped.
|
||||
func (ip *InputProcessor) run(ctx context.Context) (int, error) {
|
||||
start := time.Now()
|
||||
for idx, url := range ip.Inputs {
|
||||
if ip.MaxRuntime > 0 && time.Since(start) > ip.MaxRuntime {
|
||||
atomic.AddInt32(&ip.terminatedByMaxRuntime, 1)
|
||||
return nil
|
||||
return stopMaxRuntime, nil
|
||||
}
|
||||
input := url.URL
|
||||
meas, err := ip.Experiment.MeasureWithContext(ctx, idx, input)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
meas.AddAnnotations(ip.Annotations)
|
||||
meas.Options = ip.Options
|
||||
err = ip.Submitter.Submit(ctx, idx, meas)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
// Note: must be after submission because submission modifies
|
||||
// the measurement to include the report ID.
|
||||
err = ip.Saver.SaveMeasurement(idx, meas)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return stopNormal, nil
|
||||
}
|
||||
|
||||
@@ -150,10 +150,11 @@ func TestInputProcessorGood(t *testing.T) {
|
||||
Submitter: NewInputProcessorSubmitterWrapper(submitter),
|
||||
}
|
||||
ctx := context.Background()
|
||||
if err := ip.Run(ctx); err != nil {
|
||||
reason, err := ip.run(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ip.terminatedByMaxRuntime > 0 {
|
||||
if reason != stopNormal {
|
||||
t.Fatal("terminated by max runtime!?")
|
||||
}
|
||||
if len(fipe.M) != 2 || len(saver.M) != 2 || len(submitter.M) != 2 {
|
||||
@@ -192,10 +193,11 @@ func TestInputProcessorMaxRuntime(t *testing.T) {
|
||||
Submitter: NewInputProcessorSubmitterWrapper(submitter),
|
||||
}
|
||||
ctx := context.Background()
|
||||
if err := ip.Run(ctx); err != nil {
|
||||
reason, err := ip.run(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ip.terminatedByMaxRuntime <= 0 {
|
||||
if reason != stopMaxRuntime {
|
||||
t.Fatal("not terminated by max runtime")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
// Package fsx contains file system extension
|
||||
package fsx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Open is a wrapper for os.Open that ensures that we're opening a file.
|
||||
func Open(pathname string) (fs.File, error) {
|
||||
return OpenWithFS(filesystem{}, pathname)
|
||||
}
|
||||
|
||||
// OpenWithFS is like Open but with explicit file system argument.
|
||||
func OpenWithFS(fs fs.FS, pathname string) (fs.File, error) {
|
||||
file, err := fs.Open(pathname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
file.Close()
|
||||
return nil, err
|
||||
}
|
||||
if info.IsDir() {
|
||||
file.Close()
|
||||
return nil, fmt.Errorf(
|
||||
"input path points to a directory: %w", syscall.EISDIR)
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
|
||||
// filesystem is a private implementation of fs.FS.
|
||||
type filesystem struct{}
|
||||
|
||||
// Open implements fs.FS.Open.
|
||||
func (filesystem) Open(pathname string) (fs.File, error) {
|
||||
return os.Open(pathname)
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
package fsx_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/fsx"
|
||||
)
|
||||
|
||||
var StateBaseDir = "./testdata/"
|
||||
|
||||
type FailingStatFS struct {
|
||||
CloseCount *int32
|
||||
}
|
||||
|
||||
type FailingStatFile struct {
|
||||
CloseCount *int32
|
||||
}
|
||||
|
||||
var errStatFailed = errors.New("stat failed")
|
||||
|
||||
func (FailingStatFile) Stat() (os.FileInfo, error) {
|
||||
return nil, errStatFailed
|
||||
}
|
||||
|
||||
func (f FailingStatFS) Open(pathname string) (fs.File, error) {
|
||||
return FailingStatFile(f), nil
|
||||
}
|
||||
|
||||
func (fs FailingStatFile) Close() error {
|
||||
if fs.CloseCount != nil {
|
||||
atomic.AddInt32(fs.CloseCount, 1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (FailingStatFile) Read([]byte) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func TestOpenWithFailingStat(t *testing.T) {
|
||||
var count int32
|
||||
_, err := fsx.OpenWithFS(FailingStatFS{CloseCount: &count}, StateBaseDir+"testfile.txt")
|
||||
if !errors.Is(err, errStatFailed) {
|
||||
t.Errorf("expected error with invalid FS: %+v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Error("expected counter to be equal to 1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenNonexistentFile(t *testing.T) {
|
||||
_, err := fsx.Open(StateBaseDir + "invalidtestfile.txt")
|
||||
if !errors.Is(err, syscall.ENOENT) {
|
||||
t.Errorf("not the error we expected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenDirectoryShouldFail(t *testing.T) {
|
||||
_, err := fsx.Open(StateBaseDir)
|
||||
if !errors.Is(err, syscall.EISDIR) {
|
||||
t.Fatalf("not the error we expected: %+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpeningExistingFileShouldWork(t *testing.T) {
|
||||
file, err := fsx.Open(StateBaseDir + "testfile.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
my test input
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/kvstore"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
|
||||
"github.com/ooni/probe-cli/v3/internal/kvstore"
|
||||
)
|
||||
|
||||
// Session allows to mock sessions.
|
||||
@@ -67,7 +67,7 @@ func (sess *Session) FetchURLList(
|
||||
|
||||
// KeyValueStore returns the configured key-value store.
|
||||
func (sess *Session) KeyValueStore() model.KeyValueStore {
|
||||
return kvstore.NewMemoryKeyValueStore()
|
||||
return &kvstore.Memory{}
|
||||
}
|
||||
|
||||
// Logger implements ExperimentSession.Logger
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
// Package multierror contains code to manage multiple errors.
|
||||
package multierror
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Union is the logical union of several errors. The Union will
|
||||
// appear to be the Root error, except that it will actually
|
||||
// be possible to look deeper and see specific sub errors that
|
||||
// occurred using errors.As and errors.Is.
|
||||
type Union struct {
|
||||
Children []error
|
||||
Root error
|
||||
}
|
||||
|
||||
// New creates a new Union error instance.
|
||||
func New(root error) *Union {
|
||||
return &Union{Root: root}
|
||||
}
|
||||
|
||||
// Unwrap returns the Root error of the Union error.
|
||||
func (err Union) Unwrap() error {
|
||||
return err.Root
|
||||
}
|
||||
|
||||
// Add adds the specified child error to the Union error.
|
||||
func (err *Union) Add(child error) {
|
||||
err.Children = append(err.Children, child)
|
||||
}
|
||||
|
||||
// AddWithPrefix adds the specified child error to the Union error
|
||||
// with the specified prefix before the child error.
|
||||
func (err *Union) AddWithPrefix(prefix string, child error) {
|
||||
err.Add(fmt.Errorf("%s: %w", prefix, child))
|
||||
}
|
||||
|
||||
// Is returns whether the Union error contains at least one child
|
||||
// error that is exactly the specified target error.
|
||||
func (err Union) Is(target error) bool {
|
||||
if errors.Is(err.Root, target) {
|
||||
return true
|
||||
}
|
||||
for _, c := range err.Children {
|
||||
if errors.Is(c, target) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Error returns a string representation of the Union error.
|
||||
func (err Union) Error() string {
|
||||
var sb strings.Builder
|
||||
sb.WriteString(err.Root.Error())
|
||||
sb.WriteString(": [")
|
||||
for _, c := range err.Children {
|
||||
sb.WriteString(" ")
|
||||
sb.WriteString(c.Error())
|
||||
sb.WriteString(";")
|
||||
}
|
||||
sb.WriteString("]")
|
||||
return sb.String()
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package multierror_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/multierror"
|
||||
)
|
||||
|
||||
func TestEmpty(t *testing.T) {
|
||||
root := errors.New("antani")
|
||||
var err error = multierror.New(root)
|
||||
if err.Error() != "antani: []" {
|
||||
t.Fatal("unexpected Error value")
|
||||
}
|
||||
if !errors.Is(err, root) {
|
||||
t.Fatal("error should be root")
|
||||
}
|
||||
if !errors.Is(errors.Unwrap(err), root) {
|
||||
t.Fatal("unwrapping did not return root")
|
||||
}
|
||||
if errors.Is(err, io.EOF) {
|
||||
t.Fatal("error should not be EOF")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonEmpty(t *testing.T) {
|
||||
root := errors.New("antani")
|
||||
container := multierror.New(root)
|
||||
container.AddWithPrefix("first operation failed", io.EOF)
|
||||
container.AddWithPrefix("second operation failed", context.Canceled)
|
||||
var err error = container
|
||||
expect := "antani: [ first operation failed: EOF; second operation failed: context canceled;]"
|
||||
if diff := cmp.Diff(err.Error(), expect); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
if !errors.Is(err, root) {
|
||||
t.Fatal("error should be root")
|
||||
}
|
||||
if !errors.Is(errors.Unwrap(err), root) {
|
||||
t.Fatal("unwrapping did not return root")
|
||||
}
|
||||
if !errors.Is(err, io.EOF) {
|
||||
t.Fatal("error should be EOF")
|
||||
}
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("error should be context.Canceled")
|
||||
}
|
||||
var as *multierror.Union
|
||||
if !errors.As(err, &as) {
|
||||
t.Fatal("cannot cast error to multierror.Union")
|
||||
}
|
||||
if !errors.Is(as.Root, root) {
|
||||
t.Fatal("unexpected root")
|
||||
}
|
||||
if len(as.Children) != 2 {
|
||||
t.Fatal("unexpected number of children")
|
||||
}
|
||||
}
|
||||
|
||||
type SpecificRootError struct {
|
||||
Value int
|
||||
}
|
||||
|
||||
func (sre SpecificRootError) Error() string {
|
||||
return fmt.Sprintf("%d", sre.Value)
|
||||
}
|
||||
|
||||
func TestAsWorksForRoot(t *testing.T) {
|
||||
const expected = 144
|
||||
var (
|
||||
err error = multierror.New(&SpecificRootError{Value: expected})
|
||||
sre *SpecificRootError
|
||||
)
|
||||
if !errors.As(err, &sre) {
|
||||
t.Fatal("cannot cast error to original type")
|
||||
}
|
||||
if sre.Value != expected {
|
||||
t.Fatal("unexpected sre.Value")
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
// Package platform returns the platform name. The name returned here
|
||||
// is compatible with the names returned by Measurement Kit.
|
||||
package platform
|
||||
|
||||
import "runtime"
|
||||
|
||||
// Name returns the platform name. The returned value is one of:
|
||||
//
|
||||
// 1. "android"
|
||||
// 2. "ios"
|
||||
// 3. "linux"
|
||||
// 5. "macos"
|
||||
// 4. "windows"
|
||||
// 5. "unknown"
|
||||
//
|
||||
// The android, ios, linux, macos, windows, and unknown strings are
|
||||
// also returned by Measurement Kit. As a known bug, the detection of
|
||||
// darwin-based systems relies on the architecture, when CGO support
|
||||
// has been disabled. In such case, the code will return "ios" when
|
||||
// using arm{,64} and "macos" when using x86{,_64}.
|
||||
func Name() string {
|
||||
if name := cgoname(); name != "unknown" {
|
||||
return name
|
||||
}
|
||||
return puregoname(runtime.GOOS, runtime.GOARCH)
|
||||
}
|
||||
|
||||
func puregoname(goos, goarch string) string {
|
||||
switch goos {
|
||||
case "android", "linux", "windows":
|
||||
return goos
|
||||
case "darwin":
|
||||
return detectDarwin(goarch)
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func detectDarwin(goarch string) string {
|
||||
switch goarch {
|
||||
case "386", "amd64":
|
||||
return "macos"
|
||||
case "arm", "arm64":
|
||||
return "ios"
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
// +build cgo
|
||||
|
||||
package platform
|
||||
|
||||
//
|
||||
// /* Guess the platform in which we are.
|
||||
//
|
||||
// See: <https://sourceforge.net/p/predef/wiki/OperatingSystems/>
|
||||
// <http://stackoverflow.com/a/18729350> */
|
||||
//
|
||||
//#if defined __ANDROID__
|
||||
//# define OONI_PLATFORM "android"
|
||||
//#elif defined __linux__
|
||||
//# define OONI_PLATFORM "linux"
|
||||
//#elif defined _WIN32
|
||||
//# define OONI_PLATFORM "windows"
|
||||
//#elif defined __APPLE__
|
||||
//# include <TargetConditionals.h>
|
||||
//# if TARGET_OS_IPHONE
|
||||
//# define OONI_PLATFORM "ios"
|
||||
//# else
|
||||
//# define OONI_PLATFORM "macos"
|
||||
//# endif
|
||||
//#else
|
||||
//# define OONI_PLATFORM "unknown"
|
||||
//#endif
|
||||
import "C"
|
||||
|
||||
func cgoname() string {
|
||||
return C.OONI_PLATFORM
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
// +build !cgo
|
||||
|
||||
package platform
|
||||
|
||||
func cgoname() string {
|
||||
return "unknown"
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGood(t *testing.T) {
|
||||
var expected bool
|
||||
switch Name() {
|
||||
case "android", "ios", "linux", "macos", "windows":
|
||||
expected = true
|
||||
}
|
||||
if !expected {
|
||||
t.Fatal("unexpected platform name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPuregoname(t *testing.T) {
|
||||
var runtimevariables = []struct {
|
||||
expected string
|
||||
goarch string
|
||||
goos string
|
||||
}{{
|
||||
expected: "android",
|
||||
goarch: "*",
|
||||
goos: "android",
|
||||
}, {
|
||||
expected: "ios",
|
||||
goarch: "arm64",
|
||||
goos: "darwin",
|
||||
}, {
|
||||
expected: "ios",
|
||||
goarch: "arm",
|
||||
goos: "darwin",
|
||||
}, {
|
||||
expected: "linux",
|
||||
goarch: "*",
|
||||
goos: "linux",
|
||||
}, {
|
||||
expected: "macos",
|
||||
goarch: "amd64",
|
||||
goos: "darwin",
|
||||
}, {
|
||||
expected: "macos",
|
||||
goarch: "386",
|
||||
goos: "darwin",
|
||||
}, {
|
||||
expected: "unknown",
|
||||
goarch: "*",
|
||||
goos: "solaris",
|
||||
}, {
|
||||
expected: "unknown",
|
||||
goarch: "mips",
|
||||
goos: "darwin",
|
||||
}, {
|
||||
expected: "windows",
|
||||
goarch: "*",
|
||||
goos: "windows",
|
||||
}}
|
||||
for _, v := range runtimevariables {
|
||||
t.Run(fmt.Sprintf("with %s/%s", v.goos, v.goarch), func(t *testing.T) {
|
||||
if puregoname(v.goos, v.goarch) != v.expected {
|
||||
t.Fatal("unexpected results")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
// Package randx contains math/rand extensions
|
||||
package randx
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const (
|
||||
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
lowercase = "abcdefghijklmnopqrstuvwxyz"
|
||||
letters = uppercase + lowercase
|
||||
)
|
||||
|
||||
func lettersWithString(n int, letterBytes string) string {
|
||||
// See https://stackoverflow.com/questions/22892120
|
||||
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letterBytes[rnd.Intn(len(letterBytes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// Letters return a string composed of random letters
|
||||
func Letters(n int) string {
|
||||
return lettersWithString(n, letters)
|
||||
}
|
||||
|
||||
// LettersUppercase return a string composed of random uppercase letters
|
||||
func LettersUppercase(n int) string {
|
||||
return lettersWithString(n, uppercase)
|
||||
}
|
||||
|
||||
// ChangeCapitalization returns a new string where the capitalization
|
||||
// of each character is changed at random.
|
||||
func ChangeCapitalization(source string) (dest string) {
|
||||
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for _, chr := range source {
|
||||
if unicode.IsLower(chr) && rnd.Float64() <= 0.5 {
|
||||
dest += string(unicode.ToUpper(chr))
|
||||
} else if unicode.IsUpper(chr) && rnd.Float64() <= 0.5 {
|
||||
dest += string(unicode.ToLower(chr))
|
||||
} else {
|
||||
dest += string(chr)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package randx_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/randx"
|
||||
)
|
||||
|
||||
func TestLetters(t *testing.T) {
|
||||
str := randx.Letters(1024)
|
||||
for _, chr := range str {
|
||||
if (chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z') {
|
||||
continue
|
||||
}
|
||||
t.Fatal("invalid input char")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLettersUppercase(t *testing.T) {
|
||||
str := randx.LettersUppercase(1024)
|
||||
for _, chr := range str {
|
||||
if chr >= 'A' && chr <= 'Z' {
|
||||
continue
|
||||
}
|
||||
t.Fatal("invalid input char")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChangeCapitalization(t *testing.T) {
|
||||
str := randx.Letters(2048)
|
||||
if randx.ChangeCapitalization(str) == str {
|
||||
t.Fatal("capitalization not changed")
|
||||
}
|
||||
}
|
||||
@@ -5,13 +5,16 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/sessionresolver"
|
||||
"github.com/ooni/probe-cli/v3/internal/kvstore"
|
||||
)
|
||||
|
||||
func TestSessionResolverGood(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
reso := &sessionresolver.Resolver{}
|
||||
reso := &sessionresolver.Resolver{
|
||||
KVStore: &kvstore.Memory{},
|
||||
}
|
||||
defer reso.CloseIdleConnections()
|
||||
if reso.Network() != "sessionresolver" {
|
||||
t.Fatal("unexpected Network")
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package sessionresolver
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func (r *Resolver) kvstore() KVStore {
|
||||
defer r.mu.Unlock()
|
||||
r.mu.Lock()
|
||||
if r.KVStore == nil {
|
||||
r.KVStore = &memkvstore{}
|
||||
}
|
||||
return r.KVStore
|
||||
}
|
||||
|
||||
var errMemkvstoreNotFound = errors.New("memkvstore: not found")
|
||||
|
||||
type memkvstore struct {
|
||||
m map[string][]byte
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (kvs *memkvstore) Get(key string) ([]byte, error) {
|
||||
defer kvs.mu.Unlock()
|
||||
kvs.mu.Lock()
|
||||
out, good := kvs.m[key]
|
||||
if !good {
|
||||
return nil, fmt.Errorf("%w: %s", errMemkvstoreNotFound, key)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (kvs *memkvstore) Set(key string, value []byte) error {
|
||||
defer kvs.mu.Unlock()
|
||||
kvs.mu.Lock()
|
||||
if kvs.m == nil {
|
||||
kvs.m = make(map[string][]byte)
|
||||
}
|
||||
kvs.m[key] = value
|
||||
return nil
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package sessionresolver
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestKVStoreCustom(t *testing.T) {
|
||||
kvs := &memkvstore{}
|
||||
reso := &Resolver{KVStore: kvs}
|
||||
o := reso.kvstore()
|
||||
if o != kvs {
|
||||
t.Fatal("not the kvstore we expected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemkvstoreGetNotFound(t *testing.T) {
|
||||
reso := &Resolver{}
|
||||
key := "antani"
|
||||
out, err := reso.kvstore().Get(key)
|
||||
if !errors.Is(err, errMemkvstoreNotFound) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if out != nil {
|
||||
t.Fatal("expected nil here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemkvstoreRoundTrip(t *testing.T) {
|
||||
reso := &Resolver{}
|
||||
key := []string{"antani", "mascetti"}
|
||||
value := [][]byte{[]byte(`mascetti`), []byte(`antani`)}
|
||||
for idx := 0; idx < 2; idx++ {
|
||||
if err := reso.kvstore().Set(key[idx], value[idx]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := reso.kvstore().Get(key[idx])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(value[idx], out); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,9 +33,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/multierror"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/multierror"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
// Resolver is the session resolver. Resolver will try to use
|
||||
@@ -45,6 +45,9 @@ import (
|
||||
// and therefore we can generally give preference to underlying
|
||||
// DoT/DoH resolvers that work better.
|
||||
//
|
||||
// Make sure you fill the mandatory fields (indicated below)
|
||||
// before using this data structure.
|
||||
//
|
||||
// You MUST NOT modify public fields of this structure once it
|
||||
// has been created, because that MAY lead to data races.
|
||||
//
|
||||
@@ -57,10 +60,9 @@ type Resolver struct {
|
||||
// field is not set, then we won't count the bytes.
|
||||
ByteCounter *bytecounter.Counter
|
||||
|
||||
// KVStore is the optional key-value store where you
|
||||
// KVStore is the MANDATORY key-value store where you
|
||||
// want us to write statistics about which resolver is
|
||||
// working better in your network. If this field is
|
||||
// not set, then we'll use a in-memory store.
|
||||
// working better in your network.
|
||||
KVStore KVStore
|
||||
|
||||
// Logger is the optional logger you want us to use
|
||||
|
||||
@@ -6,11 +6,12 @@ import (
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/multierror"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/kvstore"
|
||||
"github.com/ooni/probe-cli/v3/internal/multierror"
|
||||
)
|
||||
|
||||
func TestNetworkWorks(t *testing.T) {
|
||||
@@ -30,7 +31,7 @@ func TestAddressWorks(t *testing.T) {
|
||||
func TestTypicalUsageWithFailure(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // fail immediately
|
||||
reso := &Resolver{}
|
||||
reso := &Resolver{KVStore: &kvstore.Memory{}}
|
||||
addrs, err := reso.LookupHost(ctx, "ooni.org")
|
||||
if !errors.Is(err, ErrLookupHost) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
@@ -82,6 +83,7 @@ func TestTypicalUsageWithSuccess(t *testing.T) {
|
||||
expected := []string{"8.8.8.8", "8.8.4.4"}
|
||||
ctx := context.Background()
|
||||
reso := &Resolver{
|
||||
KVStore: &kvstore.Memory{},
|
||||
dnsClientMaker: &fakeDNSClientMaker{
|
||||
reso: &FakeResolver{Data: expected},
|
||||
},
|
||||
@@ -252,7 +254,7 @@ func TestMaybeConfusionManyEntries(t *testing.T) {
|
||||
|
||||
func TestResolverWorksWithProxy(t *testing.T) {
|
||||
var (
|
||||
works int32
|
||||
works = &atomicx.Int64{}
|
||||
startuperr = make(chan error)
|
||||
listench = make(chan net.Listener)
|
||||
done = make(chan interface{})
|
||||
@@ -273,7 +275,7 @@ func TestResolverWorksWithProxy(t *testing.T) {
|
||||
// shutdown by the main goroutine.
|
||||
return
|
||||
}
|
||||
atomic.AddInt32(&works, 1)
|
||||
works.Add(1)
|
||||
conn.Close()
|
||||
}
|
||||
}()
|
||||
@@ -283,10 +285,13 @@ func TestResolverWorksWithProxy(t *testing.T) {
|
||||
}
|
||||
listener := <-listench
|
||||
// use the proxy
|
||||
reso := &Resolver{ProxyURL: &url.URL{
|
||||
Scheme: "socks5",
|
||||
Host: listener.Addr().String(),
|
||||
}}
|
||||
reso := &Resolver{
|
||||
ProxyURL: &url.URL{
|
||||
Scheme: "socks5",
|
||||
Host: listener.Addr().String(),
|
||||
},
|
||||
KVStore: &kvstore.Memory{},
|
||||
}
|
||||
ctx := context.Background()
|
||||
addrs, err := reso.LookupHost(ctx, "ooni.org")
|
||||
// cleanly shutdown the listener
|
||||
@@ -299,7 +304,7 @@ func TestResolverWorksWithProxy(t *testing.T) {
|
||||
if addrs != nil {
|
||||
t.Fatal("expected nil addrs")
|
||||
}
|
||||
if works < 1 {
|
||||
if works.Load() < 1 {
|
||||
t.Fatal("expected to see a positive number of entries here")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,15 @@ type resolverinfo struct {
|
||||
Score float64
|
||||
}
|
||||
|
||||
// ErrNilKVStore indicates that the KVStore is nil.
|
||||
var ErrNilKVStore = errors.New("sessionresolver: kvstore is nil")
|
||||
|
||||
// readstate reads the resolver state from disk
|
||||
func (r *Resolver) readstate() ([]*resolverinfo, error) {
|
||||
data, err := r.kvstore().Get(storekey)
|
||||
if r.KVStore == nil {
|
||||
return nil, ErrNilKVStore
|
||||
}
|
||||
data, err := r.KVStore.Get(storekey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -85,9 +91,12 @@ func (r *Resolver) readstatedefault() []*resolverinfo {
|
||||
|
||||
// writestate writes the state on the kvstore.
|
||||
func (r *Resolver) writestate(ri []*resolverinfo) error {
|
||||
if r.KVStore == nil {
|
||||
return ErrNilKVStore
|
||||
}
|
||||
data, err := r.getCodec().Encode(ri)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.kvstore().Set(storekey, data)
|
||||
return r.KVStore.Set(storekey, data)
|
||||
}
|
||||
|
||||
@@ -3,12 +3,25 @@ package sessionresolver
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/kvstore"
|
||||
)
|
||||
|
||||
func TestReadStateNothingInKVStore(t *testing.T) {
|
||||
reso := &Resolver{KVStore: &memkvstore{}}
|
||||
func TestReadStateNoKVStore(t *testing.T) {
|
||||
reso := &Resolver{}
|
||||
out, err := reso.readstate()
|
||||
if !errors.Is(err, errMemkvstoreNotFound) {
|
||||
if !errors.Is(err, ErrNilKVStore) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if out != nil {
|
||||
t.Fatal("expected nil here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadStateNothingInKVStore(t *testing.T) {
|
||||
reso := &Resolver{KVStore: &kvstore.Memory{}}
|
||||
out, err := reso.readstate()
|
||||
if !errors.Is(err, kvstore.ErrNoSuchKey) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if out != nil {
|
||||
@@ -19,7 +32,7 @@ func TestReadStateNothingInKVStore(t *testing.T) {
|
||||
func TestReadStateDecodeError(t *testing.T) {
|
||||
errMocked := errors.New("mocked error")
|
||||
reso := &Resolver{
|
||||
KVStore: &memkvstore{},
|
||||
KVStore: &kvstore.Memory{},
|
||||
codec: &FakeCodec{DecodeErr: errMocked},
|
||||
}
|
||||
if err := reso.KVStore.Set(storekey, []byte(`[]`)); err != nil {
|
||||
@@ -35,9 +48,9 @@ func TestReadStateDecodeError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadStateAndPruneReadStateError(t *testing.T) {
|
||||
reso := &Resolver{KVStore: &memkvstore{}}
|
||||
reso := &Resolver{KVStore: &kvstore.Memory{}}
|
||||
out, err := reso.readstateandprune()
|
||||
if !errors.Is(err, errMemkvstoreNotFound) {
|
||||
if !errors.Is(err, kvstore.ErrNoSuchKey) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if out != nil {
|
||||
@@ -46,7 +59,7 @@ func TestReadStateAndPruneReadStateError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadStateAndPruneWithUnsupportedEntries(t *testing.T) {
|
||||
reso := &Resolver{KVStore: &memkvstore{}}
|
||||
reso := &Resolver{KVStore: &kvstore.Memory{}}
|
||||
var in []*resolverinfo
|
||||
in = append(in, &resolverinfo{})
|
||||
if err := reso.writestate(in); err != nil {
|
||||
@@ -62,7 +75,7 @@ func TestReadStateAndPruneWithUnsupportedEntries(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadStateDefaultWithMissingEntries(t *testing.T) {
|
||||
reso := &Resolver{KVStore: &memkvstore{}}
|
||||
reso := &Resolver{KVStore: &kvstore.Memory{}}
|
||||
// let us simulate that we have just one entry here
|
||||
existingURL := "https://dns.google/dns-query"
|
||||
existingScore := 0.88
|
||||
@@ -100,12 +113,27 @@ func TestReadStateDefaultWithMissingEntries(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteStateNoKVStore(t *testing.T) {
|
||||
reso := &Resolver{}
|
||||
existingURL := "https://dns.google/dns-query"
|
||||
existingScore := 0.88
|
||||
var in []*resolverinfo
|
||||
in = append(in, &resolverinfo{
|
||||
URL: existingURL,
|
||||
Score: existingScore,
|
||||
})
|
||||
if err := reso.writestate(in); !errors.Is(err, ErrNilKVStore) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteStateCannotSerialize(t *testing.T) {
|
||||
errMocked := errors.New("mocked error")
|
||||
reso := &Resolver{
|
||||
codec: &FakeCodec{
|
||||
EncodeErr: errMocked,
|
||||
},
|
||||
KVStore: &kvstore.Memory{},
|
||||
}
|
||||
existingURL := "https://dns.google/dns-query"
|
||||
existingScore := 0.88
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/rogpeppe/go-internal/lockedfile"
|
||||
)
|
||||
|
||||
// KVStore is a simple, atomic key-value store. The user of
|
||||
// probe-engine should supply an implementation of this interface,
|
||||
// which will be used by probe-engine to store specific data.
|
||||
@@ -15,30 +7,3 @@ type KVStore interface {
|
||||
Get(key string) (value []byte, err error)
|
||||
Set(key string, value []byte) (err error)
|
||||
}
|
||||
|
||||
// FileSystemKVStore is a directory based KVStore
|
||||
type FileSystemKVStore struct {
|
||||
basedir string
|
||||
}
|
||||
|
||||
// NewFileSystemKVStore creates a new FileSystemKVStore.
|
||||
func NewFileSystemKVStore(basedir string) (kvs *FileSystemKVStore, err error) {
|
||||
if err = os.MkdirAll(basedir, 0700); err == nil {
|
||||
kvs = &FileSystemKVStore{basedir: basedir}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (kvs *FileSystemKVStore) filename(key string) string {
|
||||
return filepath.Join(kvs.basedir, key)
|
||||
}
|
||||
|
||||
// Get returns the specified key's value
|
||||
func (kvs *FileSystemKVStore) Get(key string) ([]byte, error) {
|
||||
return lockedfile.Read(kvs.filename(key))
|
||||
}
|
||||
|
||||
// Set sets the value of a specific key
|
||||
func (kvs *FileSystemKVStore) Set(key string, value []byte) error {
|
||||
return lockedfile.Write(kvs.filename(key), bytes.NewReader(value), 0600)
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
// Package kvstore contains key-value stores
|
||||
package kvstore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// MemoryKeyValueStore is an in-memory key-value store
|
||||
type MemoryKeyValueStore struct {
|
||||
m map[string][]byte
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// NewMemoryKeyValueStore creates a new in-memory key-value store
|
||||
func NewMemoryKeyValueStore() *MemoryKeyValueStore {
|
||||
return &MemoryKeyValueStore{
|
||||
m: make(map[string][]byte),
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns a key from the key value store
|
||||
func (kvs *MemoryKeyValueStore) Get(key string) ([]byte, error) {
|
||||
var (
|
||||
err error
|
||||
ok bool
|
||||
value []byte
|
||||
)
|
||||
kvs.mu.Lock()
|
||||
defer kvs.mu.Unlock()
|
||||
value, ok = kvs.m[key]
|
||||
if !ok {
|
||||
err = errors.New("no such key")
|
||||
}
|
||||
return value, err
|
||||
}
|
||||
|
||||
// Set sets a key into the key value store
|
||||
func (kvs *MemoryKeyValueStore) Set(key string, value []byte) error {
|
||||
kvs.mu.Lock()
|
||||
defer kvs.mu.Unlock()
|
||||
kvs.m[key] = value
|
||||
return nil
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package kvstore
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNoSuchKey(t *testing.T) {
|
||||
kvs := NewMemoryKeyValueStore()
|
||||
value, err := kvs.Get("nonexistent")
|
||||
if err == nil {
|
||||
t.Fatal("expected an error here")
|
||||
}
|
||||
if value != nil {
|
||||
t.Fatal("expected empty string here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExistingKey(t *testing.T) {
|
||||
kvs := NewMemoryKeyValueStore()
|
||||
if err := kvs.Set("antani", []byte("mascetti")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value, err := kvs.Get("antani")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(value) != "mascetti" {
|
||||
t.Fatal("not the result we expected")
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestKVStoreIntegration(t *testing.T) {
|
||||
var (
|
||||
err error
|
||||
kvstore KVStore
|
||||
)
|
||||
kvstore, err = NewFileSystemKVStore(
|
||||
filepath.Join("testdata", "kvstore2"),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value := []byte("foobar")
|
||||
if err := kvstore.Set("antani", value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ovalue, err := kvstore.Get("antani")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(ovalue, value) {
|
||||
t.Fatal("invalid value")
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,12 @@ package dialid
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
)
|
||||
|
||||
type contextkey struct{}
|
||||
|
||||
var id = atomicx.NewInt64()
|
||||
var id = &atomicx.Int64{}
|
||||
|
||||
// WithDialID returns a copy of ctx with DialID
|
||||
func WithDialID(ctx context.Context) context.Context {
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
type stdoutHandler struct{}
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/connid"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/dialid"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
||||
@@ -28,7 +28,7 @@ type TraceTripper struct {
|
||||
// NewTraceTripper creates a new Transport.
|
||||
func NewTraceTripper(roundTripper http.RoundTripper) *TraceTripper {
|
||||
return &TraceTripper{
|
||||
readAllErrs: atomicx.NewInt64(),
|
||||
readAllErrs: &atomicx.Int64{},
|
||||
readAll: ioutil.ReadAll,
|
||||
roundTripper: roundTripper,
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@ package transactionid
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
)
|
||||
|
||||
type contextkey struct{}
|
||||
|
||||
var id = atomicx.NewInt64()
|
||||
var id = &atomicx.Int64{}
|
||||
|
||||
// WithTransactionID returns a copy of ctx with TransactionID
|
||||
func WithTransactionID(ctx context.Context) context.Context {
|
||||
|
||||
@@ -20,11 +20,11 @@ import (
|
||||
"time"
|
||||
|
||||
goptlib "git.torproject.org/pluggable-transports/goptlib.git"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/handlers"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
"gitlab.com/yawning/obfs4.git/transports"
|
||||
obfs4base "gitlab.com/yawning/obfs4.git/transports/base"
|
||||
)
|
||||
@@ -37,7 +37,7 @@ type channelHandler struct {
|
||||
func newChannelHandler(ch chan<- modelx.Measurement) *channelHandler {
|
||||
return &channelHandler{
|
||||
ch: ch,
|
||||
lateWrites: atomicx.NewInt64(),
|
||||
lateWrites: &atomicx.Int64{},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package bytecounter
|
||||
|
||||
import "github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
import "github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
|
||||
// Counter counts bytes sent and received.
|
||||
type Counter struct {
|
||||
@@ -10,7 +10,7 @@ type Counter struct {
|
||||
|
||||
// New creates a new Counter.
|
||||
func New() *Counter {
|
||||
return &Counter{Received: atomicx.NewInt64(), Sent: atomicx.NewInt64()}
|
||||
return &Counter{Received: &atomicx.Int64{}, Sent: &atomicx.Int64{}}
|
||||
}
|
||||
|
||||
// CountBytesSent adds count to the bytes sent counter.
|
||||
|
||||
@@ -39,7 +39,7 @@ import (
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/selfcensor"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
// Logger is the logger assumed by this package
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
var privateIPBlocks []*net.IPNet
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
)
|
||||
|
||||
type FakeDialer struct {
|
||||
@@ -109,11 +109,11 @@ type FakeResolver struct {
|
||||
}
|
||||
|
||||
func NewFakeResolverThatFails() FakeResolver {
|
||||
return FakeResolver{NumFailures: atomicx.NewInt64(), Err: errNotFound}
|
||||
return FakeResolver{NumFailures: &atomicx.Int64{}, Err: errNotFound}
|
||||
}
|
||||
|
||||
func NewFakeResolverWithResult(r []string) FakeResolver {
|
||||
return FakeResolver{NumFailures: atomicx.NewInt64(), Result: r}
|
||||
return FakeResolver{NumFailures: &atomicx.Int64{}, Result: r}
|
||||
}
|
||||
|
||||
var errNotFound = &net.DNSError{
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
)
|
||||
|
||||
// RoundTripper represents an abstract DNS transport.
|
||||
@@ -38,7 +38,7 @@ func NewSerialResolver(t RoundTripper) SerialResolver {
|
||||
return SerialResolver{
|
||||
Encoder: MiekgEncoder{},
|
||||
Decoder: MiekgDecoder{},
|
||||
NumTimeouts: atomicx.NewInt64(),
|
||||
NumTimeouts: &atomicx.Int64{},
|
||||
Txp: t,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
)
|
||||
|
||||
// Spec indicates what self censorship techniques to implement.
|
||||
@@ -61,8 +61,8 @@ type Spec struct {
|
||||
}
|
||||
|
||||
var (
|
||||
attempts *atomicx.Int64 = atomicx.NewInt64()
|
||||
enabled *atomicx.Int64 = atomicx.NewInt64()
|
||||
attempts *atomicx.Int64 = &atomicx.Int64{}
|
||||
enabled *atomicx.Int64 = &atomicx.Int64{}
|
||||
mu sync.Mutex
|
||||
spec *Spec
|
||||
)
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
# Package ./internal/engine/ooapi
|
||||
|
||||
Automatically generated API clients for speaking with OONI servers.
|
||||
|
||||
Please, run `go doc ./internal/engine/ooapi` to see API documentation.
|
||||
@@ -1,47 +0,0 @@
|
||||
package apimodel
|
||||
|
||||
// CheckInRequestWebConnectivity contains WebConnectivity
|
||||
// specific parameters to include into CheckInRequest
|
||||
type CheckInRequestWebConnectivity struct {
|
||||
CategoryCodes []string `json:"category_codes"`
|
||||
}
|
||||
|
||||
// CheckInRequest is the check-in API request
|
||||
type CheckInRequest struct {
|
||||
Charging bool `json:"charging"`
|
||||
OnWiFi bool `json:"on_wifi"`
|
||||
Platform string `json:"platform"`
|
||||
ProbeASN string `json:"probe_asn"`
|
||||
ProbeCC string `json:"probe_cc"`
|
||||
RunType string `json:"run_type"`
|
||||
SoftwareName string `json:"software_name"`
|
||||
SoftwareVersion string `json:"software_version"`
|
||||
WebConnectivity CheckInRequestWebConnectivity `json:"web_connectivity"`
|
||||
}
|
||||
|
||||
// CheckInResponseURLInfo contains information about an URL.
|
||||
type CheckInResponseURLInfo struct {
|
||||
CategoryCode string `json:"category_code"`
|
||||
CountryCode string `json:"country_code"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// CheckInResponseWebConnectivity contains WebConnectivity
|
||||
// specific information of a CheckInResponse
|
||||
type CheckInResponseWebConnectivity struct {
|
||||
ReportID string `json:"report_id"`
|
||||
URLs []CheckInResponseURLInfo `json:"urls"`
|
||||
}
|
||||
|
||||
// CheckInResponse is the check-in API response
|
||||
type CheckInResponse struct {
|
||||
ProbeASN string `json:"probe_asn"`
|
||||
ProbeCC string `json:"probe_cc"`
|
||||
Tests CheckInResponseTests `json:"tests"`
|
||||
V int64 `json:"v"`
|
||||
}
|
||||
|
||||
// CheckInResponseTests contains configuration for tests
|
||||
type CheckInResponseTests struct {
|
||||
WebConnectivity CheckInResponseWebConnectivity `json:"web_connectivity"`
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package apimodel
|
||||
|
||||
// CheckReportIDRequest is the CheckReportID request.
|
||||
type CheckReportIDRequest struct {
|
||||
ReportID string `query:"report_id" required:"true"`
|
||||
}
|
||||
|
||||
// CheckReportIDResponse is the CheckReportID response.
|
||||
type CheckReportIDResponse struct {
|
||||
Error string `json:"error"`
|
||||
Found bool `json:"found"`
|
||||
V int64 `json:"v"`
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
// Package apimodel describes the data types used by OONI's API.
|
||||
//
|
||||
// If you edit this package to integrate the data model, remember to
|
||||
// run `go generate ./...`.
|
||||
//
|
||||
// We annotate fields with tagging. When a field should be sent
|
||||
// over as JSON, use the usual `json` tag.
|
||||
//
|
||||
// When a field needs to be sent using the query string, use
|
||||
// the `query` tag instead. We limit what can be sent using the
|
||||
// query string to int64, string, and bool.
|
||||
//
|
||||
// The `path` tag indicates that the URL path contains a
|
||||
// template. We will replace the value of this field with
|
||||
// the template. Note that the template should use the
|
||||
// Go name of the field (e.g. `{{ .ReportID }}`) as opposed
|
||||
// to the name in the tag, which is only used when we
|
||||
// generate the API Swagger.
|
||||
//
|
||||
// The `required` tag indicates required fields. A required
|
||||
// field cannot be empty (for the Go definition of empty).
|
||||
package apimodel
|
||||
@@ -1,15 +0,0 @@
|
||||
package apimodel
|
||||
|
||||
import "time"
|
||||
|
||||
// LoginRequest is the login API request
|
||||
type LoginRequest struct {
|
||||
ClientID string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
// LoginResponse is the login API response
|
||||
type LoginResponse struct {
|
||||
Expire time.Time `json:"expire"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package apimodel
|
||||
|
||||
// MeasurementMetaRequest is the MeasurementMeta Request.
|
||||
type MeasurementMetaRequest struct {
|
||||
ReportID string `query:"report_id" required:"true"`
|
||||
Full bool `query:"full"`
|
||||
Input string `query:"input"`
|
||||
}
|
||||
|
||||
// MeasurementMetaResponse is the MeasurementMeta Response.
|
||||
type MeasurementMetaResponse struct {
|
||||
Anomaly bool `json:"anomaly"`
|
||||
CategoryCode string `json:"category_code"`
|
||||
Confirmed bool `json:"confirmed"`
|
||||
Failure bool `json:"failure"`
|
||||
Input string `json:"input"`
|
||||
MeasurementStartTime string `json:"measurement_start_time"`
|
||||
ProbeASN int64 `json:"probe_asn"`
|
||||
ProbeCC string `json:"probe_cc"`
|
||||
RawMeasurement string `json:"raw_measurement"`
|
||||
ReportID string `json:"report_id"`
|
||||
Scores string `json:"scores"`
|
||||
TestName string `json:"test_name"`
|
||||
TestStartTime string `json:"test_start_time"`
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package apimodel
|
||||
|
||||
// OpenReportRequest is the OpenReport request.
|
||||
type OpenReportRequest struct {
|
||||
DataFormatVersion string `json:"data_format_version"`
|
||||
Format string `json:"format"`
|
||||
ProbeASN string `json:"probe_asn"`
|
||||
ProbeCC string `json:"probe_cc"`
|
||||
SoftwareName string `json:"software_name"`
|
||||
SoftwareVersion string `json:"software_version"`
|
||||
TestName string `json:"test_name"`
|
||||
TestStartTime string `json:"test_start_time"`
|
||||
TestVersion string `json:"test_version"`
|
||||
}
|
||||
|
||||
// OpenReportResponse is the OpenReport response.
|
||||
type OpenReportResponse struct {
|
||||
BackendVersion string `json:"backend_version"`
|
||||
ReportID string `json:"report_id"`
|
||||
SupportedFormats []string `json:"supported_formats"`
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package apimodel
|
||||
|
||||
// PsiphonConfigRequest is the request for the PsiphonConfig API
|
||||
type PsiphonConfigRequest struct{}
|
||||
|
||||
// PsiphonConfigResponse is the response from the PsiphonConfig API
|
||||
type PsiphonConfigResponse map[string]interface{}
|
||||
@@ -1,26 +0,0 @@
|
||||
package apimodel
|
||||
|
||||
// RegisterRequest is the request for the Register API.
|
||||
type RegisterRequest struct {
|
||||
// just password
|
||||
Password string `json:"password"`
|
||||
|
||||
// metadata
|
||||
AvailableBandwidth string `json:"available_bandwidth,omitempty"`
|
||||
DeviceToken string `json:"device_token,omitempty"`
|
||||
Language string `json:"language,omitempty"`
|
||||
NetworkType string `json:"network_type,omitempty"`
|
||||
Platform string `json:"platform"`
|
||||
ProbeASN string `json:"probe_asn"`
|
||||
ProbeCC string `json:"probe_cc"`
|
||||
ProbeFamily string `json:"probe_family,omitempty"`
|
||||
ProbeTimezone string `json:"probe_timezone,omitempty"`
|
||||
SoftwareName string `json:"software_name"`
|
||||
SoftwareVersion string `json:"software_version"`
|
||||
SupportedTests []string `json:"supported_tests"`
|
||||
}
|
||||
|
||||
// RegisterResponse is the response from the Register API.
|
||||
type RegisterResponse struct {
|
||||
ClientID string `json:"client_id"`
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package apimodel
|
||||
|
||||
// SubmitMeasurementRequest is the SubmitMeasurement request.
|
||||
type SubmitMeasurementRequest struct {
|
||||
ReportID string `path:"report_id"`
|
||||
Format string `json:"format"`
|
||||
Content interface{} `json:"content"`
|
||||
}
|
||||
|
||||
// SubmitMeasurementResponse is the SubmitMeasurement response.
|
||||
type SubmitMeasurementResponse struct {
|
||||
MeasurementUID string `json:"measurement_uid"`
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package apimodel
|
||||
|
||||
// TestHelpersRequest is the TestHelpers request.
|
||||
type TestHelpersRequest struct{}
|
||||
|
||||
// TestHelpersResponse is the TestHelpers response.
|
||||
type TestHelpersResponse map[string][]TestHelpersHelperInfo
|
||||
|
||||
// TestHelpersHelperInfo is a single helper within the
|
||||
// response returned by the TestHelpers API.
|
||||
type TestHelpersHelperInfo struct {
|
||||
Address string `json:"address"`
|
||||
Type string `json:"type"`
|
||||
Front string `json:"front,omitempty"`
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package apimodel
|
||||
|
||||
// TorTargetsRequest is a request for the TorTargets API.
|
||||
type TorTargetsRequest struct{}
|
||||
|
||||
// TorTargetsResponse is the response from the TorTargets API.
|
||||
type TorTargetsResponse map[string]TorTargetsTarget
|
||||
|
||||
// TorTargetsTarget is a target for the tor experiment.
|
||||
type TorTargetsTarget struct {
|
||||
Address string `json:"address"`
|
||||
Name string `json:"name"`
|
||||
Params map[string][]string `json:"params"`
|
||||
Protocol string `json:"protocol"`
|
||||
Source string `json:"source"`
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package apimodel
|
||||
|
||||
// URLsRequest is the URLs request.
|
||||
type URLsRequest struct {
|
||||
CategoryCodes string `query:"category_codes"`
|
||||
CountryCode string `query:"country_code"`
|
||||
Limit int64 `query:"limit"`
|
||||
}
|
||||
|
||||
// URLsResponse is the URLs response.
|
||||
type URLsResponse struct {
|
||||
Metadata URLsMetadata `json:"metadata"`
|
||||
Results []URLsResponseURL `json:"results"`
|
||||
}
|
||||
|
||||
// URLsMetadata contains metadata in the URLs response.
|
||||
type URLsMetadata struct {
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
|
||||
// URLsResponseURL is a single URL in the URLs response.
|
||||
type URLsResponseURL struct {
|
||||
CategoryCode string `json:"category_code"`
|
||||
CountryCode string `json:"country_code"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
@@ -1,607 +0,0 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2021-05-12 09:15:00.422051399 +0200 CEST m=+0.000129449
|
||||
|
||||
package ooapi
|
||||
|
||||
//go:generate go run ./internal/generator -file apis.go
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
||||
)
|
||||
|
||||
// simpleCheckReportIDAPI implements the CheckReportID API.
|
||||
type simpleCheckReportIDAPI struct {
|
||||
BaseURL string // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
RequestMaker RequestMaker // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
|
||||
func (api *simpleCheckReportIDAPI) baseURL() string {
|
||||
if api.BaseURL != "" {
|
||||
return api.BaseURL
|
||||
}
|
||||
return "https://ps1.ooni.io"
|
||||
}
|
||||
|
||||
func (api *simpleCheckReportIDAPI) requestMaker() RequestMaker {
|
||||
if api.RequestMaker != nil {
|
||||
return api.RequestMaker
|
||||
}
|
||||
return &defaultRequestMaker{}
|
||||
}
|
||||
|
||||
func (api *simpleCheckReportIDAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *simpleCheckReportIDAPI) httpClient() HTTPClient {
|
||||
if api.HTTPClient != nil {
|
||||
return api.HTTPClient
|
||||
}
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
// Call calls the CheckReportID API.
|
||||
func (api *simpleCheckReportIDAPI) Call(ctx context.Context, req *apimodel.CheckReportIDRequest) (*apimodel.CheckReportIDResponse, error) {
|
||||
httpReq, err := api.newRequest(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Add("Accept", "application/json")
|
||||
if api.UserAgent != "" {
|
||||
httpReq.Header.Add("User-Agent", api.UserAgent)
|
||||
}
|
||||
return api.newResponse(api.httpClient().Do(httpReq))
|
||||
}
|
||||
|
||||
// simpleCheckInAPI implements the CheckIn API.
|
||||
type simpleCheckInAPI struct {
|
||||
BaseURL string // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
RequestMaker RequestMaker // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
|
||||
func (api *simpleCheckInAPI) baseURL() string {
|
||||
if api.BaseURL != "" {
|
||||
return api.BaseURL
|
||||
}
|
||||
return "https://ps1.ooni.io"
|
||||
}
|
||||
|
||||
func (api *simpleCheckInAPI) requestMaker() RequestMaker {
|
||||
if api.RequestMaker != nil {
|
||||
return api.RequestMaker
|
||||
}
|
||||
return &defaultRequestMaker{}
|
||||
}
|
||||
|
||||
func (api *simpleCheckInAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *simpleCheckInAPI) httpClient() HTTPClient {
|
||||
if api.HTTPClient != nil {
|
||||
return api.HTTPClient
|
||||
}
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
// Call calls the CheckIn API.
|
||||
func (api *simpleCheckInAPI) Call(ctx context.Context, req *apimodel.CheckInRequest) (*apimodel.CheckInResponse, error) {
|
||||
httpReq, err := api.newRequest(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Add("Accept", "application/json")
|
||||
if api.UserAgent != "" {
|
||||
httpReq.Header.Add("User-Agent", api.UserAgent)
|
||||
}
|
||||
return api.newResponse(api.httpClient().Do(httpReq))
|
||||
}
|
||||
|
||||
// simpleLoginAPI implements the Login API.
|
||||
type simpleLoginAPI struct {
|
||||
BaseURL string // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
RequestMaker RequestMaker // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
|
||||
func (api *simpleLoginAPI) baseURL() string {
|
||||
if api.BaseURL != "" {
|
||||
return api.BaseURL
|
||||
}
|
||||
return "https://ps1.ooni.io"
|
||||
}
|
||||
|
||||
func (api *simpleLoginAPI) requestMaker() RequestMaker {
|
||||
if api.RequestMaker != nil {
|
||||
return api.RequestMaker
|
||||
}
|
||||
return &defaultRequestMaker{}
|
||||
}
|
||||
|
||||
func (api *simpleLoginAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *simpleLoginAPI) httpClient() HTTPClient {
|
||||
if api.HTTPClient != nil {
|
||||
return api.HTTPClient
|
||||
}
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
// Call calls the Login API.
|
||||
func (api *simpleLoginAPI) Call(ctx context.Context, req *apimodel.LoginRequest) (*apimodel.LoginResponse, error) {
|
||||
httpReq, err := api.newRequest(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Add("Accept", "application/json")
|
||||
if api.UserAgent != "" {
|
||||
httpReq.Header.Add("User-Agent", api.UserAgent)
|
||||
}
|
||||
return api.newResponse(api.httpClient().Do(httpReq))
|
||||
}
|
||||
|
||||
// simpleMeasurementMetaAPI implements the MeasurementMeta API.
|
||||
type simpleMeasurementMetaAPI struct {
|
||||
BaseURL string // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
RequestMaker RequestMaker // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
|
||||
func (api *simpleMeasurementMetaAPI) baseURL() string {
|
||||
if api.BaseURL != "" {
|
||||
return api.BaseURL
|
||||
}
|
||||
return "https://ps1.ooni.io"
|
||||
}
|
||||
|
||||
func (api *simpleMeasurementMetaAPI) requestMaker() RequestMaker {
|
||||
if api.RequestMaker != nil {
|
||||
return api.RequestMaker
|
||||
}
|
||||
return &defaultRequestMaker{}
|
||||
}
|
||||
|
||||
func (api *simpleMeasurementMetaAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *simpleMeasurementMetaAPI) httpClient() HTTPClient {
|
||||
if api.HTTPClient != nil {
|
||||
return api.HTTPClient
|
||||
}
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
// Call calls the MeasurementMeta API.
|
||||
func (api *simpleMeasurementMetaAPI) Call(ctx context.Context, req *apimodel.MeasurementMetaRequest) (*apimodel.MeasurementMetaResponse, error) {
|
||||
httpReq, err := api.newRequest(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Add("Accept", "application/json")
|
||||
if api.UserAgent != "" {
|
||||
httpReq.Header.Add("User-Agent", api.UserAgent)
|
||||
}
|
||||
return api.newResponse(api.httpClient().Do(httpReq))
|
||||
}
|
||||
|
||||
// simpleRegisterAPI implements the Register API.
|
||||
type simpleRegisterAPI struct {
|
||||
BaseURL string // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
RequestMaker RequestMaker // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
|
||||
func (api *simpleRegisterAPI) baseURL() string {
|
||||
if api.BaseURL != "" {
|
||||
return api.BaseURL
|
||||
}
|
||||
return "https://ps1.ooni.io"
|
||||
}
|
||||
|
||||
func (api *simpleRegisterAPI) requestMaker() RequestMaker {
|
||||
if api.RequestMaker != nil {
|
||||
return api.RequestMaker
|
||||
}
|
||||
return &defaultRequestMaker{}
|
||||
}
|
||||
|
||||
func (api *simpleRegisterAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *simpleRegisterAPI) httpClient() HTTPClient {
|
||||
if api.HTTPClient != nil {
|
||||
return api.HTTPClient
|
||||
}
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
// Call calls the Register API.
|
||||
func (api *simpleRegisterAPI) Call(ctx context.Context, req *apimodel.RegisterRequest) (*apimodel.RegisterResponse, error) {
|
||||
httpReq, err := api.newRequest(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Add("Accept", "application/json")
|
||||
if api.UserAgent != "" {
|
||||
httpReq.Header.Add("User-Agent", api.UserAgent)
|
||||
}
|
||||
return api.newResponse(api.httpClient().Do(httpReq))
|
||||
}
|
||||
|
||||
// simpleTestHelpersAPI implements the TestHelpers API.
|
||||
type simpleTestHelpersAPI struct {
|
||||
BaseURL string // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
RequestMaker RequestMaker // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
|
||||
func (api *simpleTestHelpersAPI) baseURL() string {
|
||||
if api.BaseURL != "" {
|
||||
return api.BaseURL
|
||||
}
|
||||
return "https://ps1.ooni.io"
|
||||
}
|
||||
|
||||
func (api *simpleTestHelpersAPI) requestMaker() RequestMaker {
|
||||
if api.RequestMaker != nil {
|
||||
return api.RequestMaker
|
||||
}
|
||||
return &defaultRequestMaker{}
|
||||
}
|
||||
|
||||
func (api *simpleTestHelpersAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *simpleTestHelpersAPI) httpClient() HTTPClient {
|
||||
if api.HTTPClient != nil {
|
||||
return api.HTTPClient
|
||||
}
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
// Call calls the TestHelpers API.
|
||||
func (api *simpleTestHelpersAPI) Call(ctx context.Context, req *apimodel.TestHelpersRequest) (apimodel.TestHelpersResponse, error) {
|
||||
httpReq, err := api.newRequest(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Add("Accept", "application/json")
|
||||
if api.UserAgent != "" {
|
||||
httpReq.Header.Add("User-Agent", api.UserAgent)
|
||||
}
|
||||
return api.newResponse(api.httpClient().Do(httpReq))
|
||||
}
|
||||
|
||||
// simplePsiphonConfigAPI implements the PsiphonConfig API.
|
||||
type simplePsiphonConfigAPI struct {
|
||||
BaseURL string // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
Token string // mandatory
|
||||
RequestMaker RequestMaker // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
|
||||
// WithToken returns a copy of the API where the
|
||||
// value of the Token field is replaced with token.
|
||||
func (api *simplePsiphonConfigAPI) WithToken(token string) callerForPsiphonConfigAPI {
|
||||
out := &simplePsiphonConfigAPI{}
|
||||
out.BaseURL = api.BaseURL
|
||||
out.HTTPClient = api.HTTPClient
|
||||
out.JSONCodec = api.JSONCodec
|
||||
out.RequestMaker = api.RequestMaker
|
||||
out.UserAgent = api.UserAgent
|
||||
out.Token = token
|
||||
return out
|
||||
}
|
||||
|
||||
func (api *simplePsiphonConfigAPI) baseURL() string {
|
||||
if api.BaseURL != "" {
|
||||
return api.BaseURL
|
||||
}
|
||||
return "https://ps1.ooni.io"
|
||||
}
|
||||
|
||||
func (api *simplePsiphonConfigAPI) requestMaker() RequestMaker {
|
||||
if api.RequestMaker != nil {
|
||||
return api.RequestMaker
|
||||
}
|
||||
return &defaultRequestMaker{}
|
||||
}
|
||||
|
||||
func (api *simplePsiphonConfigAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *simplePsiphonConfigAPI) httpClient() HTTPClient {
|
||||
if api.HTTPClient != nil {
|
||||
return api.HTTPClient
|
||||
}
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
// Call calls the PsiphonConfig API.
|
||||
func (api *simplePsiphonConfigAPI) Call(ctx context.Context, req *apimodel.PsiphonConfigRequest) (apimodel.PsiphonConfigResponse, error) {
|
||||
httpReq, err := api.newRequest(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Add("Accept", "application/json")
|
||||
if api.Token == "" {
|
||||
return nil, ErrMissingToken
|
||||
}
|
||||
httpReq.Header.Add("Authorization", newAuthorizationHeader(api.Token))
|
||||
if api.UserAgent != "" {
|
||||
httpReq.Header.Add("User-Agent", api.UserAgent)
|
||||
}
|
||||
return api.newResponse(api.httpClient().Do(httpReq))
|
||||
}
|
||||
|
||||
// simpleTorTargetsAPI implements the TorTargets API.
|
||||
type simpleTorTargetsAPI struct {
|
||||
BaseURL string // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
Token string // mandatory
|
||||
RequestMaker RequestMaker // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
|
||||
// WithToken returns a copy of the API where the
|
||||
// value of the Token field is replaced with token.
|
||||
func (api *simpleTorTargetsAPI) WithToken(token string) callerForTorTargetsAPI {
|
||||
out := &simpleTorTargetsAPI{}
|
||||
out.BaseURL = api.BaseURL
|
||||
out.HTTPClient = api.HTTPClient
|
||||
out.JSONCodec = api.JSONCodec
|
||||
out.RequestMaker = api.RequestMaker
|
||||
out.UserAgent = api.UserAgent
|
||||
out.Token = token
|
||||
return out
|
||||
}
|
||||
|
||||
func (api *simpleTorTargetsAPI) baseURL() string {
|
||||
if api.BaseURL != "" {
|
||||
return api.BaseURL
|
||||
}
|
||||
return "https://ps1.ooni.io"
|
||||
}
|
||||
|
||||
func (api *simpleTorTargetsAPI) requestMaker() RequestMaker {
|
||||
if api.RequestMaker != nil {
|
||||
return api.RequestMaker
|
||||
}
|
||||
return &defaultRequestMaker{}
|
||||
}
|
||||
|
||||
func (api *simpleTorTargetsAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *simpleTorTargetsAPI) httpClient() HTTPClient {
|
||||
if api.HTTPClient != nil {
|
||||
return api.HTTPClient
|
||||
}
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
// Call calls the TorTargets API.
|
||||
func (api *simpleTorTargetsAPI) Call(ctx context.Context, req *apimodel.TorTargetsRequest) (apimodel.TorTargetsResponse, error) {
|
||||
httpReq, err := api.newRequest(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Add("Accept", "application/json")
|
||||
if api.Token == "" {
|
||||
return nil, ErrMissingToken
|
||||
}
|
||||
httpReq.Header.Add("Authorization", newAuthorizationHeader(api.Token))
|
||||
if api.UserAgent != "" {
|
||||
httpReq.Header.Add("User-Agent", api.UserAgent)
|
||||
}
|
||||
return api.newResponse(api.httpClient().Do(httpReq))
|
||||
}
|
||||
|
||||
// simpleURLsAPI implements the URLs API.
|
||||
type simpleURLsAPI struct {
|
||||
BaseURL string // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
RequestMaker RequestMaker // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
|
||||
func (api *simpleURLsAPI) baseURL() string {
|
||||
if api.BaseURL != "" {
|
||||
return api.BaseURL
|
||||
}
|
||||
return "https://ps1.ooni.io"
|
||||
}
|
||||
|
||||
func (api *simpleURLsAPI) requestMaker() RequestMaker {
|
||||
if api.RequestMaker != nil {
|
||||
return api.RequestMaker
|
||||
}
|
||||
return &defaultRequestMaker{}
|
||||
}
|
||||
|
||||
func (api *simpleURLsAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *simpleURLsAPI) httpClient() HTTPClient {
|
||||
if api.HTTPClient != nil {
|
||||
return api.HTTPClient
|
||||
}
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
// Call calls the URLs API.
|
||||
func (api *simpleURLsAPI) Call(ctx context.Context, req *apimodel.URLsRequest) (*apimodel.URLsResponse, error) {
|
||||
httpReq, err := api.newRequest(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Add("Accept", "application/json")
|
||||
if api.UserAgent != "" {
|
||||
httpReq.Header.Add("User-Agent", api.UserAgent)
|
||||
}
|
||||
return api.newResponse(api.httpClient().Do(httpReq))
|
||||
}
|
||||
|
||||
// simpleOpenReportAPI implements the OpenReport API.
|
||||
type simpleOpenReportAPI struct {
|
||||
BaseURL string // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
RequestMaker RequestMaker // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
|
||||
func (api *simpleOpenReportAPI) baseURL() string {
|
||||
if api.BaseURL != "" {
|
||||
return api.BaseURL
|
||||
}
|
||||
return "https://ps1.ooni.io"
|
||||
}
|
||||
|
||||
func (api *simpleOpenReportAPI) requestMaker() RequestMaker {
|
||||
if api.RequestMaker != nil {
|
||||
return api.RequestMaker
|
||||
}
|
||||
return &defaultRequestMaker{}
|
||||
}
|
||||
|
||||
func (api *simpleOpenReportAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *simpleOpenReportAPI) httpClient() HTTPClient {
|
||||
if api.HTTPClient != nil {
|
||||
return api.HTTPClient
|
||||
}
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
// Call calls the OpenReport API.
|
||||
func (api *simpleOpenReportAPI) Call(ctx context.Context, req *apimodel.OpenReportRequest) (*apimodel.OpenReportResponse, error) {
|
||||
httpReq, err := api.newRequest(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Add("Accept", "application/json")
|
||||
if api.UserAgent != "" {
|
||||
httpReq.Header.Add("User-Agent", api.UserAgent)
|
||||
}
|
||||
return api.newResponse(api.httpClient().Do(httpReq))
|
||||
}
|
||||
|
||||
// simpleSubmitMeasurementAPI implements the SubmitMeasurement API.
|
||||
type simpleSubmitMeasurementAPI struct {
|
||||
BaseURL string // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
RequestMaker RequestMaker // optional
|
||||
TemplateExecutor templateExecutor // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
|
||||
func (api *simpleSubmitMeasurementAPI) baseURL() string {
|
||||
if api.BaseURL != "" {
|
||||
return api.BaseURL
|
||||
}
|
||||
return "https://ps1.ooni.io"
|
||||
}
|
||||
|
||||
func (api *simpleSubmitMeasurementAPI) requestMaker() RequestMaker {
|
||||
if api.RequestMaker != nil {
|
||||
return api.RequestMaker
|
||||
}
|
||||
return &defaultRequestMaker{}
|
||||
}
|
||||
|
||||
func (api *simpleSubmitMeasurementAPI) jsonCodec() JSONCodec {
|
||||
if api.JSONCodec != nil {
|
||||
return api.JSONCodec
|
||||
}
|
||||
return &defaultJSONCodec{}
|
||||
}
|
||||
|
||||
func (api *simpleSubmitMeasurementAPI) templateExecutor() templateExecutor {
|
||||
if api.TemplateExecutor != nil {
|
||||
return api.TemplateExecutor
|
||||
}
|
||||
return &defaultTemplateExecutor{}
|
||||
}
|
||||
|
||||
func (api *simpleSubmitMeasurementAPI) httpClient() HTTPClient {
|
||||
if api.HTTPClient != nil {
|
||||
return api.HTTPClient
|
||||
}
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
// Call calls the SubmitMeasurement API.
|
||||
func (api *simpleSubmitMeasurementAPI) Call(ctx context.Context, req *apimodel.SubmitMeasurementRequest) (*apimodel.SubmitMeasurementResponse, error) {
|
||||
httpReq, err := api.newRequest(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Add("Accept", "application/json")
|
||||
if api.UserAgent != "" {
|
||||
httpReq.Header.Add("User-Agent", api.UserAgent)
|
||||
}
|
||||
return api.newResponse(api.httpClient().Do(httpReq))
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,98 +0,0 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2021-05-12 09:15:01.869492095 +0200 CEST m=+0.000168945
|
||||
|
||||
package ooapi
|
||||
|
||||
//go:generate go run ./internal/generator -file caching.go
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
||||
)
|
||||
|
||||
// withCacheMeasurementMetaAPI implements caching for simpleMeasurementMetaAPI.
|
||||
type withCacheMeasurementMetaAPI struct {
|
||||
API callerForMeasurementMetaAPI // mandatory
|
||||
GobCodec GobCodec // optional
|
||||
KVStore KVStore // mandatory
|
||||
}
|
||||
|
||||
type cacheEntryForMeasurementMetaAPI struct {
|
||||
Req *apimodel.MeasurementMetaRequest
|
||||
Resp *apimodel.MeasurementMetaResponse
|
||||
}
|
||||
|
||||
// Call calls the API and implements caching.
|
||||
func (c *withCacheMeasurementMetaAPI) Call(ctx context.Context, req *apimodel.MeasurementMetaRequest) (*apimodel.MeasurementMetaResponse, error) {
|
||||
if resp, _ := c.readcache(req); resp != nil {
|
||||
return resp, nil
|
||||
}
|
||||
resp, err := c.API.Call(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := c.writecache(req, resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *withCacheMeasurementMetaAPI) gobCodec() GobCodec {
|
||||
if c.GobCodec != nil {
|
||||
return c.GobCodec
|
||||
}
|
||||
return &defaultGobCodec{}
|
||||
}
|
||||
|
||||
func (c *withCacheMeasurementMetaAPI) getcache() ([]cacheEntryForMeasurementMetaAPI, error) {
|
||||
data, err := c.KVStore.Get("MeasurementMeta.cache")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []cacheEntryForMeasurementMetaAPI
|
||||
if err := c.gobCodec().Decode(data, &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *withCacheMeasurementMetaAPI) setcache(in []cacheEntryForMeasurementMetaAPI) error {
|
||||
data, err := c.gobCodec().Encode(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.KVStore.Set("MeasurementMeta.cache", data)
|
||||
}
|
||||
|
||||
func (c *withCacheMeasurementMetaAPI) readcache(req *apimodel.MeasurementMetaRequest) (*apimodel.MeasurementMetaResponse, error) {
|
||||
cache, err := c.getcache()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, cur := range cache {
|
||||
if reflect.DeepEqual(req, cur.Req) {
|
||||
return cur.Resp, nil
|
||||
}
|
||||
}
|
||||
return nil, errCacheNotFound
|
||||
}
|
||||
|
||||
func (c *withCacheMeasurementMetaAPI) writecache(req *apimodel.MeasurementMetaRequest, resp *apimodel.MeasurementMetaResponse) error {
|
||||
cache, _ := c.getcache()
|
||||
out := []cacheEntryForMeasurementMetaAPI{{Req: req, Resp: resp}}
|
||||
const toomany = 64
|
||||
for idx, cur := range cache {
|
||||
if reflect.DeepEqual(req, cur.Req) {
|
||||
continue // we already updated the cache
|
||||
}
|
||||
if idx > toomany {
|
||||
break
|
||||
}
|
||||
out = append(out, cur)
|
||||
}
|
||||
return c.setcache(out)
|
||||
}
|
||||
|
||||
var _ callerForMeasurementMetaAPI = &withCacheMeasurementMetaAPI{}
|
||||
@@ -1,222 +0,0 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2021-05-12 09:15:02.497717446 +0200 CEST m=+0.000113904
|
||||
|
||||
package ooapi
|
||||
|
||||
//go:generate go run ./internal/generator -file caching_test.go
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
||||
)
|
||||
|
||||
func TestCachesimpleMeasurementMetaAPISuccess(t *testing.T) {
|
||||
ff := &fakeFill{}
|
||||
var expect *apimodel.MeasurementMetaResponse
|
||||
ff.fill(&expect)
|
||||
cache := &withCacheMeasurementMetaAPI{
|
||||
API: &FakeMeasurementMetaAPI{
|
||||
Response: expect,
|
||||
},
|
||||
KVStore: &MemKVStore{},
|
||||
}
|
||||
var req *apimodel.MeasurementMetaRequest
|
||||
ff.fill(&req)
|
||||
ctx := context.Background()
|
||||
resp, err := cache.Call(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non-nil response")
|
||||
}
|
||||
if diff := cmp.Diff(expect, resp); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachesimpleMeasurementMetaAPIWriteCacheError(t *testing.T) {
|
||||
errMocked := errors.New("mocked error")
|
||||
ff := &fakeFill{}
|
||||
var expect *apimodel.MeasurementMetaResponse
|
||||
ff.fill(&expect)
|
||||
cache := &withCacheMeasurementMetaAPI{
|
||||
API: &FakeMeasurementMetaAPI{
|
||||
Response: expect,
|
||||
},
|
||||
KVStore: &FakeKVStore{SetError: errMocked},
|
||||
}
|
||||
var req *apimodel.MeasurementMetaRequest
|
||||
ff.fill(&req)
|
||||
ctx := context.Background()
|
||||
resp, err := cache.Call(ctx, req)
|
||||
if !errors.Is(err, errMocked) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if resp != nil {
|
||||
t.Fatal("expected nil response")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachesimpleMeasurementMetaAPIFailureWithNoCache(t *testing.T) {
|
||||
errMocked := errors.New("mocked error")
|
||||
ff := &fakeFill{}
|
||||
cache := &withCacheMeasurementMetaAPI{
|
||||
API: &FakeMeasurementMetaAPI{
|
||||
Err: errMocked,
|
||||
},
|
||||
KVStore: &MemKVStore{},
|
||||
}
|
||||
var req *apimodel.MeasurementMetaRequest
|
||||
ff.fill(&req)
|
||||
ctx := context.Background()
|
||||
resp, err := cache.Call(ctx, req)
|
||||
if !errors.Is(err, errMocked) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if resp != nil {
|
||||
t.Fatal("expected nil response")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachesimpleMeasurementMetaAPIFailureWithPreviousCache(t *testing.T) {
|
||||
ff := &fakeFill{}
|
||||
var expect *apimodel.MeasurementMetaResponse
|
||||
ff.fill(&expect)
|
||||
fakeapi := &FakeMeasurementMetaAPI{
|
||||
Response: expect,
|
||||
}
|
||||
cache := &withCacheMeasurementMetaAPI{
|
||||
API: fakeapi,
|
||||
KVStore: &MemKVStore{},
|
||||
}
|
||||
var req *apimodel.MeasurementMetaRequest
|
||||
ff.fill(&req)
|
||||
ctx := context.Background()
|
||||
// first pass with no error at all
|
||||
// use a separate scope to be sure we avoid mistakes
|
||||
{
|
||||
resp, err := cache.Call(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non-nil response")
|
||||
}
|
||||
if diff := cmp.Diff(expect, resp); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
// second pass with failure
|
||||
errMocked := errors.New("mocked error")
|
||||
fakeapi.Err = errMocked
|
||||
fakeapi.Response = nil
|
||||
resp2, err := cache.Call(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp2 == nil {
|
||||
t.Fatal("expected non-nil response")
|
||||
}
|
||||
if diff := cmp.Diff(expect, resp2); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachesimpleMeasurementMetaAPISetcacheWithEncodeError(t *testing.T) {
|
||||
ff := &fakeFill{}
|
||||
errMocked := errors.New("mocked error")
|
||||
var in []cacheEntryForMeasurementMetaAPI
|
||||
ff.fill(&in)
|
||||
cache := &withCacheMeasurementMetaAPI{
|
||||
GobCodec: &FakeCodec{EncodeErr: errMocked},
|
||||
}
|
||||
err := cache.setcache(in)
|
||||
if !errors.Is(err, errMocked) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachesimpleMeasurementMetaAPIReadCacheNotFound(t *testing.T) {
|
||||
ff := &fakeFill{}
|
||||
var incache []cacheEntryForMeasurementMetaAPI
|
||||
ff.fill(&incache)
|
||||
cache := &withCacheMeasurementMetaAPI{
|
||||
KVStore: &MemKVStore{},
|
||||
}
|
||||
err := cache.setcache(incache)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var req *apimodel.MeasurementMetaRequest
|
||||
ff.fill(&req)
|
||||
out, err := cache.readcache(req)
|
||||
if !errors.Is(err, errCacheNotFound) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if out != nil {
|
||||
t.Fatal("expected nil here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachesimpleMeasurementMetaAPIWriteCacheDuplicate(t *testing.T) {
|
||||
ff := &fakeFill{}
|
||||
var req *apimodel.MeasurementMetaRequest
|
||||
ff.fill(&req)
|
||||
var resp1 *apimodel.MeasurementMetaResponse
|
||||
ff.fill(&resp1)
|
||||
var resp2 *apimodel.MeasurementMetaResponse
|
||||
ff.fill(&resp2)
|
||||
cache := &withCacheMeasurementMetaAPI{
|
||||
KVStore: &MemKVStore{},
|
||||
}
|
||||
err := cache.writecache(req, resp1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = cache.writecache(req, resp2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := cache.readcache(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out == nil {
|
||||
t.Fatal("expected non-nil here")
|
||||
}
|
||||
if diff := cmp.Diff(resp2, out); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachesimpleMeasurementMetaAPICacheSizeLimited(t *testing.T) {
|
||||
ff := &fakeFill{}
|
||||
cache := &withCacheMeasurementMetaAPI{
|
||||
KVStore: &MemKVStore{},
|
||||
}
|
||||
var prev int
|
||||
for {
|
||||
var req *apimodel.MeasurementMetaRequest
|
||||
ff.fill(&req)
|
||||
var resp *apimodel.MeasurementMetaResponse
|
||||
ff.fill(&resp)
|
||||
err := cache.writecache(req, resp)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := cache.getcache()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(out) > prev {
|
||||
prev = len(out)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2021-05-12 09:15:03.02266641 +0200 CEST m=+0.000097757
|
||||
|
||||
package ooapi
|
||||
|
||||
//go:generate go run ./internal/generator -file callers.go
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
||||
)
|
||||
|
||||
// callerForCheckReportIDAPI represents any type exposing a method
|
||||
// like simpleCheckReportIDAPI.Call.
|
||||
type callerForCheckReportIDAPI interface {
|
||||
Call(ctx context.Context, req *apimodel.CheckReportIDRequest) (*apimodel.CheckReportIDResponse, error)
|
||||
}
|
||||
|
||||
// callerForCheckInAPI represents any type exposing a method
|
||||
// like simpleCheckInAPI.Call.
|
||||
type callerForCheckInAPI interface {
|
||||
Call(ctx context.Context, req *apimodel.CheckInRequest) (*apimodel.CheckInResponse, error)
|
||||
}
|
||||
|
||||
// callerForLoginAPI represents any type exposing a method
|
||||
// like simpleLoginAPI.Call.
|
||||
type callerForLoginAPI interface {
|
||||
Call(ctx context.Context, req *apimodel.LoginRequest) (*apimodel.LoginResponse, error)
|
||||
}
|
||||
|
||||
// callerForMeasurementMetaAPI represents any type exposing a method
|
||||
// like simpleMeasurementMetaAPI.Call.
|
||||
type callerForMeasurementMetaAPI interface {
|
||||
Call(ctx context.Context, req *apimodel.MeasurementMetaRequest) (*apimodel.MeasurementMetaResponse, error)
|
||||
}
|
||||
|
||||
// callerForRegisterAPI represents any type exposing a method
|
||||
// like simpleRegisterAPI.Call.
|
||||
type callerForRegisterAPI interface {
|
||||
Call(ctx context.Context, req *apimodel.RegisterRequest) (*apimodel.RegisterResponse, error)
|
||||
}
|
||||
|
||||
// callerForTestHelpersAPI represents any type exposing a method
|
||||
// like simpleTestHelpersAPI.Call.
|
||||
type callerForTestHelpersAPI interface {
|
||||
Call(ctx context.Context, req *apimodel.TestHelpersRequest) (apimodel.TestHelpersResponse, error)
|
||||
}
|
||||
|
||||
// callerForPsiphonConfigAPI represents any type exposing a method
|
||||
// like simplePsiphonConfigAPI.Call.
|
||||
type callerForPsiphonConfigAPI interface {
|
||||
Call(ctx context.Context, req *apimodel.PsiphonConfigRequest) (apimodel.PsiphonConfigResponse, error)
|
||||
}
|
||||
|
||||
// callerForTorTargetsAPI represents any type exposing a method
|
||||
// like simpleTorTargetsAPI.Call.
|
||||
type callerForTorTargetsAPI interface {
|
||||
Call(ctx context.Context, req *apimodel.TorTargetsRequest) (apimodel.TorTargetsResponse, error)
|
||||
}
|
||||
|
||||
// callerForURLsAPI represents any type exposing a method
|
||||
// like simpleURLsAPI.Call.
|
||||
type callerForURLsAPI interface {
|
||||
Call(ctx context.Context, req *apimodel.URLsRequest) (*apimodel.URLsResponse, error)
|
||||
}
|
||||
|
||||
// callerForOpenReportAPI represents any type exposing a method
|
||||
// like simpleOpenReportAPI.Call.
|
||||
type callerForOpenReportAPI interface {
|
||||
Call(ctx context.Context, req *apimodel.OpenReportRequest) (*apimodel.OpenReportResponse, error)
|
||||
}
|
||||
|
||||
// callerForSubmitMeasurementAPI represents any type exposing a method
|
||||
// like simpleSubmitMeasurementAPI.Call.
|
||||
type callerForSubmitMeasurementAPI interface {
|
||||
Call(ctx context.Context, req *apimodel.SubmitMeasurementRequest) (*apimodel.SubmitMeasurementResponse, error)
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package ooapi
|
||||
|
||||
// Client is a client for speaking with the OONI API. Make sure you
|
||||
// fill in the mandatory fields.
|
||||
type Client struct {
|
||||
BaseURL string // optional
|
||||
GobCodec GobCodec // optional
|
||||
HTTPClient HTTPClient // optional
|
||||
JSONCodec JSONCodec // optional
|
||||
KVStore KVStore // mandatory
|
||||
RequestMaker RequestMaker // optional
|
||||
UserAgent string // optional
|
||||
}
|
||||
@@ -1,214 +0,0 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2021-05-12 09:15:03.586305848 +0200 CEST m=+0.000123000
|
||||
|
||||
package ooapi
|
||||
|
||||
//go:generate go run ./internal/generator -file clientcall.go
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
||||
)
|
||||
|
||||
func (c *Client) newCheckReportIDCaller() callerForCheckReportIDAPI {
|
||||
return &simpleCheckReportIDAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
}
|
||||
}
|
||||
|
||||
// CheckReportID calls the CheckReportID API.
|
||||
func (c *Client) CheckReportID(
|
||||
ctx context.Context, req *apimodel.CheckReportIDRequest,
|
||||
) (*apimodel.CheckReportIDResponse, error) {
|
||||
api := c.newCheckReportIDCaller()
|
||||
return api.Call(ctx, req)
|
||||
}
|
||||
|
||||
func (c *Client) newCheckInCaller() callerForCheckInAPI {
|
||||
return &simpleCheckInAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
}
|
||||
}
|
||||
|
||||
// CheckIn calls the CheckIn API.
|
||||
func (c *Client) CheckIn(
|
||||
ctx context.Context, req *apimodel.CheckInRequest,
|
||||
) (*apimodel.CheckInResponse, error) {
|
||||
api := c.newCheckInCaller()
|
||||
return api.Call(ctx, req)
|
||||
}
|
||||
|
||||
func (c *Client) newMeasurementMetaCaller() callerForMeasurementMetaAPI {
|
||||
return &withCacheMeasurementMetaAPI{
|
||||
API: &simpleMeasurementMetaAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
},
|
||||
GobCodec: c.GobCodec,
|
||||
KVStore: c.KVStore,
|
||||
}
|
||||
}
|
||||
|
||||
// MeasurementMeta calls the MeasurementMeta API.
|
||||
func (c *Client) MeasurementMeta(
|
||||
ctx context.Context, req *apimodel.MeasurementMetaRequest,
|
||||
) (*apimodel.MeasurementMetaResponse, error) {
|
||||
api := c.newMeasurementMetaCaller()
|
||||
return api.Call(ctx, req)
|
||||
}
|
||||
|
||||
func (c *Client) newTestHelpersCaller() callerForTestHelpersAPI {
|
||||
return &simpleTestHelpersAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
}
|
||||
}
|
||||
|
||||
// TestHelpers calls the TestHelpers API.
|
||||
func (c *Client) TestHelpers(
|
||||
ctx context.Context, req *apimodel.TestHelpersRequest,
|
||||
) (apimodel.TestHelpersResponse, error) {
|
||||
api := c.newTestHelpersCaller()
|
||||
return api.Call(ctx, req)
|
||||
}
|
||||
|
||||
func (c *Client) newPsiphonConfigCaller() callerForPsiphonConfigAPI {
|
||||
return &withLoginPsiphonConfigAPI{
|
||||
API: &simplePsiphonConfigAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
},
|
||||
JSONCodec: c.JSONCodec,
|
||||
KVStore: c.KVStore,
|
||||
RegisterAPI: &simpleRegisterAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
},
|
||||
LoginAPI: &simpleLoginAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// PsiphonConfig calls the PsiphonConfig API.
|
||||
func (c *Client) PsiphonConfig(
|
||||
ctx context.Context, req *apimodel.PsiphonConfigRequest,
|
||||
) (apimodel.PsiphonConfigResponse, error) {
|
||||
api := c.newPsiphonConfigCaller()
|
||||
return api.Call(ctx, req)
|
||||
}
|
||||
|
||||
func (c *Client) newTorTargetsCaller() callerForTorTargetsAPI {
|
||||
return &withLoginTorTargetsAPI{
|
||||
API: &simpleTorTargetsAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
},
|
||||
JSONCodec: c.JSONCodec,
|
||||
KVStore: c.KVStore,
|
||||
RegisterAPI: &simpleRegisterAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
},
|
||||
LoginAPI: &simpleLoginAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// TorTargets calls the TorTargets API.
|
||||
func (c *Client) TorTargets(
|
||||
ctx context.Context, req *apimodel.TorTargetsRequest,
|
||||
) (apimodel.TorTargetsResponse, error) {
|
||||
api := c.newTorTargetsCaller()
|
||||
return api.Call(ctx, req)
|
||||
}
|
||||
|
||||
func (c *Client) newURLsCaller() callerForURLsAPI {
|
||||
return &simpleURLsAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
}
|
||||
}
|
||||
|
||||
// URLs calls the URLs API.
|
||||
func (c *Client) URLs(
|
||||
ctx context.Context, req *apimodel.URLsRequest,
|
||||
) (*apimodel.URLsResponse, error) {
|
||||
api := c.newURLsCaller()
|
||||
return api.Call(ctx, req)
|
||||
}
|
||||
|
||||
func (c *Client) newOpenReportCaller() callerForOpenReportAPI {
|
||||
return &simpleOpenReportAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
}
|
||||
}
|
||||
|
||||
// OpenReport calls the OpenReport API.
|
||||
func (c *Client) OpenReport(
|
||||
ctx context.Context, req *apimodel.OpenReportRequest,
|
||||
) (*apimodel.OpenReportResponse, error) {
|
||||
api := c.newOpenReportCaller()
|
||||
return api.Call(ctx, req)
|
||||
}
|
||||
|
||||
func (c *Client) newSubmitMeasurementCaller() callerForSubmitMeasurementAPI {
|
||||
return &simpleSubmitMeasurementAPI{
|
||||
BaseURL: c.BaseURL,
|
||||
HTTPClient: c.HTTPClient,
|
||||
JSONCodec: c.JSONCodec,
|
||||
RequestMaker: c.RequestMaker,
|
||||
UserAgent: c.UserAgent,
|
||||
}
|
||||
}
|
||||
|
||||
// SubmitMeasurement calls the SubmitMeasurement API.
|
||||
func (c *Client) SubmitMeasurement(
|
||||
ctx context.Context, req *apimodel.SubmitMeasurementRequest,
|
||||
) (*apimodel.SubmitMeasurementResponse, error) {
|
||||
api := c.newSubmitMeasurementCaller()
|
||||
return api.Call(ctx, req)
|
||||
}
|
||||
@@ -1,898 +0,0 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2021-05-12 09:15:04.198485035 +0200 CEST m=+0.000114145
|
||||
|
||||
package ooapi
|
||||
|
||||
//go:generate go run ./internal/generator -file clientcall_test.go
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
||||
)
|
||||
|
||||
type handleClientCallCheckReportID struct {
|
||||
accept string
|
||||
body []byte
|
||||
contentType string
|
||||
count int32
|
||||
method string
|
||||
mu sync.Mutex
|
||||
resp *apimodel.CheckReportIDResponse
|
||||
url *url.URL
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func (h *handleClientCallCheckReportID) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ff := fakeFill{}
|
||||
defer h.mu.Unlock()
|
||||
h.mu.Lock()
|
||||
if h.count > 0 {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.count++
|
||||
if r.Body != nil {
|
||||
data, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.body = data
|
||||
}
|
||||
h.method = r.Method
|
||||
h.url = r.URL
|
||||
h.accept = r.Header.Get("Accept")
|
||||
h.contentType = r.Header.Get("Content-Type")
|
||||
h.userAgent = r.Header.Get("User-Agent")
|
||||
var out *apimodel.CheckReportIDResponse
|
||||
ff.fill(&out)
|
||||
h.resp = out
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func TestCheckReportIDClientCallRoundTrip(t *testing.T) {
|
||||
// setup
|
||||
handler := &handleClientCallCheckReportID{}
|
||||
srvr := httptest.NewServer(handler)
|
||||
defer srvr.Close()
|
||||
req := &apimodel.CheckReportIDRequest{}
|
||||
ff := &fakeFill{}
|
||||
ff.fill(&req)
|
||||
clnt := &Client{KVStore: &MemKVStore{}, BaseURL: srvr.URL}
|
||||
ff.fill(&clnt.UserAgent)
|
||||
// issue request
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.CheckReportID(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non-nil response here")
|
||||
}
|
||||
// compare our response and server's one
|
||||
if diff := cmp.Diff(handler.resp, resp); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
// check whether headers are OK
|
||||
if handler.accept != "application/json" {
|
||||
t.Fatal("invalid accept header")
|
||||
}
|
||||
if handler.userAgent != clnt.UserAgent {
|
||||
t.Fatal("invalid user-agent header")
|
||||
}
|
||||
// check whether the method is OK
|
||||
if handler.method != "GET" {
|
||||
t.Fatal("invalid method")
|
||||
}
|
||||
// check the query
|
||||
api := &simpleCheckReportIDAPI{BaseURL: srvr.URL}
|
||||
httpReq, err := api.newRequest(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.Path, httpReq.URL.Path); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.RawQuery, httpReq.URL.RawQuery); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
type handleClientCallCheckIn struct {
|
||||
accept string
|
||||
body []byte
|
||||
contentType string
|
||||
count int32
|
||||
method string
|
||||
mu sync.Mutex
|
||||
resp *apimodel.CheckInResponse
|
||||
url *url.URL
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func (h *handleClientCallCheckIn) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ff := fakeFill{}
|
||||
defer h.mu.Unlock()
|
||||
h.mu.Lock()
|
||||
if h.count > 0 {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.count++
|
||||
if r.Body != nil {
|
||||
data, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.body = data
|
||||
}
|
||||
h.method = r.Method
|
||||
h.url = r.URL
|
||||
h.accept = r.Header.Get("Accept")
|
||||
h.contentType = r.Header.Get("Content-Type")
|
||||
h.userAgent = r.Header.Get("User-Agent")
|
||||
var out *apimodel.CheckInResponse
|
||||
ff.fill(&out)
|
||||
h.resp = out
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func TestCheckInClientCallRoundTrip(t *testing.T) {
|
||||
// setup
|
||||
handler := &handleClientCallCheckIn{}
|
||||
srvr := httptest.NewServer(handler)
|
||||
defer srvr.Close()
|
||||
req := &apimodel.CheckInRequest{}
|
||||
ff := &fakeFill{}
|
||||
ff.fill(&req)
|
||||
clnt := &Client{KVStore: &MemKVStore{}, BaseURL: srvr.URL}
|
||||
ff.fill(&clnt.UserAgent)
|
||||
// issue request
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.CheckIn(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non-nil response here")
|
||||
}
|
||||
// compare our response and server's one
|
||||
if diff := cmp.Diff(handler.resp, resp); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
// check whether headers are OK
|
||||
if handler.accept != "application/json" {
|
||||
t.Fatal("invalid accept header")
|
||||
}
|
||||
if handler.userAgent != clnt.UserAgent {
|
||||
t.Fatal("invalid user-agent header")
|
||||
}
|
||||
// check whether the method is OK
|
||||
if handler.method != "POST" {
|
||||
t.Fatal("invalid method")
|
||||
}
|
||||
// check the body
|
||||
if handler.contentType != "application/json" {
|
||||
t.Fatal("invalid content-type header")
|
||||
}
|
||||
got := &apimodel.CheckInRequest{}
|
||||
if err := json.Unmarshal(handler.body, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(req, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
type handleClientCallMeasurementMeta struct {
|
||||
accept string
|
||||
body []byte
|
||||
contentType string
|
||||
count int32
|
||||
method string
|
||||
mu sync.Mutex
|
||||
resp *apimodel.MeasurementMetaResponse
|
||||
url *url.URL
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func (h *handleClientCallMeasurementMeta) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ff := fakeFill{}
|
||||
defer h.mu.Unlock()
|
||||
h.mu.Lock()
|
||||
if h.count > 0 {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.count++
|
||||
if r.Body != nil {
|
||||
data, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.body = data
|
||||
}
|
||||
h.method = r.Method
|
||||
h.url = r.URL
|
||||
h.accept = r.Header.Get("Accept")
|
||||
h.contentType = r.Header.Get("Content-Type")
|
||||
h.userAgent = r.Header.Get("User-Agent")
|
||||
var out *apimodel.MeasurementMetaResponse
|
||||
ff.fill(&out)
|
||||
h.resp = out
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func TestMeasurementMetaClientCallRoundTrip(t *testing.T) {
|
||||
// setup
|
||||
handler := &handleClientCallMeasurementMeta{}
|
||||
srvr := httptest.NewServer(handler)
|
||||
defer srvr.Close()
|
||||
req := &apimodel.MeasurementMetaRequest{}
|
||||
ff := &fakeFill{}
|
||||
ff.fill(&req)
|
||||
clnt := &Client{KVStore: &MemKVStore{}, BaseURL: srvr.URL}
|
||||
ff.fill(&clnt.UserAgent)
|
||||
// issue request
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.MeasurementMeta(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non-nil response here")
|
||||
}
|
||||
// compare our response and server's one
|
||||
if diff := cmp.Diff(handler.resp, resp); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
// check whether headers are OK
|
||||
if handler.accept != "application/json" {
|
||||
t.Fatal("invalid accept header")
|
||||
}
|
||||
if handler.userAgent != clnt.UserAgent {
|
||||
t.Fatal("invalid user-agent header")
|
||||
}
|
||||
// check whether the method is OK
|
||||
if handler.method != "GET" {
|
||||
t.Fatal("invalid method")
|
||||
}
|
||||
// check the query
|
||||
api := &simpleMeasurementMetaAPI{BaseURL: srvr.URL}
|
||||
httpReq, err := api.newRequest(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.Path, httpReq.URL.Path); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.RawQuery, httpReq.URL.RawQuery); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
type handleClientCallTestHelpers struct {
|
||||
accept string
|
||||
body []byte
|
||||
contentType string
|
||||
count int32
|
||||
method string
|
||||
mu sync.Mutex
|
||||
resp apimodel.TestHelpersResponse
|
||||
url *url.URL
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func (h *handleClientCallTestHelpers) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ff := fakeFill{}
|
||||
defer h.mu.Unlock()
|
||||
h.mu.Lock()
|
||||
if h.count > 0 {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.count++
|
||||
if r.Body != nil {
|
||||
data, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.body = data
|
||||
}
|
||||
h.method = r.Method
|
||||
h.url = r.URL
|
||||
h.accept = r.Header.Get("Accept")
|
||||
h.contentType = r.Header.Get("Content-Type")
|
||||
h.userAgent = r.Header.Get("User-Agent")
|
||||
var out apimodel.TestHelpersResponse
|
||||
ff.fill(&out)
|
||||
h.resp = out
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func TestTestHelpersClientCallRoundTrip(t *testing.T) {
|
||||
// setup
|
||||
handler := &handleClientCallTestHelpers{}
|
||||
srvr := httptest.NewServer(handler)
|
||||
defer srvr.Close()
|
||||
req := &apimodel.TestHelpersRequest{}
|
||||
ff := &fakeFill{}
|
||||
ff.fill(&req)
|
||||
clnt := &Client{KVStore: &MemKVStore{}, BaseURL: srvr.URL}
|
||||
ff.fill(&clnt.UserAgent)
|
||||
// issue request
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.TestHelpers(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non-nil response here")
|
||||
}
|
||||
// compare our response and server's one
|
||||
if diff := cmp.Diff(handler.resp, resp); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
// check whether headers are OK
|
||||
if handler.accept != "application/json" {
|
||||
t.Fatal("invalid accept header")
|
||||
}
|
||||
if handler.userAgent != clnt.UserAgent {
|
||||
t.Fatal("invalid user-agent header")
|
||||
}
|
||||
// check whether the method is OK
|
||||
if handler.method != "GET" {
|
||||
t.Fatal("invalid method")
|
||||
}
|
||||
// check the query
|
||||
api := &simpleTestHelpersAPI{BaseURL: srvr.URL}
|
||||
httpReq, err := api.newRequest(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.Path, httpReq.URL.Path); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.RawQuery, httpReq.URL.RawQuery); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
type handleClientCallPsiphonConfig struct {
|
||||
accept string
|
||||
body []byte
|
||||
contentType string
|
||||
count int32
|
||||
method string
|
||||
mu sync.Mutex
|
||||
resp apimodel.PsiphonConfigResponse
|
||||
url *url.URL
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func (h *handleClientCallPsiphonConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ff := fakeFill{}
|
||||
if r.URL.Path == "/api/v1/register" {
|
||||
var out apimodel.RegisterResponse
|
||||
ff.fill(&out)
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/api/v1/login" {
|
||||
var out apimodel.LoginResponse
|
||||
ff.fill(&out)
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
return
|
||||
}
|
||||
defer h.mu.Unlock()
|
||||
h.mu.Lock()
|
||||
if h.count > 0 {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.count++
|
||||
if r.Body != nil {
|
||||
data, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.body = data
|
||||
}
|
||||
h.method = r.Method
|
||||
h.url = r.URL
|
||||
h.accept = r.Header.Get("Accept")
|
||||
h.contentType = r.Header.Get("Content-Type")
|
||||
h.userAgent = r.Header.Get("User-Agent")
|
||||
var out apimodel.PsiphonConfigResponse
|
||||
ff.fill(&out)
|
||||
h.resp = out
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func TestPsiphonConfigClientCallRoundTrip(t *testing.T) {
|
||||
// setup
|
||||
handler := &handleClientCallPsiphonConfig{}
|
||||
srvr := httptest.NewServer(handler)
|
||||
defer srvr.Close()
|
||||
req := &apimodel.PsiphonConfigRequest{}
|
||||
ff := &fakeFill{}
|
||||
ff.fill(&req)
|
||||
clnt := &Client{KVStore: &MemKVStore{}, BaseURL: srvr.URL}
|
||||
ff.fill(&clnt.UserAgent)
|
||||
// issue request
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.PsiphonConfig(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non-nil response here")
|
||||
}
|
||||
// compare our response and server's one
|
||||
if diff := cmp.Diff(handler.resp, resp); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
// check whether headers are OK
|
||||
if handler.accept != "application/json" {
|
||||
t.Fatal("invalid accept header")
|
||||
}
|
||||
if handler.userAgent != clnt.UserAgent {
|
||||
t.Fatal("invalid user-agent header")
|
||||
}
|
||||
// check whether the method is OK
|
||||
if handler.method != "GET" {
|
||||
t.Fatal("invalid method")
|
||||
}
|
||||
// check the query
|
||||
api := &simplePsiphonConfigAPI{BaseURL: srvr.URL}
|
||||
httpReq, err := api.newRequest(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.Path, httpReq.URL.Path); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.RawQuery, httpReq.URL.RawQuery); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
type handleClientCallTorTargets struct {
|
||||
accept string
|
||||
body []byte
|
||||
contentType string
|
||||
count int32
|
||||
method string
|
||||
mu sync.Mutex
|
||||
resp apimodel.TorTargetsResponse
|
||||
url *url.URL
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func (h *handleClientCallTorTargets) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ff := fakeFill{}
|
||||
if r.URL.Path == "/api/v1/register" {
|
||||
var out apimodel.RegisterResponse
|
||||
ff.fill(&out)
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/api/v1/login" {
|
||||
var out apimodel.LoginResponse
|
||||
ff.fill(&out)
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
return
|
||||
}
|
||||
defer h.mu.Unlock()
|
||||
h.mu.Lock()
|
||||
if h.count > 0 {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.count++
|
||||
if r.Body != nil {
|
||||
data, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.body = data
|
||||
}
|
||||
h.method = r.Method
|
||||
h.url = r.URL
|
||||
h.accept = r.Header.Get("Accept")
|
||||
h.contentType = r.Header.Get("Content-Type")
|
||||
h.userAgent = r.Header.Get("User-Agent")
|
||||
var out apimodel.TorTargetsResponse
|
||||
ff.fill(&out)
|
||||
h.resp = out
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func TestTorTargetsClientCallRoundTrip(t *testing.T) {
|
||||
// setup
|
||||
handler := &handleClientCallTorTargets{}
|
||||
srvr := httptest.NewServer(handler)
|
||||
defer srvr.Close()
|
||||
req := &apimodel.TorTargetsRequest{}
|
||||
ff := &fakeFill{}
|
||||
ff.fill(&req)
|
||||
clnt := &Client{KVStore: &MemKVStore{}, BaseURL: srvr.URL}
|
||||
ff.fill(&clnt.UserAgent)
|
||||
// issue request
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.TorTargets(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non-nil response here")
|
||||
}
|
||||
// compare our response and server's one
|
||||
if diff := cmp.Diff(handler.resp, resp); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
// check whether headers are OK
|
||||
if handler.accept != "application/json" {
|
||||
t.Fatal("invalid accept header")
|
||||
}
|
||||
if handler.userAgent != clnt.UserAgent {
|
||||
t.Fatal("invalid user-agent header")
|
||||
}
|
||||
// check whether the method is OK
|
||||
if handler.method != "GET" {
|
||||
t.Fatal("invalid method")
|
||||
}
|
||||
// check the query
|
||||
api := &simpleTorTargetsAPI{BaseURL: srvr.URL}
|
||||
httpReq, err := api.newRequest(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.Path, httpReq.URL.Path); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.RawQuery, httpReq.URL.RawQuery); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
type handleClientCallURLs struct {
|
||||
accept string
|
||||
body []byte
|
||||
contentType string
|
||||
count int32
|
||||
method string
|
||||
mu sync.Mutex
|
||||
resp *apimodel.URLsResponse
|
||||
url *url.URL
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func (h *handleClientCallURLs) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ff := fakeFill{}
|
||||
defer h.mu.Unlock()
|
||||
h.mu.Lock()
|
||||
if h.count > 0 {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.count++
|
||||
if r.Body != nil {
|
||||
data, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.body = data
|
||||
}
|
||||
h.method = r.Method
|
||||
h.url = r.URL
|
||||
h.accept = r.Header.Get("Accept")
|
||||
h.contentType = r.Header.Get("Content-Type")
|
||||
h.userAgent = r.Header.Get("User-Agent")
|
||||
var out *apimodel.URLsResponse
|
||||
ff.fill(&out)
|
||||
h.resp = out
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func TestURLsClientCallRoundTrip(t *testing.T) {
|
||||
// setup
|
||||
handler := &handleClientCallURLs{}
|
||||
srvr := httptest.NewServer(handler)
|
||||
defer srvr.Close()
|
||||
req := &apimodel.URLsRequest{}
|
||||
ff := &fakeFill{}
|
||||
ff.fill(&req)
|
||||
clnt := &Client{KVStore: &MemKVStore{}, BaseURL: srvr.URL}
|
||||
ff.fill(&clnt.UserAgent)
|
||||
// issue request
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.URLs(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non-nil response here")
|
||||
}
|
||||
// compare our response and server's one
|
||||
if diff := cmp.Diff(handler.resp, resp); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
// check whether headers are OK
|
||||
if handler.accept != "application/json" {
|
||||
t.Fatal("invalid accept header")
|
||||
}
|
||||
if handler.userAgent != clnt.UserAgent {
|
||||
t.Fatal("invalid user-agent header")
|
||||
}
|
||||
// check whether the method is OK
|
||||
if handler.method != "GET" {
|
||||
t.Fatal("invalid method")
|
||||
}
|
||||
// check the query
|
||||
api := &simpleURLsAPI{BaseURL: srvr.URL}
|
||||
httpReq, err := api.newRequest(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.Path, httpReq.URL.Path); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
if diff := cmp.Diff(handler.url.RawQuery, httpReq.URL.RawQuery); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
type handleClientCallOpenReport struct {
|
||||
accept string
|
||||
body []byte
|
||||
contentType string
|
||||
count int32
|
||||
method string
|
||||
mu sync.Mutex
|
||||
resp *apimodel.OpenReportResponse
|
||||
url *url.URL
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func (h *handleClientCallOpenReport) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ff := fakeFill{}
|
||||
defer h.mu.Unlock()
|
||||
h.mu.Lock()
|
||||
if h.count > 0 {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.count++
|
||||
if r.Body != nil {
|
||||
data, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.body = data
|
||||
}
|
||||
h.method = r.Method
|
||||
h.url = r.URL
|
||||
h.accept = r.Header.Get("Accept")
|
||||
h.contentType = r.Header.Get("Content-Type")
|
||||
h.userAgent = r.Header.Get("User-Agent")
|
||||
var out *apimodel.OpenReportResponse
|
||||
ff.fill(&out)
|
||||
h.resp = out
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func TestOpenReportClientCallRoundTrip(t *testing.T) {
|
||||
// setup
|
||||
handler := &handleClientCallOpenReport{}
|
||||
srvr := httptest.NewServer(handler)
|
||||
defer srvr.Close()
|
||||
req := &apimodel.OpenReportRequest{}
|
||||
ff := &fakeFill{}
|
||||
ff.fill(&req)
|
||||
clnt := &Client{KVStore: &MemKVStore{}, BaseURL: srvr.URL}
|
||||
ff.fill(&clnt.UserAgent)
|
||||
// issue request
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.OpenReport(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non-nil response here")
|
||||
}
|
||||
// compare our response and server's one
|
||||
if diff := cmp.Diff(handler.resp, resp); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
// check whether headers are OK
|
||||
if handler.accept != "application/json" {
|
||||
t.Fatal("invalid accept header")
|
||||
}
|
||||
if handler.userAgent != clnt.UserAgent {
|
||||
t.Fatal("invalid user-agent header")
|
||||
}
|
||||
// check whether the method is OK
|
||||
if handler.method != "POST" {
|
||||
t.Fatal("invalid method")
|
||||
}
|
||||
// check the body
|
||||
if handler.contentType != "application/json" {
|
||||
t.Fatal("invalid content-type header")
|
||||
}
|
||||
got := &apimodel.OpenReportRequest{}
|
||||
if err := json.Unmarshal(handler.body, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(req, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
type handleClientCallSubmitMeasurement struct {
|
||||
accept string
|
||||
body []byte
|
||||
contentType string
|
||||
count int32
|
||||
method string
|
||||
mu sync.Mutex
|
||||
resp *apimodel.SubmitMeasurementResponse
|
||||
url *url.URL
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func (h *handleClientCallSubmitMeasurement) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
ff := fakeFill{}
|
||||
defer h.mu.Unlock()
|
||||
h.mu.Lock()
|
||||
if h.count > 0 {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.count++
|
||||
if r.Body != nil {
|
||||
data, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
h.body = data
|
||||
}
|
||||
h.method = r.Method
|
||||
h.url = r.URL
|
||||
h.accept = r.Header.Get("Accept")
|
||||
h.contentType = r.Header.Get("Content-Type")
|
||||
h.userAgent = r.Header.Get("User-Agent")
|
||||
var out *apimodel.SubmitMeasurementResponse
|
||||
ff.fill(&out)
|
||||
h.resp = out
|
||||
data, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
w.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func TestSubmitMeasurementClientCallRoundTrip(t *testing.T) {
|
||||
// setup
|
||||
handler := &handleClientCallSubmitMeasurement{}
|
||||
srvr := httptest.NewServer(handler)
|
||||
defer srvr.Close()
|
||||
req := &apimodel.SubmitMeasurementRequest{}
|
||||
ff := &fakeFill{}
|
||||
ff.fill(&req)
|
||||
clnt := &Client{KVStore: &MemKVStore{}, BaseURL: srvr.URL}
|
||||
ff.fill(&clnt.UserAgent)
|
||||
// issue request
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.SubmitMeasurement(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non-nil response here")
|
||||
}
|
||||
// compare our response and server's one
|
||||
if diff := cmp.Diff(handler.resp, resp); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
// check whether headers are OK
|
||||
if handler.accept != "application/json" {
|
||||
t.Fatal("invalid accept header")
|
||||
}
|
||||
if handler.userAgent != clnt.UserAgent {
|
||||
t.Fatal("invalid user-agent header")
|
||||
}
|
||||
// check whether the method is OK
|
||||
if handler.method != "POST" {
|
||||
t.Fatal("invalid method")
|
||||
}
|
||||
// check the body
|
||||
if handler.contentType != "application/json" {
|
||||
t.Fatal("invalid content-type header")
|
||||
}
|
||||
got := &apimodel.SubmitMeasurementRequest{}
|
||||
if err := json.Unmarshal(handler.body, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(req, got); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2021-05-12 09:15:04.793154609 +0200 CEST m=+0.000108739
|
||||
|
||||
package ooapi
|
||||
|
||||
//go:generate go run ./internal/generator -file cloners.go
|
||||
|
||||
// clonerForPsiphonConfigAPI represents any type exposing a method
|
||||
// like simplePsiphonConfigAPI.WithToken.
|
||||
type clonerForPsiphonConfigAPI interface {
|
||||
WithToken(token string) callerForPsiphonConfigAPI
|
||||
}
|
||||
|
||||
// clonerForTorTargetsAPI represents any type exposing a method
|
||||
// like simpleTorTargetsAPI.WithToken.
|
||||
type clonerForTorTargetsAPI interface {
|
||||
WithToken(token string) callerForTorTargetsAPI
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package ooapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type defaultRequestMaker struct{}
|
||||
|
||||
func (*defaultRequestMaker) NewRequest(
|
||||
ctx context.Context, method, URL string, body io.Reader) (*http.Request, error) {
|
||||
return http.NewRequestWithContext(ctx, method, URL, body)
|
||||
}
|
||||
|
||||
type defaultJSONCodec struct{}
|
||||
|
||||
func (*defaultJSONCodec) Encode(v interface{}) ([]byte, error) {
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
func (*defaultJSONCodec) Decode(b []byte, v interface{}) error {
|
||||
return json.Unmarshal(b, v)
|
||||
}
|
||||
|
||||
type defaultTemplateExecutor struct{}
|
||||
|
||||
func (*defaultTemplateExecutor) Execute(tmpl string, v interface{}) (string, error) {
|
||||
to, err := template.New("t").Parse(tmpl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var sb strings.Builder
|
||||
if err := to.Execute(&sb, v); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return sb.String(), nil
|
||||
}
|
||||
|
||||
type defaultGobCodec struct{}
|
||||
|
||||
func (*defaultGobCodec) Encode(v interface{}) ([]byte, error) {
|
||||
var bb bytes.Buffer
|
||||
if err := gob.NewEncoder(&bb).Encode(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bb.Bytes(), nil
|
||||
}
|
||||
|
||||
func (*defaultGobCodec) Decode(b []byte, v interface{}) error {
|
||||
return gob.NewDecoder(bytes.NewReader(b)).Decode(v)
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package ooapi
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDefaultTemplateExecutorParseError(t *testing.T) {
|
||||
te := &defaultTemplateExecutor{}
|
||||
out, err := te.Execute("{{ .Foo", nil)
|
||||
if err == nil || !strings.HasSuffix(err.Error(), "unclosed action") {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if out != "" {
|
||||
t.Fatal("expected empty string")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultTemplateExecutorExecError(t *testing.T) {
|
||||
te := &defaultTemplateExecutor{}
|
||||
arg := make(chan interface{})
|
||||
out, err := te.Execute("{{ .Foo }}", arg)
|
||||
if err == nil || !strings.Contains(err.Error(), `can't evaluate field Foo`) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if out != "" {
|
||||
t.Fatal("expected empty string")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultGobCodecEncodeError(t *testing.T) {
|
||||
codec := &defaultGobCodec{}
|
||||
arg := make(chan interface{})
|
||||
data, err := codec.Encode(arg)
|
||||
if err == nil || !strings.Contains(err.Error(), "can't handle type") {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if data != nil {
|
||||
t.Fatal("expected nil data")
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package ooapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// JSONCodec is a JSON encoder and decoder.
|
||||
type JSONCodec interface {
|
||||
// Encode encodes v as a serialized JSON byte slice.
|
||||
Encode(v interface{}) ([]byte, error)
|
||||
|
||||
// Decode decodes the serialized JSON byte slice into v.
|
||||
Decode(b []byte, v interface{}) error
|
||||
}
|
||||
|
||||
// RequestMaker makes an HTTP request.
|
||||
type RequestMaker interface {
|
||||
// NewRequest creates a new HTTP request.
|
||||
NewRequest(ctx context.Context, method, URL string, body io.Reader) (*http.Request, error)
|
||||
}
|
||||
|
||||
// templateExecutor parses and executes a text template.
|
||||
type templateExecutor interface {
|
||||
// Execute takes in input a template string and some piece of data. It
|
||||
// returns either a string where template parameters have been replaced,
|
||||
// on success, or an error, on failure.
|
||||
Execute(tmpl string, v interface{}) (string, error)
|
||||
}
|
||||
|
||||
// HTTPClient is the interface of a generic HTTP client.
|
||||
type HTTPClient interface {
|
||||
// Do should work like http.Client.Do.
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
// GobCodec is a Gob encoder and decoder.
|
||||
type GobCodec interface {
|
||||
// Encode encodes v as a serialized gob byte slice.
|
||||
Encode(v interface{}) ([]byte, error)
|
||||
|
||||
// Decode decodes the serialized gob byte slice into v.
|
||||
Decode(b []byte, v interface{}) error
|
||||
}
|
||||
|
||||
// KVStore is a key-value store.
|
||||
type KVStore interface {
|
||||
// Get gets a value from the key-value store.
|
||||
Get(key string) ([]byte, error)
|
||||
|
||||
// Set stores a value into the key-value store.
|
||||
Set(key string, value []byte) error
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
// Package ooapi contains a client for the OONI API. We
|
||||
// automatically generate the code in this package from the
|
||||
// apimodel and internal/generator packages.
|
||||
//
|
||||
// Usage
|
||||
//
|
||||
// You need to create a Client. Make sure you set all
|
||||
// the mandatory fields. You will then have a function
|
||||
// for every supported OONI API. This function will
|
||||
// take in input a context and a request. You need to
|
||||
// fill the request, of course. The return value is
|
||||
// either a response or an error.
|
||||
//
|
||||
// If an API requires login, we will automatically
|
||||
// perform the login. If an API uses caching, we will
|
||||
// automatically use the cache.
|
||||
//
|
||||
// See the example describing auto-login for more information
|
||||
// on how to use auto-login.
|
||||
//
|
||||
// Design
|
||||
//
|
||||
// Most of the code in this package is auto-generated from the
|
||||
// data model in ./apimodel and the definition of APIs provided
|
||||
// by ./internal/generator/spec.go.
|
||||
//
|
||||
// We keep the generated files up-to-date by running
|
||||
//
|
||||
// go generate ./...
|
||||
//
|
||||
// We have tests that ensure that the definition of the API
|
||||
// used here is reasonably close to the server's one.
|
||||
//
|
||||
// Testing
|
||||
//
|
||||
// The following command
|
||||
//
|
||||
// go test ./...
|
||||
//
|
||||
// will, among other things, ensure that the our API spec
|
||||
// is consistent with the server's one. Running
|
||||
//
|
||||
// go test -short ./...
|
||||
//
|
||||
// will exclude most (slow) integration tests.
|
||||
//
|
||||
// Architecture
|
||||
//
|
||||
// The ./apimodel package contains the definition of request
|
||||
// and response messages. We rely on tagging to specify how
|
||||
// we should encode and decode messages.
|
||||
//
|
||||
// The ./internal/generator contains code to generate most
|
||||
// code in this package. In particular, the spec.go file is
|
||||
// the specification of the APIs.
|
||||
package ooapi
|
||||
@@ -1,14 +0,0 @@
|
||||
package ooapi
|
||||
|
||||
import "errors"
|
||||
|
||||
// Errors defined by this package.
|
||||
var (
|
||||
ErrAPICallFailed = errors.New("ooapi: API call failed")
|
||||
ErrEmptyField = errors.New("ooapi: empty field")
|
||||
ErrHTTPFailure = errors.New("ooapi: http request failed")
|
||||
ErrJSONLiteralNull = errors.New("ooapi: server returned us a literal null")
|
||||
ErrMissingToken = errors.New("ooapi: missing auth token")
|
||||
ErrUnauthorized = errors.New("ooapi: not authorized")
|
||||
errCacheNotFound = errors.New("ooapi: not found in cache")
|
||||
)
|
||||
@@ -1,96 +0,0 @@
|
||||
package ooapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type FakeCodec struct {
|
||||
DecodeErr error
|
||||
EncodeData []byte
|
||||
EncodeErr error
|
||||
}
|
||||
|
||||
func (mc *FakeCodec) Encode(v interface{}) ([]byte, error) {
|
||||
return mc.EncodeData, mc.EncodeErr
|
||||
}
|
||||
|
||||
func (mc *FakeCodec) Decode(b []byte, v interface{}) error {
|
||||
return mc.DecodeErr
|
||||
}
|
||||
|
||||
type FakeHTTPClient struct {
|
||||
Err error
|
||||
Resp *http.Response
|
||||
}
|
||||
|
||||
func (c *FakeHTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
time.Sleep(10 * time.Microsecond)
|
||||
if req.Body != nil {
|
||||
_, _ = ioutil.ReadAll(req.Body)
|
||||
req.Body.Close()
|
||||
}
|
||||
if c.Err != nil {
|
||||
return nil, c.Err
|
||||
}
|
||||
c.Resp.Request = req // non thread safe but it doesn't matter
|
||||
return c.Resp, nil
|
||||
}
|
||||
|
||||
type FakeBody struct {
|
||||
Data []byte
|
||||
Err error
|
||||
}
|
||||
|
||||
func (fb *FakeBody) Read(p []byte) (int, error) {
|
||||
time.Sleep(10 * time.Microsecond)
|
||||
if fb.Err != nil {
|
||||
return 0, fb.Err
|
||||
}
|
||||
if len(fb.Data) <= 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
n := copy(p, fb.Data)
|
||||
fb.Data = fb.Data[n:]
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (fb *FakeBody) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type FakeRequestMaker struct {
|
||||
Req *http.Request
|
||||
Err error
|
||||
}
|
||||
|
||||
func (frm *FakeRequestMaker) NewRequest(
|
||||
ctx context.Context, method, URL string, body io.Reader) (*http.Request, error) {
|
||||
return frm.Req, frm.Err
|
||||
}
|
||||
|
||||
type FakeTemplateExecutor struct {
|
||||
Out string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (fte *FakeTemplateExecutor) Execute(tmpl string, v interface{}) (string, error) {
|
||||
return fte.Out, fte.Err
|
||||
}
|
||||
|
||||
type FakeKVStore struct {
|
||||
SetError error
|
||||
GetData []byte
|
||||
GetError error
|
||||
}
|
||||
|
||||
func (fs *FakeKVStore) Get(key string) ([]byte, error) {
|
||||
return fs.GetData, fs.GetError
|
||||
}
|
||||
|
||||
func (fs *FakeKVStore) Set(key string, value []byte) error {
|
||||
return fs.SetError
|
||||
}
|
||||
@@ -1,190 +0,0 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2021-05-12 09:15:05.331414434 +0200 CEST m=+0.000124504
|
||||
|
||||
package ooapi
|
||||
|
||||
//go:generate go run ./internal/generator -file fakeapi_test.go
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
||||
)
|
||||
|
||||
type FakeCheckReportIDAPI struct {
|
||||
Err error
|
||||
Response *apimodel.CheckReportIDResponse
|
||||
CountCall int32
|
||||
}
|
||||
|
||||
func (fapi *FakeCheckReportIDAPI) Call(ctx context.Context, req *apimodel.CheckReportIDRequest) (*apimodel.CheckReportIDResponse, error) {
|
||||
atomic.AddInt32(&fapi.CountCall, 1)
|
||||
return fapi.Response, fapi.Err
|
||||
}
|
||||
|
||||
var (
|
||||
_ callerForCheckReportIDAPI = &FakeCheckReportIDAPI{}
|
||||
)
|
||||
|
||||
type FakeCheckInAPI struct {
|
||||
Err error
|
||||
Response *apimodel.CheckInResponse
|
||||
CountCall int32
|
||||
}
|
||||
|
||||
func (fapi *FakeCheckInAPI) Call(ctx context.Context, req *apimodel.CheckInRequest) (*apimodel.CheckInResponse, error) {
|
||||
atomic.AddInt32(&fapi.CountCall, 1)
|
||||
return fapi.Response, fapi.Err
|
||||
}
|
||||
|
||||
var (
|
||||
_ callerForCheckInAPI = &FakeCheckInAPI{}
|
||||
)
|
||||
|
||||
type FakeLoginAPI struct {
|
||||
Err error
|
||||
Response *apimodel.LoginResponse
|
||||
CountCall int32
|
||||
}
|
||||
|
||||
func (fapi *FakeLoginAPI) Call(ctx context.Context, req *apimodel.LoginRequest) (*apimodel.LoginResponse, error) {
|
||||
atomic.AddInt32(&fapi.CountCall, 1)
|
||||
return fapi.Response, fapi.Err
|
||||
}
|
||||
|
||||
var (
|
||||
_ callerForLoginAPI = &FakeLoginAPI{}
|
||||
)
|
||||
|
||||
type FakeMeasurementMetaAPI struct {
|
||||
Err error
|
||||
Response *apimodel.MeasurementMetaResponse
|
||||
CountCall int32
|
||||
}
|
||||
|
||||
func (fapi *FakeMeasurementMetaAPI) Call(ctx context.Context, req *apimodel.MeasurementMetaRequest) (*apimodel.MeasurementMetaResponse, error) {
|
||||
atomic.AddInt32(&fapi.CountCall, 1)
|
||||
return fapi.Response, fapi.Err
|
||||
}
|
||||
|
||||
var (
|
||||
_ callerForMeasurementMetaAPI = &FakeMeasurementMetaAPI{}
|
||||
)
|
||||
|
||||
type FakeRegisterAPI struct {
|
||||
Err error
|
||||
Response *apimodel.RegisterResponse
|
||||
CountCall int32
|
||||
}
|
||||
|
||||
func (fapi *FakeRegisterAPI) Call(ctx context.Context, req *apimodel.RegisterRequest) (*apimodel.RegisterResponse, error) {
|
||||
atomic.AddInt32(&fapi.CountCall, 1)
|
||||
return fapi.Response, fapi.Err
|
||||
}
|
||||
|
||||
var (
|
||||
_ callerForRegisterAPI = &FakeRegisterAPI{}
|
||||
)
|
||||
|
||||
type FakeTestHelpersAPI struct {
|
||||
Err error
|
||||
Response apimodel.TestHelpersResponse
|
||||
CountCall int32
|
||||
}
|
||||
|
||||
func (fapi *FakeTestHelpersAPI) Call(ctx context.Context, req *apimodel.TestHelpersRequest) (apimodel.TestHelpersResponse, error) {
|
||||
atomic.AddInt32(&fapi.CountCall, 1)
|
||||
return fapi.Response, fapi.Err
|
||||
}
|
||||
|
||||
var (
|
||||
_ callerForTestHelpersAPI = &FakeTestHelpersAPI{}
|
||||
)
|
||||
|
||||
type FakePsiphonConfigAPI struct {
|
||||
WithResult callerForPsiphonConfigAPI
|
||||
Err error
|
||||
Response apimodel.PsiphonConfigResponse
|
||||
CountCall int32
|
||||
}
|
||||
|
||||
func (fapi *FakePsiphonConfigAPI) Call(ctx context.Context, req *apimodel.PsiphonConfigRequest) (apimodel.PsiphonConfigResponse, error) {
|
||||
atomic.AddInt32(&fapi.CountCall, 1)
|
||||
return fapi.Response, fapi.Err
|
||||
}
|
||||
|
||||
func (fapi *FakePsiphonConfigAPI) WithToken(token string) callerForPsiphonConfigAPI {
|
||||
return fapi.WithResult
|
||||
}
|
||||
|
||||
var (
|
||||
_ callerForPsiphonConfigAPI = &FakePsiphonConfigAPI{}
|
||||
_ clonerForPsiphonConfigAPI = &FakePsiphonConfigAPI{}
|
||||
)
|
||||
|
||||
type FakeTorTargetsAPI struct {
|
||||
WithResult callerForTorTargetsAPI
|
||||
Err error
|
||||
Response apimodel.TorTargetsResponse
|
||||
CountCall int32
|
||||
}
|
||||
|
||||
func (fapi *FakeTorTargetsAPI) Call(ctx context.Context, req *apimodel.TorTargetsRequest) (apimodel.TorTargetsResponse, error) {
|
||||
atomic.AddInt32(&fapi.CountCall, 1)
|
||||
return fapi.Response, fapi.Err
|
||||
}
|
||||
|
||||
func (fapi *FakeTorTargetsAPI) WithToken(token string) callerForTorTargetsAPI {
|
||||
return fapi.WithResult
|
||||
}
|
||||
|
||||
var (
|
||||
_ callerForTorTargetsAPI = &FakeTorTargetsAPI{}
|
||||
_ clonerForTorTargetsAPI = &FakeTorTargetsAPI{}
|
||||
)
|
||||
|
||||
type FakeURLsAPI struct {
|
||||
Err error
|
||||
Response *apimodel.URLsResponse
|
||||
CountCall int32
|
||||
}
|
||||
|
||||
func (fapi *FakeURLsAPI) Call(ctx context.Context, req *apimodel.URLsRequest) (*apimodel.URLsResponse, error) {
|
||||
atomic.AddInt32(&fapi.CountCall, 1)
|
||||
return fapi.Response, fapi.Err
|
||||
}
|
||||
|
||||
var (
|
||||
_ callerForURLsAPI = &FakeURLsAPI{}
|
||||
)
|
||||
|
||||
type FakeOpenReportAPI struct {
|
||||
Err error
|
||||
Response *apimodel.OpenReportResponse
|
||||
CountCall int32
|
||||
}
|
||||
|
||||
func (fapi *FakeOpenReportAPI) Call(ctx context.Context, req *apimodel.OpenReportRequest) (*apimodel.OpenReportResponse, error) {
|
||||
atomic.AddInt32(&fapi.CountCall, 1)
|
||||
return fapi.Response, fapi.Err
|
||||
}
|
||||
|
||||
var (
|
||||
_ callerForOpenReportAPI = &FakeOpenReportAPI{}
|
||||
)
|
||||
|
||||
type FakeSubmitMeasurementAPI struct {
|
||||
Err error
|
||||
Response *apimodel.SubmitMeasurementResponse
|
||||
CountCall int32
|
||||
}
|
||||
|
||||
func (fapi *FakeSubmitMeasurementAPI) Call(ctx context.Context, req *apimodel.SubmitMeasurementRequest) (*apimodel.SubmitMeasurementResponse, error) {
|
||||
atomic.AddInt32(&fapi.CountCall, 1)
|
||||
return fapi.Response, fapi.Err
|
||||
}
|
||||
|
||||
var (
|
||||
_ callerForSubmitMeasurementAPI = &FakeSubmitMeasurementAPI{}
|
||||
)
|
||||
@@ -1,146 +0,0 @@
|
||||
package ooapi
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
||||
)
|
||||
|
||||
// fakeFill fills specific data structures with random data. The only
|
||||
// exception to this behaviour is time.Time, which is instead filled
|
||||
// with the current time plus a small random number of seconds.
|
||||
//
|
||||
// We use this implementation to initialize data in our model. The code
|
||||
// has been written with that in mind. It will require some hammering in
|
||||
// case we extend the model with new field types.
|
||||
type fakeFill struct {
|
||||
mu sync.Mutex
|
||||
now func() time.Time
|
||||
rnd *rand.Rand
|
||||
}
|
||||
|
||||
func (ff *fakeFill) getRandLocked() *rand.Rand {
|
||||
if ff.rnd == nil {
|
||||
now := time.Now
|
||||
if ff.now != nil {
|
||||
now = ff.now
|
||||
}
|
||||
ff.rnd = rand.New(rand.NewSource(now().UnixNano()))
|
||||
}
|
||||
return ff.rnd
|
||||
}
|
||||
|
||||
func (ff *fakeFill) getRandomString() string {
|
||||
defer ff.mu.Unlock()
|
||||
ff.mu.Lock()
|
||||
rnd := ff.getRandLocked()
|
||||
n := rnd.Intn(63) + 1
|
||||
// See https://stackoverflow.com/a/31832326
|
||||
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
b := make([]rune, n)
|
||||
for i := range b {
|
||||
b[i] = letterRunes[rnd.Intn(len(letterRunes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (ff *fakeFill) getRandomInt64() int64 {
|
||||
defer ff.mu.Unlock()
|
||||
ff.mu.Lock()
|
||||
rnd := ff.getRandLocked()
|
||||
return rnd.Int63()
|
||||
}
|
||||
|
||||
func (ff *fakeFill) getRandomBool() bool {
|
||||
defer ff.mu.Unlock()
|
||||
ff.mu.Lock()
|
||||
rnd := ff.getRandLocked()
|
||||
return rnd.Float64() >= 0.5
|
||||
}
|
||||
|
||||
func (ff *fakeFill) getRandomSmallPositiveInt() int {
|
||||
defer ff.mu.Unlock()
|
||||
ff.mu.Lock()
|
||||
rnd := ff.getRandLocked()
|
||||
return int(rnd.Int63n(8)) + 1 // safe cast
|
||||
}
|
||||
|
||||
func (ff *fakeFill) doFill(v reflect.Value) {
|
||||
for v.Type().Kind() == reflect.Ptr {
|
||||
if v.IsNil() {
|
||||
// if the pointer is nil, allocate an element
|
||||
v.Set(reflect.New(v.Type().Elem()))
|
||||
}
|
||||
// switch to the element
|
||||
v = v.Elem()
|
||||
}
|
||||
switch v.Type().Kind() {
|
||||
case reflect.String:
|
||||
v.SetString(ff.getRandomString())
|
||||
case reflect.Int64:
|
||||
v.SetInt(ff.getRandomInt64())
|
||||
case reflect.Bool:
|
||||
v.SetBool(ff.getRandomBool())
|
||||
case reflect.Struct:
|
||||
if v.Type().String() == "time.Time" {
|
||||
// Implementation note: we treat the time specially
|
||||
// and we avoid attempting to set its fields.
|
||||
v.Set(reflect.ValueOf(time.Now().Add(
|
||||
time.Duration(ff.getRandomSmallPositiveInt()) * time.Second)))
|
||||
return
|
||||
}
|
||||
for idx := 0; idx < v.NumField(); idx++ {
|
||||
ff.doFill(v.Field(idx)) // visit all fields
|
||||
}
|
||||
case reflect.Slice:
|
||||
kind := v.Type().Elem()
|
||||
total := ff.getRandomSmallPositiveInt()
|
||||
for idx := 0; idx < total; idx++ {
|
||||
value := reflect.New(kind) // make a new element
|
||||
ff.doFill(value)
|
||||
v.Set(reflect.Append(v, value.Elem())) // append to slice
|
||||
}
|
||||
case reflect.Map:
|
||||
if v.Type().Key().Kind() != reflect.String {
|
||||
return // not supported
|
||||
}
|
||||
v.Set(reflect.MakeMap(v.Type())) // we need to init the map
|
||||
total := ff.getRandomSmallPositiveInt()
|
||||
kind := v.Type().Elem()
|
||||
for idx := 0; idx < total; idx++ {
|
||||
value := reflect.New(kind)
|
||||
ff.doFill(value)
|
||||
v.SetMapIndex(reflect.ValueOf(ff.getRandomString()), value.Elem())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fill fills in with random data.
|
||||
func (ff *fakeFill) fill(in interface{}) {
|
||||
ff.doFill(reflect.ValueOf(in))
|
||||
}
|
||||
|
||||
func TestFakeFillAllocatesIntoAPointerToPointer(t *testing.T) {
|
||||
var req *apimodel.URLsRequest
|
||||
ff := &fakeFill{}
|
||||
ff.fill(&req)
|
||||
if req == nil {
|
||||
t.Fatal("we expected non nil here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFakeFillAllocatesIntoAMapLike(t *testing.T) {
|
||||
var resp apimodel.TorTargetsResponse
|
||||
ff := &fakeFill{}
|
||||
ff.fill(&resp)
|
||||
if resp == nil {
|
||||
t.Fatal("we expected non nil here")
|
||||
}
|
||||
if len(resp) < 1 {
|
||||
t.Fatal("we expected some data here")
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package ooapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type VerboseHTTPClient struct {
|
||||
T *testing.T
|
||||
}
|
||||
|
||||
func (c *VerboseHTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
c.T.Logf("> %s %s", req.Method, req.URL.String())
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
c.T.Logf("< %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
c.T.Logf("< %d", resp.StatusCode)
|
||||
return resp, nil
|
||||
}
|
||||
@@ -1,165 +0,0 @@
|
||||
package ooapi_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
||||
)
|
||||
|
||||
func TestWithRealServerDoCheckIn(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
req := &apimodel.CheckInRequest{
|
||||
Charging: true,
|
||||
OnWiFi: true,
|
||||
Platform: "android",
|
||||
ProbeASN: "AS12353",
|
||||
ProbeCC: "IT",
|
||||
RunType: "timed",
|
||||
SoftwareName: "ooniprobe-android",
|
||||
SoftwareVersion: "2.7.1",
|
||||
WebConnectivity: apimodel.CheckInRequestWebConnectivity{
|
||||
CategoryCodes: []string{"NEWS", "CULTR"},
|
||||
},
|
||||
}
|
||||
httpClnt := &ooapi.VerboseHTTPClient{T: t}
|
||||
clnt := &ooapi.Client{HTTPClient: httpClnt, KVStore: &ooapi.MemKVStore{}}
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.CheckIn(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non nil pointer here")
|
||||
}
|
||||
for idx, url := range resp.Tests.WebConnectivity.URLs {
|
||||
if idx >= 3 {
|
||||
break
|
||||
}
|
||||
t.Logf("- %+v", url)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithRealServerDoCheckReportID(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
req := &apimodel.CheckReportIDRequest{
|
||||
ReportID: "20210223T093606Z_ndt_JO_8376_n1_kDYToqrugDY54Soy",
|
||||
}
|
||||
clnt := &ooapi.Client{KVStore: &ooapi.MemKVStore{}}
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.CheckReportID(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non nil pointer here")
|
||||
}
|
||||
t.Logf("%+v", resp)
|
||||
}
|
||||
|
||||
func TestWithRealServerDoMeasurementMeta(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
req := &apimodel.MeasurementMetaRequest{
|
||||
ReportID: "20210223T093606Z_ndt_JO_8376_n1_kDYToqrugDY54Soy",
|
||||
}
|
||||
clnt := &ooapi.Client{KVStore: &ooapi.MemKVStore{}}
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.MeasurementMeta(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non nil pointer here")
|
||||
}
|
||||
t.Logf("%+v", resp)
|
||||
}
|
||||
|
||||
func TestWithRealServerDoOpenReport(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
req := &apimodel.OpenReportRequest{
|
||||
DataFormatVersion: "0.2.0",
|
||||
Format: "json",
|
||||
ProbeASN: "AS137",
|
||||
ProbeCC: "IT",
|
||||
SoftwareName: "miniooni",
|
||||
SoftwareVersion: "0.1.0-dev",
|
||||
TestName: "example",
|
||||
TestStartTime: "2018-11-01 15:33:20",
|
||||
TestVersion: "0.1.0",
|
||||
}
|
||||
clnt := &ooapi.Client{KVStore: &ooapi.MemKVStore{}}
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.OpenReport(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non nil pointer here")
|
||||
}
|
||||
t.Logf("%+v", resp)
|
||||
}
|
||||
|
||||
func TestWithRealServerDoPsiphonConfig(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
req := &apimodel.PsiphonConfigRequest{}
|
||||
httpClnt := &ooapi.VerboseHTTPClient{T: t}
|
||||
clnt := &ooapi.Client{HTTPClient: httpClnt, KVStore: &ooapi.MemKVStore{}}
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.PsiphonConfig(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non nil pointer here")
|
||||
}
|
||||
t.Logf("%+v", resp != nil)
|
||||
}
|
||||
|
||||
func TestWithRealServerDoTorTargets(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
req := &apimodel.TorTargetsRequest{}
|
||||
httpClnt := &ooapi.VerboseHTTPClient{T: t}
|
||||
clnt := &ooapi.Client{HTTPClient: httpClnt, KVStore: &ooapi.MemKVStore{}}
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.TorTargets(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non nil pointer here")
|
||||
}
|
||||
t.Logf("%+v", resp != nil)
|
||||
}
|
||||
|
||||
func TestWithRealServerDoURLs(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skip test in short mode")
|
||||
}
|
||||
req := &apimodel.URLsRequest{
|
||||
CountryCode: "IT",
|
||||
Limit: 3,
|
||||
}
|
||||
clnt := &ooapi.Client{KVStore: &ooapi.MemKVStore{}}
|
||||
ctx := context.Background()
|
||||
resp, err := clnt.URLs(ctx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("expected non nil pointer here")
|
||||
}
|
||||
t.Logf("%+v", resp)
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// apiField contains the fields of an API data structure
|
||||
type apiField struct {
|
||||
// name is the field name
|
||||
name string
|
||||
|
||||
// kind is the filed type
|
||||
kind string
|
||||
|
||||
// comment is a brief comment to document the field
|
||||
comment string
|
||||
|
||||
// ifLogin indicates whether this field should only be
|
||||
// emitted when the API requires login
|
||||
ifLogin bool
|
||||
|
||||
// ifTemplate indicates whether this field should only be
|
||||
// emitted when the URL path is a template
|
||||
ifTemplate bool
|
||||
|
||||
// noClone is true when this field should not be copied
|
||||
// from the parent data structure when cloning
|
||||
noClone bool
|
||||
}
|
||||
|
||||
var apiFields = []apiField{{
|
||||
name: "BaseURL",
|
||||
kind: "string",
|
||||
comment: "optional",
|
||||
}, {
|
||||
name: "HTTPClient",
|
||||
kind: "HTTPClient",
|
||||
comment: "optional",
|
||||
}, {
|
||||
name: "JSONCodec",
|
||||
kind: "JSONCodec",
|
||||
comment: "optional",
|
||||
}, {
|
||||
name: "Token",
|
||||
kind: "string",
|
||||
comment: "mandatory",
|
||||
ifLogin: true,
|
||||
noClone: true,
|
||||
}, {
|
||||
name: "RequestMaker",
|
||||
kind: "RequestMaker",
|
||||
comment: "optional",
|
||||
}, {
|
||||
name: "TemplateExecutor",
|
||||
kind: "templateExecutor",
|
||||
comment: "optional",
|
||||
ifTemplate: true,
|
||||
}, {
|
||||
name: "UserAgent",
|
||||
kind: "string",
|
||||
comment: "optional",
|
||||
}}
|
||||
|
||||
func (d *Descriptor) genNewAPI(sb *strings.Builder) {
|
||||
fmt.Fprintf(sb, "// %s implements the %s API.\n", d.APIStructName(), d.Name)
|
||||
fmt.Fprintf(sb, "type %s struct {\n", d.APIStructName())
|
||||
for _, f := range apiFields {
|
||||
if !d.RequiresLogin && f.ifLogin {
|
||||
continue
|
||||
}
|
||||
if !d.URLPath.IsTemplate && f.ifTemplate {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(sb, "\t%s %s // %s\n", f.name, f.kind, f.comment)
|
||||
}
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
|
||||
if d.RequiresLogin {
|
||||
fmt.Fprintf(sb, "// WithToken returns a copy of the API where the\n")
|
||||
fmt.Fprintf(sb, "// value of the Token field is replaced with token.\n")
|
||||
fmt.Fprintf(sb, "func (api *%s) WithToken(token string) %s {\n",
|
||||
d.APIStructName(), d.CallerInterfaceName())
|
||||
fmt.Fprintf(sb, "out := &%s{}\n", d.APIStructName())
|
||||
for _, f := range apiFields {
|
||||
if !d.URLPath.IsTemplate && f.ifTemplate {
|
||||
continue
|
||||
}
|
||||
if f.noClone == true {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(sb, "out.%s = api.%s\n", f.name, f.name)
|
||||
}
|
||||
fmt.Fprint(sb, "out.Token = token\n")
|
||||
fmt.Fprint(sb, "return out\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
fmt.Fprintf(sb, "func (api *%s) baseURL() string {\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\tif api.BaseURL != \"\" {\n")
|
||||
fmt.Fprint(sb, "\t\treturn api.BaseURL\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\treturn \"https://ps1.ooni.io\"\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
|
||||
fmt.Fprintf(sb, "func (api *%s) requestMaker() RequestMaker {\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\tif api.RequestMaker != nil {\n")
|
||||
fmt.Fprint(sb, "\t\treturn api.RequestMaker\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\treturn &defaultRequestMaker{}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
|
||||
fmt.Fprintf(sb, "func (api *%s) jsonCodec() JSONCodec {\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\tif api.JSONCodec != nil {\n")
|
||||
fmt.Fprint(sb, "\t\treturn api.JSONCodec\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\treturn &defaultJSONCodec{}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
|
||||
if d.URLPath.IsTemplate {
|
||||
fmt.Fprintf(
|
||||
sb, "func (api *%s) templateExecutor() templateExecutor {\n",
|
||||
d.APIStructName())
|
||||
fmt.Fprint(sb, "\tif api.TemplateExecutor != nil {\n")
|
||||
fmt.Fprint(sb, "\t\treturn api.TemplateExecutor\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\treturn &defaultTemplateExecutor{}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
fmt.Fprintf(
|
||||
sb, "func (api *%s) httpClient() HTTPClient {\n",
|
||||
d.APIStructName())
|
||||
fmt.Fprint(sb, "\tif api.HTTPClient != nil {\n")
|
||||
fmt.Fprint(sb, "\t\treturn api.HTTPClient\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\treturn http.DefaultClient\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
|
||||
fmt.Fprintf(sb, "// Call calls the %s API.\n", d.Name)
|
||||
fmt.Fprintf(
|
||||
sb, "func (api *%s) Call(ctx context.Context, req %s) (%s, error) {\n",
|
||||
d.APIStructName(), d.RequestTypeName(), d.ResponseTypeName())
|
||||
fmt.Fprint(sb, "\thttpReq, err := api.newRequest(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif err != nil {\n")
|
||||
fmt.Fprint(sb, "\t\treturn nil, err\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\thttpReq.Header.Add(\"Accept\", \"application/json\")\n")
|
||||
if d.RequiresLogin {
|
||||
fmt.Fprint(sb, "\tif api.Token == \"\" {\n")
|
||||
fmt.Fprint(sb, "\t\treturn nil, ErrMissingToken\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\thttpReq.Header.Add(\"Authorization\", newAuthorizationHeader(api.Token))\n")
|
||||
}
|
||||
fmt.Fprint(sb, "\tif api.UserAgent != \"\" {\n")
|
||||
fmt.Fprint(sb, "\t\thttpReq.Header.Add(\"User-Agent\", api.UserAgent)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\treturn api.newResponse(api.httpClient().Do(httpReq))\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
// GenAPIsGo generates apis.go.
|
||||
func GenAPIsGo(file string) {
|
||||
var sb strings.Builder
|
||||
fmt.Fprint(&sb, "// Code generated by go generate; DO NOT EDIT.\n")
|
||||
fmt.Fprintf(&sb, "// %s\n\n", time.Now())
|
||||
fmt.Fprint(&sb, "package ooapi\n\n")
|
||||
fmt.Fprintf(&sb, "//go:generate go run ./internal/generator -file %s\n\n", file)
|
||||
fmt.Fprint(&sb, "import (\n")
|
||||
fmt.Fprint(&sb, "\t\"context\"\n")
|
||||
fmt.Fprint(&sb, "\t\"net/http\"\n")
|
||||
fmt.Fprint(&sb, "\n")
|
||||
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel\"\n")
|
||||
fmt.Fprint(&sb, ")\n")
|
||||
for _, desc := range Descriptors {
|
||||
desc.genNewAPI(&sb)
|
||||
}
|
||||
writefile(file, &sb)
|
||||
}
|
||||
@@ -1,461 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (d *Descriptor) genTestNewRequest(sb *strings.Builder) {
|
||||
fmt.Fprintf(sb, "\treq := &%s{}\n", d.RequestTypeNameAsStruct())
|
||||
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
|
||||
fmt.Fprint(sb, "\tff.fill(req)\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestInvalidURL(sb *strings.Builder) {
|
||||
fmt.Fprintf(sb, "func Test%sInvalidURL(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprintf(sb, "\tapi := &%s{\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\t\tBaseURL: \"\\t\", // invalid\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
d.genTestNewRequest(sb)
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif err == nil || !strings.HasSuffix(err.Error(), \"invalid control character in URL\") {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestWithMissingToken(sb *strings.Builder) {
|
||||
if d.RequiresLogin == false {
|
||||
return // does not make sense when login isn't required
|
||||
}
|
||||
fmt.Fprintf(sb, "func Test%sWithMissingToken(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprintf(sb, "\tapi := &%s{} // no token\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
d.genTestNewRequest(sb)
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif !errors.Is(err, ErrMissingToken) {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestWithHTTPErr(sb *strings.Builder) {
|
||||
fmt.Fprintf(sb, "func Test%sWithHTTPErr(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
|
||||
fmt.Fprint(sb, "\tclnt := &FakeHTTPClient{Err: errMocked}\n")
|
||||
fmt.Fprintf(sb, "\tapi := &%s{\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\t\tHTTPClient: clnt,\n")
|
||||
if d.RequiresLogin == true {
|
||||
fmt.Fprint(sb, "\t\tToken: \"fakeToken\",\n")
|
||||
}
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
d.genTestNewRequest(sb)
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif !errors.Is(err, errMocked) {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestMarshalErr(sb *strings.Builder) {
|
||||
if d.Method != "POST" {
|
||||
return // does not make sense when we don't send a request body
|
||||
}
|
||||
fmt.Fprintf(sb, "func Test%sMarshalErr(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
|
||||
fmt.Fprintf(sb, "\tapi := &%s{\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\t\tJSONCodec: &FakeCodec{EncodeErr: errMocked},\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
d.genTestNewRequest(sb)
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif !errors.Is(err, errMocked) {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestWithNewRequestErr(sb *strings.Builder) {
|
||||
fmt.Fprintf(sb, "func Test%sWithNewRequestErr(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
|
||||
fmt.Fprintf(sb, "\tapi := &%s{\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\t\tRequestMaker: &FakeRequestMaker{Err: errMocked},\n")
|
||||
if d.RequiresLogin == true {
|
||||
fmt.Fprint(sb, "\t\tToken: \"fakeToken\",\n")
|
||||
}
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
d.genTestNewRequest(sb)
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif !errors.Is(err, errMocked) {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestWith401(sb *strings.Builder) {
|
||||
fmt.Fprintf(sb, "func Test%sWith401(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprint(sb, "\tclnt := &FakeHTTPClient{Resp: &http.Response{StatusCode: 401}}\n")
|
||||
fmt.Fprintf(sb, "\tapi := &%s{\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\t\tHTTPClient: clnt,\n")
|
||||
if d.RequiresLogin == true {
|
||||
fmt.Fprint(sb, "\t\tToken: \"fakeToken\",\n")
|
||||
}
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
d.genTestNewRequest(sb)
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif !errors.Is(err, ErrUnauthorized) {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestWith400(sb *strings.Builder) {
|
||||
fmt.Fprintf(sb, "func Test%sWith400(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprint(sb, "\tclnt := &FakeHTTPClient{Resp: &http.Response{StatusCode: 400}}\n")
|
||||
fmt.Fprintf(sb, "\tapi := &%s{\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\t\tHTTPClient: clnt,\n")
|
||||
if d.RequiresLogin == true {
|
||||
fmt.Fprint(sb, "\t\tToken: \"fakeToken\",\n")
|
||||
}
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
d.genTestNewRequest(sb)
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif !errors.Is(err, ErrHTTPFailure) {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestWithResponseBodyReadErr(sb *strings.Builder) {
|
||||
fmt.Fprintf(sb, "func Test%sWithResponseBodyReadErr(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
|
||||
fmt.Fprint(sb, "\tclnt := &FakeHTTPClient{Resp: &http.Response{\n")
|
||||
fmt.Fprint(sb, "\t\tStatusCode: 200,\n")
|
||||
fmt.Fprint(sb, "\t\tBody: &FakeBody{Err: errMocked},\n")
|
||||
fmt.Fprint(sb, "\t}}\n")
|
||||
fmt.Fprintf(sb, "\tapi := &%s{\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\t\tHTTPClient: clnt,\n")
|
||||
if d.RequiresLogin == true {
|
||||
fmt.Fprint(sb, "\t\tToken: \"fakeToken\",\n")
|
||||
}
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
d.genTestNewRequest(sb)
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif !errors.Is(err, errMocked) {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestWithUnmarshalFailure(sb *strings.Builder) {
|
||||
fmt.Fprintf(sb, "func Test%sWithUnmarshalFailure(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
|
||||
fmt.Fprint(sb, "\tclnt := &FakeHTTPClient{Resp: &http.Response{\n")
|
||||
fmt.Fprint(sb, "\t\tStatusCode: 200,\n")
|
||||
fmt.Fprint(sb, "\t\tBody: &FakeBody{Data: []byte(`{}`)},\n")
|
||||
fmt.Fprint(sb, "\t}}\n")
|
||||
fmt.Fprintf(sb, "\tapi := &%s{\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\t\tHTTPClient: clnt,\n")
|
||||
fmt.Fprintf(sb, "\t\tJSONCodec: &FakeCodec{DecodeErr: errMocked},\n")
|
||||
if d.RequiresLogin == true {
|
||||
fmt.Fprint(sb, "\t\tToken: \"fakeToken\",\n")
|
||||
}
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
d.genTestNewRequest(sb)
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif !errors.Is(err, errMocked) {\n")
|
||||
fmt.Fprintf(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestRoundTrip(sb *strings.Builder) {
|
||||
// generate the type of the handler
|
||||
fmt.Fprintf(sb, "type handle%s struct {\n", d.Name)
|
||||
fmt.Fprint(sb, "\taccept string\n")
|
||||
fmt.Fprint(sb, "\tbody []byte\n")
|
||||
fmt.Fprint(sb, "\tcontentType string\n")
|
||||
fmt.Fprint(sb, "\tcount int32\n")
|
||||
fmt.Fprint(sb, "\tmethod string\n")
|
||||
fmt.Fprint(sb, "\tmu sync.Mutex\n")
|
||||
fmt.Fprintf(sb, "\tresp %s\n", d.ResponseTypeName())
|
||||
fmt.Fprint(sb, "\turl *url.URL\n")
|
||||
fmt.Fprint(sb, "\tuserAgent string\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
|
||||
// generate the handling function
|
||||
fmt.Fprintf(sb,
|
||||
"func (h *handle%s) ServeHTTP(w http.ResponseWriter, r *http.Request) {",
|
||||
d.Name)
|
||||
fmt.Fprint(sb, "\tdefer h.mu.Unlock()\n")
|
||||
fmt.Fprint(sb, "\th.mu.Lock()\n")
|
||||
fmt.Fprint(sb, "\tif h.count > 0 {\n")
|
||||
fmt.Fprint(sb, "\t\tw.WriteHeader(400)\n")
|
||||
fmt.Fprint(sb, "\t\treturn\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\th.count++\n")
|
||||
fmt.Fprint(sb, "\tif r.Body != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tdata, err := ioutil.ReadAll(r.Body)\n")
|
||||
fmt.Fprint(sb, "\t\tif err != nil {\n")
|
||||
fmt.Fprintf(sb, "\t\t\tw.WriteHeader(400)\n")
|
||||
fmt.Fprintf(sb, "\t\t\treturn\n")
|
||||
fmt.Fprint(sb, "\t\t}\n")
|
||||
fmt.Fprint(sb, "\t\th.body = data\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\th.method = r.Method\n")
|
||||
fmt.Fprint(sb, "\th.url = r.URL\n")
|
||||
fmt.Fprint(sb, "\th.accept = r.Header.Get(\"Accept\")\n")
|
||||
fmt.Fprint(sb, "\th.contentType = r.Header.Get(\"Content-Type\")\n")
|
||||
fmt.Fprint(sb, "\th.userAgent = r.Header.Get(\"User-Agent\")\n")
|
||||
fmt.Fprintf(sb, "\tvar out %s\n", d.ResponseTypeName())
|
||||
fmt.Fprint(sb, "\tff := fakeFill{}\n")
|
||||
fmt.Fprint(sb, "\tff.fill(&out)\n")
|
||||
fmt.Fprintf(sb, "\th.resp = out\n")
|
||||
fmt.Fprintf(sb, "\tdata, err := json.Marshal(out)\n")
|
||||
fmt.Fprintf(sb, "\tif err != nil {\n")
|
||||
fmt.Fprintf(sb, "\t\tw.WriteHeader(400)\n")
|
||||
fmt.Fprintf(sb, "\t\treturn\n")
|
||||
fmt.Fprintf(sb, "\t}\n")
|
||||
fmt.Fprintf(sb, "\tw.Write(data)\n")
|
||||
fmt.Fprintf(sb, "\t}\n\n")
|
||||
|
||||
// generate the test itself
|
||||
fmt.Fprintf(sb, "func Test%sRoundTrip(t *testing.T) {\n", d.Name)
|
||||
|
||||
fmt.Fprint(sb, "\t// setup\n")
|
||||
fmt.Fprintf(sb, "\thandler := &handle%s{}\n", d.Name)
|
||||
fmt.Fprint(sb, "\tsrvr := httptest.NewServer(handler)\n")
|
||||
fmt.Fprint(sb, "\tdefer srvr.Close()\n")
|
||||
fmt.Fprintf(sb, "\treq := &%s{}\n", d.RequestTypeNameAsStruct())
|
||||
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
|
||||
fmt.Fprint(sb, "\tff.fill(&req)\n")
|
||||
fmt.Fprintf(sb, "\tapi := &%s{BaseURL: srvr.URL}\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\tff.fill(&api.UserAgent)\n")
|
||||
if d.RequiresLogin {
|
||||
fmt.Fprint(sb, "\tff.fill(&api.Token)\n")
|
||||
}
|
||||
|
||||
fmt.Fprint(sb, "\t// issue request\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif err != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp == nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected non-nil response here\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
|
||||
fmt.Fprint(sb, "\t// compare our response and server's one\n")
|
||||
fmt.Fprint(sb, "\tif diff := cmp.Diff(handler.resp, resp); diff != \"\" {")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(diff)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
|
||||
fmt.Fprint(sb, "\t// check whether headers are OK\n")
|
||||
fmt.Fprint(sb, "\tif handler.accept != \"application/json\" {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid accept header\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif handler.userAgent != api.UserAgent {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid user-agent header\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
|
||||
fmt.Fprint(sb, "\t// check whether the method is OK\n")
|
||||
fmt.Fprintf(sb, "\tif handler.method != \"%s\" {\n", d.Method)
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid method\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
|
||||
if d.Method == "POST" {
|
||||
fmt.Fprint(sb, "\t// check the body\n")
|
||||
fmt.Fprint(sb, "\tif handler.contentType != \"application/json\" {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid content-type header\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprintf(sb, "\tgot := &%s{}\n", d.RequestTypeNameAsStruct())
|
||||
fmt.Fprintf(sb, "\tif err := json.Unmarshal(handler.body, &got); err != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif diff := cmp.Diff(req, got); diff != \"\" {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(diff)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
} else {
|
||||
fmt.Fprint(sb, "\t// check the query\n")
|
||||
fmt.Fprint(sb, "\thttpReq, err := api.newRequest(context.Background(), req)\n")
|
||||
fmt.Fprint(sb, "\tif err != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif diff := cmp.Diff(handler.url.Path, httpReq.URL.Path); diff != \"\" {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(diff)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif diff := cmp.Diff(handler.url.RawQuery, httpReq.URL.RawQuery); diff != \"\" {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(diff)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
}
|
||||
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestResponseLiteralNull(sb *strings.Builder) {
|
||||
switch d.ResponseTypeKind() {
|
||||
case reflect.Map:
|
||||
// fallthrough
|
||||
case reflect.Struct:
|
||||
return // test not applicable
|
||||
}
|
||||
fmt.Fprintf(sb, "func Test%sResponseLiteralNull(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprint(sb, "\tclnt := &FakeHTTPClient{Resp: &http.Response{\n")
|
||||
fmt.Fprint(sb, "\t\tStatusCode: 200,\n")
|
||||
fmt.Fprint(sb, "\t\tBody: &FakeBody{Data: []byte(`null`)},\n")
|
||||
fmt.Fprint(sb, "\t}}\n")
|
||||
fmt.Fprintf(sb, "\tapi := &%s{\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\t\tHTTPClient: clnt,\n")
|
||||
if d.RequiresLogin == true {
|
||||
fmt.Fprint(sb, "\t\tToken: \"fakeToken\",\n")
|
||||
}
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
d.genTestNewRequest(sb)
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif !errors.Is(err, ErrJSONLiteralNull) {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestMandatoryFields(sb *strings.Builder) {
|
||||
fields := d.StructFieldsWithTag(d.Request, tagForRequired)
|
||||
if len(fields) < 1 {
|
||||
return // nothing to test
|
||||
}
|
||||
fmt.Fprintf(sb, "func Test%sMandatoryFields(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprint(sb, "\tclnt := &FakeHTTPClient{Resp: &http.Response{\n")
|
||||
fmt.Fprint(sb, "\t\tStatusCode: 500,\n")
|
||||
fmt.Fprint(sb, "\t}}\n")
|
||||
fmt.Fprintf(sb, "\tapi := &%s{\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\t\tHTTPClient: clnt,\n")
|
||||
if d.RequiresLogin == true {
|
||||
fmt.Fprint(sb, "\t\tToken: \"fakeToken\",\n")
|
||||
}
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
fmt.Fprintf(sb, "\treq := &%s{} // deliberately empty\n", d.RequestTypeNameAsStruct())
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif !errors.Is(err, ErrEmptyField) {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
func (d *Descriptor) genTestTemplateErr(sb *strings.Builder) {
|
||||
if !d.URLPath.IsTemplate {
|
||||
return // nothing to test
|
||||
}
|
||||
fmt.Fprintf(sb, "func Test%sTemplateErr(t *testing.T) {\n", d.Name)
|
||||
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
|
||||
fmt.Fprint(sb, "\tclnt := &FakeHTTPClient{Resp: &http.Response{\n")
|
||||
fmt.Fprint(sb, "\t\tStatusCode: 500,\n")
|
||||
fmt.Fprint(sb, "\t}}\n")
|
||||
fmt.Fprintf(sb, "\tapi := &%s{\n", d.APIStructName())
|
||||
fmt.Fprint(sb, "\t\tHTTPClient: clnt,\n")
|
||||
if d.RequiresLogin == true {
|
||||
fmt.Fprint(sb, "\t\tToken: \"fakeToken\",\n")
|
||||
}
|
||||
fmt.Fprint(sb, "\t\tTemplateExecutor: &FakeTemplateExecutor{Err: errMocked},\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tctx := context.Background()\n")
|
||||
d.genTestNewRequest(sb)
|
||||
fmt.Fprint(sb, "\tresp, err := api.Call(ctx, req)\n")
|
||||
fmt.Fprint(sb, "\tif !errors.Is(err, errMocked) {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "\tif resp != nil {\n")
|
||||
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil resp\")\n")
|
||||
fmt.Fprint(sb, "\t}\n")
|
||||
fmt.Fprint(sb, "}\n\n")
|
||||
}
|
||||
|
||||
// TODO(bassosimone): we should add a panic for every switch for
|
||||
// the type of a request or a response for robustness.
|
||||
|
||||
func (d *Descriptor) genAPITests(sb *strings.Builder) {
|
||||
d.genTestInvalidURL(sb)
|
||||
d.genTestWithMissingToken(sb)
|
||||
d.genTestWithHTTPErr(sb)
|
||||
d.genTestMarshalErr(sb)
|
||||
d.genTestWithNewRequestErr(sb)
|
||||
d.genTestWith401(sb)
|
||||
d.genTestWith400(sb)
|
||||
d.genTestWithResponseBodyReadErr(sb)
|
||||
d.genTestWithUnmarshalFailure(sb)
|
||||
d.genTestRoundTrip(sb)
|
||||
d.genTestResponseLiteralNull(sb)
|
||||
d.genTestMandatoryFields(sb)
|
||||
d.genTestTemplateErr(sb)
|
||||
}
|
||||
|
||||
// GenAPIsTestGo generates apis_test.go.
|
||||
func GenAPIsTestGo(file string) {
|
||||
var sb strings.Builder
|
||||
fmt.Fprint(&sb, "// Code generated by go generate; DO NOT EDIT.\n")
|
||||
fmt.Fprintf(&sb, "// %s\n\n", time.Now())
|
||||
fmt.Fprint(&sb, "package ooapi\n\n")
|
||||
fmt.Fprintf(&sb, "//go:generate go run ./internal/generator -file %s\n\n", file)
|
||||
fmt.Fprint(&sb, "import (\n")
|
||||
fmt.Fprint(&sb, "\t\"context\"\n")
|
||||
fmt.Fprint(&sb, "\t\"encoding/json\"\n")
|
||||
fmt.Fprint(&sb, "\t\"errors\"\n")
|
||||
fmt.Fprint(&sb, "\t\"io/ioutil\"\n")
|
||||
fmt.Fprint(&sb, "\t\"net/http/httptest\"\n")
|
||||
fmt.Fprint(&sb, "\t\"net/http\"\n")
|
||||
fmt.Fprint(&sb, "\t\"net/url\"\n")
|
||||
fmt.Fprint(&sb, "\t\"strings\"\n")
|
||||
fmt.Fprint(&sb, "\t\"testing\"\n")
|
||||
fmt.Fprint(&sb, "\t\"sync\"\n")
|
||||
fmt.Fprint(&sb, "\n")
|
||||
fmt.Fprint(&sb, "\t\"github.com/google/go-cmp/cmp\"\n")
|
||||
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel\"\n")
|
||||
fmt.Fprint(&sb, ")\n")
|
||||
for _, desc := range Descriptors {
|
||||
desc.genAPITests(&sb)
|
||||
}
|
||||
writefile(file, &sb)
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user