2021-02-02 12:05:47 +01:00
|
|
|
package quicdialer_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/quicdialer"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
2022-01-07 18:33:37 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
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,
|
|
|
|
}
|
|
|
|
saver := &trace.Saver{}
|
|
|
|
dlr := quicdialer.HandshakeSaver{
|
2022-01-07 18:33:37 +01:00
|
|
|
QUICDialer: &netxlite.QUICDialerQUICGo{
|
2021-06-25 17:04:24 +02:00
|
|
|
QUICListener: &netxlite.QUICListenerStdlib{},
|
2021-06-25 16:20:08 +02:00
|
|
|
},
|
|
|
|
Saver: saver,
|
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,
|
|
|
|
}
|
|
|
|
saver := &trace.Saver{}
|
|
|
|
dlr := quicdialer.HandshakeSaver{
|
2022-01-07 18:33:37 +01:00
|
|
|
QUICDialer: &netxlite.QUICDialerQUICGo{
|
2021-06-25 17:04:24 +02:00
|
|
|
QUICListener: &netxlite.QUICListenerStdlib{},
|
2021-06-25 16:20:08 +02:00
|
|
|
},
|
|
|
|
Saver: saver,
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|