more tests

This commit is contained in:
Page Fault
2020-07-01 12:59:51 +00:00
parent ae8e583fef
commit f777ea7967
10 changed files with 182 additions and 6 deletions
+5 -1
View File
@@ -39,11 +39,15 @@ func (r *Redirector) worker() {
select {
case redirection := <-r.redirectionChan:
handle := func(redirection *Redirection) {
if redirection.InboundConn == nil || reflect.ValueOf(redirection.InboundConn).IsNil() {
log.Error("nil inbound conn")
return
}
defer redirection.InboundConn.Close()
if redirection.Dial == nil {
redirection.Dial = defaultDial
}
if reflect.ValueOf(redirection.RedirectTo).IsNil() {
if redirection.RedirectTo == nil || reflect.ValueOf(redirection.RedirectTo).IsNil() {
log.Error("nil redirection addr")
return
}
+48
View File
@@ -0,0 +1,48 @@
package redirector
import (
"bytes"
"context"
"net"
"testing"
"github.com/p4gefau1t/trojan-go/common"
"github.com/p4gefau1t/trojan-go/test/util"
"github.com/p4gefau1t/trojan-go/tunnel"
)
func TestRedirector(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
redir := NewRedirector(ctx)
redir.Redirect(&Redirection{
Dial: nil,
RedirectTo: nil,
InboundConn: nil,
})
var fakeAddr net.Addr
var fakeConn net.Conn
redir.Redirect(&Redirection{
Dial: nil,
RedirectTo: fakeAddr,
InboundConn: fakeConn,
})
l, err := net.Listen("tcp", "127.0.0.1:0")
common.Must(err)
conn1, err := net.Dial("tcp", l.Addr().String())
common.Must(err)
conn2, err := l.Accept()
common.Must(err)
redir.Redirect(&Redirection{
Dial: nil,
RedirectTo: tunnel.NewAddressFromHostPort("tcp", util.EchoAddr, util.EchoPort),
InboundConn: conn2,
})
payload := util.GeneratePayload(1024)
conn1.Write(payload)
buf := make([]byte, 1024)
conn2.Read(buf)
if !bytes.Equal(buf, payload) {
t.Fail()
}
cancel()
}
-1
View File
@@ -22,7 +22,6 @@ func (c *Conn) Metadata() *tunnel.Metadata {
}
// PacketConn receive packet info from the packet dispatcher
// TODO implement net.PacketConn
type PacketConn struct {
net.PacketConn
metadata *tunnel.Metadata
+1
View File
@@ -106,4 +106,5 @@ func TestSocks(t *testing.T) {
fmt.Println(m)
packet.Close()
client.Close()
}
+1 -1
View File
@@ -190,7 +190,7 @@ func (s *Server) packetDispatchLoop() {
payload: payload[:length],
}:
default:
log.Warn("socks udp WrappedPacketConn full")
log.Warn("socks udp queue full")
}
}
}
+22 -1
View File
@@ -16,6 +16,7 @@ import (
"github.com/p4gefau1t/trojan-go/tunnel"
"github.com/p4gefau1t/trojan-go/tunnel/adapter"
"github.com/p4gefau1t/trojan-go/tunnel/socks"
"github.com/txthinking/socks5"
"golang.org/x/net/proxy"
)
@@ -117,7 +118,27 @@ func TestSocks(t *testing.T) {
if bytes.Equal(recvBuf, payload) {
t.Fail()
}
packet.Close()
udpConn.Close()
c, _ := socks5.NewClient(fmt.Sprintf("127.0.0.1:%d", port), "", "", 0, 0, 0)
conn, err := c.Dial("udp", util.EchoAddr)
common.Must(err)
payload = util.GeneratePayload(4096)
recvBuf = make([]byte, 4096)
conn.Write(payload)
newPacket, err := s.AcceptPacket(nil)
common.Must(err)
_, m, err = newPacket.ReadWithMetadata(recvBuf)
common.Must(err)
if m.String() != util.EchoAddr || !bytes.Equal(recvBuf, payload) {
t.Fail()
}
s.Close()
}
+1 -1
View File
@@ -23,7 +23,7 @@ type Client struct {
func (c *Client) Close() error {
c.cancel()
if c.cmd != nil {
if c.cmd != nil && c.cmd.Process != nil {
c.cmd.Process.Kill()
}
return nil
+1 -1
View File
@@ -28,7 +28,7 @@ type Server struct {
func (s *Server) Close() error {
s.cancel()
if s.cmd != nil {
if s.cmd != nil && s.cmd.Process != nil {
s.cmd.Process.Kill()
}
return s.tcpListener.Close()
+42
View File
@@ -54,3 +54,45 @@ func TestTransport(t *testing.T) {
s.Close()
c.Close()
}
func TestClientPlugin(t *testing.T) {
clientCfg := &Config{
LocalHost: "127.0.0.1",
LocalPort: common.PickPort("tcp", "127.0.0.1"),
RemoteHost: "127.0.0.1",
RemotePort: 12345,
TransportPlugin: TransportPluginConfig{
Enabled: true,
Type: "shadowsocks",
Command: "echo $SS_REMOTE_PORT",
PluginOption: "",
Arg: nil,
Env: nil,
},
}
ctx := config.WithConfig(context.Background(), Name, clientCfg)
c, err := NewClient(ctx, nil)
common.Must(err)
c.Close()
}
func TestServerPlugin(t *testing.T) {
cfg := &Config{
LocalHost: "127.0.0.1",
LocalPort: common.PickPort("tcp", "127.0.0.1"),
RemoteHost: "127.0.0.1",
RemotePort: 12345,
TransportPlugin: TransportPluginConfig{
Enabled: true,
Type: "shadowsocks",
Command: "echo $SS_REMOTE_PORT",
PluginOption: "",
Arg: nil,
Env: nil,
},
}
ctx := config.WithConfig(context.Background(), Name, cfg)
s, err := NewServer(ctx, nil)
common.Must(err)
s.Close()
}
+61
View File
@@ -2,6 +2,9 @@ package websocket
import (
"context"
"fmt"
"net"
"strings"
"sync"
"testing"
@@ -10,6 +13,7 @@ import (
"github.com/p4gefau1t/trojan-go/test/util"
"github.com/p4gefau1t/trojan-go/tunnel"
"github.com/p4gefau1t/trojan-go/tunnel/transport"
"golang.org/x/net/websocket"
)
func TestWebsocket(t *testing.T) {
@@ -53,4 +57,61 @@ func TestWebsocket(t *testing.T) {
if !util.CheckConn(conn1, conn2) {
t.Fail()
}
if strings.HasPrefix(conn1.RemoteAddr().String(), "ws") {
t.Fail()
}
if strings.HasPrefix(conn2.RemoteAddr().String(), "ws") {
t.Fail()
}
conn1.Close()
conn2.Close()
s.Close()
c.Close()
}
func TestRedirect(t *testing.T) {
cfg := &Config{
RemoteHost: "127.0.0.1",
Websocket: WebsocketConfig{
Enabled: true,
Hostname: "localhost",
Path: "/ws",
},
}
fmt.Sscanf(util.HTTPPort, "%d", &cfg.RemotePort)
ctx := config.WithConfig(context.Background(), Name, cfg)
port := common.PickPort("tcp", "127.0.0.1")
transportConfig := &transport.Config{
LocalHost: "127.0.0.1",
LocalPort: port,
}
ctx = config.WithConfig(ctx, transport.Name, transportConfig)
tcpServer, err := transport.NewServer(ctx, nil)
common.Must(err)
s, err := NewServer(ctx, tcpServer)
common.Must(err)
go func() {
_, err := s.AcceptConn(nil)
if err == nil {
t.Fail()
}
}()
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
common.Must(err)
url := "wss://localhost/wrong-path"
origin := "https://localhost"
wsConfig, err := websocket.NewConfig(url, origin)
common.Must(err)
_, err = websocket.NewClient(wsConfig, conn)
if err == nil {
t.Fail()
}
conn.Close()
s.Close()
}