feat: tlsping and tcpping using step-by-step (#815)
## Checklist - [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md) - [x] reference issue for this pull request: https://github.com/ooni/probe/issues/2158 - [x] if you changed anything related how experiments work and you need to reflect these changes in the ooni/spec repository, please link to the related ooni/spec pull request: https://github.com/ooni/spec/pull/250 ## Description This diff refactors the codebase to reimplement tlsping and tcpping to use the step-by-step measurements style. See docs/design/dd-003-step-by-step.md for more information on the step-by-step measurement style.
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/fakefill"
|
||||
"github.com/ooni/probe-cli/v3/internal/testingx"
|
||||
)
|
||||
|
||||
func TestArchivalExtSpec(t *testing.T) {
|
||||
@@ -301,7 +301,7 @@ func TestHTTPBody(t *testing.T) {
|
||||
// we make a mistake and apply the above change (which will in turn
|
||||
// break correct JSON serialization), the this test will fail.
|
||||
var body ArchivalHTTPBody
|
||||
ff := &fakefill.Filler{}
|
||||
ff := &testingx.FakeFiller{}
|
||||
ff.Fill(&body)
|
||||
data := ArchivalMaybeBinaryData(body)
|
||||
if diff := cmp.Diff(body, data); diff != "" {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package mocks
|
||||
|
||||
//
|
||||
// Mocks for model.Trace
|
||||
//
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
// Trace allows mocking model.Trace.
|
||||
type Trace struct {
|
||||
MockTimeNow func() time.Time
|
||||
|
||||
MockOnConnectDone func(
|
||||
started time.Time, network, domain, remoteAddr string, err error, finished time.Time)
|
||||
|
||||
MockOnTLSHandshakeStart func(now time.Time, remoteAddr string, config *tls.Config)
|
||||
|
||||
MockOnTLSHandshakeDone func(started time.Time, remoteAddr string, config *tls.Config,
|
||||
state tls.ConnectionState, err error, finished time.Time)
|
||||
}
|
||||
|
||||
var _ model.Trace = &Trace{}
|
||||
|
||||
func (t *Trace) TimeNow() time.Time {
|
||||
return t.MockTimeNow()
|
||||
}
|
||||
|
||||
func (t *Trace) OnConnectDone(
|
||||
started time.Time, network, domain, remoteAddr string, err error, finished time.Time) {
|
||||
t.MockOnConnectDone(started, network, domain, remoteAddr, err, finished)
|
||||
}
|
||||
|
||||
func (t *Trace) OnTLSHandshakeStart(now time.Time, remoteAddr string, config *tls.Config) {
|
||||
t.MockOnTLSHandshakeStart(now, remoteAddr, config)
|
||||
}
|
||||
|
||||
func (t *Trace) OnTLSHandshakeDone(started time.Time, remoteAddr string, config *tls.Config,
|
||||
state tls.ConnectionState, err error, finished time.Time) {
|
||||
t.MockOnTLSHandshakeDone(started, remoteAddr, config, state, err, finished)
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTrace(t *testing.T) {
|
||||
t.Run("TimeNow", func(t *testing.T) {
|
||||
now := time.Now()
|
||||
tx := &Trace{
|
||||
MockTimeNow: func() time.Time {
|
||||
return now
|
||||
},
|
||||
}
|
||||
if !tx.TimeNow().Equal(now) {
|
||||
t.Fatal("not working as intended")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("OnConnectDone", func(t *testing.T) {
|
||||
var called bool
|
||||
tx := &Trace{
|
||||
MockOnConnectDone: func(started time.Time, network, domain, remoteAddr string, err error, finished time.Time) {
|
||||
called = true
|
||||
},
|
||||
}
|
||||
tx.OnConnectDone(
|
||||
time.Now(),
|
||||
"tcp",
|
||||
"dns.google",
|
||||
"8.8.8.8:443",
|
||||
nil,
|
||||
time.Now(),
|
||||
)
|
||||
if !called {
|
||||
t.Fatal("not called")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("OnTLSHandshakeStart", func(t *testing.T) {
|
||||
var called bool
|
||||
tx := &Trace{
|
||||
MockOnTLSHandshakeStart: func(now time.Time, remoteAddr string, config *tls.Config) {
|
||||
called = true
|
||||
},
|
||||
}
|
||||
tx.OnTLSHandshakeStart(time.Now(), "8.8.8.8:443", &tls.Config{})
|
||||
if !called {
|
||||
t.Fatal("not called")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("OnTLSHandshakeDone", func(t *testing.T) {
|
||||
var called bool
|
||||
tx := &Trace{
|
||||
MockOnTLSHandshakeDone: func(started time.Time, remoteAddr string, config *tls.Config, state tls.ConnectionState, err error, finished time.Time) {
|
||||
called = true
|
||||
},
|
||||
}
|
||||
tx.OnTLSHandshakeDone(
|
||||
time.Now(),
|
||||
"8.8.8.8:443",
|
||||
&tls.Config{},
|
||||
tls.ConnectionState{},
|
||||
nil,
|
||||
time.Now(),
|
||||
)
|
||||
if !called {
|
||||
t.Fatal("not called")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -292,6 +292,76 @@ type TLSHandshaker interface {
|
||||
net.Conn, tls.ConnectionState, error)
|
||||
}
|
||||
|
||||
// Trace allows to collect measurement traces. A trace is injected into
|
||||
// netx operations using context.WithValue. Netx code retrieves the trace
|
||||
// using context.Value. See docs/design/dd-003-step-by-step.md for the
|
||||
// design document explaining why we implemented context-based tracing.
|
||||
type Trace interface {
|
||||
// TimeNow returns the current time. Normally, this should be the same
|
||||
// value returned by time.Now but you may want to manipulate the time
|
||||
// returned when testing to have deterministic tests. To this end, you
|
||||
// can use functionality exported by the ./internal/testingx pkg.
|
||||
TimeNow() time.Time
|
||||
|
||||
// OnConnectDone is called when connect terminates.
|
||||
//
|
||||
// Arguments:
|
||||
//
|
||||
// - started is when we called connect;
|
||||
//
|
||||
// - network is the network we're using (one of "tcp" and "udp");
|
||||
//
|
||||
// - domain is the domain for which we're calling connect. If the user called
|
||||
// connect for an IP address and a port, then domain will be an IP address;
|
||||
//
|
||||
// - remoteAddr is the TCP endpoint with which we are connecting: it will
|
||||
// consist of an IP address and a port (e.g., 8.8.8.8:443, [::1]:5421);
|
||||
//
|
||||
// - err is the result of connect: either an error or nil;
|
||||
//
|
||||
// - finished is when connect returned.
|
||||
//
|
||||
// The error passed to this function will always be wrapped such that the
|
||||
// string returned by Error is an OONI error.
|
||||
OnConnectDone(
|
||||
started time.Time, network, domain, remoteAddr string, err error, finished time.Time)
|
||||
|
||||
// OnTLSHandshakeStart is called when the TLS handshake starts.
|
||||
//
|
||||
// Arguments:
|
||||
//
|
||||
// - now is the moment before we start the handshake;
|
||||
//
|
||||
// - remoteAddr is the TCP endpoint with which we are connecting: it will
|
||||
// consist of an IP address and a port (e.g., 8.8.8.8:443, [::1]:5421);
|
||||
//
|
||||
// - config is the non-nil TLS config we're using.
|
||||
OnTLSHandshakeStart(now time.Time, remoteAddr string, config *tls.Config)
|
||||
|
||||
// OnTLSHandshakeDone is called when the TLS handshake terminates.
|
||||
//
|
||||
// Arguments:
|
||||
//
|
||||
// - started is when we started the handshake;
|
||||
//
|
||||
// - remoteAddr is the TCP endpoint with which we are connecting: it will
|
||||
// consist of an IP address and a port (e.g., 8.8.8.8:443, [::1]:5421);
|
||||
//
|
||||
// - config is the non-nil TLS config we're using;
|
||||
//
|
||||
// - state is the state of the TLS connection after the handshake, where all
|
||||
// fields are zero-initialized if the handshake failed;
|
||||
//
|
||||
// - err is the result of the handshake: either an error or nil;
|
||||
//
|
||||
// - finished is right after the handshake.
|
||||
//
|
||||
// The error passed to this function will always be wrapped such that the
|
||||
// string returned by Error is an OONI error.
|
||||
OnTLSHandshakeDone(started time.Time, remoteAddr string, config *tls.Config,
|
||||
state tls.ConnectionState, err error, finished time.Time)
|
||||
}
|
||||
|
||||
// UDPLikeConn is a net.PacketConn with some extra functions
|
||||
// required to convince the QUIC library (lucas-clemente/quic-go)
|
||||
// to inflate the receive buffer of the connection.
|
||||
|
||||
Reference in New Issue
Block a user