2022-05-31 21:53:01 +02:00
|
|
|
package tracex
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2022-06-01 23:15:47 +02:00
|
|
|
"crypto/x509"
|
2022-05-31 21:53:01 +02:00
|
|
|
"errors"
|
|
|
|
"net"
|
2021-02-02 12:05:47 +01:00
|
|
|
"testing"
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-02-02 12:05:47 +01:00
|
|
|
"github.com/lucas-clemente/quic-go"
|
2022-01-07 18:33:37 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2022-05-31 21:53:01 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func TestQUICDialerSaver(t *testing.T) {
|
|
|
|
t.Run("DialContext", func(t *testing.T) {
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
checkStartEventFields := func(t *testing.T, value *EventValue) {
|
|
|
|
if value.Address != "8.8.8.8:443" {
|
|
|
|
t.Fatal("invalid Address")
|
|
|
|
}
|
|
|
|
if !value.NoTLSVerify {
|
|
|
|
t.Fatal("expected NoTLSVerify to be true")
|
|
|
|
}
|
|
|
|
if value.Proto != "udp" {
|
|
|
|
t.Fatal("wrong protocol")
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(value.TLSNextProtos, []string{"h3"}); diff != "" {
|
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
if value.TLSServerName != "dns.google" {
|
|
|
|
t.Fatal("invalid TLSServerName")
|
|
|
|
}
|
|
|
|
if value.Time.IsZero() {
|
|
|
|
t.Fatal("expected non zero time")
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
checkStartedEvent := func(t *testing.T, ev Event) {
|
|
|
|
if _, good := ev.(*EventQUICHandshakeStart); !good {
|
|
|
|
t.Fatal("invalid event type")
|
|
|
|
}
|
|
|
|
value := ev.Value()
|
|
|
|
checkStartEventFields(t, value)
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
checkDoneEventFieldsSuccess := func(t *testing.T, value *EventValue) {
|
|
|
|
if value.Duration <= 0 {
|
|
|
|
t.Fatal("expected non-zero duration")
|
|
|
|
}
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
if value.Err.IsNotNil() {
|
2022-06-01 23:15:47 +02:00
|
|
|
t.Fatal("expected no error here")
|
|
|
|
}
|
|
|
|
if value.TLSCipherSuite != "TLS_RSA_WITH_RC4_128_SHA" {
|
|
|
|
t.Fatal("invalid cipher suite")
|
|
|
|
}
|
|
|
|
if value.TLSNegotiatedProto != "h3" {
|
|
|
|
t.Fatal("invalid negotiated protocol")
|
|
|
|
}
|
2022-06-02 11:07:02 +02:00
|
|
|
if diff := cmp.Diff(value.TLSPeerCerts, [][]byte{{1, 2, 3, 4}}); diff != "" {
|
2022-06-01 23:15:47 +02:00
|
|
|
t.Fatal(diff)
|
|
|
|
}
|
|
|
|
if value.TLSVersion != "TLSv1.3" {
|
|
|
|
t.Fatal("invalid TLS version")
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
|
|
|
|
checkDoneEvent := func(t *testing.T, ev Event, fun func(t *testing.T, value *EventValue)) {
|
|
|
|
if _, good := ev.(*EventQUICHandshakeDone); !good {
|
|
|
|
t.Fatal("invalid event type")
|
|
|
|
}
|
|
|
|
value := ev.Value()
|
|
|
|
checkStartEventFields(t, value)
|
|
|
|
fun(t, value)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
|
|
|
saver := &Saver{}
|
|
|
|
returnedConn := &mocks.QUICEarlyConnection{
|
|
|
|
MockConnectionState: func() quic.ConnectionState {
|
|
|
|
cs := quic.ConnectionState{}
|
|
|
|
cs.TLS.ConnectionState.CipherSuite = tls.TLS_RSA_WITH_RC4_128_SHA
|
|
|
|
cs.TLS.NegotiatedProtocol = "h3"
|
2022-06-02 11:07:02 +02:00
|
|
|
cs.TLS.PeerCertificates = []*x509.Certificate{{
|
|
|
|
Raw: []byte{1, 2, 3, 4},
|
|
|
|
}}
|
2022-06-01 23:15:47 +02:00
|
|
|
cs.TLS.Version = tls.VersionTLS13
|
|
|
|
return cs
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dialer := saver.WrapQUICDialer(&mocks.QUICDialer{
|
2022-08-19 11:26:50 +02:00
|
|
|
MockDialContext: func(ctx context.Context, address string,
|
2022-06-01 23:15:47 +02:00
|
|
|
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
|
|
|
return returnedConn, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ctx := context.Background()
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
NextProtos: []string{"h3"},
|
|
|
|
ServerName: "dns.google",
|
|
|
|
}
|
|
|
|
quicConfig := &quic.Config{}
|
2022-08-19 11:26:50 +02:00
|
|
|
conn, err := dialer.DialContext(ctx, "8.8.8.8:443", tlsConfig, quicConfig)
|
2022-06-01 23:15:47 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
t.Fatal("expected non-nil conn")
|
|
|
|
}
|
|
|
|
events := saver.Read()
|
|
|
|
if len(events) != 2 {
|
|
|
|
t.Fatal("expected two events")
|
|
|
|
}
|
|
|
|
checkStartedEvent(t, events[0])
|
|
|
|
checkDoneEvent(t, events[1], checkDoneEventFieldsSuccess)
|
|
|
|
})
|
|
|
|
|
|
|
|
checkDoneEventFieldsFailure := func(t *testing.T, value *EventValue) {
|
|
|
|
if value.Duration <= 0 {
|
|
|
|
t.Fatal("expected non-zero duration")
|
|
|
|
}
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
if value.Err.IsNil() {
|
2022-06-01 23:15:47 +02:00
|
|
|
t.Fatal("expected non-nil error here")
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2022-05-31 21:53:01 +02:00
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
t.Run("on failure", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
saver := &Saver{}
|
|
|
|
dialer := saver.WrapQUICDialer(&mocks.QUICDialer{
|
2022-08-19 11:26:50 +02:00
|
|
|
MockDialContext: func(ctx context.Context, address string,
|
2022-06-01 23:15:47 +02:00
|
|
|
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlyConnection, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
})
|
|
|
|
ctx := context.Background()
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
NextProtos: []string{"h3"},
|
|
|
|
ServerName: "dns.google",
|
|
|
|
}
|
|
|
|
quicConfig := &quic.Config{}
|
2022-08-19 11:26:50 +02:00
|
|
|
conn, err := dialer.DialContext(ctx, "8.8.8.8:443", tlsConfig, quicConfig)
|
2022-06-01 23:15:47 +02:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
if conn != nil {
|
|
|
|
t.Fatal("expected nil conn")
|
|
|
|
}
|
|
|
|
events := saver.Read()
|
|
|
|
if len(events) != 2 {
|
|
|
|
t.Fatal("expected two events")
|
|
|
|
}
|
|
|
|
checkStartedEvent(t, events[0])
|
|
|
|
checkDoneEvent(t, events[1], checkDoneEventFieldsFailure)
|
|
|
|
})
|
2022-06-01 07:44:54 +02:00
|
|
|
})
|
2022-06-01 23:15:47 +02:00
|
|
|
|
|
|
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
|
|
|
var called bool
|
|
|
|
child := &mocks.QUICDialer{
|
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
called = true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dialer := &QUICDialerSaver{
|
|
|
|
QUICDialer: child,
|
|
|
|
Saver: &Saver{},
|
|
|
|
}
|
|
|
|
dialer.CloseIdleConnections()
|
|
|
|
if !called {
|
|
|
|
t.Fatal("not called")
|
|
|
|
}
|
2022-05-31 21:53:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-04 14:58:48 +02:00
|
|
|
func TestWrapQUICListener(t *testing.T) {
|
|
|
|
var saver *Saver
|
|
|
|
ql := &mocks.QUICListener{}
|
|
|
|
if saver.WrapQUICListener(ql) != ql {
|
|
|
|
t.Fatal("unexpected result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func TestQUICListenerSaver(t *testing.T) {
|
|
|
|
t.Run("on failure", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
saver := &Saver{}
|
|
|
|
qls := saver.WrapQUICListener(&mocks.QUICListener{
|
|
|
|
MockListen: func(addr *net.UDPAddr) (model.UDPLikeConn, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
})
|
|
|
|
pconn, err := qls.Listen(&net.UDPAddr{
|
|
|
|
IP: []byte{},
|
|
|
|
Port: 8080,
|
|
|
|
Zone: "",
|
|
|
|
})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("unexpected error", err)
|
2022-05-31 21:53:01 +02:00
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
if pconn != nil {
|
|
|
|
t.Fatal("expected nil pconn here")
|
2022-05-31 21:53:01 +02:00
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
|
|
|
saver := &Saver{}
|
|
|
|
returnedConn := &mocks.UDPLikeConn{}
|
|
|
|
qls := saver.WrapQUICListener(&mocks.QUICListener{
|
|
|
|
MockListen: func(addr *net.UDPAddr) (model.UDPLikeConn, error) {
|
|
|
|
return returnedConn, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
pconn, err := qls.Listen(&net.UDPAddr{
|
|
|
|
IP: []byte{},
|
|
|
|
Port: 8080,
|
|
|
|
Zone: "",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2022-05-31 21:53:01 +02:00
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
wconn := pconn.(*quicPacketConnWrapper)
|
|
|
|
if wconn.UDPLikeConn != returnedConn {
|
|
|
|
t.Fatal("invalid underlying connection")
|
2022-05-31 21:53:01 +02:00
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
if wconn.saver != saver {
|
|
|
|
t.Fatal("invalid saver")
|
2022-05-31 21:53:01 +02:00
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQUICPacketConnWrapper(t *testing.T) {
|
|
|
|
t.Run("ReadFrom", func(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("on failure", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
saver := &Saver{}
|
|
|
|
conn := &quicPacketConnWrapper{
|
|
|
|
UDPLikeConn: &mocks.UDPLikeConn{
|
|
|
|
MockReadFrom: func(p []byte) (int, net.Addr, error) {
|
|
|
|
return 0, nil, expected
|
|
|
|
},
|
|
|
|
},
|
|
|
|
saver: saver,
|
|
|
|
}
|
|
|
|
buf := make([]byte, 1<<17)
|
|
|
|
count, addr, err := conn.ReadFrom(buf)
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
if count != 0 {
|
|
|
|
t.Fatal("invalid count")
|
|
|
|
}
|
|
|
|
if addr != nil {
|
|
|
|
t.Fatal("invalid addr")
|
|
|
|
}
|
|
|
|
events := saver.Read()
|
|
|
|
if len(events) != 1 {
|
|
|
|
t.Fatal("invalid number of events")
|
|
|
|
}
|
|
|
|
ev0 := events[0]
|
|
|
|
if _, good := ev0.(*EventReadFromOperation); !good {
|
|
|
|
t.Fatal("invalid event type")
|
|
|
|
}
|
|
|
|
value := ev0.Value()
|
|
|
|
if value.Address != "" {
|
|
|
|
t.Fatal("invalid Address")
|
|
|
|
}
|
|
|
|
if len(value.Data) != 0 {
|
|
|
|
t.Fatal("invalid Data")
|
|
|
|
}
|
|
|
|
if value.Duration <= 0 {
|
|
|
|
t.Fatal("expected nonzero duration")
|
|
|
|
}
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
if value.Err != "unknown_failure: mocked error" {
|
2022-06-01 23:15:47 +02:00
|
|
|
t.Fatal("unexpected value.Err", value.Err)
|
|
|
|
}
|
|
|
|
if value.NumBytes != 0 {
|
|
|
|
t.Fatal("expected NumBytes")
|
|
|
|
}
|
|
|
|
if value.Time.IsZero() {
|
|
|
|
t.Fatal("expected nonzero Time")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
|
|
|
expected := []byte{1, 2, 3, 4}
|
|
|
|
saver := &Saver{}
|
|
|
|
expectedAddr := &mocks.Addr{
|
|
|
|
MockString: func() string {
|
|
|
|
return "8.8.8.8:443"
|
|
|
|
},
|
|
|
|
MockNetwork: func() string {
|
|
|
|
return "udp"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
conn := &quicPacketConnWrapper{
|
|
|
|
UDPLikeConn: &mocks.UDPLikeConn{
|
|
|
|
MockReadFrom: func(p []byte) (int, net.Addr, error) {
|
|
|
|
copy(p, expected)
|
|
|
|
return len(expected), expectedAddr, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
saver: saver,
|
|
|
|
}
|
|
|
|
buf := make([]byte, 1<<17)
|
|
|
|
count, addr, err := conn.ReadFrom(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if count != 4 {
|
|
|
|
t.Fatal("invalid count")
|
|
|
|
}
|
|
|
|
if addr != expectedAddr {
|
|
|
|
t.Fatal("invalid addr")
|
|
|
|
}
|
|
|
|
events := saver.Read()
|
|
|
|
if len(events) != 1 {
|
|
|
|
t.Fatal("invalid number of events")
|
|
|
|
}
|
|
|
|
ev0 := events[0]
|
|
|
|
if _, good := ev0.(*EventReadFromOperation); !good {
|
|
|
|
t.Fatal("invalid event type")
|
|
|
|
}
|
|
|
|
value := ev0.Value()
|
|
|
|
if value.Address != "8.8.8.8:443" {
|
|
|
|
t.Fatal("invalid Address")
|
|
|
|
}
|
|
|
|
if len(value.Data) != 4 {
|
|
|
|
t.Fatal("invalid Data")
|
|
|
|
}
|
|
|
|
if value.Duration <= 0 {
|
|
|
|
t.Fatal("expected nonzero duration")
|
|
|
|
}
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
if value.Err.IsNotNil() {
|
2022-06-01 23:15:47 +02:00
|
|
|
t.Fatal("unexpected value.Err", value.Err)
|
|
|
|
}
|
|
|
|
if value.NumBytes != 4 {
|
|
|
|
t.Fatal("expected NumBytes")
|
|
|
|
}
|
|
|
|
if value.Time.IsZero() {
|
|
|
|
t.Fatal("expected nonzero Time")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("WriteTo", func(t *testing.T) {
|
|
|
|
|
|
|
|
t.Run("on failure", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
saver := &Saver{}
|
|
|
|
conn := &quicPacketConnWrapper{
|
|
|
|
UDPLikeConn: &mocks.UDPLikeConn{
|
|
|
|
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
|
|
|
return 0, expected
|
|
|
|
},
|
|
|
|
},
|
|
|
|
saver: saver,
|
|
|
|
}
|
|
|
|
destAddr := &mocks.Addr{
|
|
|
|
MockString: func() string {
|
|
|
|
return "8.8.8.8:443"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf := make([]byte, 7)
|
|
|
|
count, err := conn.WriteTo(buf, destAddr)
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("unexpected err", err)
|
|
|
|
}
|
|
|
|
if count != 0 {
|
|
|
|
t.Fatal("invalid count")
|
|
|
|
}
|
|
|
|
events := saver.Read()
|
|
|
|
if len(events) != 1 {
|
|
|
|
t.Fatal("invalid number of events")
|
|
|
|
}
|
|
|
|
ev0 := events[0]
|
|
|
|
if _, good := ev0.(*EventWriteToOperation); !good {
|
|
|
|
t.Fatal("invalid event type")
|
|
|
|
}
|
|
|
|
value := ev0.Value()
|
|
|
|
if value.Address != "8.8.8.8:443" {
|
|
|
|
t.Fatal("invalid Address")
|
|
|
|
}
|
|
|
|
if len(value.Data) != 0 {
|
|
|
|
t.Fatal("invalid Data")
|
|
|
|
}
|
|
|
|
if value.Duration <= 0 {
|
|
|
|
t.Fatal("expected nonzero duration")
|
|
|
|
}
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
if value.Err != "unknown_failure: mocked error" {
|
2022-06-01 23:15:47 +02:00
|
|
|
t.Fatal("unexpected value.Err", value.Err)
|
|
|
|
}
|
|
|
|
if value.NumBytes != 0 {
|
|
|
|
t.Fatal("expected NumBytes")
|
|
|
|
}
|
|
|
|
if value.Time.IsZero() {
|
|
|
|
t.Fatal("expected nonzero Time")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("on success", func(t *testing.T) {
|
|
|
|
saver := &Saver{}
|
|
|
|
conn := &quicPacketConnWrapper{
|
|
|
|
UDPLikeConn: &mocks.UDPLikeConn{
|
|
|
|
MockWriteTo: func(p []byte, addr net.Addr) (int, error) {
|
|
|
|
return 1, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
saver: saver,
|
|
|
|
}
|
|
|
|
destAddr := &mocks.Addr{
|
|
|
|
MockString: func() string {
|
|
|
|
return "8.8.8.8:443"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf := make([]byte, 7)
|
|
|
|
count, err := conn.WriteTo(buf, destAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if count != 1 {
|
|
|
|
t.Fatal("invalid count")
|
|
|
|
}
|
|
|
|
events := saver.Read()
|
|
|
|
if len(events) != 1 {
|
|
|
|
t.Fatal("invalid number of events")
|
|
|
|
}
|
|
|
|
ev0 := events[0]
|
|
|
|
if _, good := ev0.(*EventWriteToOperation); !good {
|
|
|
|
t.Fatal("invalid event type")
|
|
|
|
}
|
|
|
|
value := ev0.Value()
|
|
|
|
if value.Address != "8.8.8.8:443" {
|
|
|
|
t.Fatal("invalid Address")
|
|
|
|
}
|
|
|
|
if len(value.Data) != 1 {
|
|
|
|
t.Fatal("invalid Data")
|
|
|
|
}
|
|
|
|
if value.Duration <= 0 {
|
|
|
|
t.Fatal("expected nonzero duration")
|
|
|
|
}
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
if value.Err.IsNotNil() {
|
2022-06-01 23:15:47 +02:00
|
|
|
t.Fatal("unexpected value.Err", value.Err)
|
|
|
|
}
|
|
|
|
if value.NumBytes != 1 {
|
|
|
|
t.Fatal("expected NumBytes")
|
|
|
|
}
|
|
|
|
if value.Time.IsZero() {
|
|
|
|
t.Fatal("expected nonzero Time")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2022-05-31 21:53:01 +02:00
|
|
|
}
|