mirror of https://github.com/ginuerzh/gost
6 changed files with 529 additions and 26 deletions
@ -0,0 +1,223 @@ |
|||||
|
package gost |
||||
|
|
||||
|
import ( |
||||
|
"crypto/rand" |
||||
|
"crypto/tls" |
||||
|
"net/http/httptest" |
||||
|
"net/url" |
||||
|
"testing" |
||||
|
) |
||||
|
|
||||
|
func autoHTTPProxyRoundtrip(targetURL string, data []byte, clientInfo *url.Userinfo, serverInfo []*url.Userinfo) error { |
||||
|
ln, err := TCPListener("") |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
client := &Client{ |
||||
|
Connector: HTTPConnector(clientInfo), |
||||
|
Transporter: TCPTransporter(), |
||||
|
} |
||||
|
|
||||
|
server := &Server{ |
||||
|
Listener: ln, |
||||
|
Handler: AutoHandler( |
||||
|
UsersHandlerOption(serverInfo...), |
||||
|
), |
||||
|
} |
||||
|
|
||||
|
go server.Run() |
||||
|
defer server.Close() |
||||
|
|
||||
|
return proxyRoundtrip(client, server, targetURL, data) |
||||
|
} |
||||
|
|
||||
|
func TestAutoHTTPProxy(t *testing.T) { |
||||
|
httpSrv := httptest.NewServer(httpTestHandler) |
||||
|
defer httpSrv.Close() |
||||
|
|
||||
|
sendData := make([]byte, 128) |
||||
|
rand.Read(sendData) |
||||
|
|
||||
|
for i, tc := range httpProxyTests { |
||||
|
err := autoHTTPProxyRoundtrip(httpSrv.URL, sendData, tc.cliUser, tc.srvUsers) |
||||
|
if err == nil { |
||||
|
if tc.errStr != "" { |
||||
|
t.Errorf("#%d should failed with error %s", i, tc.errStr) |
||||
|
} |
||||
|
} else { |
||||
|
if tc.errStr == "" { |
||||
|
t.Errorf("#%d got error %v", i, err) |
||||
|
} |
||||
|
if err.Error() != tc.errStr { |
||||
|
t.Errorf("#%d got error %v, want %v", i, err, tc.errStr) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func autoSocks5ProxyRoundtrip(targetURL string, data []byte, clientInfo *url.Userinfo, serverInfo []*url.Userinfo) error { |
||||
|
ln, err := TCPListener("") |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
client := &Client{ |
||||
|
Connector: SOCKS5Connector(clientInfo), |
||||
|
Transporter: TCPTransporter(), |
||||
|
} |
||||
|
|
||||
|
server := &Server{ |
||||
|
Handler: AutoHandler(UsersHandlerOption(serverInfo...)), |
||||
|
Listener: ln, |
||||
|
} |
||||
|
|
||||
|
go server.Run() |
||||
|
defer server.Close() |
||||
|
|
||||
|
return proxyRoundtrip(client, server, targetURL, data) |
||||
|
} |
||||
|
|
||||
|
func TestAutoSOCKS5Proxy(t *testing.T) { |
||||
|
cert, err := GenCertificate() |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
DefaultTLSConfig = &tls.Config{ |
||||
|
Certificates: []tls.Certificate{cert}, |
||||
|
} |
||||
|
|
||||
|
httpSrv := httptest.NewServer(httpTestHandler) |
||||
|
defer httpSrv.Close() |
||||
|
|
||||
|
sendData := make([]byte, 128) |
||||
|
rand.Read(sendData) |
||||
|
|
||||
|
for i, tc := range socks5ProxyTests { |
||||
|
err := autoSocks5ProxyRoundtrip(httpSrv.URL, sendData, |
||||
|
tc.cliUser, |
||||
|
tc.srvUsers, |
||||
|
) |
||||
|
if err == nil { |
||||
|
if !tc.pass { |
||||
|
t.Errorf("#%d should failed", i) |
||||
|
} |
||||
|
} else { |
||||
|
// t.Logf("#%d %v", i, err)
|
||||
|
if tc.pass { |
||||
|
t.Errorf("#%d got error: %v", i, err) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func autoSOCKS4ProxyRoundtrip(targetURL string, data []byte) error { |
||||
|
ln, err := TCPListener("") |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
client := &Client{ |
||||
|
Connector: SOCKS4Connector(), |
||||
|
Transporter: TCPTransporter(), |
||||
|
} |
||||
|
|
||||
|
server := &Server{ |
||||
|
Listener: ln, |
||||
|
Handler: AutoHandler(), |
||||
|
} |
||||
|
go server.Run() |
||||
|
defer server.Close() |
||||
|
|
||||
|
return proxyRoundtrip(client, server, targetURL, data) |
||||
|
} |
||||
|
|
||||
|
func TestAutoSOCKS4Proxy(t *testing.T) { |
||||
|
httpSrv := httptest.NewServer(httpTestHandler) |
||||
|
defer httpSrv.Close() |
||||
|
|
||||
|
sendData := make([]byte, 128) |
||||
|
rand.Read(sendData) |
||||
|
|
||||
|
err := autoSOCKS4ProxyRoundtrip(httpSrv.URL, sendData) |
||||
|
// t.Logf("#%d %v", i, err)
|
||||
|
if err != nil { |
||||
|
t.Errorf("got error: %v", err) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func autoSocks4aProxyRoundtrip(targetURL string, data []byte) error { |
||||
|
ln, err := TCPListener("") |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
client := &Client{ |
||||
|
Connector: SOCKS4AConnector(), |
||||
|
Transporter: TCPTransporter(), |
||||
|
} |
||||
|
|
||||
|
server := &Server{ |
||||
|
Listener: ln, |
||||
|
Handler: AutoHandler(), |
||||
|
} |
||||
|
|
||||
|
go server.Run() |
||||
|
defer server.Close() |
||||
|
|
||||
|
return proxyRoundtrip(client, server, targetURL, data) |
||||
|
} |
||||
|
|
||||
|
func TestAutoSOCKS4AProxy(t *testing.T) { |
||||
|
httpSrv := httptest.NewServer(httpTestHandler) |
||||
|
defer httpSrv.Close() |
||||
|
|
||||
|
sendData := make([]byte, 128) |
||||
|
rand.Read(sendData) |
||||
|
|
||||
|
err := autoSocks4aProxyRoundtrip(httpSrv.URL, sendData) |
||||
|
// t.Logf("#%d %v", i, err)
|
||||
|
if err != nil { |
||||
|
t.Errorf("got error: %v", err) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func autoSSProxyRoundtrip(targetURL string, data []byte, clientInfo *url.Userinfo, serverInfo *url.Userinfo) error { |
||||
|
ln, err := TCPListener("") |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
client := &Client{ |
||||
|
Connector: ShadowConnector(clientInfo), |
||||
|
Transporter: TCPTransporter(), |
||||
|
} |
||||
|
|
||||
|
server := &Server{ |
||||
|
Handler: AutoHandler(UsersHandlerOption(serverInfo)), |
||||
|
Listener: ln, |
||||
|
} |
||||
|
|
||||
|
go server.Run() |
||||
|
defer server.Close() |
||||
|
|
||||
|
return proxyRoundtrip(client, server, targetURL, data) |
||||
|
} |
||||
|
|
||||
|
func TestAutoSSProxy(t *testing.T) { |
||||
|
httpSrv := httptest.NewServer(httpTestHandler) |
||||
|
defer httpSrv.Close() |
||||
|
|
||||
|
sendData := make([]byte, 128) |
||||
|
rand.Read(sendData) |
||||
|
|
||||
|
for i, tc := range ssTests { |
||||
|
err := autoSSProxyRoundtrip(httpSrv.URL, sendData, |
||||
|
tc.clientCipher, |
||||
|
tc.serverCipher, |
||||
|
) |
||||
|
if err == nil { |
||||
|
t.Errorf("#%d should failed", i) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,121 @@ |
|||||
|
package gost |
||||
|
|
||||
|
import ( |
||||
|
"bytes" |
||||
|
"io" |
||||
|
"net" |
||||
|
"testing" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
var hostsLookupTests = []struct { |
||||
|
hosts []Host |
||||
|
host string |
||||
|
ip net.IP |
||||
|
}{ |
||||
|
{nil, "", nil}, |
||||
|
{nil, "example.com", nil}, |
||||
|
{[]Host{}, "", nil}, |
||||
|
{[]Host{}, "example.com", nil}, |
||||
|
{[]Host{NewHost(nil, "")}, "", nil}, |
||||
|
{[]Host{NewHost(nil, "example.com")}, "example.com", nil}, |
||||
|
{[]Host{NewHost(net.IPv4(192, 168, 1, 1), "")}, "", nil}, |
||||
|
{[]Host{NewHost(net.IPv4(192, 168, 1, 1), "example.com")}, "example.com", net.IPv4(192, 168, 1, 1)}, |
||||
|
{[]Host{NewHost(net.IPv4(192, 168, 1, 1), "example.com")}, "example", nil}, |
||||
|
{[]Host{NewHost(net.IPv4(192, 168, 1, 1), "example.com", "example", "examples")}, "example", net.IPv4(192, 168, 1, 1)}, |
||||
|
{[]Host{NewHost(net.IPv4(192, 168, 1, 1), "example.com", "example", "examples")}, "examples", net.IPv4(192, 168, 1, 1)}, |
||||
|
} |
||||
|
|
||||
|
func TestHostsLookup(t *testing.T) { |
||||
|
for i, tc := range hostsLookupTests { |
||||
|
hosts := NewHosts(tc.hosts...) |
||||
|
ip := hosts.Lookup(tc.host) |
||||
|
if !ip.Equal(tc.ip) { |
||||
|
t.Errorf("#%d test failed: lookup should be %s, got %s", i, tc.ip, ip) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
var HostsReloadTests = []struct { |
||||
|
r io.Reader |
||||
|
period time.Duration |
||||
|
host string |
||||
|
ip net.IP |
||||
|
stopped bool |
||||
|
}{ |
||||
|
{ |
||||
|
r: nil, |
||||
|
period: 0, |
||||
|
host: "", |
||||
|
ip: nil, |
||||
|
}, |
||||
|
{ |
||||
|
r: bytes.NewBufferString(""), |
||||
|
period: 0, |
||||
|
host: "example.com", |
||||
|
ip: nil, |
||||
|
}, |
||||
|
{ |
||||
|
r: bytes.NewBufferString("reload 10s"), |
||||
|
period: 10 * time.Second, |
||||
|
host: "example.com", |
||||
|
ip: nil, |
||||
|
}, |
||||
|
{ |
||||
|
r: bytes.NewBufferString("reload 10s\n192.168.1.1"), |
||||
|
period: 10 * time.Second, |
||||
|
host: "", |
||||
|
ip: nil, |
||||
|
}, |
||||
|
{ |
||||
|
r: bytes.NewBufferString("#reload 10s\n192.168.1.1 example.com"), |
||||
|
period: 0, |
||||
|
host: "example.com", |
||||
|
ip: net.IPv4(192, 168, 1, 1), |
||||
|
}, |
||||
|
{ |
||||
|
r: bytes.NewBufferString("#reload 10s\n#192.168.1.1 example.com"), |
||||
|
period: 0, |
||||
|
host: "example.com", |
||||
|
ip: nil, |
||||
|
stopped: true, |
||||
|
}, |
||||
|
{ |
||||
|
r: bytes.NewBufferString("#reload 10s\n192.168.1.1 example.com example examples"), |
||||
|
period: 0, |
||||
|
host: "example", |
||||
|
ip: net.IPv4(192, 168, 1, 1), |
||||
|
stopped: true, |
||||
|
}, |
||||
|
{ |
||||
|
r: bytes.NewBufferString("#reload 10s\n192.168.1.1 example.com example examples"), |
||||
|
period: 0, |
||||
|
host: "examples", |
||||
|
ip: net.IPv4(192, 168, 1, 1), |
||||
|
stopped: true, |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
func TestHostsReload(t *testing.T) { |
||||
|
for i, tc := range HostsReloadTests { |
||||
|
hosts := NewHosts() |
||||
|
if err := hosts.Reload(tc.r); err != nil { |
||||
|
t.Error(err) |
||||
|
} |
||||
|
if hosts.Period() != tc.period { |
||||
|
t.Errorf("#%d test failed: period value should be %v, got %v", |
||||
|
i, tc.period, hosts.Period()) |
||||
|
} |
||||
|
ip := hosts.Lookup(tc.host) |
||||
|
if !ip.Equal(tc.ip) { |
||||
|
t.Errorf("#%d test failed: lookup should be %s, got %s", i, tc.ip, ip) |
||||
|
} |
||||
|
if tc.stopped { |
||||
|
hosts.Stop() |
||||
|
} |
||||
|
if hosts.Stopped() != tc.stopped { |
||||
|
t.Errorf("#%d test failed: stopped value should be %v, got %v", |
||||
|
i, tc.stopped, hosts.Stopped()) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue