Skip to content

Commit

Permalink
[backport] refactor(stunreachability): input required and must be an …
Browse files Browse the repository at this point in the history
…URL (#630)

Here we're refactoring stunreachability to not provide internally a
default input and to take in input an URL rather than a string.

The related ooni/spec change is ooni/spec#227.

This diff has been extracted from #539.

Because the original diff was large, I'm splitting it in a set of
more easily manageable diffs.

The reference issue is ooni/probe#1814, which
is complex enough to require us to proceed incrementally.

This diff WILL need to be backported to release/3.11.
  • Loading branch information
bassosimone committed Dec 3, 2021
1 parent dcf2986 commit 9ec2ebc
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 90 deletions.
2 changes: 1 addition & 1 deletion internal/engine/allexperiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ var experimentsByName = map[string]func(*Session) *ExperimentBuilder{
))
},
config: &stunreachability.Config{},
inputPolicy: InputOptional,
inputPolicy: InputStrictlyRequired,
}
},

Expand Down
3 changes: 3 additions & 0 deletions internal/engine/experiment/stunreachability/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"time"
)

// TODO(bassosimone): we should use internal/netxlite/mocks rather
// than rolling out a custom type private to this package.

type FakeConn struct {
ReadError error
ReadData []byte
Expand Down
35 changes: 28 additions & 7 deletions internal/engine/experiment/stunreachability/stunreachability.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package stunreachability

import (
"context"
"errors"
"fmt"
"net"
"net/url"
"time"

"github.com/ooni/probe-cli/v3/internal/engine/legacy/errorsx"
Expand All @@ -20,7 +22,7 @@ import (

const (
testName = "stunreachability"
testVersion = "0.2.0"
testVersion = "0.3.0"
)

// Config contains the experiment config.
Expand Down Expand Up @@ -64,6 +66,15 @@ func wrap(err error) error {
}.MaybeBuild()
}

// errStunMissingInput means that the user did not provide any input
var errStunMissingInput = errors.New("stun: missing input")

// errStunMissingPortInURL means the URL is missing the port
var errStunMissingPortInURL = errors.New("stun: missing port in URL")

// errUnsupportedURLScheme means we don't support the URL scheme
var errUnsupportedURLScheme = errors.New("stun: unsupported URL scheme")

// Run implements ExperimentMeasurer.Run.
func (m *Measurer) Run(
ctx context.Context, sess model.ExperimentSession,
Expand All @@ -72,7 +83,21 @@ func (m *Measurer) Run(
tk := new(TestKeys)
measurement.TestKeys = tk
registerExtensions(measurement)
if err := wrap(tk.run(ctx, m.config, sess, measurement, callbacks)); err != nil {
input := string(measurement.Input)
if input == "" {
return errStunMissingInput
}
URL, err := url.Parse(input)
if err != nil {
return err
}
if URL.Port() == "" {
return errStunMissingPortInURL
}
if URL.Scheme != "stun" {
return errUnsupportedURLScheme
}
if err := wrap(tk.run(ctx, m.config, sess, measurement, callbacks, URL.Host)); err != nil {
s := err.Error()
tk.Failure = &s
return err
Expand All @@ -83,12 +108,8 @@ func (m *Measurer) Run(
func (tk *TestKeys) run(
ctx context.Context, config Config, sess model.ExperimentSession,
measurement *model.Measurement, callbacks model.ExperimentCallbacks,
endpoint string,
) error {
const defaultAddress = "stun.l.google.com:19302"
endpoint := string(measurement.Input)
if endpoint == "" {
endpoint = defaultAddress
}
callbacks.OnProgress(0, fmt.Sprintf("stunreachability: measuring: %s...", endpoint))
defer callbacks.OnProgress(
1, fmt.Sprintf("stunreachability: measuring: %s... done", endpoint))
Expand Down

This file was deleted.

Loading

0 comments on commit 9ec2ebc

Please sign in to comment.