232 lines
5.3 KiB
Go
232 lines
5.3 KiB
Go
package main
|
||
|
||
import (
|
||
"bufio"
|
||
"fmt"
|
||
"github.com/TransX/log"
|
||
"io"
|
||
"net"
|
||
"os"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
func serverBin(t *testing.T) {
|
||
file, _ := os.Open("test/hex.bin")
|
||
reader := bufio.NewReader(file)
|
||
listener, err := net.Listen("tcp4", "127.0.0.1:1244")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
var nCount byte
|
||
nCount = 0
|
||
for {
|
||
conn, err := listener.Accept()
|
||
log.Info("Test Server Incoming %s", conn.RemoteAddr().String())
|
||
if err != nil {
|
||
log.Error(err.Error())
|
||
}
|
||
|
||
bytes := make([]byte, 1024*32)
|
||
/////<- 当发送端的cache大于4097时,把这些注释掉
|
||
_bytes := make([]byte, 1024*32)
|
||
add := 0
|
||
n := 0
|
||
for {
|
||
/////->
|
||
n, err = io.ReadAtLeast(conn, _bytes, 4097)
|
||
if err != nil {
|
||
log.Error("Test Server read err %s", err.Error())
|
||
break
|
||
}
|
||
log.Info("Test Server read per time %d", n)
|
||
////<-
|
||
copy(bytes[add:add+n], _bytes[:n])
|
||
add += n
|
||
log.Info("add %d from %s", add, conn.RemoteAddr().String())
|
||
if add == 4097 {
|
||
n = 4097
|
||
break
|
||
}
|
||
}
|
||
////->
|
||
log.Info("Test Server read %d bytes from %s", n, conn.RemoteAddr().String())
|
||
if bytes[0] != nCount {
|
||
log.Error("package sequence not right. it's %d. should be %d", bytes[0], nCount)
|
||
}
|
||
|
||
log.Info("Test Server receive with count %d", nCount)
|
||
nCount++
|
||
if n == 0 {
|
||
log.Error("read n=0")
|
||
os.Exit(-2)
|
||
}
|
||
binBytes := make([]byte, n-1)
|
||
pos, _ := file.Seek(0, 1)
|
||
log.Info("Test Server file pos %d", pos)
|
||
nBinByte, err := reader.Read(binBytes)
|
||
if err != nil {
|
||
log.Error("Test Server read bin %s", err.Error())
|
||
break
|
||
}
|
||
for {
|
||
if nBinByte != n-1 {
|
||
log.Info("Test Server read file %d, it should be %d", nBinByte, n-1)
|
||
file.Seek(pos, 0)
|
||
nBinByte, err = reader.Read(binBytes)
|
||
if err != nil {
|
||
log.Error("Test Server read bin %s", err.Error())
|
||
break
|
||
}
|
||
continue
|
||
}
|
||
break
|
||
}
|
||
|
||
log.Info("Test Server read file %d, it should be %d", nBinByte, n-1)
|
||
for i := 0; i < nBinByte; i++ {
|
||
if binBytes[i] != bytes[i+1] {
|
||
log.Error("Test Server read not consistent at %d. read:%c receive:%c", i, binBytes[i], bytes[i+1])
|
||
}
|
||
|
||
}
|
||
////
|
||
f, _ := os.Create("server.bin")
|
||
f.Write(bytes[1 : nBinByte+1])
|
||
f.Close()
|
||
////
|
||
log.Info("Test Server. All Matches.")
|
||
|
||
// log.Info("Test Server Receive %s", string(bytes[:n]))
|
||
_, err = conn.Write([]byte("OK"))
|
||
log.Info("Test Server write")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
conn.Close()
|
||
log.Info("Test Server closed")
|
||
}
|
||
}
|
||
|
||
func serverText(t *testing.T) {
|
||
file, _ := os.Open("test/cipher.txt")
|
||
reader := bufio.NewReader(file)
|
||
listener, err := net.Listen("tcp4", "127.0.0.1:1244")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
for {
|
||
conn, err := listener.Accept()
|
||
log.Info("Test Server Incoming %s", conn.RemoteAddr().String())
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
bytes := make([]byte, 1024*32)
|
||
n, err := conn.Read(bytes)
|
||
line, err := reader.ReadString('\n')
|
||
if err != nil {
|
||
log.Error("client read %", err)
|
||
break
|
||
}
|
||
if string(bytes[:n]) != line {
|
||
t.Fatalf("cipher failed %s", string(bytes[:n]))
|
||
t.Fatalf("%s %s", len(bytes[:n]), len([]byte(line)))
|
||
for i := 0; i < n; i++ {
|
||
log.Error("%d", bytes[i]-[]byte(line)[i])
|
||
}
|
||
|
||
}
|
||
log.Info("Test Server Receive %s", string(bytes[:n]))
|
||
_, err = conn.Write([]byte("OK"))
|
||
log.Info("Test Server write")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
conn.Close()
|
||
log.Info("Test Server closed")
|
||
}
|
||
}
|
||
|
||
func clientBin(t *testing.T) {
|
||
file, _ := os.Open("test/hex.bin")
|
||
reader := bufio.NewReader(file)
|
||
var nCount byte
|
||
nCount = 0
|
||
for {
|
||
conn, err := net.Dial("tcp4", "127.0.0.1:1200")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
binBytes := make([]byte, 1024*4)
|
||
nBinBytes, err := reader.Read(binBytes)
|
||
if err != nil {
|
||
log.Error("client read %", err)
|
||
|
||
}
|
||
toBinWrite := make([]byte, len(binBytes)+1)
|
||
copy(toBinWrite[1:], binBytes[:nBinBytes])
|
||
toBinWrite[0] = nCount
|
||
n, _ := conn.Write(toBinWrite)
|
||
if n != nBinBytes+1 {
|
||
log.Error("client not write enough bytes")
|
||
}
|
||
// log.Info("Test Client write %s", string(binBytes[:n]))
|
||
log.Info("Test Client write %d bytes with count %d", n, nCount)
|
||
nCount++
|
||
bytes := make([]byte, 1024*32)
|
||
n, err = conn.Read(bytes)
|
||
log.Info("Test Client read")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
log.Info("Test Client Receive %s", bytes[:n])
|
||
fmt.Println("Test Client Receive ", string(bytes[:n]))
|
||
time.Sleep(time.Second * 2)
|
||
conn.Close()
|
||
log.Info("Test Client closed")
|
||
|
||
}
|
||
}
|
||
|
||
func clientText(t *testing.T) {
|
||
file, _ := os.Open("test/cipher.txt")
|
||
reader := bufio.NewReader(file)
|
||
for {
|
||
conn, err := net.Dial("tcp4", "127.0.0.1:1200")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
line, err := reader.ReadString('\n')
|
||
if err != nil {
|
||
log.Error("client read %", err)
|
||
|
||
}
|
||
bytesLine := []byte(line)
|
||
n, _ := conn.Write(bytesLine)
|
||
log.Info("Test Client write %s", string(bytesLine[:n]))
|
||
log.Info("Test Client write")
|
||
bytes := make([]byte, 1024*32)
|
||
n, err = conn.Read(bytes)
|
||
log.Info("Test Client read")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
log.Info("Test Client Receive %s", bytes[:n])
|
||
time.Sleep(time.Second * 2)
|
||
conn.Close()
|
||
log.Info("Test Client closed")
|
||
|
||
}
|
||
}
|
||
|
||
func TestTunnel(t *testing.T) {
|
||
log.LogTo("stdout", "ERROR")
|
||
log.Info("Test Start testing.")
|
||
go serverBin(t)
|
||
go clientBin(t)
|
||
trans1 := NewTransTCP()
|
||
go trans1.Start("1200", "127.0.0.1", "1201", "client")
|
||
trans2 := NewTransTCP()
|
||
trans2.Start("1201", "127.0.0.1", "1244", "server")
|
||
}
|