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:
Simone Basso
2021-06-04 10:34:18 +02:00
committed by GitHub
parent 2a7fdcd810
commit 33de701263
169 changed files with 1137 additions and 1004 deletions
+47
View File
@@ -0,0 +1,47 @@
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"`
}
+13
View File
@@ -0,0 +1,13 @@
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"`
}
+22
View File
@@ -0,0 +1,22 @@
// 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
+15
View File
@@ -0,0 +1,15 @@
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"`
}
@@ -0,0 +1,25 @@
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"`
}
+21
View File
@@ -0,0 +1,21 @@
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"`
}
+7
View File
@@ -0,0 +1,7 @@
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{}
+26
View File
@@ -0,0 +1,26 @@
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"`
}
@@ -0,0 +1,13 @@
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"`
}
+15
View File
@@ -0,0 +1,15 @@
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"`
}
+16
View File
@@ -0,0 +1,16 @@
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"`
}
+26
View File
@@ -0,0 +1,26 @@
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"`
}
+607
View File
@@ -0,0 +1,607 @@
// 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/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
+98
View File
@@ -0,0 +1,98 @@
// 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/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{}
+223
View File
@@ -0,0 +1,223 @@
// 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/kvstore"
"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel"
)
func TestCachesimpleMeasurementMetaAPISuccess(t *testing.T) {
ff := &fakeFill{}
var expect *apimodel.MeasurementMetaResponse
ff.fill(&expect)
cache := &withCacheMeasurementMetaAPI{
API: &FakeMeasurementMetaAPI{
Response: expect,
},
KVStore: &kvstore.Memory{},
}
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: &kvstore.Memory{},
}
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: &kvstore.Memory{},
}
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: &kvstore.Memory{},
}
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: &kvstore.Memory{},
}
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: &kvstore.Memory{},
}
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
}
}
+78
View File
@@ -0,0 +1,78 @@
// 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/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)
}
+13
View File
@@ -0,0 +1,13 @@
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
}
+214
View File
@@ -0,0 +1,214 @@
// 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/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)
}
+899
View File
@@ -0,0 +1,899 @@
// 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/kvstore"
"github.com/ooni/probe-cli/v3/internal/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: &kvstore.Memory{}, 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: &kvstore.Memory{}, 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: &kvstore.Memory{}, 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: &kvstore.Memory{}, 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: &kvstore.Memory{}, 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: &kvstore.Memory{}, 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: &kvstore.Memory{}, 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: &kvstore.Memory{}, 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: &kvstore.Memory{}, 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)
}
}
+18
View File
@@ -0,0 +1,18 @@
// 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
}
+57
View File
@@ -0,0 +1,57 @@
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)
}
+41
View File
@@ -0,0 +1,41 @@
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")
}
}
+54
View File
@@ -0,0 +1,54 @@
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
}
+56
View File
@@ -0,0 +1,56 @@
// 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
+14
View File
@@ -0,0 +1,14 @@
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")
)
+96
View File
@@ -0,0 +1,96 @@
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
}
+212
View File
@@ -0,0 +1,212 @@
// 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"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel"
)
type FakeCheckReportIDAPI struct {
Err error
Response *apimodel.CheckReportIDResponse
CountCall *atomicx.Int64
}
func (fapi *FakeCheckReportIDAPI) Call(ctx context.Context, req *apimodel.CheckReportIDRequest) (*apimodel.CheckReportIDResponse, error) {
if fapi.CountCall != nil {
fapi.CountCall.Add(1)
}
return fapi.Response, fapi.Err
}
var (
_ callerForCheckReportIDAPI = &FakeCheckReportIDAPI{}
)
type FakeCheckInAPI struct {
Err error
Response *apimodel.CheckInResponse
CountCall *atomicx.Int64
}
func (fapi *FakeCheckInAPI) Call(ctx context.Context, req *apimodel.CheckInRequest) (*apimodel.CheckInResponse, error) {
if fapi.CountCall != nil {
fapi.CountCall.Add(1)
}
return fapi.Response, fapi.Err
}
var (
_ callerForCheckInAPI = &FakeCheckInAPI{}
)
type FakeLoginAPI struct {
Err error
Response *apimodel.LoginResponse
CountCall *atomicx.Int64
}
func (fapi *FakeLoginAPI) Call(ctx context.Context, req *apimodel.LoginRequest) (*apimodel.LoginResponse, error) {
if fapi.CountCall != nil {
fapi.CountCall.Add(1)
}
return fapi.Response, fapi.Err
}
var (
_ callerForLoginAPI = &FakeLoginAPI{}
)
type FakeMeasurementMetaAPI struct {
Err error
Response *apimodel.MeasurementMetaResponse
CountCall *atomicx.Int64
}
func (fapi *FakeMeasurementMetaAPI) Call(ctx context.Context, req *apimodel.MeasurementMetaRequest) (*apimodel.MeasurementMetaResponse, error) {
if fapi.CountCall != nil {
fapi.CountCall.Add(1)
}
return fapi.Response, fapi.Err
}
var (
_ callerForMeasurementMetaAPI = &FakeMeasurementMetaAPI{}
)
type FakeRegisterAPI struct {
Err error
Response *apimodel.RegisterResponse
CountCall *atomicx.Int64
}
func (fapi *FakeRegisterAPI) Call(ctx context.Context, req *apimodel.RegisterRequest) (*apimodel.RegisterResponse, error) {
if fapi.CountCall != nil {
fapi.CountCall.Add(1)
}
return fapi.Response, fapi.Err
}
var (
_ callerForRegisterAPI = &FakeRegisterAPI{}
)
type FakeTestHelpersAPI struct {
Err error
Response apimodel.TestHelpersResponse
CountCall *atomicx.Int64
}
func (fapi *FakeTestHelpersAPI) Call(ctx context.Context, req *apimodel.TestHelpersRequest) (apimodel.TestHelpersResponse, error) {
if fapi.CountCall != nil {
fapi.CountCall.Add(1)
}
return fapi.Response, fapi.Err
}
var (
_ callerForTestHelpersAPI = &FakeTestHelpersAPI{}
)
type FakePsiphonConfigAPI struct {
WithResult callerForPsiphonConfigAPI
Err error
Response apimodel.PsiphonConfigResponse
CountCall *atomicx.Int64
}
func (fapi *FakePsiphonConfigAPI) Call(ctx context.Context, req *apimodel.PsiphonConfigRequest) (apimodel.PsiphonConfigResponse, error) {
if fapi.CountCall != nil {
fapi.CountCall.Add(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 *atomicx.Int64
}
func (fapi *FakeTorTargetsAPI) Call(ctx context.Context, req *apimodel.TorTargetsRequest) (apimodel.TorTargetsResponse, error) {
if fapi.CountCall != nil {
fapi.CountCall.Add(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 *atomicx.Int64
}
func (fapi *FakeURLsAPI) Call(ctx context.Context, req *apimodel.URLsRequest) (*apimodel.URLsResponse, error) {
if fapi.CountCall != nil {
fapi.CountCall.Add(1)
}
return fapi.Response, fapi.Err
}
var (
_ callerForURLsAPI = &FakeURLsAPI{}
)
type FakeOpenReportAPI struct {
Err error
Response *apimodel.OpenReportResponse
CountCall *atomicx.Int64
}
func (fapi *FakeOpenReportAPI) Call(ctx context.Context, req *apimodel.OpenReportRequest) (*apimodel.OpenReportResponse, error) {
if fapi.CountCall != nil {
fapi.CountCall.Add(1)
}
return fapi.Response, fapi.Err
}
var (
_ callerForOpenReportAPI = &FakeOpenReportAPI{}
)
type FakeSubmitMeasurementAPI struct {
Err error
Response *apimodel.SubmitMeasurementResponse
CountCall *atomicx.Int64
}
func (fapi *FakeSubmitMeasurementAPI) Call(ctx context.Context, req *apimodel.SubmitMeasurementRequest) (*apimodel.SubmitMeasurementResponse, error) {
if fapi.CountCall != nil {
fapi.CountCall.Add(1)
}
return fapi.Response, fapi.Err
}
var (
_ callerForSubmitMeasurementAPI = &FakeSubmitMeasurementAPI{}
)
+146
View File
@@ -0,0 +1,146 @@
package ooapi
import (
"math/rand"
"reflect"
"sync"
"testing"
"time"
"github.com/ooni/probe-cli/v3/internal/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")
}
}
+21
View File
@@ -0,0 +1,21 @@
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
}
+166
View File
@@ -0,0 +1,166 @@
package ooapi_test
import (
"context"
"testing"
"github.com/ooni/probe-cli/v3/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/ooapi"
"github.com/ooni/probe-cli/v3/internal/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: &kvstore.Memory{}}
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: &kvstore.Memory{}}
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: &kvstore.Memory{}}
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: &kvstore.Memory{}}
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: &kvstore.Memory{}}
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: &kvstore.Memory{}}
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: &kvstore.Memory{}}
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)
}
+180
View File
@@ -0,0 +1,180 @@
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/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n")
for _, desc := range Descriptors {
desc.genNewAPI(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,461 @@
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/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n")
for _, desc := range Descriptors {
desc.genAPITests(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,130 @@
package main
import (
"fmt"
"strings"
"time"
)
func (d *Descriptor) genNewCache(sb *strings.Builder) {
fmt.Fprintf(sb, "// %s implements caching for %s.\n",
d.WithCacheAPIStructName(), d.APIStructName())
fmt.Fprintf(sb, "type %s struct {\n", d.WithCacheAPIStructName())
fmt.Fprintf(sb, "\tAPI %s // mandatory\n", d.CallerInterfaceName())
fmt.Fprint(sb, "\tGobCodec GobCodec // optional\n")
fmt.Fprint(sb, "\tKVStore KVStore // mandatory\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "type %s struct {\n", d.CacheEntryName())
fmt.Fprintf(sb, "\tReq %s\n", d.RequestTypeName())
fmt.Fprintf(sb, "\tResp %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "// Call calls the API and implements caching.\n")
fmt.Fprintf(sb, "func (c *%s) Call(ctx context.Context, req %s) (%s, error) {\n",
d.WithCacheAPIStructName(), d.RequestTypeName(), d.ResponseTypeName())
if d.CachePolicy == CacheAlways {
fmt.Fprint(sb, "\tif resp, _ := c.readcache(req); resp != nil {\n")
fmt.Fprint(sb, "\t\treturn resp, nil\n")
fmt.Fprint(sb, "\t}\n")
}
fmt.Fprint(sb, "\tresp, err := c.API.Call(ctx, req)\n")
fmt.Fprint(sb, "\tif err != nil {\n")
if d.CachePolicy == CacheFallback {
fmt.Fprint(sb, "\t\tif resp, _ := c.readcache(req); resp != nil {\n")
fmt.Fprint(sb, "\t\t\treturn resp, nil\n")
fmt.Fprint(sb, "\t\t}\n")
}
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif err := c.writecache(req, resp); err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn resp, nil\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (c *%s) gobCodec() GobCodec {\n", d.WithCacheAPIStructName())
fmt.Fprint(sb, "\tif c.GobCodec != nil {\n")
fmt.Fprint(sb, "\t\treturn c.GobCodec\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn &defaultGobCodec{}\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (c *%s) getcache() ([]%s, error) {\n",
d.WithCacheAPIStructName(), d.CacheEntryName())
fmt.Fprintf(sb, "\tdata, err := c.KVStore.Get(\"%s\")\n", d.CacheKey())
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar out []%s\n", d.CacheEntryName())
fmt.Fprint(sb, "\tif err := c.gobCodec().Decode(data, &out); err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn out, nil\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (c *%s) setcache(in []%s) error {\n",
d.WithCacheAPIStructName(), d.CacheEntryName())
fmt.Fprint(sb, "\tdata, err := c.gobCodec().Encode(in)\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\treturn c.KVStore.Set(\"%s\", data)\n", d.CacheKey())
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (c *%s) readcache(req %s) (%s, error) {\n",
d.WithCacheAPIStructName(), d.RequestTypeName(), d.ResponseTypeName())
fmt.Fprint(sb, "\tcache, err := c.getcache()\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tfor _, cur := range cache {\n")
fmt.Fprint(sb, "\t\tif reflect.DeepEqual(req, cur.Req) {\n")
fmt.Fprint(sb, "\t\t\treturn cur.Resp, nil\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn nil, errCacheNotFound\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (c *%s) writecache(req %s, resp %s) error {\n",
d.WithCacheAPIStructName(), d.RequestTypeName(), d.ResponseTypeName())
fmt.Fprint(sb, "\tcache, _ := c.getcache()\n")
fmt.Fprintf(sb, "\tout := []%s{{Req: req, Resp: resp}}\n", d.CacheEntryName())
fmt.Fprint(sb, "\tconst toomany = 64\n")
fmt.Fprint(sb, "\tfor idx, cur := range cache {\n")
fmt.Fprint(sb, "\t\tif reflect.DeepEqual(req, cur.Req) {\n")
fmt.Fprint(sb, "\t\t\tcontinue // we already updated the cache\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif idx > toomany {\n")
fmt.Fprint(sb, "\t\t\tbreak\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tout = append(out, cur)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn c.setcache(out)\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "var _ %s = &%s{}\n\n", d.CallerInterfaceName(),
d.WithCacheAPIStructName())
}
// GenCachingGo generates caching.go.
func GenCachingGo(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\"reflect\"\n")
fmt.Fprint(&sb, "\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n")
for _, desc := range Descriptors {
if desc.CachePolicy == CacheNone {
continue
}
desc.genNewCache(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,275 @@
package main
import (
"fmt"
"strings"
"time"
)
func (d *Descriptor) genTestCacheSuccess(sb *strings.Builder) {
fmt.Fprintf(sb, "func TestCache%sSuccess(t *testing.T) {\n", d.APIStructName())
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar expect %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&expect)\n")
fmt.Fprintf(sb, "\tcache := &%s{\n", d.WithCacheAPIStructName())
fmt.Fprintf(sb, "\t\tAPI: &%s{\n", d.FakeAPIStructName())
fmt.Fprint(sb, "\t\t\tResponse: expect,\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\tresp, err := cache.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\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif diff := cmp.Diff(expect, resp); diff != \"\" {\n")
fmt.Fprint(sb, "\t\tt.Fatal(diff)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestWriteCacheError(sb *strings.Builder) {
fmt.Fprintf(sb, "func TestCache%sWriteCacheError(t *testing.T) {\n", d.APIStructName())
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar expect %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&expect)\n")
fmt.Fprintf(sb, "\tcache := &%s{\n", d.WithCacheAPIStructName())
fmt.Fprintf(sb, "\t\tAPI: &%s{\n", d.FakeAPIStructName())
fmt.Fprint(sb, "\t\t\tResponse: expect,\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tKVStore: &FakeKVStore{SetError: errMocked},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\tresp, err := cache.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 response\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestFailureWithNoCache(sb *strings.Builder) {
fmt.Fprintf(sb, "func TestCache%sFailureWithNoCache(t *testing.T) {\n", d.APIStructName())
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tcache := &%s{\n", d.WithCacheAPIStructName())
fmt.Fprintf(sb, "\t\tAPI: &%s{\n", d.FakeAPIStructName())
fmt.Fprint(sb, "\t\t\tErr: errMocked,\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\tresp, err := cache.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 response\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestFailureWithPreviousCache(sb *strings.Builder) {
// This works for both caching policies.
fmt.Fprintf(sb, "func TestCache%sFailureWithPreviousCache(t *testing.T) {\n", d.APIStructName())
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar expect %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&expect)\n")
fmt.Fprintf(sb, "\tfakeapi := &%s{\n", d.FakeAPIStructName())
fmt.Fprint(sb, "\t\tResponse: expect,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tcache := &%s{\n", d.WithCacheAPIStructName())
fmt.Fprint(sb, "\t\tAPI: fakeapi,\n")
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\t// first pass with no error at all\n")
fmt.Fprint(sb, "\t// use a separate scope to be sure we avoid mistakes\n")
fmt.Fprint(sb, "\t{\n")
fmt.Fprint(sb, "\t\tresp, err := cache.Call(ctx, req)\n")
fmt.Fprint(sb, "\t\tif err != nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(err)\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif resp == nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"expected non-nil response\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif diff := cmp.Diff(expect, resp); diff != \"\" {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(diff)\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t// second pass with failure\n")
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
fmt.Fprint(sb, "\tfakeapi.Err = errMocked\n")
fmt.Fprint(sb, "\tfakeapi.Response = nil\n")
fmt.Fprint(sb, "\tresp2, err := cache.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 resp2 == nil {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"expected non-nil response\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif diff := cmp.Diff(expect, resp2); diff != \"\" {\n")
fmt.Fprint(sb, "\t\tt.Fatal(diff)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestSetcacheWithEncodeError(sb *strings.Builder) {
fmt.Fprintf(sb, "func TestCache%sSetcacheWithEncodeError(t *testing.T) {\n", d.APIStructName())
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
fmt.Fprintf(sb, "\tvar in []%s\n", d.CacheEntryName())
fmt.Fprint(sb, "\tff.fill(&in)\n")
fmt.Fprintf(sb, "\tcache := &%s{\n", d.WithCacheAPIStructName())
fmt.Fprint(sb, "\t\tGobCodec: &FakeCodec{EncodeErr: errMocked},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\terr := cache.setcache(in)\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, "}\n\n")
}
func (d *Descriptor) genTestReadCacheNotFound(sb *strings.Builder) {
if fields := d.StructFields(d.Request); len(fields) <= 0 {
// this test cannot work when there are no fields in the
// request because we will always find a match.
// TODO(bassosimone): how to avoid having uncovered code?
return
}
fmt.Fprintf(sb, "func TestCache%sReadCacheNotFound(t *testing.T) {\n", d.APIStructName())
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar incache []%s\n", d.CacheEntryName())
fmt.Fprint(sb, "\tff.fill(&incache)\n")
fmt.Fprintf(sb, "\tcache := &%s{\n", d.WithCacheAPIStructName())
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\terr := cache.setcache(incache)\n")
fmt.Fprintf(sb, "\tif err != nil {\n")
fmt.Fprintf(sb, "\t\tt.Fatal(err)\n")
fmt.Fprintf(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprintf(sb, "\tout, err := cache.readcache(req)\n")
fmt.Fprint(sb, "\tif !errors.Is(err, errCacheNotFound) {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif out != nil {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"expected nil here\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestWriteCacheDuplicate(sb *strings.Builder) {
fmt.Fprintf(sb, "func TestCache%sWriteCacheDuplicate(t *testing.T) {\n", d.APIStructName())
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprintf(sb, "\tvar resp1 %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&resp1)\n")
fmt.Fprintf(sb, "\tvar resp2 %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&resp2)\n")
fmt.Fprintf(sb, "\tcache := &%s{\n", d.WithCacheAPIStructName())
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\terr := cache.writecache(req, resp1)\n")
fmt.Fprintf(sb, "\tif err != nil {\n")
fmt.Fprintf(sb, "\t\tt.Fatal(err)\n")
fmt.Fprintf(sb, "\t}\n")
fmt.Fprintf(sb, "\terr = cache.writecache(req, resp2)\n")
fmt.Fprintf(sb, "\tif err != nil {\n")
fmt.Fprintf(sb, "\t\tt.Fatal(err)\n")
fmt.Fprintf(sb, "\t}\n")
fmt.Fprintf(sb, "\tout, err := cache.readcache(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 out == nil {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"expected non-nil here\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif diff := cmp.Diff(resp2, out); diff != \"\" {\n")
fmt.Fprint(sb, "\t\tt.Fatal(diff)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestCachSizeLimited(sb *strings.Builder) {
if fields := d.StructFields(d.Request); len(fields) <= 0 {
// this test cannot work when there are no fields in the
// request because we will always find a match.
// TODO(bassosimone): how to avoid having uncovered code?
return
}
fmt.Fprintf(sb, "func TestCache%sCacheSizeLimited(t *testing.T) {\n", d.APIStructName())
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tcache := &%s{\n", d.WithCacheAPIStructName())
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar prev int\n")
fmt.Fprintf(sb, "\tfor {\n")
fmt.Fprintf(sb, "\t\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\t\tff.fill(&req)\n")
fmt.Fprintf(sb, "\t\tvar resp %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\t\tff.fill(&resp)\n")
fmt.Fprintf(sb, "\t\terr := cache.writecache(req, resp)\n")
fmt.Fprintf(sb, "\t\tif err != nil {\n")
fmt.Fprintf(sb, "\t\t\tt.Fatal(err)\n")
fmt.Fprintf(sb, "\t\t}\n")
fmt.Fprintf(sb, "\t\tout, err := cache.getcache()\n")
fmt.Fprint(sb, "\t\tif err != nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(err)\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif len(out) > prev {\n")
fmt.Fprint(sb, "\t\t\tprev = len(out)\n")
fmt.Fprint(sb, "\t\t\tcontinue\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tbreak\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
// GenCachingTestGo generates caching_test.go.
func GenCachingTestGo(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\"errors\"\n")
fmt.Fprint(&sb, "\t\"testing\"\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/kvstore\"\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n")
for _, desc := range Descriptors {
if desc.CachePolicy == CacheNone {
continue
}
desc.genTestCacheSuccess(&sb)
desc.genTestWriteCacheError(&sb)
desc.genTestFailureWithNoCache(&sb)
desc.genTestFailureWithPreviousCache(&sb)
desc.genTestSetcacheWithEncodeError(&sb)
desc.genTestReadCacheNotFound(&sb)
desc.genTestWriteCacheDuplicate(&sb)
desc.genTestCachSizeLimited(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,35 @@
package main
import (
"fmt"
"strings"
"time"
)
func (d *Descriptor) genNewCaller(sb *strings.Builder) {
fmt.Fprintf(sb, "// %s represents any type exposing a method\n",
d.CallerInterfaceName())
fmt.Fprintf(sb, "// like %s.Call.\n", d.APIStructName())
fmt.Fprintf(sb, "type %s interface {\n", d.CallerInterfaceName())
fmt.Fprintf(sb, "\tCall(ctx context.Context, req %s) (%s, error)\n",
d.RequestTypeName(), d.ResponseTypeName())
fmt.Fprint(sb, "}\n\n")
}
// GenCallersGo generates callers.go.
func GenCallersGo(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, "\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n")
for _, desc := range Descriptors {
desc.genNewCaller(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,104 @@
package main
import (
"fmt"
"strings"
"time"
)
func (d *Descriptor) clientMakeAPIBase(sb *strings.Builder) {
fmt.Fprintf(sb, "&%s{\n", d.APIStructName())
for _, field := range apiFields {
if field.ifLogin || field.ifTemplate {
continue
}
fmt.Fprintf(sb, "\t%s: c.%s,\n", field.name, field.name)
}
fmt.Fprint(sb, "}")
}
func (d *Descriptor) clientMakeAPI(sb *strings.Builder) {
if d.RequiresLogin && d.CachePolicy != CacheNone {
panic("we don't support requiresLogin with caching")
}
if d.RequiresLogin {
fmt.Fprintf(sb, "&%s{\n", d.WithLoginAPIStructName())
fmt.Fprint(sb, "\tAPI:")
d.clientMakeAPIBase(sb)
fmt.Fprint(sb, ",\n")
fmt.Fprint(sb, "\tJSONCodec: c.JSONCodec,\n")
fmt.Fprint(sb, "\tKVStore: c.KVStore,\n")
fmt.Fprint(sb, "\tRegisterAPI: &simpleRegisterAPI{\n")
for _, field := range apiFields {
if field.ifLogin || field.ifTemplate {
continue
}
fmt.Fprintf(sb, "\t%s: c.%s,\n", field.name, field.name)
}
fmt.Fprint(sb, "\t},\n")
fmt.Fprint(sb, "\tLoginAPI: &simpleLoginAPI{\n")
for _, field := range apiFields {
if field.ifLogin || field.ifTemplate {
continue
}
fmt.Fprintf(sb, "\t%s: c.%s,\n", field.name, field.name)
}
fmt.Fprint(sb, "\t},\n")
fmt.Fprint(sb, "}\n")
return
}
if d.CachePolicy != CacheNone {
fmt.Fprintf(sb, "&%s{\n", d.WithCacheAPIStructName())
fmt.Fprint(sb, "\tAPI:")
d.clientMakeAPIBase(sb)
fmt.Fprint(sb, ",\n")
fmt.Fprint(sb, "\tGobCodec: c.GobCodec,\n")
fmt.Fprint(sb, "\tKVStore: c.KVStore,\n")
fmt.Fprint(sb, "}\n")
return
}
d.clientMakeAPIBase(sb)
fmt.Fprint(sb, "\n")
}
func (d *Descriptor) genClientNewCaller(sb *strings.Builder) {
fmt.Fprintf(sb, "func (c *Client) new%sCaller() ", d.Name)
fmt.Fprintf(sb, "%s {\n", d.CallerInterfaceName())
fmt.Fprint(sb, "\treturn ")
d.clientMakeAPI(sb)
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genClientCall(sb *strings.Builder) {
fmt.Fprintf(sb, "// %s calls the %s API.\n", d.Name, d.Name)
fmt.Fprintf(sb, "func (c *Client) %s(\n", d.Name)
fmt.Fprintf(sb, "ctx context.Context, req %s,\n) ", d.RequestTypeName())
fmt.Fprintf(sb, "(%s, error) {\n", d.ResponseTypeName())
fmt.Fprintf(sb, "\tapi := c.new%sCaller()\n", d.Name)
fmt.Fprint(sb, "\treturn api.Call(ctx, req)\n")
fmt.Fprint(sb, "}\n\n")
}
// GenClientCallGo generates clientcall.go.
func GenClientCallGo(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, "\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n")
for _, desc := range Descriptors {
switch desc.Name {
case "Register", "Login":
// We don't want to generate these APIs as toplevel.
continue
}
desc.genClientNewCaller(&sb)
desc.genClientCall(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,182 @@
package main
import (
"fmt"
"strings"
"time"
)
func (d *Descriptor) genTestClientCallRoundTrip(sb *strings.Builder) {
// generate the type of the handler
fmt.Fprintf(sb, "type handleClientCall%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 *handleClientCall%s) ServeHTTP(w http.ResponseWriter, r *http.Request) {",
d.Name)
fmt.Fprint(sb, "\tff := fakeFill{}\n")
if d.RequiresLogin {
fmt.Fprintf(sb, "\tif r.URL.Path == \"/api/v1/register\" {\n")
fmt.Fprintf(sb, "\t\tvar out apimodel.RegisterResponse\n")
fmt.Fprintf(sb, "\t\tff.fill(&out)\n")
fmt.Fprintf(sb, "\t\tdata, err := json.Marshal(out)\n")
fmt.Fprintf(sb, "\t\tif err != nil {\n")
fmt.Fprintf(sb, "\t\t\tw.WriteHeader(400)\n")
fmt.Fprintf(sb, "\t\t\treturn\n")
fmt.Fprintf(sb, "\t\t}\n")
fmt.Fprintf(sb, "\t\tw.Write(data)\n")
fmt.Fprintf(sb, "\t\treturn\n")
fmt.Fprintf(sb, "\t}\n")
fmt.Fprintf(sb, "\tif r.URL.Path == \"/api/v1/login\" {\n")
fmt.Fprintf(sb, "\t\tvar out apimodel.LoginResponse\n")
fmt.Fprintf(sb, "\t\tff.fill(&out)\n")
fmt.Fprintf(sb, "\t\tdata, err := json.Marshal(out)\n")
fmt.Fprintf(sb, "\t\tif err != nil {\n")
fmt.Fprintf(sb, "\t\t\tw.WriteHeader(400)\n")
fmt.Fprintf(sb, "\t\t\treturn\n")
fmt.Fprintf(sb, "\t\t}\n")
fmt.Fprintf(sb, "\t\tw.Write(data)\n")
fmt.Fprintf(sb, "\t\treturn\n")
fmt.Fprintf(sb, "\t}\n")
}
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.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%sClientCallRoundTrip(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\t// setup\n")
fmt.Fprintf(sb, "\thandler := &handleClientCall%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.Fprint(sb, "\tclnt := &Client{KVStore: &kvstore.Memory{}, BaseURL: srvr.URL}\n")
fmt.Fprint(sb, "\tff.fill(&clnt.UserAgent)\n")
fmt.Fprint(sb, "\t// issue request\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprintf(sb, "\tresp, err := clnt.%s(ctx, req)\n", d.Name)
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 != clnt.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.Fprintf(sb, "\tapi := &%s{BaseURL: srvr.URL}\n", d.APIStructName())
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")
}
// GenClientCallTestGo generates clientcall_test.go.
func GenClientCallTestGo(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\"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\"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/kvstore\"\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n")
for _, desc := range Descriptors {
if desc.Name == "Login" || desc.Name == "Register" {
continue // they cannot be called directly
}
desc.genTestClientCallRoundTrip(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,32 @@
package main
import (
"fmt"
"strings"
"time"
)
func (d *Descriptor) genNewCloner(sb *strings.Builder) {
fmt.Fprintf(sb, "// %s represents any type exposing a method\n",
d.ClonerInterfaceName())
fmt.Fprintf(sb, "// like %s.WithToken.\n", d.APIStructName())
fmt.Fprintf(sb, "type %s interface {\n", d.ClonerInterfaceName())
fmt.Fprintf(sb, "\tWithToken(token string) %s\n", d.CallerInterfaceName())
fmt.Fprint(sb, "}\n\n")
}
// GenClonersGo generates cloners.go.
func GenClonersGo(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)
for _, desc := range Descriptors {
if !desc.RequiresLogin {
continue
}
desc.genNewCloner(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,61 @@
package main
import (
"fmt"
"strings"
"time"
)
func (d *Descriptor) genNewFakeAPI(sb *strings.Builder) {
fmt.Fprintf(sb, "type %s struct {\n", d.FakeAPIStructName())
if d.RequiresLogin {
fmt.Fprintf(sb, "\tWithResult %s\n", d.CallerInterfaceName())
}
fmt.Fprint(sb, "\tErr error\n")
fmt.Fprintf(sb, "\tResponse %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tCountCall *atomicx.Int64\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (fapi *%s) Call(ctx context.Context, req %s) (%s, error) {\n",
d.FakeAPIStructName(), d.RequestTypeName(), d.ResponseTypeName())
fmt.Fprint(sb, "\tif fapi.CountCall != nil {\n")
fmt.Fprint(sb, "\t\tfapi.CountCall.Add(1)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn fapi.Response, fapi.Err\n")
fmt.Fprint(sb, "}\n\n")
if d.RequiresLogin {
fmt.Fprintf(sb, "func (fapi *%s) WithToken(token string) %s {\n",
d.FakeAPIStructName(), d.CallerInterfaceName())
fmt.Fprint(sb, "\treturn fapi.WithResult\n")
fmt.Fprint(sb, "}\n\n")
}
fmt.Fprint(sb, "var (\n")
fmt.Fprintf(sb, "\t_ %s = &%s{}\n", d.CallerInterfaceName(),
d.FakeAPIStructName())
if d.RequiresLogin {
fmt.Fprintf(sb, "\t_ %s = &%s{}\n", d.ClonerInterfaceName(),
d.FakeAPIStructName())
}
fmt.Fprint(sb, ")\n\n")
}
// GenFakeAPITestGo generates fakeapi_test.go.
func GenFakeAPITestGo(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, "\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/atomicx\"\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n")
for _, desc := range Descriptors {
desc.genNewFakeAPI(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,57 @@
// Command generator generates code in the ooapi package.
//
// To this end, it uses the content of the apimodel package as
// well as the content of the spec.go file.
//
// The apimodel package defines the model, i.e., the structure
// of requests and responses and how messages should be sent
// and received.
//
// The spec.go file describes all the implemented APIs.
//
// If you change apimodel or spec.go, remember to run the
// `go generate ./...` command to regenerate all files.
package main
import (
"flag"
"fmt"
)
var flagFile = flag.String("file", "", "Indicate which file to regenerate")
func main() {
flag.Parse()
switch file := *flagFile; file {
case "apis.go":
GenAPIsGo(file)
case "responses.go":
GenResponsesGo(file)
case "requests.go":
GenRequestsGo(file)
case "swagger_test.go":
GenSwaggerTestGo(file)
case "apis_test.go":
GenAPIsTestGo(file)
case "callers.go":
GenCallersGo(file)
case "caching.go":
GenCachingGo(file)
case "login.go":
GenLoginGo(file)
case "cloners.go":
GenClonersGo(file)
case "fakeapi_test.go":
GenFakeAPITestGo(file)
case "caching_test.go":
GenCachingTestGo(file)
case "login_test.go":
GenLoginTestGo(file)
case "clientcall.go":
GenClientCallGo(file)
case "clientcall_test.go":
GenClientCallTestGo(file)
default:
panic(fmt.Sprintf("don't know how to create this file: %s", file))
}
}
+182
View File
@@ -0,0 +1,182 @@
package main
import (
"fmt"
"strings"
"time"
)
func (d *Descriptor) genNewLogin(sb *strings.Builder) {
fmt.Fprintf(sb, "// %s implements login for %s.\n",
d.WithLoginAPIStructName(), d.APIStructName())
fmt.Fprintf(sb, "type %s struct {\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\tAPI %s // mandatory\n", d.ClonerInterfaceName())
fmt.Fprint(sb, "\tJSONCodec JSONCodec // optional\n")
fmt.Fprint(sb, "\tKVStore KVStore // mandatory\n")
fmt.Fprint(sb, "\tRegisterAPI callerForRegisterAPI // mandatory\n")
fmt.Fprint(sb, "\tLoginAPI callerForLoginAPI // mandatory\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "// Call logins, if needed, then calls the API.\n")
fmt.Fprintf(sb, "func (api *%s) Call(ctx context.Context, req %s) (%s, error) {\n",
d.WithLoginAPIStructName(), d.RequestTypeName(), d.ResponseTypeName())
fmt.Fprint(sb, "\ttoken, err := api.maybeLogin(ctx)\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tresp, err := api.API.WithToken(token).Call(ctx, req)\n")
fmt.Fprint(sb, "\tif errors.Is(err, ErrUnauthorized) {\n")
fmt.Fprint(sb, "\t\t// Maybe the clock is just off? Let's try to obtain\n")
fmt.Fprint(sb, "\t\t// a token again and see if this fixes it.\n")
fmt.Fprint(sb, "\t\tif token, err = api.forceLogin(ctx); err == nil {\n")
fmt.Fprint(sb, "\t\t\tswitch resp, err = api.API.WithToken(token).Call(ctx, req); err {\n")
fmt.Fprint(sb, "\t\t\tcase nil:\n")
fmt.Fprint(sb, "\t\t\t\treturn resp, nil\n")
fmt.Fprint(sb, "\t\t\tcase ErrUnauthorized:\n")
fmt.Fprint(sb, "\t\t\t\t// fallthrough\n")
fmt.Fprint(sb, "\t\t\tdefault:\n")
fmt.Fprint(sb, "\t\t\t\treturn nil, err\n")
fmt.Fprint(sb, "\t\t\t}\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\t// Okay, this seems a broader problem. How about we try\n")
fmt.Fprint(sb, "\t\t// and re-register ourselves again instead?\n")
fmt.Fprint(sb, "\t\ttoken, err = api.forceRegister(ctx)\n")
fmt.Fprint(sb, "\t\tif err != nil {\n")
fmt.Fprint(sb, "\t\t\treturn nil, err\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tresp, err = api.API.WithToken(token).Call(ctx, req)\n")
fmt.Fprint(sb, "\t\t// fallthrough\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn resp, nil\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (api *%s) jsonCodec() JSONCodec {\n",
d.WithLoginAPIStructName())
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")
fmt.Fprintf(sb, "func (api *%s) readstate() (*loginState, error) {\n",
d.WithLoginAPIStructName())
fmt.Fprint(sb, "\tdata, err := api.KVStore.Get(loginKey)\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tvar ls loginState\n")
fmt.Fprint(sb, "\tif err := api.jsonCodec().Decode(data, &ls); err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn &ls, nil\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (api *%s) writestate(ls *loginState) error {\n",
d.WithLoginAPIStructName())
fmt.Fprint(sb, "\tdata, err := api.jsonCodec().Encode(*ls)\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn api.KVStore.Set(loginKey, data)\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (api *%s) doRegister(ctx context.Context, password string) (string, error) {\n",
d.WithLoginAPIStructName())
fmt.Fprint(sb, "\treq := newRegisterRequest(password)\n")
fmt.Fprint(sb, "\tls := &loginState{}\n")
fmt.Fprint(sb, "\tresp, err := api.RegisterAPI.Call(ctx, req)\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn \"\", err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tls.ClientID = resp.ClientID\n")
fmt.Fprint(sb, "\tls.Password = req.Password\n")
fmt.Fprint(sb, "\treturn api.doLogin(ctx, ls)\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (api *%s) forceRegister(ctx context.Context) (string, error) {\n",
d.WithLoginAPIStructName())
fmt.Fprint(sb, "\tvar password string\n")
fmt.Fprint(sb, "\t// If we already have a previous password, let us keep\n")
fmt.Fprint(sb, "\t// using it. This will allow a new version of the API to\n")
fmt.Fprint(sb, "\t// be able to continue to identify this probe. (This\n")
fmt.Fprint(sb, "\t// assumes that we have a stateless API that generates\n")
fmt.Fprint(sb, "\t// the user ID as a signature of the password plus a\n")
fmt.Fprint(sb, "\t// timestamp and that the key to generate the signature\n")
fmt.Fprint(sb, "\t// is not lost. If all these conditions are met, we\n")
fmt.Fprint(sb, "\t// can then serve better test targets to more long running\n")
fmt.Fprint(sb, "\t// (and therefore trusted) probes.)\n")
fmt.Fprint(sb, "\tif ls, err := api.readstate(); err == nil {\n")
fmt.Fprint(sb, "\t\tpassword = ls.Password\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif password == \"\" {\n")
fmt.Fprint(sb, "\t\tpassword = newRandomPassword()\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn api.doRegister(ctx, password)\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (api *%s) forceLogin(ctx context.Context) (string, error) {\n",
d.WithLoginAPIStructName())
fmt.Fprint(sb, "\tls, err := api.readstate()\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn \"\", err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn api.doLogin(ctx, ls)\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (api *%s) maybeLogin(ctx context.Context) (string, error) {\n",
d.WithLoginAPIStructName())
fmt.Fprint(sb, "\tls, _ := api.readstate()\n")
fmt.Fprint(sb, "\tif ls == nil || !ls.credentialsValid() {\n")
fmt.Fprint(sb, "\t\treturn api.forceRegister(ctx)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif !ls.tokenValid() {\n")
fmt.Fprint(sb, "\t\treturn api.doLogin(ctx, ls)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn ls.Token, nil\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "func (api *%s) doLogin(ctx context.Context, ls *loginState) (string, error) {\n",
d.WithLoginAPIStructName())
fmt.Fprint(sb, "\treq := &apimodel.LoginRequest{\n")
fmt.Fprint(sb, "\t\tClientID: ls.ClientID,\n")
fmt.Fprint(sb, "\t\tPassword: ls.Password,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tresp, err := api.LoginAPI.Call(ctx, req)\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn \"\", err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tls.Token = resp.Token\n")
fmt.Fprint(sb, "\tls.Expire = resp.Expire\n")
fmt.Fprint(sb, "\tif err := api.writestate(ls); err != nil {\n")
fmt.Fprint(sb, "\t\treturn \"\", err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\treturn ls.Token, nil\n")
fmt.Fprint(sb, "}\n\n")
fmt.Fprintf(sb, "var _ %s = &%s{}\n\n", d.CallerInterfaceName(),
d.WithLoginAPIStructName())
}
// GenLoginGo generates login.go.
func GenLoginGo(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\"errors\"\n")
fmt.Fprint(&sb, "\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n")
for _, desc := range Descriptors {
if !desc.RequiresLogin {
continue
}
desc.genNewLogin(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,899 @@
package main
import (
"fmt"
"strings"
"time"
)
func (d *Descriptor) genTestRegisterAndLoginSuccess(sb *strings.Builder) {
fmt.Fprintf(sb, "func TestRegisterAndLogin%sSuccess(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar expect %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&expect)\n")
fmt.Fprint(sb, "\tregisterAPI := &FakeRegisterAPI{\n")
fmt.Fprint(sb, "\t\tResponse: &apimodel.RegisterResponse{\n")
fmt.Fprint(sb, "\t\t\tClientID: \"antani-antani\",\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t\tloginAPI := &FakeLoginAPI{\n")
fmt.Fprint(sb, "\t\t\tResponse: &apimodel.LoginResponse{\n")
fmt.Fprint(sb, "\t\t\t\tExpire: time.Now().Add(3600*time.Second),\n")
fmt.Fprint(sb, "\t\t\t\tToken: \"antani-antani-token\",\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\t\tAPI: &%s{\n", d.FakeAPIStructName())
fmt.Fprintf(sb, "\t\t\tWithResult: &%s{\n", d.FakeAPIStructName())
fmt.Fprint(sb, "\t\t\t\tResponse: expect,\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\t\tLoginAPI: loginAPI,\n")
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\tresp, err := login.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\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif diff := cmp.Diff(expect, resp); diff != \"\" {\n")
fmt.Fprint(sb, "\t\tt.Fatal(diff)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif loginAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid loginAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif registerAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid registerAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestContinueUsingToken(sb *strings.Builder) {
fmt.Fprintf(sb, "func Test%sContinueUsingToken(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar expect %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&expect)\n")
fmt.Fprint(sb, "\tregisterAPI := &FakeRegisterAPI{\n")
fmt.Fprint(sb, "\t\tResponse: &apimodel.RegisterResponse{\n")
fmt.Fprint(sb, "\t\t\tClientID: \"antani-antani\",\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t\tloginAPI := &FakeLoginAPI{\n")
fmt.Fprint(sb, "\t\t\tResponse: &apimodel.LoginResponse{\n")
fmt.Fprint(sb, "\t\t\t\tExpire: time.Now().Add(3600*time.Second),\n")
fmt.Fprint(sb, "\t\t\t\tToken: \"antani-antani-token\",\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\t\tAPI: &%s{\n", d.FakeAPIStructName())
fmt.Fprintf(sb, "\t\t\tWithResult: &%s{\n", d.FakeAPIStructName())
fmt.Fprint(sb, "\t\t\t\tResponse: expect,\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\t\tLoginAPI: loginAPI,\n")
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\t// step 1: we register and login and use the token\n")
fmt.Fprint(sb, "\t// inside a scope just to avoid mistakes\n")
fmt.Fprint(sb, "\t{\n")
fmt.Fprint(sb, "\t\tresp, err := login.Call(ctx, req)\n")
fmt.Fprint(sb, "\t\tif err != nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(err)\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif resp == nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"expected non-nil response\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif diff := cmp.Diff(expect, resp); diff != \"\" {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(diff)\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif loginAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid loginAPI.CountCall\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif registerAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid registerAPI.CountCall\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t// step 2: we disable register and login but we\n")
fmt.Fprint(sb, "\t// should be okay because of the token\n")
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
fmt.Fprint(sb, "\tregisterAPI.Err = errMocked\n")
fmt.Fprint(sb, "\tregisterAPI.Response = nil\n")
fmt.Fprint(sb, "\tloginAPI.Err = errMocked\n")
fmt.Fprint(sb, "\tloginAPI.Response = nil\n")
fmt.Fprint(sb, "\tresp, err := login.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\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif diff := cmp.Diff(expect, resp); diff != \"\" {\n")
fmt.Fprint(sb, "\t\tt.Fatal(diff)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif loginAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid loginAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif registerAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid registerAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestWithValidButExpiredToken(sb *strings.Builder) {
fmt.Fprintf(sb, "func Test%sWithValidButExpiredToken(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar expect %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&expect)\n")
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
fmt.Fprint(sb, "\tregisterAPI := &FakeRegisterAPI{\n")
fmt.Fprint(sb, "\t\tErr: errMocked,\n")
fmt.Fprint(sb, "\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t\tloginAPI := &FakeLoginAPI{\n")
fmt.Fprint(sb, "\t\t\tResponse: &apimodel.LoginResponse{\n")
fmt.Fprint(sb, "\t\t\t\tExpire: time.Now().Add(3600*time.Second),\n")
fmt.Fprint(sb, "\t\t\t\tToken: \"antani-antani-token\",\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\t\tAPI: &%s{\n", d.FakeAPIStructName())
fmt.Fprintf(sb, "\t\t\tWithResult: &%s{\n", d.FakeAPIStructName())
fmt.Fprint(sb, "\t\t\t\tResponse: expect,\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\t\tLoginAPI: loginAPI,\n")
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tls := &loginState{\n")
fmt.Fprintf(sb, "\t\tClientID: \"antani-antani\",\n")
fmt.Fprintf(sb, "\t\tExpire: time.Now().Add(-5 * time.Second),\n")
fmt.Fprintf(sb, "\t\tToken: \"antani-antani-token\",\n")
fmt.Fprintf(sb, "\t\tPassword: \"antani-antani-password\",\n")
fmt.Fprintf(sb, "\t}\n")
fmt.Fprintf(sb, "\tif err := login.writestate(ls); err != nil {\n")
fmt.Fprintf(sb, "\t\tt.Fatal(err)\n")
fmt.Fprintf(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\tresp, err := login.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\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif diff := cmp.Diff(expect, resp); diff != \"\" {\n")
fmt.Fprint(sb, "\t\tt.Fatal(diff)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif loginAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid loginAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif registerAPI.CountCall.Load() != 0 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid registerAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestWithRegisterAPIError(sb *strings.Builder) {
fmt.Fprintf(sb, "func Test%sWithRegisterAPIError(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar expect %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&expect)\n")
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
fmt.Fprint(sb, "\tregisterAPI := &FakeRegisterAPI{\n")
fmt.Fprint(sb, "\t\tErr: errMocked,\n")
fmt.Fprint(sb, "\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\t\tAPI: &%s{\n", d.FakeAPIStructName())
fmt.Fprintf(sb, "\t\t\tWithResult: &%s{\n", d.FakeAPIStructName())
fmt.Fprint(sb, "\t\t\t\tResponse: expect,\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\tresp, err := login.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 response\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif registerAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid registerAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestWithLoginFailure(sb *strings.Builder) {
fmt.Fprintf(sb, "func Test%sWithLoginFailure(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar expect %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&expect)\n")
fmt.Fprint(sb, "\tregisterAPI := &FakeRegisterAPI{\n")
fmt.Fprint(sb, "\t\tResponse: &apimodel.RegisterResponse{\n")
fmt.Fprint(sb, "\t\t\tClientID: \"antani-antani\",\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
fmt.Fprint(sb, "\t\tloginAPI := &FakeLoginAPI{\n")
fmt.Fprint(sb, "\t\t\tErr: errMocked,\n")
fmt.Fprint(sb, "\t\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\t\tAPI: &%s{\n", d.FakeAPIStructName())
fmt.Fprintf(sb, "\t\t\tWithResult: &%s{\n", d.FakeAPIStructName())
fmt.Fprint(sb, "\t\t\t\tResponse: expect,\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\t\tLoginAPI: loginAPI,\n")
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\tresp, err := login.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 response\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif loginAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid loginAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif registerAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid registerAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestRegisterAndLoginThenFail(sb *strings.Builder) {
fmt.Fprintf(sb, "func TestRegisterAndLogin%sThenFail(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar expect %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&expect)\n")
fmt.Fprint(sb, "\tregisterAPI := &FakeRegisterAPI{\n")
fmt.Fprint(sb, "\t\tResponse: &apimodel.RegisterResponse{\n")
fmt.Fprint(sb, "\t\t\tClientID: \"antani-antani\",\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t\tloginAPI := &FakeLoginAPI{\n")
fmt.Fprint(sb, "\t\t\tResponse: &apimodel.LoginResponse{\n")
fmt.Fprint(sb, "\t\t\t\tExpire: time.Now().Add(3600*time.Second),\n")
fmt.Fprint(sb, "\t\t\t\tToken: \"antani-antani-token\",\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\t\tAPI: &%s{\n", d.FakeAPIStructName())
fmt.Fprintf(sb, "\t\t\tWithResult: &%s{\n", d.FakeAPIStructName())
fmt.Fprint(sb, "\t\t\t\tErr: errMocked,\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\t\tLoginAPI: loginAPI,\n")
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\tresp, err := login.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 response\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif loginAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid loginAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif registerAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid registerAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestTheDatabaseIsReplaced(sb *strings.Builder) {
fmt.Fprintf(sb, "func Test%sTheDatabaseIsReplaced(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprint(sb, "\thandler := &LoginHandler{\n")
fmt.Fprint(sb, "\t\tlogins: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\tregisters: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\tt: t,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tsrvr := httptest.NewServer(handler)\n")
fmt.Fprint(sb, "\tdefer srvr.Close()\n")
fmt.Fprint(sb, "\tregisterAPI := &simpleRegisterAPI{\n")
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t\tloginAPI := &simpleLoginAPI{\n")
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprintf(sb, "\tbaseAPI := &%s{\n", d.APIStructName())
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\tAPI : baseAPI,\n")
fmt.Fprint(sb, "\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\tLoginAPI: loginAPI,\n")
fmt.Fprint(sb, "\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\t// step 1: we register and login and use the token\n")
fmt.Fprint(sb, "\t// inside a scope just to avoid mistakes\n")
fmt.Fprint(sb, "\t{\n")
fmt.Fprint(sb, "\t\tresp, err := login.Call(ctx, req)\n")
fmt.Fprint(sb, "\t\tif err != nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(err)\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif resp == nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"expected non-nil response\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif handler.logins.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid handler.logins\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif handler.registers.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid handler.registers\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t// step 2: we forget accounts and try again.\n")
fmt.Fprint(sb, "\thandler.forgetLogins()\n")
fmt.Fprint(sb, "\tresp, err := login.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\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif handler.logins.Load() != 3 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid handler.logins\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif handler.registers.Load() != 2 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid handler.registers\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestTheDatabaseIsReplacedThenFailure(sb *strings.Builder) {
fmt.Fprintf(sb, "func Test%sTheDatabaseIsReplacedThenFailure(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprint(sb, "\thandler := &LoginHandler{\n")
fmt.Fprint(sb, "\t\tlogins: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\tregisters: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\tt: t,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tsrvr := httptest.NewServer(handler)\n")
fmt.Fprint(sb, "\tdefer srvr.Close()\n")
fmt.Fprint(sb, "\tregisterAPI := &simpleRegisterAPI{\n")
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t\tloginAPI := &simpleLoginAPI{\n")
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprintf(sb, "\tbaseAPI := &%s{\n", d.APIStructName())
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\tAPI : baseAPI,\n")
fmt.Fprint(sb, "\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\tLoginAPI: loginAPI,\n")
fmt.Fprint(sb, "\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\t// step 1: we register and login and use the token\n")
fmt.Fprint(sb, "\t// inside a scope just to avoid mistakes\n")
fmt.Fprint(sb, "\t{\n")
fmt.Fprint(sb, "\t\tresp, err := login.Call(ctx, req)\n")
fmt.Fprint(sb, "\t\tif err != nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(err)\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif resp == nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"expected non-nil response\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif handler.logins.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid handler.logins\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif handler.registers.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid handler.registers\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t// step 2: we forget accounts and try again.\n")
fmt.Fprint(sb, "\t// but registrations are also failing.\n")
fmt.Fprint(sb, "\thandler.forgetLogins()\n")
fmt.Fprint(sb, "\thandler.noRegister = true\n")
fmt.Fprint(sb, "\tresp, err := login.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 response\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif handler.logins.Load() != 2 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid handler.logins\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif handler.registers.Load() != 2 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid handler.registers\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestRegisterAndLoginCannotWriteState(sb *strings.Builder) {
fmt.Fprintf(sb, "func TestRegisterAndLogin%sCannotWriteState(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar expect %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&expect)\n")
fmt.Fprint(sb, "\tregisterAPI := &FakeRegisterAPI{\n")
fmt.Fprint(sb, "\t\tResponse: &apimodel.RegisterResponse{\n")
fmt.Fprint(sb, "\t\t\tClientID: \"antani-antani\",\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t\tloginAPI := &FakeLoginAPI{\n")
fmt.Fprint(sb, "\t\t\tResponse: &apimodel.LoginResponse{\n")
fmt.Fprint(sb, "\t\t\t\tExpire: time.Now().Add(3600*time.Second),\n")
fmt.Fprint(sb, "\t\t\t\tToken: \"antani-antani-token\",\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t\tCountCall: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\t\tAPI: &%s{\n", d.FakeAPIStructName())
fmt.Fprintf(sb, "\t\t\tWithResult: &%s{\n", d.FakeAPIStructName())
fmt.Fprint(sb, "\t\t\t\tResponse: expect,\n")
fmt.Fprint(sb, "\t\t\t},\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\t\tLoginAPI: loginAPI,\n")
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t\tJSONCodec: &FakeCodec{\n")
fmt.Fprint(sb, "\t\t\tEncodeErr: errMocked,\n")
fmt.Fprint(sb, "\t\t},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\tresp, err := login.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 response\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif loginAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid loginAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif registerAPI.CountCall.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid registerAPI.CountCall\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestReadStateDecodeFailure(sb *strings.Builder) {
fmt.Fprintf(sb, "func Test%sReadStateDecodeFailure(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprintf(sb, "\tvar expect %s\n", d.ResponseTypeName())
fmt.Fprint(sb, "\tff.fill(&expect)\n")
fmt.Fprint(sb, "\terrMocked := errors.New(\"mocked error\")\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprint(sb, "\t\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t\tJSONCodec: &FakeCodec{DecodeErr: errMocked},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tls := &loginState{\n")
fmt.Fprintf(sb, "\t\tClientID: \"antani-antani\",\n")
fmt.Fprintf(sb, "\t\tExpire: time.Now().Add(-5 * time.Second),\n")
fmt.Fprintf(sb, "\t\tToken: \"antani-antani-token\",\n")
fmt.Fprintf(sb, "\t\tPassword: \"antani-antani-password\",\n")
fmt.Fprintf(sb, "\t}\n")
fmt.Fprintf(sb, "\tif err := login.writestate(ls); err != nil {\n")
fmt.Fprintf(sb, "\t\tt.Fatal(err)\n")
fmt.Fprintf(sb, "\t}\n")
fmt.Fprintf(sb, "\tout, err := login.forceLogin(context.Background())\n")
fmt.Fprintf(sb, "if !errors.Is(err, errMocked) {\n")
fmt.Fprintf(sb, "\t\tt.Fatal(\"not the error we expected\", err)\n")
fmt.Fprintf(sb, "\t}\n")
fmt.Fprintf(sb, "if out != \"\" {\n")
fmt.Fprintf(sb, "\t\tt.Fatal(\"expected empty string here\")\n")
fmt.Fprintf(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestClockIsOffThenSuccess(sb *strings.Builder) {
fmt.Fprintf(sb, "func Test%sClockIsOffThenSuccess(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprint(sb, "\thandler := &LoginHandler{\n")
fmt.Fprint(sb, "\t\tlogins: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\tregisters: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\tt: t,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tsrvr := httptest.NewServer(handler)\n")
fmt.Fprint(sb, "\tdefer srvr.Close()\n")
fmt.Fprint(sb, "\tregisterAPI := &simpleRegisterAPI{\n")
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t\tloginAPI := &simpleLoginAPI{\n")
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprintf(sb, "\tbaseAPI := &%s{\n", d.APIStructName())
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\tAPI : baseAPI,\n")
fmt.Fprint(sb, "\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\tLoginAPI: loginAPI,\n")
fmt.Fprint(sb, "\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\t// step 1: we register and login and use the token\n")
fmt.Fprint(sb, "\t// inside a scope just to avoid mistakes\n")
fmt.Fprint(sb, "\t{\n")
fmt.Fprint(sb, "\t\tresp, err := login.Call(ctx, req)\n")
fmt.Fprint(sb, "\t\tif err != nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(err)\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif resp == nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"expected non-nil response\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif handler.logins.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid handler.logins\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif handler.registers.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid handler.registers\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t// step 2: we forget tokens and try again.\n")
fmt.Fprint(sb, "\t// this should simulate the client clock\n")
fmt.Fprint(sb, "\t// being off and considering a token still valid\n")
fmt.Fprint(sb, "\thandler.forgetTokens()\n")
fmt.Fprint(sb, "\tresp, err := login.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\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif handler.logins.Load() != 2 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid handler.logins\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif handler.registers.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid handler.registers\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestClockIsOffThen401(sb *strings.Builder) {
fmt.Fprintf(sb, "func Test%sClockIsOffThen401(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprint(sb, "\thandler := &LoginHandler{\n")
fmt.Fprint(sb, "\t\tlogins: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\tregisters: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\tt: t,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tsrvr := httptest.NewServer(handler)\n")
fmt.Fprint(sb, "\tdefer srvr.Close()\n")
fmt.Fprint(sb, "\tregisterAPI := &simpleRegisterAPI{\n")
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t\tloginAPI := &simpleLoginAPI{\n")
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprintf(sb, "\tbaseAPI := &%s{\n", d.APIStructName())
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\tAPI : baseAPI,\n")
fmt.Fprint(sb, "\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\tLoginAPI: loginAPI,\n")
fmt.Fprint(sb, "\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\t// step 1: we register and login and use the token\n")
fmt.Fprint(sb, "\t// inside a scope just to avoid mistakes\n")
fmt.Fprint(sb, "\t{\n")
fmt.Fprint(sb, "\t\tresp, err := login.Call(ctx, req)\n")
fmt.Fprint(sb, "\t\tif err != nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(err)\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif resp == nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"expected non-nil response\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif handler.logins.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid handler.logins\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif handler.registers.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid handler.registers\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t// step 2: we forget tokens and try again.\n")
fmt.Fprint(sb, "\t// this should simulate the client clock\n")
fmt.Fprint(sb, "\t// being off and considering a token still valid\n")
fmt.Fprint(sb, "\thandler.forgetTokens()\n")
fmt.Fprint(sb, "\thandler.failCallWith = []int{401, 401}\n")
fmt.Fprint(sb, "\tresp, err := login.Call(ctx, req)\n")
fmt.Fprint(sb, "\tif err != nil {\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 non-nil response\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif handler.logins.Load() != 3 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid handler.logins\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif handler.registers.Load() != 2 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid handler.registers\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
func (d *Descriptor) genTestClockIsOffThen500(sb *strings.Builder) {
fmt.Fprintf(sb, "func Test%sClockIsOffThen500(t *testing.T) {\n", d.Name)
fmt.Fprint(sb, "\tff := &fakeFill{}\n")
fmt.Fprint(sb, "\thandler := &LoginHandler{\n")
fmt.Fprint(sb, "\t\tlogins: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\tregisters: &atomicx.Int64{},\n")
fmt.Fprint(sb, "\t\tt: t,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tsrvr := httptest.NewServer(handler)\n")
fmt.Fprint(sb, "\tdefer srvr.Close()\n")
fmt.Fprint(sb, "\tregisterAPI := &simpleRegisterAPI{\n")
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t\tloginAPI := &simpleLoginAPI{\n")
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprintf(sb, "\tbaseAPI := &%s{\n", d.APIStructName())
fmt.Fprint(sb, "\t\tHTTPClient: &VerboseHTTPClient{T: t},\n")
fmt.Fprint(sb, "\t\tBaseURL: srvr.URL,\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tlogin := &%s{\n", d.WithLoginAPIStructName())
fmt.Fprintf(sb, "\tAPI : baseAPI,\n")
fmt.Fprint(sb, "\tRegisterAPI: registerAPI,\n")
fmt.Fprint(sb, "\tLoginAPI: loginAPI,\n")
fmt.Fprint(sb, "\tKVStore: &kvstore.Memory{},\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tvar req %s\n", d.RequestTypeName())
fmt.Fprint(sb, "\tff.fill(&req)\n")
fmt.Fprint(sb, "\tctx := context.Background()\n")
fmt.Fprint(sb, "\t// step 1: we register and login and use the token\n")
fmt.Fprint(sb, "\t// inside a scope just to avoid mistakes\n")
fmt.Fprint(sb, "\t{\n")
fmt.Fprint(sb, "\t\tresp, err := login.Call(ctx, req)\n")
fmt.Fprint(sb, "\t\tif err != nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(err)\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif resp == nil {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"expected non-nil response\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif handler.logins.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid handler.logins\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t\tif handler.registers.Load() != 1 {\n")
fmt.Fprint(sb, "\t\t\tt.Fatal(\"invalid handler.registers\")\n")
fmt.Fprint(sb, "\t\t}\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\t// step 2: we forget tokens and try again.\n")
fmt.Fprint(sb, "\t// this should simulate the client clock\n")
fmt.Fprint(sb, "\t// being off and considering a token still valid\n")
fmt.Fprint(sb, "\thandler.forgetTokens()\n")
fmt.Fprint(sb, "\thandler.failCallWith = []int{401, 500}\n")
fmt.Fprint(sb, "\tresp, err := login.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 response\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif handler.logins.Load() != 2 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid handler.logins\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif handler.registers.Load() != 1 {\n")
fmt.Fprint(sb, "\t\tt.Fatal(\"invalid handler.registers\")\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "}\n\n")
}
// GenLoginTestGo generates login_test.go.
func GenLoginTestGo(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\"errors\"\n")
fmt.Fprint(&sb, "\t\"net/http/httptest\"\n")
fmt.Fprint(&sb, "\t\"testing\"\n")
fmt.Fprint(&sb, "\t\"time\"\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/atomicx\"\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/kvstore\"\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n")
for _, desc := range Descriptors {
if !desc.RequiresLogin {
continue
}
desc.genTestRegisterAndLoginSuccess(&sb)
desc.genTestContinueUsingToken(&sb)
desc.genTestWithValidButExpiredToken(&sb)
desc.genTestWithRegisterAPIError(&sb)
desc.genTestWithLoginFailure(&sb)
desc.genTestRegisterAndLoginThenFail(&sb)
desc.genTestTheDatabaseIsReplaced(&sb)
desc.genTestRegisterAndLoginCannotWriteState(&sb)
desc.genTestReadStateDecodeFailure(&sb)
desc.genTestTheDatabaseIsReplacedThenFailure(&sb)
desc.genTestClockIsOffThenSuccess(&sb)
desc.genTestClockIsOffThen401(&sb)
desc.genTestClockIsOffThen500(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,153 @@
package main
import (
"fmt"
"reflect"
)
// TypeName returns v's package-qualified type name.
func (d *Descriptor) TypeName(v interface{}) string {
return reflect.TypeOf(v).String()
}
// RequestTypeName calls d.TypeName(d.Request).
func (d *Descriptor) RequestTypeName() string {
return d.TypeName(d.Request)
}
// ResponseTypeName calls d.TypeName(d.Response).
func (d *Descriptor) ResponseTypeName() string {
return d.TypeName(d.Response)
}
// APIStructName returns the correct struct type name
// for the API we're currently processing.
func (d *Descriptor) APIStructName() string {
return fmt.Sprintf("simple%sAPI", d.Name)
}
// FakeAPIStructName returns the correct struct type name
// for the fake for the API we're currently processing.
func (d *Descriptor) FakeAPIStructName() string {
return fmt.Sprintf("Fake%sAPI", d.Name)
}
// WithLoginAPIStructName returns the correct struct type name
// for the WithLoginAPI we're currently processing.
func (d *Descriptor) WithLoginAPIStructName() string {
return fmt.Sprintf("withLogin%sAPI", d.Name)
}
// CallerInterfaceName returns the correct caller interface name
// for the API we're currently processing.
func (d *Descriptor) CallerInterfaceName() string {
return fmt.Sprintf("callerFor%sAPI", d.Name)
}
// ClonerInterfaceName returns the correct cloner interface name
// for the API we're currently processing.
func (d *Descriptor) ClonerInterfaceName() string {
return fmt.Sprintf("clonerFor%sAPI", d.Name)
}
// WithCacheAPIStructName returns the correct struct type name for
// the cache for the API we're currently processing.
func (d *Descriptor) WithCacheAPIStructName() string {
return fmt.Sprintf("withCache%sAPI", d.Name)
}
// CacheEntryName returns the correct struct type name for the
// cache entry for the API we're currently processing.
func (d *Descriptor) CacheEntryName() string {
return fmt.Sprintf("cacheEntryFor%sAPI", d.Name)
}
// CacheKey returns the correct cache key for the API
// we're currently processing.
func (d *Descriptor) CacheKey() string {
return fmt.Sprintf("%s.cache", d.Name)
}
// StructFields returns all the struct fields of in. This function
// assumes that in is a pointer to struct, and will otherwise panic.
func (d *Descriptor) StructFields(in interface{}) []*reflect.StructField {
t := reflect.TypeOf(in)
if t.Kind() != reflect.Ptr {
panic("not a pointer")
}
t = t.Elem()
if t.Kind() != reflect.Struct {
panic("not a struct")
}
var out []*reflect.StructField
for idx := 0; idx < t.NumField(); idx++ {
f := t.Field(idx)
out = append(out, &f)
}
return out
}
// StructFieldsWithTag returns all the struct fields of
// in that have the specified tag.
func (d *Descriptor) StructFieldsWithTag(in interface{}, tag string) []*reflect.StructField {
var out []*reflect.StructField
for _, f := range d.StructFields(in) {
if f.Tag.Get(tag) != "" {
out = append(out, f)
}
}
return out
}
// RequestOrResponseTypeKind returns the type kind of in, which should
// be a request or a response. This function assumes that in is either a
// pointer to struct or a map and will panic otherwise.
func (d *Descriptor) RequestOrResponseTypeKind(in interface{}) reflect.Kind {
t := reflect.TypeOf(in)
if t.Kind() == reflect.Ptr {
t = t.Elem()
if t.Kind() != reflect.Struct {
panic("not a struct")
}
return reflect.Struct
}
if t.Kind() != reflect.Map {
panic("not a map")
}
return reflect.Map
}
// RequestTypeKind calls d.RequestOrResponseTypeKind(d.Request).
func (d *Descriptor) RequestTypeKind() reflect.Kind {
return d.RequestOrResponseTypeKind(d.Request)
}
// ResponseTypeKind calls d.RequestOrResponseTypeKind(d.Response).
func (d *Descriptor) ResponseTypeKind() reflect.Kind {
return d.RequestOrResponseTypeKind(d.Response)
}
// TypeNameAsStruct assumes that in is a pointer to struct and
// returns the type of the corresponding struct. The returned
// type is package qualified.
func (d *Descriptor) TypeNameAsStruct(in interface{}) string {
t := reflect.TypeOf(in)
if t.Kind() != reflect.Ptr {
panic("not a pointer")
}
t = t.Elem()
if t.Kind() != reflect.Struct {
panic("not a struct")
}
return t.String()
}
// RequestTypeNameAsStruct calls d.TypeNameAsStruct(d.Request)
func (d *Descriptor) RequestTypeNameAsStruct() string {
return d.TypeNameAsStruct(d.Request)
}
// ResponseTypeNameAsStruct calls d.TypeNameAsStruct(d.Response)
func (d *Descriptor) ResponseTypeNameAsStruct() string {
return d.TypeNameAsStruct(d.Response)
}
@@ -0,0 +1,141 @@
package main
import (
"fmt"
"reflect"
"strings"
"time"
)
const (
tagForQuery = "query"
tagForRequired = "required"
)
func (d *Descriptor) genNewRequestQueryElemString(sb *strings.Builder, f *reflect.StructField) {
name := f.Name
query := f.Tag.Get(tagForQuery)
if f.Tag.Get(tagForRequired) == "true" {
fmt.Fprintf(sb, "\tif req.%s == \"\" {\n", name)
fmt.Fprintf(sb, "\t\treturn nil, newErrEmptyField(\"%s\")\n", name)
fmt.Fprint(sb, "\t}\n")
fmt.Fprintf(sb, "\tq.Add(\"%s\", req.%s)\n", query, name)
return
}
fmt.Fprintf(sb, "\tif req.%s != \"\" {\n", name)
fmt.Fprintf(sb, "\t\tq.Add(\"%s\", req.%s)\n", query, name)
fmt.Fprint(sb, "\t}\n")
}
func (d *Descriptor) genNewRequestQueryElemBool(sb *strings.Builder, f *reflect.StructField) {
// required does not make much sense for a boolean field
name := f.Name
query := f.Tag.Get(tagForQuery)
fmt.Fprintf(sb, "\tif req.%s {\n", name)
fmt.Fprintf(sb, "\t\tq.Add(\"%s\", \"true\")\n", query)
fmt.Fprint(sb, "\t}\n")
}
func (d *Descriptor) genNewRequestQueryElemInt64(sb *strings.Builder, f *reflect.StructField) {
// required does not make much sense for an integer field
name := f.Name
query := f.Tag.Get(tagForQuery)
fmt.Fprintf(sb, "\tif req.%s != 0 {\n", name)
fmt.Fprintf(sb, "\t\tq.Add(\"%s\", newQueryFieldInt64(req.%s))\n", query, name)
fmt.Fprint(sb, "\t}\n")
}
func (d *Descriptor) genNewRequestQuery(sb *strings.Builder) {
if d.Method != "GET" {
return // we only generate query for GET
}
fields := d.StructFieldsWithTag(d.Request, tagForQuery)
if len(fields) <= 0 {
return
}
fmt.Fprint(sb, "\tq := url.Values{}\n")
for idx, f := range fields {
switch f.Type.Kind() {
case reflect.String:
d.genNewRequestQueryElemString(sb, f)
case reflect.Bool:
d.genNewRequestQueryElemBool(sb, f)
case reflect.Int64:
d.genNewRequestQueryElemInt64(sb, f)
default:
panic(fmt.Sprintf("unexpected query type at index %d", idx))
}
}
fmt.Fprint(sb, "\tURL.RawQuery = q.Encode()\n")
}
func (d *Descriptor) genNewRequestCallNewRequest(sb *strings.Builder) {
if d.Method == "POST" {
fmt.Fprint(sb, "\tbody, err := api.jsonCodec().Encode(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, "\tout, err := api.requestMaker().NewRequest(")
fmt.Fprintf(sb, "ctx, \"%s\", URL.String(), ", d.Method)
fmt.Fprint(sb, "bytes.NewReader(body))\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tout.Header.Set(\"Content-Type\", \"application/json\")\n")
fmt.Fprint(sb, "\treturn out, nil\n")
return
}
fmt.Fprint(sb, "\treturn api.requestMaker().NewRequest(")
fmt.Fprintf(sb, "ctx, \"%s\", URL.String(), ", d.Method)
fmt.Fprint(sb, "nil)\n")
}
func (d *Descriptor) genNewRequest(sb *strings.Builder) {
fmt.Fprintf(
sb, "func (api *%s) newRequest(ctx context.Context, req %s) %s {\n",
d.APIStructName(), d.RequestTypeName(), "(*http.Request, error)")
fmt.Fprint(sb, "\tURL, err := url.Parse(api.baseURL())\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
switch d.URLPath.IsTemplate {
case false:
fmt.Fprintf(sb, "\tURL.Path = \"%s\"\n", d.URLPath.Value)
case true:
fmt.Fprintf(
sb, "\tup, err := api.templateExecutor().Execute(\"%s\", req)\n",
d.URLPath.Value)
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tURL.Path = up\n")
}
d.genNewRequestQuery(sb)
d.genNewRequestCallNewRequest(sb)
fmt.Fprintf(sb, "}\n\n")
}
// GenRequestsGo generates requests.go.
func GenRequestsGo(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\"bytes\"\n")
fmt.Fprint(&sb, "\t\"context\"\n")
fmt.Fprint(&sb, "\t\"net/http\"\n")
fmt.Fprint(&sb, "\t\"net/url\"\n")
fmt.Fprint(&sb, "\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n\n")
for _, desc := range Descriptors {
desc.genNewRequest(&sb)
}
writefile(file, &sb)
}
@@ -0,0 +1,80 @@
package main
import (
"fmt"
"reflect"
"strings"
"time"
)
func (d *Descriptor) genNewResponse(sb *strings.Builder) {
fmt.Fprintf(sb,
"func (api *%s) newResponse(resp *http.Response, err error) (%s, error) {\n",
d.APIStructName(), d.ResponseTypeName())
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif resp.StatusCode == 401 {\n")
fmt.Fprint(sb, "\t\treturn nil, ErrUnauthorized\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tif resp.StatusCode != 200 {\n")
fmt.Fprint(sb, "\t\treturn nil, newHTTPFailure(resp.StatusCode)\n")
fmt.Fprint(sb, "\t}\n")
fmt.Fprint(sb, "\tdefer resp.Body.Close()\n")
fmt.Fprint(sb, "\treader := io.LimitReader(resp.Body, 4<<20)\n")
fmt.Fprint(sb, "\tdata, err := ioutil.ReadAll(reader)\n")
fmt.Fprint(sb, "\tif err != nil {\n")
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
switch d.ResponseTypeKind() {
case reflect.Map:
fmt.Fprintf(sb, "\tout := %s{}\n", d.ResponseTypeName())
case reflect.Struct:
fmt.Fprintf(sb, "\tout := &%s{}\n", d.ResponseTypeNameAsStruct())
}
switch d.ResponseTypeKind() {
case reflect.Map:
fmt.Fprint(sb, "\tif err := api.jsonCodec().Decode(data, &out); err != nil {\n")
case reflect.Struct:
fmt.Fprint(sb, "\tif err := api.jsonCodec().Decode(data, out); err != nil {\n")
}
fmt.Fprint(sb, "\t\treturn nil, err\n")
fmt.Fprint(sb, "\t}\n")
switch d.ResponseTypeKind() {
case reflect.Map:
// For rationale, see https://play.golang.org/p/m9-MsTaQ5wt and
// https://play.golang.org/p/6h-v-PShMk9.
fmt.Fprint(sb, "\tif out == nil {\n")
fmt.Fprint(sb, "\t\treturn nil, ErrJSONLiteralNull\n")
fmt.Fprint(sb, "\t}\n")
case reflect.Struct:
// nothing
}
fmt.Fprintf(sb, "\treturn out, nil\n")
fmt.Fprintf(sb, "}\n\n")
}
// GenResponsesGo generates responses.go.
func GenResponsesGo(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\"io\"\n")
fmt.Fprint(&sb, "\t\"io/ioutil\"\n")
fmt.Fprint(&sb, "\t\"net/http\"\n")
fmt.Fprint(&sb, "\n")
fmt.Fprint(&sb, "\t\"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel\"\n")
fmt.Fprint(&sb, ")\n\n")
for _, desc := range Descriptors {
desc.genNewResponse(&sb)
}
writefile(file, &sb)
}
+136
View File
@@ -0,0 +1,136 @@
package main
import "github.com/ooni/probe-cli/v3/internal/ooapi/apimodel"
// URLPath describes a URLPath.
type URLPath struct {
// IsTemplate indicates whether Value contains a template. A future
// version of this implementation will automatically deduce that.
IsTemplate bool
// Value is the value of the URL path.
Value string
// InSwagger indicates the corresponding name to be used in
// the Swagger specification.
InSwagger string
}
// Descriptor is an API descriptor. It tells the generator
// what code it should emit for a given API.
type Descriptor struct {
// Name is the name of the API.
Name string
// CachePolicy indicates the caching policy to use.
CachePolicy int
// RequiresLogin indicates whether the API requires login.
RequiresLogin bool
// Method is the method to use ("GET" or "POST").
Method string
// URLPath is the URL path.
URLPath URLPath
// Request is an instance of the request type.
Request interface{}
// Response is an instance of the response type.
Response interface{}
}
// These are the caching policies.
const (
// CacheNone indicates we don't use a cache.
CacheNone = iota
// CacheFallback indicates we fallback to the cache
// when there is a failure.
CacheFallback
// CacheAlways indicates that we always check the
// cache before sending a request.
CacheAlways
)
// Descriptors describes all the APIs.
//
// Note that it matters whether the requests and responses
// are pointers. Generally speaking, if the message is a
// struct, use a pointer. If it's a map, don't.
var Descriptors = []Descriptor{{
Name: "CheckReportID",
Method: "GET",
URLPath: URLPath{Value: "/api/_/check_report_id"},
Request: &apimodel.CheckReportIDRequest{},
Response: &apimodel.CheckReportIDResponse{},
}, {
Name: "CheckIn",
Method: "POST",
URLPath: URLPath{Value: "/api/v1/check-in"},
Request: &apimodel.CheckInRequest{},
Response: &apimodel.CheckInResponse{},
}, {
Name: "Login",
Method: "POST",
URLPath: URLPath{Value: "/api/v1/login"},
Request: &apimodel.LoginRequest{},
Response: &apimodel.LoginResponse{},
}, {
Name: "MeasurementMeta",
Method: "GET",
URLPath: URLPath{Value: "/api/v1/measurement_meta"},
Request: &apimodel.MeasurementMetaRequest{},
Response: &apimodel.MeasurementMetaResponse{},
CachePolicy: CacheAlways,
}, {
Name: "Register",
Method: "POST",
URLPath: URLPath{Value: "/api/v1/register"},
Request: &apimodel.RegisterRequest{},
Response: &apimodel.RegisterResponse{},
}, {
Name: "TestHelpers",
Method: "GET",
URLPath: URLPath{Value: "/api/v1/test-helpers"},
Request: &apimodel.TestHelpersRequest{},
Response: apimodel.TestHelpersResponse{},
}, {
Name: "PsiphonConfig",
RequiresLogin: true,
Method: "GET",
URLPath: URLPath{Value: "/api/v1/test-list/psiphon-config"},
Request: &apimodel.PsiphonConfigRequest{},
Response: apimodel.PsiphonConfigResponse{},
}, {
Name: "TorTargets",
RequiresLogin: true,
Method: "GET",
URLPath: URLPath{Value: "/api/v1/test-list/tor-targets"},
Request: &apimodel.TorTargetsRequest{},
Response: apimodel.TorTargetsResponse{},
}, {
Name: "URLs",
Method: "GET",
URLPath: URLPath{Value: "/api/v1/test-list/urls"},
Request: &apimodel.URLsRequest{},
Response: &apimodel.URLsResponse{},
}, {
Name: "OpenReport",
Method: "POST",
URLPath: URLPath{Value: "/report"},
Request: &apimodel.OpenReportRequest{},
Response: &apimodel.OpenReportResponse{},
}, {
Name: "SubmitMeasurement",
Method: "POST",
URLPath: URLPath{
InSwagger: "/report/{report_id}",
IsTemplate: true,
Value: "/report/{{ .ReportID }}",
},
Request: &apimodel.SubmitMeasurementRequest{},
Response: &apimodel.SubmitMeasurementResponse{},
}}
@@ -0,0 +1,194 @@
package main
import (
"encoding/json"
"fmt"
"log"
"reflect"
"strings"
"sync"
"time"
"github.com/ooni/probe-cli/v3/internal/ooapi/internal/openapi"
)
const (
tagForJSON = "json"
tagForPath = "path"
)
func (d *Descriptor) genSwaggerURLPath() string {
up := d.URLPath
if up.InSwagger != "" {
return up.InSwagger
}
if up.IsTemplate {
panic("we should always use InSwapper and IsTemplate together")
}
return up.Value
}
func (d *Descriptor) genSwaggerSchema(cur reflect.Type) *openapi.Schema {
switch cur.Kind() {
case reflect.String:
return &openapi.Schema{Type: "string"}
case reflect.Bool:
return &openapi.Schema{Type: "boolean"}
case reflect.Int64:
return &openapi.Schema{Type: "integer"}
case reflect.Slice:
return &openapi.Schema{Type: "array", Items: d.genSwaggerSchema(cur.Elem())}
case reflect.Map:
return &openapi.Schema{Type: "object"}
case reflect.Ptr:
return d.genSwaggerSchema(cur.Elem())
case reflect.Struct:
if cur.String() == "time.Time" {
// Implementation note: we don't want to dive into time.Time but
// rather we want to pretend it's a string. The JSON parser for
// time.Time can indeed reconstruct a time.Time from a string, and
// it's much easier for us to let it do the parsing.
return &openapi.Schema{Type: "string"}
}
sinfo := &openapi.Schema{Type: "object"}
var once sync.Once
initmap := func() {
sinfo.Properties = make(map[string]*openapi.Schema)
}
for idx := 0; idx < cur.NumField(); idx++ {
field := cur.Field(idx)
if field.Tag.Get(tagForPath) != "" {
continue // skipping because this is a path param
}
if field.Tag.Get(tagForQuery) != "" {
continue // skipping because this is a query param
}
v := field.Name
if j := field.Tag.Get(tagForJSON); j != "" {
j = strings.Replace(j, ",omitempty", "", 1) // remove options
if j == "-" {
continue // not exported via JSON
}
v = j
}
once.Do(initmap)
sinfo.Properties[v] = d.genSwaggerSchema(field.Type)
}
return sinfo
case reflect.Interface:
return &openapi.Schema{Type: "object"}
default:
panic("unsupported type")
}
}
func (d *Descriptor) swaggerParamForType(t reflect.Type) string {
switch t.Kind() {
case reflect.String:
return "string"
case reflect.Bool:
return "boolean"
case reflect.Int64:
return "integer"
default:
panic("unsupported type")
}
}
func (d *Descriptor) genSwaggerParams(cur reflect.Type) []*openapi.Parameter {
// when we have params the input must be a pointer to struct
if cur.Kind() != reflect.Ptr {
panic("not a pointer")
}
cur = cur.Elem()
if cur.Kind() != reflect.Struct {
panic("not a pointer to struct")
}
// now that we're sure of the type, inspect the fields
var out []*openapi.Parameter
for idx := 0; idx < cur.NumField(); idx++ {
f := cur.Field(idx)
if q := f.Tag.Get(tagForQuery); q != "" {
out = append(
out, &openapi.Parameter{
Name: q,
In: "query",
Required: f.Tag.Get(tagForRequired) == "true",
Type: d.swaggerParamForType(f.Type),
})
continue
}
if p := f.Tag.Get(tagForPath); p != "" {
out = append(out, &openapi.Parameter{
Name: p,
In: "path",
Required: true,
Type: d.swaggerParamForType(f.Type),
})
continue
}
}
return out
}
func (d *Descriptor) genSwaggerPath() (string, *openapi.Path) {
pathStr, pathInfo := d.genSwaggerURLPath(), &openapi.Path{}
rtinfo := &openapi.RoundTrip{Produces: []string{"application/json"}}
switch d.Method {
case "GET":
pathInfo.Get = rtinfo
case "POST":
rtinfo.Consumes = append(rtinfo.Consumes, "application/json")
pathInfo.Post = rtinfo
default:
panic("unsupported method")
}
rtinfo.Parameters = d.genSwaggerParams(reflect.TypeOf(d.Request))
if d.Method != "GET" {
rtinfo.Parameters = append(rtinfo.Parameters, &openapi.Parameter{
Name: "body",
In: "body",
Required: true,
Schema: d.genSwaggerSchema(reflect.TypeOf(d.Request)),
})
}
rtinfo.Responses = &openapi.Responses{Successful: openapi.Body{
Description: "all good",
Schema: d.genSwaggerSchema(reflect.TypeOf(d.Response)),
}}
return pathStr, pathInfo
}
func genSwaggerVersion() string {
return time.Now().UTC().Format("0.20060102.1150405")
}
// GenSwaggerTestGo generates swagger_test.go
func GenSwaggerTestGo(file string) {
swagger := openapi.Swagger{
Swagger: "2.0",
Info: openapi.API{
Title: "OONI API specification",
Version: genSwaggerVersion(),
},
Host: "api.ooni.io",
BasePath: "/",
Schemes: []string{"https"},
Paths: make(map[string]*openapi.Path),
}
for _, desc := range Descriptors {
pathStr, pathInfo := desc.genSwaggerPath()
swagger.Paths[pathStr] = pathInfo
}
data, err := json.MarshalIndent(swagger, "", " ")
if err != nil {
log.Fatal(err)
}
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.Fprintf(&sb, "const swagger = `%s`\n", string(data))
writefile(file, &sb)
}
@@ -0,0 +1,27 @@
package main
import (
"fmt"
"log"
"os"
"strings"
"golang.org/x/sys/execabs"
)
func writefile(name string, sb *strings.Builder) {
filep, err := os.Create(name)
if err != nil {
log.Fatal(err)
}
if _, err := fmt.Fprint(filep, sb.String()); err != nil {
log.Fatal(err)
}
if err := filep.Close(); err != nil {
log.Fatal(err)
}
cmd := execabs.Command("go", "fmt", name)
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
@@ -0,0 +1,64 @@
// Package openapi contains data structures for Swagger v2.0.
//
// We use these data structures to compare the API specification we
// have here with the one of the server.
package openapi
// Schema is the schema of a specific parameter or
// or the schema used by the response body
type Schema struct {
Properties map[string]*Schema `json:"properties,omitempty"`
Items *Schema `json:"items,omitempty"`
Type string `json:"type"`
}
// Parameter describes an input parameter, which could be in the
// URL path, in the query string, or in the request body
type Parameter struct {
In string `json:"in"`
Name string `json:"name"`
Required bool `json:"required,omitempty"`
Schema *Schema `json:"schema,omitempty"`
Type string `json:"type,omitempty"`
}
// Body describes a response body
type Body struct {
Description interface{} `json:"description,omitempty"`
Schema *Schema `json:"schema"`
}
// Responses describes the possible responses
type Responses struct {
Successful Body `json:"200"`
}
// RoundTrip describes an HTTP round trip with a given method and path
type RoundTrip struct {
Consumes []string `json:"consumes,omitempty"`
Produces []string `json:"produces,omitempty"`
Parameters []*Parameter `json:"parameters,omitempty"`
Responses *Responses `json:"responses,omitempty"`
}
// Path describes a path served by the API
type Path struct {
Get *RoundTrip `json:"get,omitempty"`
Post *RoundTrip `json:"post,omitempty"`
}
// API contains info about the API
type API struct {
Title string `json:"title"`
Version string `json:"version"`
}
// Swagger is the toplevel structure
type Swagger struct {
Swagger string `json:"swagger"`
Info API `json:"info"`
Host string `json:"host"`
BasePath string `json:"basePath"`
Schemes []string `json:"schemes"`
Paths map[string]*Path `json:"paths"`
}
+295
View File
@@ -0,0 +1,295 @@
// Code generated by go generate; DO NOT EDIT.
// 2021-05-12 09:15:06.213159282 +0200 CEST m=+0.000104135
package ooapi
//go:generate go run ./internal/generator -file login.go
import (
"context"
"errors"
"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel"
)
// withLoginPsiphonConfigAPI implements login for simplePsiphonConfigAPI.
type withLoginPsiphonConfigAPI struct {
API clonerForPsiphonConfigAPI // mandatory
JSONCodec JSONCodec // optional
KVStore KVStore // mandatory
RegisterAPI callerForRegisterAPI // mandatory
LoginAPI callerForLoginAPI // mandatory
}
// Call logins, if needed, then calls the API.
func (api *withLoginPsiphonConfigAPI) Call(ctx context.Context, req *apimodel.PsiphonConfigRequest) (apimodel.PsiphonConfigResponse, error) {
token, err := api.maybeLogin(ctx)
if err != nil {
return nil, err
}
resp, err := api.API.WithToken(token).Call(ctx, req)
if errors.Is(err, ErrUnauthorized) {
// Maybe the clock is just off? Let's try to obtain
// a token again and see if this fixes it.
if token, err = api.forceLogin(ctx); err == nil {
switch resp, err = api.API.WithToken(token).Call(ctx, req); err {
case nil:
return resp, nil
case ErrUnauthorized:
// fallthrough
default:
return nil, err
}
}
// Okay, this seems a broader problem. How about we try
// and re-register ourselves again instead?
token, err = api.forceRegister(ctx)
if err != nil {
return nil, err
}
resp, err = api.API.WithToken(token).Call(ctx, req)
// fallthrough
}
if err != nil {
return nil, err
}
return resp, nil
}
func (api *withLoginPsiphonConfigAPI) jsonCodec() JSONCodec {
if api.JSONCodec != nil {
return api.JSONCodec
}
return &defaultJSONCodec{}
}
func (api *withLoginPsiphonConfigAPI) readstate() (*loginState, error) {
data, err := api.KVStore.Get(loginKey)
if err != nil {
return nil, err
}
var ls loginState
if err := api.jsonCodec().Decode(data, &ls); err != nil {
return nil, err
}
return &ls, nil
}
func (api *withLoginPsiphonConfigAPI) writestate(ls *loginState) error {
data, err := api.jsonCodec().Encode(*ls)
if err != nil {
return err
}
return api.KVStore.Set(loginKey, data)
}
func (api *withLoginPsiphonConfigAPI) doRegister(ctx context.Context, password string) (string, error) {
req := newRegisterRequest(password)
ls := &loginState{}
resp, err := api.RegisterAPI.Call(ctx, req)
if err != nil {
return "", err
}
ls.ClientID = resp.ClientID
ls.Password = req.Password
return api.doLogin(ctx, ls)
}
func (api *withLoginPsiphonConfigAPI) forceRegister(ctx context.Context) (string, error) {
var password string
// If we already have a previous password, let us keep
// using it. This will allow a new version of the API to
// be able to continue to identify this probe. (This
// assumes that we have a stateless API that generates
// the user ID as a signature of the password plus a
// timestamp and that the key to generate the signature
// is not lost. If all these conditions are met, we
// can then serve better test targets to more long running
// (and therefore trusted) probes.)
if ls, err := api.readstate(); err == nil {
password = ls.Password
}
if password == "" {
password = newRandomPassword()
}
return api.doRegister(ctx, password)
}
func (api *withLoginPsiphonConfigAPI) forceLogin(ctx context.Context) (string, error) {
ls, err := api.readstate()
if err != nil {
return "", err
}
return api.doLogin(ctx, ls)
}
func (api *withLoginPsiphonConfigAPI) maybeLogin(ctx context.Context) (string, error) {
ls, _ := api.readstate()
if ls == nil || !ls.credentialsValid() {
return api.forceRegister(ctx)
}
if !ls.tokenValid() {
return api.doLogin(ctx, ls)
}
return ls.Token, nil
}
func (api *withLoginPsiphonConfigAPI) doLogin(ctx context.Context, ls *loginState) (string, error) {
req := &apimodel.LoginRequest{
ClientID: ls.ClientID,
Password: ls.Password,
}
resp, err := api.LoginAPI.Call(ctx, req)
if err != nil {
return "", err
}
ls.Token = resp.Token
ls.Expire = resp.Expire
if err := api.writestate(ls); err != nil {
return "", err
}
return ls.Token, nil
}
var _ callerForPsiphonConfigAPI = &withLoginPsiphonConfigAPI{}
// withLoginTorTargetsAPI implements login for simpleTorTargetsAPI.
type withLoginTorTargetsAPI struct {
API clonerForTorTargetsAPI // mandatory
JSONCodec JSONCodec // optional
KVStore KVStore // mandatory
RegisterAPI callerForRegisterAPI // mandatory
LoginAPI callerForLoginAPI // mandatory
}
// Call logins, if needed, then calls the API.
func (api *withLoginTorTargetsAPI) Call(ctx context.Context, req *apimodel.TorTargetsRequest) (apimodel.TorTargetsResponse, error) {
token, err := api.maybeLogin(ctx)
if err != nil {
return nil, err
}
resp, err := api.API.WithToken(token).Call(ctx, req)
if errors.Is(err, ErrUnauthorized) {
// Maybe the clock is just off? Let's try to obtain
// a token again and see if this fixes it.
if token, err = api.forceLogin(ctx); err == nil {
switch resp, err = api.API.WithToken(token).Call(ctx, req); err {
case nil:
return resp, nil
case ErrUnauthorized:
// fallthrough
default:
return nil, err
}
}
// Okay, this seems a broader problem. How about we try
// and re-register ourselves again instead?
token, err = api.forceRegister(ctx)
if err != nil {
return nil, err
}
resp, err = api.API.WithToken(token).Call(ctx, req)
// fallthrough
}
if err != nil {
return nil, err
}
return resp, nil
}
func (api *withLoginTorTargetsAPI) jsonCodec() JSONCodec {
if api.JSONCodec != nil {
return api.JSONCodec
}
return &defaultJSONCodec{}
}
func (api *withLoginTorTargetsAPI) readstate() (*loginState, error) {
data, err := api.KVStore.Get(loginKey)
if err != nil {
return nil, err
}
var ls loginState
if err := api.jsonCodec().Decode(data, &ls); err != nil {
return nil, err
}
return &ls, nil
}
func (api *withLoginTorTargetsAPI) writestate(ls *loginState) error {
data, err := api.jsonCodec().Encode(*ls)
if err != nil {
return err
}
return api.KVStore.Set(loginKey, data)
}
func (api *withLoginTorTargetsAPI) doRegister(ctx context.Context, password string) (string, error) {
req := newRegisterRequest(password)
ls := &loginState{}
resp, err := api.RegisterAPI.Call(ctx, req)
if err != nil {
return "", err
}
ls.ClientID = resp.ClientID
ls.Password = req.Password
return api.doLogin(ctx, ls)
}
func (api *withLoginTorTargetsAPI) forceRegister(ctx context.Context) (string, error) {
var password string
// If we already have a previous password, let us keep
// using it. This will allow a new version of the API to
// be able to continue to identify this probe. (This
// assumes that we have a stateless API that generates
// the user ID as a signature of the password plus a
// timestamp and that the key to generate the signature
// is not lost. If all these conditions are met, we
// can then serve better test targets to more long running
// (and therefore trusted) probes.)
if ls, err := api.readstate(); err == nil {
password = ls.Password
}
if password == "" {
password = newRandomPassword()
}
return api.doRegister(ctx, password)
}
func (api *withLoginTorTargetsAPI) forceLogin(ctx context.Context) (string, error) {
ls, err := api.readstate()
if err != nil {
return "", err
}
return api.doLogin(ctx, ls)
}
func (api *withLoginTorTargetsAPI) maybeLogin(ctx context.Context) (string, error) {
ls, _ := api.readstate()
if ls == nil || !ls.credentialsValid() {
return api.forceRegister(ctx)
}
if !ls.tokenValid() {
return api.doLogin(ctx, ls)
}
return ls.Token, nil
}
func (api *withLoginTorTargetsAPI) doLogin(ctx context.Context, ls *loginState) (string, error) {
req := &apimodel.LoginRequest{
ClientID: ls.ClientID,
Password: ls.Password,
}
resp, err := api.LoginAPI.Call(ctx, req)
if err != nil {
return "", err
}
ls.Token = resp.Token
ls.Expire = resp.Expire
if err := api.writestate(ls); err != nil {
return "", err
}
return ls.Token, nil
}
var _ callerForTorTargetsAPI = &withLoginTorTargetsAPI{}
File diff suppressed because it is too large Load Diff
+208
View File
@@ -0,0 +1,208 @@
package ooapi
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"sync"
"testing"
"time"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel"
)
// LoginHandler is an http.Handler to test login
type LoginHandler struct {
failCallWith []int // ignored by login and register
mu sync.Mutex
noRegister bool
state []*loginState
t *testing.T
logins *atomicx.Int64
registers *atomicx.Int64
}
func (lh *LoginHandler) forgetLogins() {
defer lh.mu.Unlock()
lh.mu.Lock()
lh.state = nil
}
func (lh *LoginHandler) forgetTokens() {
defer lh.mu.Unlock()
lh.mu.Lock()
for _, entry := range lh.state {
// This should be enough to cause all tokens to
// be expired and force clients to relogin.
//
// (It does not matter much whether the client
// clock is off, or the server clock is off,
// thanks Galileo for explaining this to us <3.)
entry.Expire = time.Now().Add(-3600 * time.Second)
}
}
func (lh *LoginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Implementation note: we don't check for the method
// for simplicity since it's already tested.
switch r.URL.Path {
case "/api/v1/register":
if lh.registers != nil {
lh.registers.Add(1)
}
lh.register(w, r)
case "/api/v1/login":
if lh.logins != nil {
lh.logins.Add(1)
}
lh.login(w, r)
case "/api/v1/test-list/psiphon-config":
lh.psiphon(w, r)
case "/api/v1/test-list/tor-targets":
lh.tor(w, r)
default:
w.WriteHeader(500)
}
}
func (lh *LoginHandler) register(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
w.WriteHeader(400)
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(400)
return
}
var req apimodel.RegisterRequest
if err := json.Unmarshal(data, &req); err != nil {
w.WriteHeader(400)
return
}
if req.Password == "" {
w.WriteHeader(400)
return
}
defer lh.mu.Unlock()
lh.mu.Lock()
if lh.noRegister {
// We have been asked to stop registering clients so
// we're going to make a boo boo.
w.WriteHeader(500)
return
}
var resp apimodel.RegisterResponse
ff := &fakeFill{}
ff.fill(&resp)
lh.state = append(lh.state, &loginState{
ClientID: resp.ClientID, Password: req.Password})
data, err = json.Marshal(&resp)
if err != nil {
w.WriteHeader(500)
return
}
lh.t.Logf("register: %+v", string(data))
w.Write(data)
}
func (lh *LoginHandler) login(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
w.WriteHeader(400)
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(400)
return
}
var req apimodel.LoginRequest
if err := json.Unmarshal(data, &req); err != nil {
w.WriteHeader(400)
return
}
defer lh.mu.Unlock()
lh.mu.Lock()
for _, s := range lh.state {
if req.ClientID == s.ClientID && req.Password == s.Password {
var resp apimodel.LoginResponse
ff := &fakeFill{}
ff.fill(&resp)
// We want the token to be many seconds in the future while
// ff.fill only sets the tokent to now plus a small delta.
resp.Expire = time.Now().Add(3600 * time.Second)
s.Expire = resp.Expire
s.Token = resp.Token
data, err = json.Marshal(&resp)
if err != nil {
w.WriteHeader(500)
return
}
lh.t.Logf("login: %+v", string(data))
w.Write(data)
return
}
}
lh.t.Log("login: 401")
w.WriteHeader(401)
}
func (lh *LoginHandler) psiphon(w http.ResponseWriter, r *http.Request) {
defer lh.mu.Unlock()
lh.mu.Lock()
if len(lh.failCallWith) > 0 {
code := lh.failCallWith[0]
lh.failCallWith = lh.failCallWith[1:]
w.WriteHeader(code)
return
}
token := strings.Replace(r.Header.Get("Authorization"), "Bearer ", "", 1)
for _, s := range lh.state {
if token == s.Token && time.Now().Before(s.Expire) {
var resp apimodel.PsiphonConfigResponse
ff := &fakeFill{}
ff.fill(&resp)
data, err := json.Marshal(&resp)
if err != nil {
w.WriteHeader(500)
return
}
lh.t.Logf("psiphon: %+v", string(data))
w.Write(data)
return
}
}
lh.t.Log("psiphon: 401")
w.WriteHeader(401)
}
func (lh *LoginHandler) tor(w http.ResponseWriter, r *http.Request) {
defer lh.mu.Unlock()
lh.mu.Lock()
if len(lh.failCallWith) > 0 {
code := lh.failCallWith[0]
lh.failCallWith = lh.failCallWith[1:]
w.WriteHeader(code)
return
}
token := strings.Replace(r.Header.Get("Authorization"), "Bearer ", "", 1)
for _, s := range lh.state {
if token == s.Token && time.Now().Before(s.Expire) {
var resp apimodel.TorTargetsResponse
ff := &fakeFill{}
ff.fill(&resp)
data, err := json.Marshal(&resp)
if err != nil {
w.WriteHeader(500)
return
}
lh.t.Logf("tor: %+v", string(data))
w.Write(data)
return
}
}
lh.t.Log("tor: 401")
w.WriteHeader(401)
}
+59
View File
@@ -0,0 +1,59 @@
package ooapi
import (
"crypto/rand"
"encoding/base64"
"time"
"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
// loginState is the struct saved in the kvstore
// to keep track of the login state.
type loginState struct {
ClientID string
Expire time.Time
Password string
Token string
}
func (ls *loginState) credentialsValid() bool {
return ls.ClientID != "" && ls.Password != ""
}
func (ls *loginState) tokenValid() bool {
return ls.Token != "" && time.Now().Add(60*time.Second).Before(ls.Expire)
}
// loginKey is the key with which loginState is saved
// into the key-value store used by Client.
const loginKey = "orchestra.state"
// newRandomPassword generates a new random password.
func newRandomPassword() string {
b := make([]byte, 48)
_, err := rand.Read(b)
runtimex.PanicOnError(err, "rand.Read failed")
return base64.StdEncoding.EncodeToString(b)
}
// newRegisterRequest creates a new RegisterRequest.
func newRegisterRequest(password string) *apimodel.RegisterRequest {
return &apimodel.RegisterRequest{
// The original implementation has as its only use case that we
// were registering and logging in for sending an update regarding
// the probe whereabouts. Yet here in probe-engine, the orchestra
// is currently only used to fetch inputs. For this purpose, we don't
// need to communicate any specific information. The code that will
// perform an update used to be responsible of doing that. Now, we
// are not using orchestra for this purpose anymore.
Platform: "miniooni",
ProbeASN: "AS0",
ProbeCC: "ZZ",
SoftwareName: "miniooni",
SoftwareVersion: "0.1.0-dev",
SupportedTests: []string{"web_connectivity"},
Password: password,
}
}
+192
View File
@@ -0,0 +1,192 @@
// Code generated by go generate; DO NOT EDIT.
// 2021-05-12 09:15:07.590481334 +0200 CEST m=+0.000085230
package ooapi
//go:generate go run ./internal/generator -file requests.go
import (
"bytes"
"context"
"net/http"
"net/url"
"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel"
)
func (api *simpleCheckReportIDAPI) newRequest(ctx context.Context, req *apimodel.CheckReportIDRequest) (*http.Request, error) {
URL, err := url.Parse(api.baseURL())
if err != nil {
return nil, err
}
URL.Path = "/api/_/check_report_id"
q := url.Values{}
if req.ReportID == "" {
return nil, newErrEmptyField("ReportID")
}
q.Add("report_id", req.ReportID)
URL.RawQuery = q.Encode()
return api.requestMaker().NewRequest(ctx, "GET", URL.String(), nil)
}
func (api *simpleCheckInAPI) newRequest(ctx context.Context, req *apimodel.CheckInRequest) (*http.Request, error) {
URL, err := url.Parse(api.baseURL())
if err != nil {
return nil, err
}
URL.Path = "/api/v1/check-in"
body, err := api.jsonCodec().Encode(req)
if err != nil {
return nil, err
}
out, err := api.requestMaker().NewRequest(ctx, "POST", URL.String(), bytes.NewReader(body))
if err != nil {
return nil, err
}
out.Header.Set("Content-Type", "application/json")
return out, nil
}
func (api *simpleLoginAPI) newRequest(ctx context.Context, req *apimodel.LoginRequest) (*http.Request, error) {
URL, err := url.Parse(api.baseURL())
if err != nil {
return nil, err
}
URL.Path = "/api/v1/login"
body, err := api.jsonCodec().Encode(req)
if err != nil {
return nil, err
}
out, err := api.requestMaker().NewRequest(ctx, "POST", URL.String(), bytes.NewReader(body))
if err != nil {
return nil, err
}
out.Header.Set("Content-Type", "application/json")
return out, nil
}
func (api *simpleMeasurementMetaAPI) newRequest(ctx context.Context, req *apimodel.MeasurementMetaRequest) (*http.Request, error) {
URL, err := url.Parse(api.baseURL())
if err != nil {
return nil, err
}
URL.Path = "/api/v1/measurement_meta"
q := url.Values{}
if req.ReportID == "" {
return nil, newErrEmptyField("ReportID")
}
q.Add("report_id", req.ReportID)
if req.Full {
q.Add("full", "true")
}
if req.Input != "" {
q.Add("input", req.Input)
}
URL.RawQuery = q.Encode()
return api.requestMaker().NewRequest(ctx, "GET", URL.String(), nil)
}
func (api *simpleRegisterAPI) newRequest(ctx context.Context, req *apimodel.RegisterRequest) (*http.Request, error) {
URL, err := url.Parse(api.baseURL())
if err != nil {
return nil, err
}
URL.Path = "/api/v1/register"
body, err := api.jsonCodec().Encode(req)
if err != nil {
return nil, err
}
out, err := api.requestMaker().NewRequest(ctx, "POST", URL.String(), bytes.NewReader(body))
if err != nil {
return nil, err
}
out.Header.Set("Content-Type", "application/json")
return out, nil
}
func (api *simpleTestHelpersAPI) newRequest(ctx context.Context, req *apimodel.TestHelpersRequest) (*http.Request, error) {
URL, err := url.Parse(api.baseURL())
if err != nil {
return nil, err
}
URL.Path = "/api/v1/test-helpers"
return api.requestMaker().NewRequest(ctx, "GET", URL.String(), nil)
}
func (api *simplePsiphonConfigAPI) newRequest(ctx context.Context, req *apimodel.PsiphonConfigRequest) (*http.Request, error) {
URL, err := url.Parse(api.baseURL())
if err != nil {
return nil, err
}
URL.Path = "/api/v1/test-list/psiphon-config"
return api.requestMaker().NewRequest(ctx, "GET", URL.String(), nil)
}
func (api *simpleTorTargetsAPI) newRequest(ctx context.Context, req *apimodel.TorTargetsRequest) (*http.Request, error) {
URL, err := url.Parse(api.baseURL())
if err != nil {
return nil, err
}
URL.Path = "/api/v1/test-list/tor-targets"
return api.requestMaker().NewRequest(ctx, "GET", URL.String(), nil)
}
func (api *simpleURLsAPI) newRequest(ctx context.Context, req *apimodel.URLsRequest) (*http.Request, error) {
URL, err := url.Parse(api.baseURL())
if err != nil {
return nil, err
}
URL.Path = "/api/v1/test-list/urls"
q := url.Values{}
if req.CategoryCodes != "" {
q.Add("category_codes", req.CategoryCodes)
}
if req.CountryCode != "" {
q.Add("country_code", req.CountryCode)
}
if req.Limit != 0 {
q.Add("limit", newQueryFieldInt64(req.Limit))
}
URL.RawQuery = q.Encode()
return api.requestMaker().NewRequest(ctx, "GET", URL.String(), nil)
}
func (api *simpleOpenReportAPI) newRequest(ctx context.Context, req *apimodel.OpenReportRequest) (*http.Request, error) {
URL, err := url.Parse(api.baseURL())
if err != nil {
return nil, err
}
URL.Path = "/report"
body, err := api.jsonCodec().Encode(req)
if err != nil {
return nil, err
}
out, err := api.requestMaker().NewRequest(ctx, "POST", URL.String(), bytes.NewReader(body))
if err != nil {
return nil, err
}
out.Header.Set("Content-Type", "application/json")
return out, nil
}
func (api *simpleSubmitMeasurementAPI) newRequest(ctx context.Context, req *apimodel.SubmitMeasurementRequest) (*http.Request, error) {
URL, err := url.Parse(api.baseURL())
if err != nil {
return nil, err
}
up, err := api.templateExecutor().Execute("/report/{{ .ReportID }}", req)
if err != nil {
return nil, err
}
URL.Path = up
body, err := api.jsonCodec().Encode(req)
if err != nil {
return nil, err
}
out, err := api.requestMaker().NewRequest(ctx, "POST", URL.String(), bytes.NewReader(body))
if err != nil {
return nil, err
}
out.Header.Set("Content-Type", "application/json")
return out, nil
}
+276
View File
@@ -0,0 +1,276 @@
// Code generated by go generate; DO NOT EDIT.
// 2021-05-12 09:15:08.237841277 +0200 CEST m=+0.000121556
package ooapi
//go:generate go run ./internal/generator -file responses.go
import (
"io"
"io/ioutil"
"net/http"
"github.com/ooni/probe-cli/v3/internal/ooapi/apimodel"
)
func (api *simpleCheckReportIDAPI) newResponse(resp *http.Response, err error) (*apimodel.CheckReportIDResponse, error) {
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, ErrUnauthorized
}
if resp.StatusCode != 200 {
return nil, newHTTPFailure(resp.StatusCode)
}
defer resp.Body.Close()
reader := io.LimitReader(resp.Body, 4<<20)
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out := &apimodel.CheckReportIDResponse{}
if err := api.jsonCodec().Decode(data, out); err != nil {
return nil, err
}
return out, nil
}
func (api *simpleCheckInAPI) newResponse(resp *http.Response, err error) (*apimodel.CheckInResponse, error) {
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, ErrUnauthorized
}
if resp.StatusCode != 200 {
return nil, newHTTPFailure(resp.StatusCode)
}
defer resp.Body.Close()
reader := io.LimitReader(resp.Body, 4<<20)
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out := &apimodel.CheckInResponse{}
if err := api.jsonCodec().Decode(data, out); err != nil {
return nil, err
}
return out, nil
}
func (api *simpleLoginAPI) newResponse(resp *http.Response, err error) (*apimodel.LoginResponse, error) {
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, ErrUnauthorized
}
if resp.StatusCode != 200 {
return nil, newHTTPFailure(resp.StatusCode)
}
defer resp.Body.Close()
reader := io.LimitReader(resp.Body, 4<<20)
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out := &apimodel.LoginResponse{}
if err := api.jsonCodec().Decode(data, out); err != nil {
return nil, err
}
return out, nil
}
func (api *simpleMeasurementMetaAPI) newResponse(resp *http.Response, err error) (*apimodel.MeasurementMetaResponse, error) {
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, ErrUnauthorized
}
if resp.StatusCode != 200 {
return nil, newHTTPFailure(resp.StatusCode)
}
defer resp.Body.Close()
reader := io.LimitReader(resp.Body, 4<<20)
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out := &apimodel.MeasurementMetaResponse{}
if err := api.jsonCodec().Decode(data, out); err != nil {
return nil, err
}
return out, nil
}
func (api *simpleRegisterAPI) newResponse(resp *http.Response, err error) (*apimodel.RegisterResponse, error) {
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, ErrUnauthorized
}
if resp.StatusCode != 200 {
return nil, newHTTPFailure(resp.StatusCode)
}
defer resp.Body.Close()
reader := io.LimitReader(resp.Body, 4<<20)
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out := &apimodel.RegisterResponse{}
if err := api.jsonCodec().Decode(data, out); err != nil {
return nil, err
}
return out, nil
}
func (api *simpleTestHelpersAPI) newResponse(resp *http.Response, err error) (apimodel.TestHelpersResponse, error) {
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, ErrUnauthorized
}
if resp.StatusCode != 200 {
return nil, newHTTPFailure(resp.StatusCode)
}
defer resp.Body.Close()
reader := io.LimitReader(resp.Body, 4<<20)
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out := apimodel.TestHelpersResponse{}
if err := api.jsonCodec().Decode(data, &out); err != nil {
return nil, err
}
if out == nil {
return nil, ErrJSONLiteralNull
}
return out, nil
}
func (api *simplePsiphonConfigAPI) newResponse(resp *http.Response, err error) (apimodel.PsiphonConfigResponse, error) {
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, ErrUnauthorized
}
if resp.StatusCode != 200 {
return nil, newHTTPFailure(resp.StatusCode)
}
defer resp.Body.Close()
reader := io.LimitReader(resp.Body, 4<<20)
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out := apimodel.PsiphonConfigResponse{}
if err := api.jsonCodec().Decode(data, &out); err != nil {
return nil, err
}
if out == nil {
return nil, ErrJSONLiteralNull
}
return out, nil
}
func (api *simpleTorTargetsAPI) newResponse(resp *http.Response, err error) (apimodel.TorTargetsResponse, error) {
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, ErrUnauthorized
}
if resp.StatusCode != 200 {
return nil, newHTTPFailure(resp.StatusCode)
}
defer resp.Body.Close()
reader := io.LimitReader(resp.Body, 4<<20)
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out := apimodel.TorTargetsResponse{}
if err := api.jsonCodec().Decode(data, &out); err != nil {
return nil, err
}
if out == nil {
return nil, ErrJSONLiteralNull
}
return out, nil
}
func (api *simpleURLsAPI) newResponse(resp *http.Response, err error) (*apimodel.URLsResponse, error) {
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, ErrUnauthorized
}
if resp.StatusCode != 200 {
return nil, newHTTPFailure(resp.StatusCode)
}
defer resp.Body.Close()
reader := io.LimitReader(resp.Body, 4<<20)
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out := &apimodel.URLsResponse{}
if err := api.jsonCodec().Decode(data, out); err != nil {
return nil, err
}
return out, nil
}
func (api *simpleOpenReportAPI) newResponse(resp *http.Response, err error) (*apimodel.OpenReportResponse, error) {
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, ErrUnauthorized
}
if resp.StatusCode != 200 {
return nil, newHTTPFailure(resp.StatusCode)
}
defer resp.Body.Close()
reader := io.LimitReader(resp.Body, 4<<20)
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out := &apimodel.OpenReportResponse{}
if err := api.jsonCodec().Decode(data, out); err != nil {
return nil, err
}
return out, nil
}
func (api *simpleSubmitMeasurementAPI) newResponse(resp *http.Response, err error) (*apimodel.SubmitMeasurementResponse, error) {
if err != nil {
return nil, err
}
if resp.StatusCode == 401 {
return nil, ErrUnauthorized
}
if resp.StatusCode != 200 {
return nil, newHTTPFailure(resp.StatusCode)
}
defer resp.Body.Close()
reader := io.LimitReader(resp.Body, 4<<20)
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
out := &apimodel.SubmitMeasurementResponse{}
if err := api.jsonCodec().Decode(data, out); err != nil {
return nil, err
}
return out, nil
}
+578
View File
@@ -0,0 +1,578 @@
// Code generated by go generate; DO NOT EDIT.
// 2021-05-12 09:15:08.888123159 +0200 CEST m=+0.000883704
package ooapi
//go:generate go run ./internal/generator -file swagger_test.go
const swagger = `{
"swagger": "2.0",
"info": {
"title": "OONI API specification",
"version": "0.20210512.5071508"
},
"host": "api.ooni.io",
"basePath": "/",
"schemes": [
"https"
],
"paths": {
"/api/_/check_report_id": {
"get": {
"produces": [
"application/json"
],
"parameters": [
{
"in": "query",
"name": "report_id",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "all good",
"schema": {
"properties": {
"error": {
"type": "string"
},
"found": {
"type": "boolean"
},
"v": {
"type": "integer"
}
},
"type": "object"
}
}
}
}
},
"/api/v1/check-in": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"properties": {
"charging": {
"type": "boolean"
},
"on_wifi": {
"type": "boolean"
},
"platform": {
"type": "string"
},
"probe_asn": {
"type": "string"
},
"probe_cc": {
"type": "string"
},
"run_type": {
"type": "string"
},
"software_name": {
"type": "string"
},
"software_version": {
"type": "string"
},
"web_connectivity": {
"properties": {
"category_codes": {
"items": {
"type": "string"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
}
}
],
"responses": {
"200": {
"description": "all good",
"schema": {
"properties": {
"probe_asn": {
"type": "string"
},
"probe_cc": {
"type": "string"
},
"tests": {
"properties": {
"web_connectivity": {
"properties": {
"report_id": {
"type": "string"
},
"urls": {
"items": {
"properties": {
"category_code": {
"type": "string"
},
"country_code": {
"type": "string"
},
"url": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
},
"v": {
"type": "integer"
}
},
"type": "object"
}
}
}
}
},
"/api/v1/login": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"properties": {
"password": {
"type": "string"
},
"username": {
"type": "string"
}
},
"type": "object"
}
}
],
"responses": {
"200": {
"description": "all good",
"schema": {
"properties": {
"expire": {
"type": "string"
},
"token": {
"type": "string"
}
},
"type": "object"
}
}
}
}
},
"/api/v1/measurement_meta": {
"get": {
"produces": [
"application/json"
],
"parameters": [
{
"in": "query",
"name": "report_id",
"required": true,
"type": "string"
},
{
"in": "query",
"name": "full",
"type": "boolean"
},
{
"in": "query",
"name": "input",
"type": "string"
}
],
"responses": {
"200": {
"description": "all good",
"schema": {
"properties": {
"anomaly": {
"type": "boolean"
},
"category_code": {
"type": "string"
},
"confirmed": {
"type": "boolean"
},
"failure": {
"type": "boolean"
},
"input": {
"type": "string"
},
"measurement_start_time": {
"type": "string"
},
"probe_asn": {
"type": "integer"
},
"probe_cc": {
"type": "string"
},
"raw_measurement": {
"type": "string"
},
"report_id": {
"type": "string"
},
"scores": {
"type": "string"
},
"test_name": {
"type": "string"
},
"test_start_time": {
"type": "string"
}
},
"type": "object"
}
}
}
}
},
"/api/v1/register": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"properties": {
"available_bandwidth": {
"type": "string"
},
"device_token": {
"type": "string"
},
"language": {
"type": "string"
},
"network_type": {
"type": "string"
},
"password": {
"type": "string"
},
"platform": {
"type": "string"
},
"probe_asn": {
"type": "string"
},
"probe_cc": {
"type": "string"
},
"probe_family": {
"type": "string"
},
"probe_timezone": {
"type": "string"
},
"software_name": {
"type": "string"
},
"software_version": {
"type": "string"
},
"supported_tests": {
"items": {
"type": "string"
},
"type": "array"
}
},
"type": "object"
}
}
],
"responses": {
"200": {
"description": "all good",
"schema": {
"properties": {
"client_id": {
"type": "string"
}
},
"type": "object"
}
}
}
}
},
"/api/v1/test-helpers": {
"get": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "all good",
"schema": {
"type": "object"
}
}
}
}
},
"/api/v1/test-list/psiphon-config": {
"get": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "all good",
"schema": {
"type": "object"
}
}
}
}
},
"/api/v1/test-list/tor-targets": {
"get": {
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "all good",
"schema": {
"type": "object"
}
}
}
}
},
"/api/v1/test-list/urls": {
"get": {
"produces": [
"application/json"
],
"parameters": [
{
"in": "query",
"name": "category_codes",
"type": "string"
},
{
"in": "query",
"name": "country_code",
"type": "string"
},
{
"in": "query",
"name": "limit",
"type": "integer"
}
],
"responses": {
"200": {
"description": "all good",
"schema": {
"properties": {
"metadata": {
"properties": {
"count": {
"type": "integer"
}
},
"type": "object"
},
"results": {
"items": {
"properties": {
"category_code": {
"type": "string"
},
"country_code": {
"type": "string"
},
"url": {
"type": "string"
}
},
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
}
}
}
},
"/report": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"properties": {
"data_format_version": {
"type": "string"
},
"format": {
"type": "string"
},
"probe_asn": {
"type": "string"
},
"probe_cc": {
"type": "string"
},
"software_name": {
"type": "string"
},
"software_version": {
"type": "string"
},
"test_name": {
"type": "string"
},
"test_start_time": {
"type": "string"
},
"test_version": {
"type": "string"
}
},
"type": "object"
}
}
],
"responses": {
"200": {
"description": "all good",
"schema": {
"properties": {
"backend_version": {
"type": "string"
},
"report_id": {
"type": "string"
},
"supported_formats": {
"items": {
"type": "string"
},
"type": "array"
}
},
"type": "object"
}
}
}
}
},
"/report/{report_id}": {
"post": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"in": "path",
"name": "report_id",
"required": true,
"type": "string"
},
{
"in": "body",
"name": "body",
"required": true,
"schema": {
"properties": {
"content": {
"type": "object"
},
"format": {
"type": "string"
}
},
"type": "object"
}
}
],
"responses": {
"200": {
"description": "all good",
"schema": {
"properties": {
"measurement_uid": {
"type": "string"
}
},
"type": "object"
}
}
}
}
}
}
}`
+158
View File
@@ -0,0 +1,158 @@
package ooapi
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"sort"
"strings"
"testing"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"github.com/ooni/probe-cli/v3/internal/ooapi/internal/openapi"
)
const (
productionURL = "https://api.ooni.io/apispec_1.json"
testingURL = "https://ams-pg-test.ooni.org/apispec_1.json"
)
func makeModel(data []byte) *openapi.Swagger {
var out openapi.Swagger
if err := json.Unmarshal(data, &out); err != nil {
log.Fatal(err)
}
// We reduce irrelevant differences by producing a common header
return &openapi.Swagger{Paths: out.Paths}
}
func getServerModel(serverURL string) *openapi.Swagger {
resp, err := http.Get(serverURL)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return makeModel(data)
}
func getClientModel() *openapi.Swagger {
return makeModel([]byte(swagger))
}
func simplifyRoundTrip(rt *openapi.RoundTrip) {
// Normalize the used name when a parameter is in body. This
// should only have a cosmetic impact on the spec.
for _, param := range rt.Parameters {
if param.In == "body" {
param.Name = "body"
}
}
// Sort parameters so the comparison does not depend on order.
sort.SliceStable(rt.Parameters, func(i, j int) bool {
left, right := rt.Parameters[i].Name, rt.Parameters[j].Name
return strings.Compare(left, right) < 0
})
// Normalize description of 200 response
rt.Responses.Successful.Description = "all good"
}
func simplifyInPlace(path *openapi.Path) *openapi.Path {
if path.Get != nil && path.Post != nil {
log.Fatal("unsupported configuration")
}
if path.Get != nil {
simplifyRoundTrip(path.Get)
}
if path.Post != nil {
simplifyRoundTrip(path.Post)
}
return path
}
func jsonify(model interface{}) string {
data, err := json.MarshalIndent(model, "", " ")
if err != nil {
log.Fatal(err)
}
return string(data)
}
type diffable struct {
name string
value string
}
func computediff(server, client *diffable) string {
d := gotextdiff.ToUnified(server.name, client.name, server.value, myers.ComputeEdits(
span.URIFromPath(server.name), server.value, client.value,
))
return fmt.Sprint(d)
}
// maybediff emits the diff between the server and the client and
// returns the length of the diff itself in bytes.
func maybediff(key string, server, client *openapi.Path) int {
diff := computediff(&diffable{
name: fmt.Sprintf("server%s.json", key),
value: jsonify(simplifyInPlace(server)),
}, &diffable{
name: fmt.Sprintf("client%s.json", key),
value: jsonify(simplifyInPlace(client)),
})
if diff != "" {
fmt.Printf("%s", diff)
}
return len(diff)
}
func compare(serverURL string) bool {
good := true
serverModel, clientModel := getServerModel(serverURL), getClientModel()
// Implementation note: the server model is richer than the client
// model, so we ignore everything not defined by the client.
var count int
for key := range serverModel.Paths {
if _, found := clientModel.Paths[key]; !found {
delete(serverModel.Paths, key)
continue
}
count++
if maybediff(key, serverModel.Paths[key], clientModel.Paths[key]) > 0 {
good = false
}
}
if count <= 0 {
panic("no element found")
}
return good
}
func TestWithProductionAPI(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
t.Log("using ", productionURL)
if !compare(productionURL) {
t.Fatal("model mismatch (see above)")
}
}
func TestWithTestingAPI(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
t.Log("using ", testingURL)
if !compare(testingURL) {
t.Fatal("model mismatch (see above)")
}
}
+23
View File
@@ -0,0 +1,23 @@
package ooapi
import "fmt"
func newErrEmptyField(field string) error {
return fmt.Errorf("%w: %s", ErrEmptyField, field)
}
func newHTTPFailure(status int) error {
return fmt.Errorf("%w: %d", ErrHTTPFailure, status)
}
func newQueryFieldInt64(v int64) string {
return fmt.Sprintf("%d", v)
}
func newQueryFieldBool(v bool) string {
return fmt.Sprintf("%v", v)
}
func newAuthorizationHeader(token string) string {
return fmt.Sprintf("Bearer %s", token)
}
+12
View File
@@ -0,0 +1,12 @@
package ooapi
import "testing"
func TestNewQueryFieldBoolWorks(t *testing.T) {
if s := newQueryFieldBool(true); s != "true" {
t.Fatal("invalid encoding of true")
}
if s := newQueryFieldBool(false); s != "false" {
t.Fatal("invalid encoding of false")
}
}