refactor(simplequicping): use step-by-step (#852)

See https://github.com/ooni/probe/issues/2159 and https://github.com/ooni/spec/pull/254
This commit is contained in:
DecFox
2022-08-17 12:49:11 +05:30
committed by GitHub
parent fbd7cd2b7e
commit 69602abe8a
16 changed files with 922 additions and 26 deletions
+1
View File
@@ -163,6 +163,7 @@ type ArchivalTCPConnectStatus struct {
//
// See https://github.com/ooni/spec/blob/master/data-formats/df-006-tlshandshake.md
type ArchivalTLSOrQUICHandshakeResult struct {
Network string `json:"network"`
Address string `json:"address"`
CipherSuite string `json:"cipher_suite"`
Failure *string `json:"failure"`
+15
View File
@@ -8,6 +8,7 @@ import (
"crypto/tls"
"time"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/model"
)
@@ -25,6 +26,11 @@ type Trace struct {
MockOnTLSHandshakeDone func(started time.Time, remoteAddr string, config *tls.Config,
state tls.ConnectionState, err error, finished time.Time)
MockOnQUICHandshakeStart func(now time.Time, remoteAddrs string, config *quic.Config)
MockOnQUICHandshakeDone func(started time.Time, remoteAddr string, qconn quic.EarlyConnection,
config *tls.Config, err error, finished time.Time)
}
var _ model.Trace = &Trace{}
@@ -51,3 +57,12 @@ func (t *Trace) OnTLSHandshakeDone(started time.Time, remoteAddr string, config
state tls.ConnectionState, err error, finished time.Time) {
t.MockOnTLSHandshakeDone(started, remoteAddr, config, state, err, finished)
}
func (t *Trace) OnQUICHandshakeStart(now time.Time, remoteAddr string, config *quic.Config) {
t.MockOnQUICHandshakeStart(now, remoteAddr, config)
}
func (t *Trace) OnQUICHandshakeDone(started time.Time, remoteAddr string, qconn quic.EarlyConnection,
config *tls.Config, err error, finished time.Time) {
t.MockOnQUICHandshakeDone(started, remoteAddr, qconn, config, err, finished)
}
+34
View File
@@ -5,6 +5,7 @@ import (
"testing"
"time"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/model"
)
@@ -95,4 +96,37 @@ func TestTrace(t *testing.T) {
t.Fatal("not called")
}
})
t.Run("OnQUICHandshakeStart", func(t *testing.T) {
var called bool
tx := &Trace{
MockOnQUICHandshakeStart: func(now time.Time, remoteAddrs string, config *quic.Config) {
called = true
},
}
tx.OnQUICHandshakeStart(time.Now(), "1.1.1.1:443", &quic.Config{})
if !called {
t.Fatal("not called")
}
})
t.Run("OnQUICHandshakeDone", func(t *testing.T) {
var called bool
tx := &Trace{
MockOnQUICHandshakeDone: func(started time.Time, remoteAddr string, qconn quic.EarlyConnection, config *tls.Config, err error, finished time.Time) {
called = true
},
}
tx.OnQUICHandshakeDone(
time.Now(),
"1.1.1.1:443",
nil,
&tls.Config{},
nil,
time.Now(),
)
if !called {
t.Fatal("not called")
}
})
}
+35
View File
@@ -382,6 +382,41 @@ type Trace interface {
// 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)
// OnQUICHandshakeStart is called before the QUIC handshake.
//
// Arguments:
//
// - now is the moment before we start the handshake;
//
// - remoteAddr is the QUIC 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 possibly-nil QUIC config we're using.
OnQUICHandshakeStart(now time.Time, remoteAddr string, quicConfig *quic.Config)
// OnQUICHandshakeDone is called after the QUIC handshake.
//
// Arguments:
//
// - started is when we started the handshake;
//
// - remoteAddr is the QUIC 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);
//
// - qconn is the QUIC connection we receive after the handshake: either
// a valid quic.EarlyConnection or nil;
//
// - config is the non-nil TLS config we are using;
//
// - 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.
OnQUICHandshakeDone(started time.Time, remoteAddr string, qconn quic.EarlyConnection,
config *tls.Config, err error, finished time.Time)
}
// UDPLikeConn is a net.PacketConn with some extra functions