Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
p4gefau1t committed Jul 12, 2020
1 parent 225347f commit fccba27
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
4 changes: 2 additions & 2 deletions common/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func HumanFriendlyTraffic(bytes uint64) string {
func PickPort(network string, host string) int {
switch network {
case "tcp":
for retry := 0; retry < 50; retry++ {
for retry := 0; retry < 16; retry++ {
l, err := net.Listen("tcp", host+":0")
if err != nil {
continue
Expand All @@ -41,7 +41,7 @@ func PickPort(network string, host string) int {
return int(p)
}
case "udp":
for retry := 0; retry < 50; retry++ {
for retry := 0; retry < 16; retry++ {
conn, err := net.ListenPacket("udp", host+":0")
if err != nil {
continue
Expand Down
37 changes: 24 additions & 13 deletions redirector/redirector_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package redirector

import (
"bytes"
"context"
"io"
"fmt"
"net"
"net/http"
"strings"
"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) {
Expand All @@ -27,26 +27,37 @@ func TestRedirector(t *testing.T) {
RedirectTo: fakeAddr,
InboundConn: fakeConn,
})
redir.Redirect(&Redirection{
Dial: nil,
RedirectTo: nil,
InboundConn: fakeConn,
})
redir.Redirect(&Redirection{
Dial: nil,
RedirectTo: fakeAddr,
InboundConn: nil,
})
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)
redirAddr, err := net.ResolveTCPAddr("tcp", util.HTTPAddr)
common.Must(err)
redir.Redirect(&Redirection{
Dial: nil,
RedirectTo: tunnel.NewAddressFromHostPort("tcp", "127.0.0.1", util.EchoPort),
RedirectTo: redirAddr,
InboundConn: conn2,
})
payload := util.GeneratePayload(128)
common.Must2(conn1.Write(payload))
buf := make([]byte, 128)
n, err := io.ReadFull(conn2, buf)
if n != 128 || err != nil {
t.Fatal(n, err)
}
if !bytes.Equal(buf, payload) {
t.Fatal("diff: ", payload, "\n", buf)
req, err := http.NewRequest("GET", "http://localhost/", nil)
common.Must(err)
req.Write(conn1)
buf := make([]byte, 1024)
conn1.Read(buf)
fmt.Println(string(buf))
if !strings.HasPrefix(string(buf), "HTTP/1.1 200 OK") {
t.Fail()
}
cancel()
conn1.Close()
Expand Down
7 changes: 5 additions & 2 deletions url/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (

func TestUrl_Handle(t *testing.T) {
urlCases := []string{
"trojan-go://[email protected]/?type=ws&host=baidu.com&path=%2fwspath&encryption=ss%3Baes-256-gcm%3Bfuckgfw",
"trojan-go://[email protected]",
"trojan-go://[email protected]/?type=ws&host=baidu.com&path=%2fwspath",
"trojan-go://[email protected]/?encryption=ss%3Baes-256-gcm%3Bfuckgfw",
"trojan-go://[email protected]/?type=ws&host=baidu.com&path=%2fwspath&encryption=ss%3Baes-256-gcm%3Bfuckgfw",
}
optionCases := []string{
"mux=true;listen=127.0.0.1:0",
Expand All @@ -23,14 +24,16 @@ func TestUrl_Handle(t *testing.T) {
url: &s,
option: &option,
}
u.Name()
u.Priority()
errChan := make(chan error, 1)
go func() {
errChan <- u.Handle()
}()
select {
case err := <-errChan:
t.Fatal(err)
case <-time.After(time.Second * 2):
case <-time.After(time.Second * 1):
}
}
}
Expand Down

0 comments on commit fccba27

Please sign in to comment.