Add and edit some tests

This commit is contained in:
Qian Wang 2019-08-08 15:08:16 +01:00
parent 82fe177c24
commit ea5bb44b0a
6 changed files with 85 additions and 3 deletions

View File

@ -17,6 +17,7 @@ func TestPipeRW(t *testing.T) {
"expecting", len(b),
"got", n,
)
return
}
if err != nil {
t.Error(
@ -24,6 +25,7 @@ func TestPipeRW(t *testing.T) {
"expecting", "nil error",
"got", err,
)
return
}
b2 := make([]byte, len(b))
@ -34,6 +36,7 @@ func TestPipeRW(t *testing.T) {
"expecting", len(b),
"got", n,
)
return
}
if err != nil {
t.Error(
@ -41,6 +44,7 @@ func TestPipeRW(t *testing.T) {
"expecting", "nil error",
"got", err,
)
return
}
if !bytes.Equal(b, b2) {
t.Error(
@ -67,6 +71,7 @@ func TestReadBlock(t *testing.T) {
"expecting", len(b),
"got", n,
)
return
}
if err != nil {
t.Error(
@ -74,6 +79,7 @@ func TestReadBlock(t *testing.T) {
"expecting", "nil error",
"got", err,
)
return
}
if !bytes.Equal(b, b2) {
t.Error(
@ -81,6 +87,7 @@ func TestReadBlock(t *testing.T) {
"expecting", b,
"got", b2,
)
return
}
}
@ -96,6 +103,7 @@ func TestPartialRead(t *testing.T) {
"expecting", len(b1),
"got", n,
)
return
}
if err != nil {
t.Error(
@ -103,6 +111,7 @@ func TestPartialRead(t *testing.T) {
"expecting", "nil error",
"got", err,
)
return
}
if b1[0] != b[0] {
t.Error(
@ -126,6 +135,7 @@ func TestPartialRead(t *testing.T) {
"expecting", "nil error",
"got", err,
)
return
}
if !bytes.Equal(b[1:], b2) {
t.Error(
@ -133,6 +143,7 @@ func TestPartialRead(t *testing.T) {
"expecting", b[1:],
"got", b2,
)
return
}
}
@ -156,6 +167,7 @@ func TestReadAfterClose(t *testing.T) {
"expecting", "nil error",
"got", err,
)
return
}
if !bytes.Equal(b, b2) {
t.Error(
@ -163,11 +175,12 @@ func TestReadAfterClose(t *testing.T) {
"expecting", b,
"got", b2,
)
return
}
}
func BenchmarkBufferedPipe_RW(b *testing.B) {
const PAYLOAD_LEN = 1300
const PAYLOAD_LEN = 1000
testData := make([]byte, PAYLOAD_LEN)
rand.Read(testData)
@ -187,6 +200,7 @@ func BenchmarkBufferedPipe_RW(b *testing.B) {
"For", "pipe write",
"got", err,
)
return
}
b.SetBytes(PAYLOAD_LEN)
}

View File

@ -37,7 +37,7 @@ func TestRecvNewFrame(t *testing.T) {
fs.writeNewFrame(frame)
}
time.Sleep(100 * time.Microsecond)
time.Sleep(100 * time.Millisecond)
var sortedResult []uint64
for x := 0; x < len(set); x++ {
@ -45,6 +45,7 @@ func TestRecvNewFrame(t *testing.T) {
n, err := sortedBuf.Read(oct)
if n != 8 || err != nil {
ct.Error("failed to read from sorted Buf", n, err)
return
}
//log.Print(p)
sortedResult = append(sortedResult, binary.BigEndian.Uint64(oct))

View File

@ -23,15 +23,18 @@ func TestGenerateObfs(t *testing.T) {
i, err := obfuscator.Obfs(testFrame, obfsBuf)
if err != nil {
ct.Error("failed to obfs ", err)
return
}
resultFrame, err := obfuscator.Deobfs(obfsBuf[:i])
if err != nil {
ct.Error("failed to deobfs ", err)
return
}
if !bytes.Equal(testFrame.Payload, resultFrame.Payload) || testFrame.StreamID != resultFrame.StreamID {
ct.Error("expecting", testFrame,
"got", resultFrame)
return
}
}
@ -97,6 +100,7 @@ func BenchmarkObfs(b *testing.B) {
n, err := obfs(testFrame, obfsBuf)
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
}
@ -111,6 +115,7 @@ func BenchmarkObfs(b *testing.B) {
n, err := obfs(testFrame, obfsBuf)
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
}
@ -122,6 +127,7 @@ func BenchmarkObfs(b *testing.B) {
n, err := obfs(testFrame, obfsBuf)
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
}
@ -135,6 +141,7 @@ func BenchmarkObfs(b *testing.B) {
n, err := obfs(testFrame, obfsBuf)
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
}
@ -168,6 +175,7 @@ func BenchmarkDeobfs(b *testing.B) {
_, err := deobfs(obfsBuf[:n])
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
}
@ -185,6 +193,7 @@ func BenchmarkDeobfs(b *testing.B) {
_, err := deobfs(obfsBuf[:n])
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
}
@ -199,6 +208,7 @@ func BenchmarkDeobfs(b *testing.B) {
_, err := deobfs(obfsBuf[:n])
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
}
@ -215,6 +225,7 @@ func BenchmarkDeobfs(b *testing.B) {
_, err := deobfs(obfsBuf[:n])
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
}

View File

@ -76,7 +76,7 @@ func TestStream_Read(t *testing.T) {
}
ch := make(chan []byte)
l, _ := net.Listen("tcp", ":0")
l, _ := net.Listen("tcp", "127.0.0.1:0")
go func() {
conn, _ := net.Dial("tcp", l.Addr().String())
for {

View File

@ -0,0 +1,30 @@
package multiplex
import (
"github.com/cbeuw/Cloak/internal/util"
"math/rand"
"testing"
)
func BenchmarkSwitchboard_Send(b *testing.B) {
sesh := MakeSession(0, UNLIMITED_VALVE, nil, util.ReadTLS)
sb := makeSwitchboard(sesh, UNLIMITED_VALVE)
hole := newBlackHole()
sb.addConn(hole)
connId, err := sb.assignRandomConn()
if err != nil {
b.Error("failed to get a random conn", err)
return
}
data := make([]byte, 1000)
rand.Read(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
n, err := sb.send(data, &connId)
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
}
}

View File

@ -0,0 +1,26 @@
package util
import (
"io"
"io/ioutil"
"math/rand"
"testing"
)
func BenchmarkPipe(b *testing.B) {
reader := rand.New(rand.NewSource(42))
buf := make([]byte, 16380)
for i := 0; i < b.N; i++ {
n, err := io.ReadAtLeast(reader, buf, 1)
if err != nil {
b.Error(err)
return
}
n, err = ioutil.Discard.Write(buf[:n])
if err != nil {
b.Error(err)
return
}
b.SetBytes(int64(n))
}
}