64bffbd941
Before finishing the ongoing refactoring and leaving whatever is left of netx in tree, I would like to restructure it so that we'll have an easy time next time we need to modify it. Currently, every functionality lives into the `netx.go` file and we have a support file called `httptransport.go`. I would like to reorganize by topic, instead. This would allow future me to more easily perform topic-specific changes. While there, improve `netx`'s documentation and duplicate some of this documentation inside `internal/README.md` to provide pointers to previous documentation, historical context, and some help to understand the logic architecture of network extensions (aka `netx`). Part of https://github.com/ooni/probe-cli/pull/396
75 lines
2.0 KiB
Go
75 lines
2.0 KiB
Go
package netx
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"testing"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/filtering"
|
|
"github.com/ooni/probe-cli/v3/internal/tracex"
|
|
)
|
|
|
|
func TestNewTLSDialer(t *testing.T) {
|
|
t.Run("we always have error wrapping", func(t *testing.T) {
|
|
server := filtering.NewTLSServer(filtering.TLSActionReset)
|
|
defer server.Close()
|
|
tdx := NewTLSDialer(Config{})
|
|
conn, err := tdx.DialTLSContext(context.Background(), "tcp", server.Endpoint())
|
|
if err == nil || err.Error() != netxlite.FailureConnectionReset {
|
|
t.Fatal("unexpected err", err)
|
|
}
|
|
if conn != nil {
|
|
t.Fatal("expected nil conn")
|
|
}
|
|
})
|
|
|
|
t.Run("we can collect measurements", func(t *testing.T) {
|
|
server := filtering.NewTLSServer(filtering.TLSActionReset)
|
|
defer server.Close()
|
|
saver := &tracex.Saver{}
|
|
tdx := NewTLSDialer(Config{
|
|
Saver: saver,
|
|
})
|
|
conn, err := tdx.DialTLSContext(context.Background(), "tcp", server.Endpoint())
|
|
if err == nil || err.Error() != netxlite.FailureConnectionReset {
|
|
t.Fatal("unexpected err", err)
|
|
}
|
|
if conn != nil {
|
|
t.Fatal("expected nil conn")
|
|
}
|
|
if len(saver.Read()) <= 0 {
|
|
t.Fatal("did not read any event")
|
|
}
|
|
})
|
|
|
|
t.Run("we can skip TLS verification", func(t *testing.T) {
|
|
server := filtering.NewTLSServer(filtering.TLSActionBlockText)
|
|
defer server.Close()
|
|
tdx := NewTLSDialer(Config{TLSConfig: &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
}})
|
|
conn, err := tdx.DialTLSContext(context.Background(), "tcp", server.Endpoint())
|
|
if err != nil {
|
|
t.Fatal(err.(*netxlite.ErrWrapper).WrappedErr)
|
|
}
|
|
conn.Close()
|
|
})
|
|
|
|
t.Run("we can set the cert pool", func(t *testing.T) {
|
|
server := filtering.NewTLSServer(filtering.TLSActionBlockText)
|
|
defer server.Close()
|
|
tdx := NewTLSDialer(Config{
|
|
TLSConfig: &tls.Config{
|
|
RootCAs: server.CertPool(),
|
|
ServerName: "dns.google",
|
|
},
|
|
})
|
|
conn, err := tdx.DialTLSContext(context.Background(), "tcp", server.Endpoint())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
conn.Close()
|
|
})
|
|
}
|