From ea5bb44b0a290786b6ca0ca1a15177aa227a5943 Mon Sep 17 00:00:00 2001 From: Qian Wang Date: Thu, 8 Aug 2019 15:08:16 +0100 Subject: [PATCH] Add and edit some tests --- internal/multiplex/bufferedPipe_test.go | 16 ++++++++++++- internal/multiplex/frameSorter_test.go | 3 ++- internal/multiplex/obfs_test.go | 11 +++++++++ internal/multiplex/stream_test.go | 2 +- internal/multiplex/switchboard_test.go | 30 +++++++++++++++++++++++++ internal/util/util_test.go | 26 +++++++++++++++++++++ 6 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 internal/multiplex/switchboard_test.go create mode 100644 internal/util/util_test.go diff --git a/internal/multiplex/bufferedPipe_test.go b/internal/multiplex/bufferedPipe_test.go index 307a7da..be2bc82 100644 --- a/internal/multiplex/bufferedPipe_test.go +++ b/internal/multiplex/bufferedPipe_test.go @@ -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) } diff --git a/internal/multiplex/frameSorter_test.go b/internal/multiplex/frameSorter_test.go index 0524caa..8671090 100644 --- a/internal/multiplex/frameSorter_test.go +++ b/internal/multiplex/frameSorter_test.go @@ -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)) diff --git a/internal/multiplex/obfs_test.go b/internal/multiplex/obfs_test.go index 621ad69..c9833ae 100644 --- a/internal/multiplex/obfs_test.go +++ b/internal/multiplex/obfs_test.go @@ -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)) } diff --git a/internal/multiplex/stream_test.go b/internal/multiplex/stream_test.go index 730fe06..d7737d2 100644 --- a/internal/multiplex/stream_test.go +++ b/internal/multiplex/stream_test.go @@ -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 { diff --git a/internal/multiplex/switchboard_test.go b/internal/multiplex/switchboard_test.go new file mode 100644 index 0000000..468c191 --- /dev/null +++ b/internal/multiplex/switchboard_test.go @@ -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)) + } +} diff --git a/internal/util/util_test.go b/internal/util/util_test.go new file mode 100644 index 0000000..7affb6b --- /dev/null +++ b/internal/util/util_test.go @@ -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)) + } +}