2021-09-08 00:59:48 +02:00
|
|
|
package netxlite_test
|
|
|
|
|
|
|
|
import (
|
2021-09-08 11:39:27 +02:00
|
|
|
"context"
|
2021-09-08 00:59:48 +02:00
|
|
|
"crypto/tls"
|
2021-09-08 11:39:27 +02:00
|
|
|
"net"
|
2021-09-08 00:59:48 +02:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-09-08 11:39:27 +02:00
|
|
|
utls "gitlab.com/yawning/utls.git"
|
2021-09-08 00:59:48 +02:00
|
|
|
)
|
|
|
|
|
2021-09-08 22:48:10 +02:00
|
|
|
func TestResolver(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("works as intended", func(t *testing.T) {
|
|
|
|
// TODO(bassosimone): this is actually an integration
|
|
|
|
// test but how to test this case?
|
|
|
|
r := netxlite.NewResolverSystem(log.Log)
|
|
|
|
defer r.CloseIdleConnections()
|
|
|
|
addrs, err := r.LookupHost(context.Background(), "dns.google.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if addrs == nil {
|
|
|
|
t.Fatal("expected non-nil result here")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-08 00:59:48 +02:00
|
|
|
func TestHTTPTransport(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("works as intended", func(t *testing.T) {
|
|
|
|
d := netxlite.NewDialerWithResolver(log.Log, netxlite.NewResolverSystem(log.Log))
|
|
|
|
td := netxlite.NewTLSDialer(d, netxlite.NewTLSHandshakerStdlib(log.Log))
|
|
|
|
txp := netxlite.NewHTTPTransport(log.Log, d, td)
|
|
|
|
client := &http.Client{Transport: txp}
|
|
|
|
resp, err := client.Get("https://www.google.com/robots.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
client.CloseIdleConnections()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTP3Transport(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skip test in short mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("works as intended", func(t *testing.T) {
|
|
|
|
d := netxlite.NewQUICDialerWithResolver(
|
|
|
|
netxlite.NewQUICListener(),
|
|
|
|
log.Log,
|
|
|
|
netxlite.NewResolverSystem(log.Log),
|
|
|
|
)
|
2021-09-08 20:49:01 +02:00
|
|
|
txp := netxlite.NewHTTP3Transport(log.Log, d, &tls.Config{})
|
2021-09-08 00:59:48 +02:00
|
|
|
client := &http.Client{Transport: txp}
|
|
|
|
resp, err := client.Get("https://www.google.com/robots.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
txp.CloseIdleConnections()
|
|
|
|
})
|
|
|
|
}
|
2021-09-08 11:39:27 +02:00
|
|
|
|
|
|
|
func TestUTLSHandshaker(t *testing.T) {
|
|
|
|
t.Run("with chrome fingerprint", func(t *testing.T) {
|
|
|
|
h := netxlite.NewTLSHandshakerUTLS(log.Log, &utls.HelloChrome_Auto)
|
|
|
|
cfg := &tls.Config{ServerName: "google.com"}
|
|
|
|
conn, err := net.Dial("tcp", "google.com:443")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error", err)
|
|
|
|
}
|
|
|
|
conn, _, err = h.Handshake(context.Background(), conn, cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error", err)
|
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
t.Fatal("nil connection")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|