transx/main_test.go

231 lines
5.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"bufio"
"fmt"
"github.com/TransX/log"
"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 {
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)
////<-
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"))
log.Info("Test Server write OK from %s to %s", conn.LocalAddr().String(), conn.RemoteAddr().String())
if err != nil {
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 {
log.Error("Test Client %s", err.Error())
}
binBytes := make([]byte, 1024*4)
nBinBytes, err := reader.Read(binBytes)
if err != nil {
if err == io.EOF {
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)
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 {
log.Error("Test Client %s", err.Error())
}
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)
}
// 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("applog/test.log", "DEBUG")
log.Info("Test Start testing.")
go serverBin(t)
go clientBin(t)
trans1 := protocol.NewTransTCP()
go trans1.Start("1200", "127.0.0.1", "1201", "client")
trans2 := protocol.NewTransTCP()
trans2.Start("1201", "127.0.0.1", "1244", "server")
}