transx/main_test.go

231 lines
5.6 KiB
Go
Raw Permalink Normal View History

package main
import (
"bufio"
2016-01-14 21:58:41 +08:00
"fmt"
"github.com/TransX/log"
2015-11-20 14:19:10 +08:00
"github.com/TransX/protocol"
"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 {
2016-01-14 21:58:41 +08:00
log.Error("Test Server %s", err.Error())
}
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)
////<-
2016-01-10 13:44:02 +08:00
if add+n >= len(bytes) {
log.Panic("serverBin reseive out of bound n:%d,add+n :%d", n, add+n)
break
}
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])
}
}
log.Info("Test Server. All Matches.")
// log.Info("Test Server Receive %s", string(bytes[:n]))
_, err = conn.Write([]byte("OK"))
2016-01-14 21:58:41 +08:00
log.Info("Test Server write OK from %s to %s", conn.LocalAddr().String(), conn.RemoteAddr().String())
if err != nil {
2016-01-14 21:58:41 +08:00
log.Error("Test Server %s ", err.Error())
}
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 {
2016-01-14 21:58:41 +08:00
log.Error("Test Client %s", err.Error())
}
binBytes := make([]byte, 1024*4)
nBinBytes, err := reader.Read(binBytes)
if err != nil {
2016-01-14 21:58:41 +08:00
if err == io.EOF {
2016-01-10 13:44:02 +08:00
fmt.Println("Test Finished.")
break
}
log.Error("client read %s", err.Error())
}
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 %d bytes with count %d", n, nCount)
nCount++
bytes := make([]byte, 1024*32)
n, err = conn.Read(bytes)
2016-01-14 21:58:41 +08:00
log.Info("Test Client read %d bytes (local add %s) msg:%s from %s", n, conn.LocalAddr().String(), bytes[:n], conn.RemoteAddr().String())
if err != nil {
2016-01-14 21:58:41 +08:00
log.Error("Test Client %s", err.Error())
}
2016-01-10 13:44:02 +08:00
time.Sleep(time.Second * 0)
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)
}
2016-01-10 13:44:02 +08:00
// 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) {
2015-11-20 14:19:10 +08:00
log.LogTo("applog/test.log", "DEBUG")
log.Info("Test Start testing.")
go serverBin(t)
go clientBin(t)
2015-11-20 14:19:10 +08:00
trans1 := protocol.NewTransTCP()
go trans1.Start("1200", "127.0.0.1", "1201", "client")
2015-11-20 14:19:10 +08:00
trans2 := protocol.NewTransTCP()
trans2.Start("1201", "127.0.0.1", "1244", "server")
}