2021-06-09 07:11:31 +02:00
|
|
|
package dialer
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2021-06-08 23:59:30 +02:00
|
|
|
"net"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2021-06-23 16:06:02 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxmocks"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProxyDialerDialContextNoProxyURL(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
2021-06-09 09:42:31 +02:00
|
|
|
d := &proxyDialer{
|
2021-06-23 16:06:02 +02:00
|
|
|
Dialer: netxmocks.Dialer{
|
2021-06-08 23:59:30 +02:00
|
|
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
},
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
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) {
|
2021-06-09 09:42:31 +02:00
|
|
|
d := &proxyDialer{
|
2021-02-02 12:05:47 +01:00
|
|
|
ProxyURL: &url.URL{Scheme: "antani"},
|
|
|
|
}
|
|
|
|
conn, err := d.DialContext(context.Background(), "tcp", "www.google.com:443")
|
2021-06-09 07:11:31 +02:00
|
|
|
if !errors.Is(err, ErrProxyUnsupportedScheme) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if conn != nil {
|
|
|
|
t.Fatal("conn is not nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyDialerDialContextWithEOF(t *testing.T) {
|
2021-06-09 07:11:31 +02:00
|
|
|
const expect = "10.0.0.1:9050"
|
2021-06-09 09:42:31 +02:00
|
|
|
d := &proxyDialer{
|
2021-06-23 16:06:02 +02:00
|
|
|
Dialer: netxmocks.Dialer{
|
2021-06-08 23:59:30 +02:00
|
|
|
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
2021-06-09 07:11:31 +02:00
|
|
|
if address != expect {
|
|
|
|
return nil, errors.New("unexpected address")
|
|
|
|
}
|
2021-06-08 23:59:30 +02:00
|
|
|
return nil, io.EOF
|
|
|
|
},
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
2021-06-09 07:11:31 +02:00
|
|
|
ProxyURL: &url.URL{Scheme: "socks5", Host: expect},
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 07:11:31 +02:00
|
|
|
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)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|