ooni-probe-cli/internal/engine/netx/dialer/proxy_test.go
Simone Basso 06ee0e55a9
refactor(netx/dialer): hide implementation complexity (#372)
* refactor(netx/dialer): hide implementation complexity

This follows the blueprint of `module.Config` and `nodule.New`
described at https://github.com/ooni/probe/issues/1591.

* fix: ndt7 bug where we were not using the right resolver

* fix(legacy/netx): clarify irrelevant implementation change

* fix: improve comments

* fix(hhfm): do not use dialer.New b/c it breaks it

Unclear to me why this is happening. Still, improve upon the
previous situation by adding a timeout.

It does not seem a priority to look into this issue now.
2021-06-09 09:42:31 +02:00

82 lines
1.9 KiB
Go

package dialer
import (
"context"
"errors"
"io"
"net"
"net/url"
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/netx/mockablex"
)
func TestProxyDialerDialContextNoProxyURL(t *testing.T) {
expected := errors.New("mocked error")
d := &proxyDialer{
Dialer: mockablex.Dialer{
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
return nil, expected
},
},
}
conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443")
if !errors.Is(err, expected) {
t.Fatal(err)
}
if conn != nil {
t.Fatal("conn is not nil")
}
}
func TestProxyDialerDialContextInvalidScheme(t *testing.T) {
d := &proxyDialer{
ProxyURL: &url.URL{Scheme: "antani"},
}
conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443")
if !errors.Is(err, ErrProxyUnsupportedScheme) {
t.Fatal("not the error we expected")
}
if conn != nil {
t.Fatal("conn is not nil")
}
}
func TestProxyDialerDialContextWithEOF(t *testing.T) {
const expect = "10.0.0.1:9050"
d := &proxyDialer{
Dialer: mockablex.Dialer{
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
if address != expect {
return nil, errors.New("unexpected address")
}
return nil, io.EOF
},
},
ProxyURL: &url.URL{Scheme: "socks5", Host: expect},
}
conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443")
if !errors.Is(err, io.EOF) {
t.Fatal("not the error we expected")
}
if conn != nil {
t.Fatal("conn is not nil")
}
}
func TestProxyDialWrapperPanics(t *testing.T) {
d := &proxyDialerWrapper{}
err := func() (rv error) {
defer func() {
if r := recover(); r != nil {
rv = r.(error)
}
}()
d.Dial("tcp", "10.0.0.1:1234")
return
}()
if err.Error() != "proxyDialerWrapper.Dial should not be called directly" {
t.Fatal("unexpected result", err)
}
}