Skip to content

Commit

Permalink
Modify testcases to avoid port confilict
Browse files Browse the repository at this point in the history
Signed-off-by: bill fort <fxbao@hotmail.com>
  • Loading branch information
billfort authored and yilunzhang committed Jul 7, 2023
1 parent 7c00d4f commit d21b671
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 95 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/nknorg/nconnect
go 1.20

require (
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5
github.com/eycorsican/go-tun2socks v1.16.11
github.com/gin-contrib/gzip v0.0.3
github.com/gin-gonic/gin v1.9.0
Expand All @@ -16,6 +17,7 @@ require (
github.com/nknorg/nkngomobile v0.0.0-20220615081414-671ad1afdfa9
github.com/nknorg/tuna v0.0.0-20230405223335-eb60c60c5953
github.com/shadowsocks/go-shadowsocks2 v0.1.2
github.com/stretchr/testify v1.8.1
github.com/txthinking/brook v0.0.0-20230418095906-76ced63f1803
github.com/txthinking/socks5 v0.0.0-20230307062227-0e1677eca4ba
golang.org/x/net v0.8.0
Expand All @@ -27,6 +29,7 @@ require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/bytedance/sonic v1.8.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gaukas/godicttls v0.0.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand Down Expand Up @@ -62,6 +65,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/phuslu/iploc v1.0.20230201 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quic-go/qtls-go1-18 v0.2.0 // indirect
github.com/quic-go/qtls-go1-19 v0.2.0 // indirect
github.com/quic-go/qtls-go1-20 v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHG
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5 h1:BjkPE3785EwPhhyuFkbINB+2a1xATwk8SNDWnJiD41g=
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5/go.mod h1:jtAfVaU/2cu1+wdSRPWE2c1N2qeAA3K4RH9pYgqwets=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
Expand Down
1 change: 1 addition & 0 deletions tests/client.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"tunaMinFee": "0.00001",
"tunaFeeRatio": 0.1,
"tunaGeoDBPath": ".",
"tunaDisableMeasureBandwidth": false,
"tunaMeasureStoragePath": ".",
"adminIdentifier": "nConnect",
"webRootPath": "web/dist",
Expand Down
1 change: 0 additions & 1 deletion tests/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tests

var port int = 1080
var proxyAddr string = "127.0.0.1:1080"

const (
numMsgs = 10
Expand Down
8 changes: 4 additions & 4 deletions tests/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package tests

import (
"fmt"
"time"

"github.com/txthinking/brook"
)

func dnsQuery() {
func dnsQuery() error {
proxyAddr := fmt.Sprintf("127.0.0.1:%v", port)
for i := 1; i <= numMsgs; i++ {
err := brook.Socks5Test(proxyAddr, "", "", "http3.ooo", "137.184.237.95", "8.8.8.8:53")
if err != nil {
fmt.Printf("TestDNSProxy try %v err: %v\n", i, err)
time.Sleep(time.Duration(i) * time.Second)
break
return err
}
}
return nil
}
58 changes: 54 additions & 4 deletions tests/main_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
package tests

import (
"flag"
"fmt"
"log"
"os"
"testing"
"time"

"github.com/nknorg/tuna/types"
)

var remoteTuna = flag.Bool("remoteTuna", false, "use remote tuna node")
var tun = flag.Bool("tun", false, "use tun device")

func TestMain(m *testing.M) {
go StartTcpServer()
go StartWebServer()
go StartUdpServer()
flag.Parse()
if *remoteTuna {
fmt.Println("We are using remote tuna node")
} else {
fmt.Println("Using local tuna node. If want to use remote tuna node, please run: go test -v -remoteTuna .")
}

go func() {
err := StartTcpServer()
if err != nil {
log.Fatalf("StartTcpServer err %v", err)
return
}
}()
go func() {
err := StartWebServer()
if err != nil {
log.Fatalf("StartWebServer err %v", err)
return
}
}()
go func() {
err := StartUdpServer()
if err != nil {
log.Fatalf("StartUdpServer err %v", err)
return
}
}()

var tunaNode *types.Node
var err error
if !(*remoteTuna) {
tunaNode, err = getTunaNode()
if err != nil {
log.Fatalf("getTunaNode err %v", err)
return
}
}

go func() {
err := startNconnect("server.json", true, true, false, tunaNode)
if err != nil {
log.Fatalf("start nconnect server err: %v", err)
return
}
}()

go StartNconnectServerWithTunaNode(true, true, false)
time.Sleep(15 * time.Second)

exitVal := m.Run()
Expand Down
26 changes: 16 additions & 10 deletions tests/proxy_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
package tests

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/require"
)

// go test -v -run=TestProxy
func TestProxy(t *testing.T) {
tuna, udp, tun := true, true, false
go func() {
err := startNconnect("client.json", tuna, udp, tun, nil)
if err != nil {
fmt.Printf("start nconnect client err: %v\n", err)
return
}
require.NoError(t, err)
}()
time.Sleep(15 * time.Second)

dnsQuery()
time.Sleep(5 * time.Second)

err := waitSSAndTunaReady()
require.NoError(t, err)

err = dnsQuery()
require.NoError(t, err)
for _, server := range servers {
StartWebClient("http://" + server + httpPort + "/httpEcho")
StartTCPClient(server + tcpPort)
StartUDPClient(server + udpPort)
err := StartWebClient("http://" + server + httpPort + "/httpEcho")
require.NoError(t, err)
err = StartTCPClient(server + tcpPort)
require.NoError(t, err)
err = StartUDPClient(server + udpPort)
require.NoError(t, err)
}
}
83 changes: 64 additions & 19 deletions tests/pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"encoding/json"
"fmt"
"log"
"net"
"os"
"time"

"github.com/cakturk/go-netstat/netstat"
"github.com/nknorg/nconnect"
"github.com/nknorg/nconnect/config"
nkn "github.com/nknorg/nkn-sdk-go"
Expand Down Expand Up @@ -40,9 +43,11 @@ func startNconnect(configFile string, tuna, udp, tun bool, n *types.Node) error
}

if opts.Client {
proxyAddr = fmt.Sprintf("127.0.0.1:%v", port)
opts.LocalSocksAddr = proxyAddr
port++
port, err := getFreePort(port)
if err != nil {
return err
}
opts.LocalSocksAddr = fmt.Sprintf("127.0.0.1:%v", port)
}
fmt.Printf("opts.RemoteAdminAddr: %+v\n", opts.RemoteAdminAddr)

Expand All @@ -56,22 +61,6 @@ func startNconnect(configFile string, tuna, udp, tun bool, n *types.Node) error
return err
}

func StartNconnectServerWithTunaNode(tuna, udp, tun bool) {
tunaNode, err := getTunaNode()
if err != nil {
fmt.Printf("getTunaNode err %v\n", err)
return
}

go func() {
err := startNconnect("server.json", tuna, udp, tun, tunaNode)
if err != nil {
fmt.Printf("start nconnect server err: %v\n", err)
return
}
}()
}

func getTunaNode() (*types.Node, error) {
tunaSeed, _ := hex.DecodeString(seedHex)
acc, err := nkn.NewAccount(tunaSeed)
Expand Down Expand Up @@ -135,3 +124,59 @@ type Person struct {
Name string
Age int
}

func getFreePort(port int) (int, error) {
for i := 0; i < 100; i++ {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%v", port))
if err != nil {
return 0, err
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
port++
continue
}

defer l.Close()

return l.Addr().(*net.TCPAddr).Port, nil
}
return 0, fmt.Errorf("can't find free port")
}

func waitSSAndTunaReady() error {
ssIsReady := false
for i := 0; i < 100; i++ {
tabs, err := netstat.TCPSocks(func(s *netstat.SockTabEntry) bool {
return s.State == netstat.Listen && s.LocalAddr.Port == uint16(port)
})
if err != nil {
fmt.Printf("waitSSAndTunaReady err: %v\n", err)
}
if len(tabs) >= 1 {
ssIsReady = true
break
}
time.Sleep(2 * time.Second)
}

if !ssIsReady {
return fmt.Errorf("ss is not ready after 200 seconds, give up")
}

for i := 0; i < 100; i++ {
tabs, err := netstat.TCPSocks(func(s *netstat.SockTabEntry) bool {
return s.State == netstat.Established && s.RemoteAddr.Port == 30020
})
if err != nil {
fmt.Printf("waitSSAndTunaReady err: %v\n", err)
}
time.Sleep(2 * time.Second)
if len(tabs) >= 1 {
return nil
}
}

return fmt.Errorf("tuna is not connected after 200 seconds, give up")
}
1 change: 1 addition & 0 deletions tests/server.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"tunaFeeRatio": 0.1,
"tunaServiceName": "reverse",
"tunaGeoDBPath": ".",
"tunaDisableMeasureBandwidth": false,
"tunaMeasureStoragePath": ".",
"adminIdentifier": "nConnect",
"webRootPath": "web/dist",
Expand Down
Loading

0 comments on commit d21b671

Please sign in to comment.