2022-05-31 21:53:01 +02:00
|
|
|
package tracex
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2022-05-31 21:53:01 +02:00
|
|
|
"errors"
|
|
|
|
"net"
|
2021-02-02 12:05:47 +01:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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-06-25 17:04:24 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-11-12 14:43:28 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/quictesting"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type MockDialer struct {
|
2022-01-07 18:33:37 +01:00
|
|
|
Dialer model.QUICDialer
|
2022-05-06 12:24:03 +02:00
|
|
|
Sess quic.EarlyConnection
|
2021-02-02 12:05:47 +01:00
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d MockDialer) DialContext(ctx context.Context, network, host string,
|
2022-05-06 12:24:03 +02:00
|
|
|
tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
if d.Dialer != nil {
|
|
|
|
return d.Dialer.DialContext(ctx, network, host, tlsCfg, cfg)
|
|
|
|
}
|
|
|
|
return d.Sess, d.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandshakeSaverSuccess(t *testing.T) {
|
2021-06-14 16:59:24 +02:00
|
|
|
nextprotos := []string{"h3"}
|
2021-11-12 14:43:28 +01:00
|
|
|
servername := quictesting.Domain
|
2021-02-02 12:05:47 +01:00
|
|
|
tlsConf := &tls.Config{
|
|
|
|
NextProtos: nextprotos,
|
|
|
|
ServerName: servername,
|
|
|
|
}
|
2022-05-31 21:53:01 +02:00
|
|
|
saver := &Saver{}
|
2022-06-01 07:44:54 +02:00
|
|
|
dlr := saver.WrapQUICDialer(&netxlite.QUICDialerQUICGo{
|
|
|
|
QUICListener: &netxlite.QUICListenerStdlib{},
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
sess, err := dlr.DialContext(context.Background(), "udp",
|
2021-11-12 14:43:28 +01:00
|
|
|
quictesting.Endpoint("443"), tlsConf, &quic.Config{})
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error", err)
|
|
|
|
}
|
|
|
|
if sess == nil {
|
|
|
|
t.Fatal("unexpected nil sess")
|
|
|
|
}
|
|
|
|
ev := saver.Read()
|
|
|
|
if len(ev) != 2 {
|
|
|
|
t.Fatal("unexpected number of events")
|
|
|
|
}
|
|
|
|
if ev[0].Name != "quic_handshake_start" {
|
|
|
|
t.Fatal("unexpected Name")
|
|
|
|
}
|
2021-11-12 14:43:28 +01:00
|
|
|
if ev[0].TLSServerName != quictesting.Domain {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected TLSServerName")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ev[0].TLSNextProtos, nextprotos) {
|
|
|
|
t.Fatal("unexpected TLSNextProtos")
|
|
|
|
}
|
|
|
|
if ev[0].Time.After(time.Now()) {
|
|
|
|
t.Fatal("unexpected Time")
|
|
|
|
}
|
|
|
|
if ev[1].Duration <= 0 {
|
|
|
|
t.Fatal("unexpected Duration")
|
|
|
|
}
|
|
|
|
if ev[1].Err != nil {
|
|
|
|
t.Fatal("unexpected Err", ev[1].Err)
|
|
|
|
}
|
|
|
|
if ev[1].Name != "quic_handshake_done" {
|
|
|
|
t.Fatal("unexpected Name")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ev[1].TLSNextProtos, nextprotos) {
|
|
|
|
t.Fatal("unexpected TLSNextProtos")
|
|
|
|
}
|
2021-11-12 14:43:28 +01:00
|
|
|
if ev[1].TLSServerName != quictesting.Domain {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected TLSServerName")
|
|
|
|
}
|
|
|
|
if ev[1].Time.Before(ev[0].Time) {
|
|
|
|
t.Fatal("unexpected Time")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandshakeSaverHostNameError(t *testing.T) {
|
2021-06-14 16:59:24 +02:00
|
|
|
nextprotos := []string{"h3"}
|
2021-06-23 11:32:53 +02:00
|
|
|
servername := "example.com"
|
2021-02-02 12:05:47 +01:00
|
|
|
tlsConf := &tls.Config{
|
|
|
|
NextProtos: nextprotos,
|
|
|
|
ServerName: servername,
|
|
|
|
}
|
2022-05-31 21:53:01 +02:00
|
|
|
saver := &Saver{}
|
2022-06-01 07:44:54 +02:00
|
|
|
dlr := saver.WrapQUICDialer(&netxlite.QUICDialerQUICGo{
|
|
|
|
QUICListener: &netxlite.QUICListenerStdlib{},
|
|
|
|
})
|
2021-02-02 12:05:47 +01:00
|
|
|
sess, err := dlr.DialContext(context.Background(), "udp",
|
2021-11-12 14:43:28 +01:00
|
|
|
quictesting.Endpoint("443"), tlsConf, &quic.Config{})
|
2021-02-02 12:05:47 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error here")
|
|
|
|
}
|
|
|
|
if sess != nil {
|
|
|
|
t.Fatal("expected nil sess here")
|
|
|
|
}
|
|
|
|
for _, ev := range saver.Read() {
|
|
|
|
if ev.Name != "quic_handshake_done" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ev.NoTLSVerify == true {
|
|
|
|
t.Fatal("expected NoTLSVerify to be false")
|
|
|
|
}
|
2021-11-12 14:43:28 +01:00
|
|
|
if !strings.HasSuffix(ev.Err.Error(), "tls: handshake failure") {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected error", ev.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 21:53:01 +02:00
|
|
|
|
|
|
|
func TestQUICListenerSaverCannotListen(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
2022-06-01 07:44:54 +02:00
|
|
|
saver := &Saver{}
|
|
|
|
qls := saver.WrapQUICListener(&mocks.QUICListener{
|
|
|
|
MockListen: func(addr *net.UDPAddr) (model.UDPLikeConn, error) {
|
|
|
|
return nil, expected
|
2022-05-31 21:53:01 +02:00
|
|
|
},
|
2022-06-01 07:44:54 +02:00
|
|
|
})
|
2022-05-31 21:53:01 +02:00
|
|
|
pconn, err := qls.Listen(&net.UDPAddr{
|
|
|
|
IP: []byte{},
|
|
|
|
Port: 8080,
|
|
|
|
Zone: "",
|
|
|
|
})
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("unexpected error", err)
|
|
|
|
}
|
|
|
|
if pconn != nil {
|
|
|
|
t.Fatal("expected nil pconn here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemDialerSuccessWithReadWrite(t *testing.T) {
|
|
|
|
// This is the most common use case for collecting reads, writes
|
|
|
|
tlsConf := &tls.Config{
|
|
|
|
NextProtos: []string{"h3"},
|
|
|
|
ServerName: quictesting.Domain,
|
|
|
|
}
|
|
|
|
saver := &Saver{}
|
|
|
|
systemdialer := &netxlite.QUICDialerQUICGo{
|
2022-06-01 07:44:54 +02:00
|
|
|
QUICListener: saver.WrapQUICListener(&netxlite.QUICListenerStdlib{}),
|
2022-05-31 21:53:01 +02:00
|
|
|
}
|
|
|
|
_, err := systemdialer.DialContext(context.Background(), "udp",
|
|
|
|
quictesting.Endpoint("443"), tlsConf, &quic.Config{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ev := saver.Read()
|
|
|
|
if len(ev) < 2 {
|
|
|
|
t.Fatal("unexpected number of events")
|
|
|
|
}
|
|
|
|
last := len(ev) - 1
|
|
|
|
for idx := 1; idx < last; idx++ {
|
|
|
|
if ev[idx].Data == nil {
|
|
|
|
t.Fatal("unexpected Data")
|
|
|
|
}
|
|
|
|
if ev[idx].Duration <= 0 {
|
|
|
|
t.Fatal("unexpected Duration")
|
|
|
|
}
|
|
|
|
if ev[idx].Err != nil {
|
|
|
|
t.Fatal("unexpected Err")
|
|
|
|
}
|
|
|
|
if ev[idx].NumBytes <= 0 {
|
|
|
|
t.Fatal("unexpected NumBytes")
|
|
|
|
}
|
|
|
|
switch ev[idx].Name {
|
|
|
|
case netxlite.ReadFromOperation, netxlite.WriteToOperation:
|
|
|
|
default:
|
|
|
|
t.Fatal("unexpected Name")
|
|
|
|
}
|
|
|
|
if ev[idx].Time.Before(ev[idx-1].Time) {
|
|
|
|
t.Fatal("unexpected Time", ev[idx].Time, ev[idx-1].Time)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|