Skip to content

Commit

Permalink
Use random testing to ensure correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson authored Oct 5, 2024
1 parent 07dd54d commit 5a2ac2c
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 39 deletions.
43 changes: 40 additions & 3 deletions protocol/ashe/engine_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ashe

import (
"encoding/binary"
"io"
"math/rand/v2"
"testing"

"github.com/mohanson/daze"
Expand All @@ -28,9 +30,44 @@ func TestProtocolAsheTCP(t *testing.T) {
cli := doa.Try(dazeClient.Dial(ctx, "tcp", EchoServerListenOn))
defer cli.Close()

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Try(io.ReadFull(cli, buf[:128]))
var (
bsz = max(4, int(rand.Uint32N(256)))
buf = make([]byte, bsz)
cnt int
rsz = int(rand.Uint32N(65536))
)

copy(buf[0:2], []byte{0x00, 0x00})
binary.BigEndian.PutUint16(buf[2:], uint16(rsz))
doa.Try(cli.Write(buf[:4]))
cnt = 0
for {
e := min(rand.IntN(bsz+1), rsz-cnt)
n := doa.Try(io.ReadFull(cli, buf[:e]))
for i := range n {
doa.Doa(buf[i] == 0x00)
}
cnt += n
if cnt == rsz {
break
}
}

copy(buf[0:2], []byte{0x01, 0x00})
binary.BigEndian.PutUint16(buf[2:], uint16(rsz))
doa.Try(cli.Write(buf[:4]))
for i := range bsz {
buf[i] = 0x00
}
cnt = 0
for {
e := min(rand.IntN(bsz+1), rsz-cnt)
n := doa.Try(cli.Write(buf[:e]))
cnt += n
if cnt == rsz {
break
}
}
}

func TestProtocolAsheTCPClientClose(t *testing.T) {
Expand Down
43 changes: 40 additions & 3 deletions protocol/baboon/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package baboon

import (
"bytes"
"encoding/binary"
"io"
"math/rand/v2"
"net/http"
"testing"

Expand Down Expand Up @@ -30,9 +32,44 @@ func TestProtocolBaboonTCP(t *testing.T) {
cli := doa.Try(dazeClient.Dial(ctx, "tcp", EchoServerListenOn))
defer cli.Close()

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Try(io.ReadFull(cli, buf[:128]))
var (
bsz = max(4, int(rand.Uint32N(256)))
buf = make([]byte, bsz)
cnt int
rsz = int(rand.Uint32N(65536))
)

copy(buf[0:2], []byte{0x00, 0x00})
binary.BigEndian.PutUint16(buf[2:], uint16(rsz))
doa.Try(cli.Write(buf[:4]))
cnt = 0
for {
e := min(rand.IntN(bsz+1), rsz-cnt)
n := doa.Try(io.ReadFull(cli, buf[:e]))
for i := range n {
doa.Doa(buf[i] == 0x00)
}
cnt += n
if cnt == rsz {
break
}
}

copy(buf[0:2], []byte{0x01, 0x00})
binary.BigEndian.PutUint16(buf[2:], uint16(rsz))
doa.Try(cli.Write(buf[:4]))
for i := range bsz {
buf[i] = 0x00
}
cnt = 0
for {
e := min(rand.IntN(bsz+1), rsz-cnt)
n := doa.Try(cli.Write(buf[:e]))
cnt += n
if cnt == rsz {
break
}
}
}

func TestProtocolBaboonTCPClientClose(t *testing.T) {
Expand Down
43 changes: 40 additions & 3 deletions protocol/czar/engine_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package czar

import (
"encoding/binary"
"io"
"math/rand/v2"
"testing"

"github.com/mohanson/daze"
Expand Down Expand Up @@ -29,9 +31,44 @@ func TestProtocolCzarTCP(t *testing.T) {
cli := doa.Try(dazeClient.Dial(ctx, "tcp", EchoServerListenOn))
defer cli.Close()

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Try(io.ReadFull(cli, buf[:128]))
var (
bsz = max(4, int(rand.Uint32N(256)))
buf = make([]byte, bsz)
cnt int
rsz = int(rand.Uint32N(65536))
)

copy(buf[0:2], []byte{0x00, 0x00})
binary.BigEndian.PutUint16(buf[2:], uint16(rsz))
doa.Try(cli.Write(buf[:4]))
cnt = 0
for {
e := min(rand.IntN(bsz+1), rsz-cnt)
n := doa.Try(io.ReadFull(cli, buf[:e]))
for i := range n {
doa.Doa(buf[i] == 0x00)
}
cnt += n
if cnt == rsz {
break
}
}

copy(buf[0:2], []byte{0x01, 0x00})
binary.BigEndian.PutUint16(buf[2:], uint16(rsz))
doa.Try(cli.Write(buf[:4]))
for i := range bsz {
buf[i] = 0x00
}
cnt = 0
for {
e := min(rand.IntN(bsz+1), rsz-cnt)
n := doa.Try(cli.Write(buf[:e]))
cnt += n
if cnt == rsz {
break
}
}
}

func TestProtocolCzarTCPClientClose(t *testing.T) {
Expand Down
95 changes: 68 additions & 27 deletions protocol/czar/mux_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package czar

import (
"encoding/binary"
"errors"
"io"
"log"
"math/rand/v2"
"net"
"strings"
"testing"
Expand All @@ -22,12 +24,44 @@ func TestProtocolCzarMux(t *testing.T) {
cli := doa.Try(mux.Open())
defer cli.Close()

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Doa(doa.Try(io.ReadFull(cli, buf[:128])) == 128)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Doa(doa.Try(io.ReadFull(cli, buf[:64])) == 64)
doa.Doa(doa.Try(io.ReadFull(cli, buf[:64])) == 64)
var (
bsz = max(4, int(rand.Uint32N(256)))
buf = make([]byte, bsz)
cnt int
rsz = int(rand.Uint32N(65536))
)

copy(buf[0:2], []byte{0x00, 0x00})
binary.BigEndian.PutUint16(buf[2:], uint16(rsz))
doa.Try(cli.Write(buf[:4]))
cnt = 0
for {
e := min(rand.IntN(bsz+1), rsz-cnt)
n := doa.Try(io.ReadFull(cli, buf[:e]))
for i := range n {
doa.Doa(buf[i] == 0x00)
}
cnt += n
if cnt == rsz {
break
}
}

copy(buf[0:2], []byte{0x01, 0x00})
binary.BigEndian.PutUint16(buf[2:], uint16(rsz))
doa.Try(cli.Write(buf[:4]))
for i := range bsz {
buf[i] = 0x00
}
cnt = 0
for {
e := min(rand.IntN(bsz+1), rsz-cnt)
n := doa.Try(cli.Write(buf[:e]))
cnt += n
if cnt == rsz {
break
}
}
}

func TestProtocolCzarMuxStreamClientClose(t *testing.T) {
Expand Down Expand Up @@ -57,26 +91,6 @@ func TestProtocolCzarMuxStreamServerClose(t *testing.T) {
doa.Doa(doa.Err(io.ReadFull(cli, buf[:1])) == io.EOF)
}

func TestProtocolCzarMuxClientClose(t *testing.T) {
remote := Tester{daze.NewTester(EchoServerListenOn)}
remote.Mux()
defer remote.Close()

mux := NewMuxClient(doa.Try(net.Dial("tcp", EchoServerListenOn)))
defer mux.Close()
cli := doa.Try(mux.Open())
defer cli.Close()

mux.con.Close()
buf := make([]byte, 2048)
er0 := doa.Err(mux.Open())
doa.Doa(strings.Contains(er0.Error(), "use of closed network connection"))
er1 := doa.Err(io.ReadFull(cli, buf[:1]))
doa.Doa(strings.Contains(er1.Error(), "use of closed network connection"))
er2 := doa.Err(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Doa(strings.Contains(er2.Error(), "use of closed network connection"))
}

func TestProtocolCzarMuxStreamClientReuse(t *testing.T) {
remote := Tester{daze.NewTester(EchoServerListenOn)}
remote.Mux()
Expand All @@ -89,8 +103,15 @@ func TestProtocolCzarMuxStreamClientReuse(t *testing.T) {
cl0 := doa.Try(mux.Open())
cl0.Write([]byte{0x00, 0x00, 0x80, 0x00})
cl0.Close()

for {
idx := doa.Try(mux.idp.Get())
mux.idp.Put(idx)
if idx == 0x00 {
break
}
}
cl1 := doa.Try(mux.Open())
doa.Doa(cl1.idx == 0x00)
doa.Try(cl1.Write([]byte{0x00, 0x01, 0x80, 0x00}))
doa.Doa(doa.Try(io.ReadFull(cl1, buf)) == 0x8000)
for i := range 0x8000 {
Expand All @@ -99,6 +120,26 @@ func TestProtocolCzarMuxStreamClientReuse(t *testing.T) {
cl1.Close()
}

func TestProtocolCzarMuxClientClose(t *testing.T) {
remote := Tester{daze.NewTester(EchoServerListenOn)}
remote.Mux()
defer remote.Close()

mux := NewMuxClient(doa.Try(net.Dial("tcp", EchoServerListenOn)))
defer mux.Close()
cli := doa.Try(mux.Open())
defer cli.Close()

mux.con.Close()
buf := make([]byte, 2048)
er0 := doa.Err(mux.Open())
doa.Doa(strings.Contains(er0.Error(), "use of closed network connection"))
er1 := doa.Err(io.ReadFull(cli, buf[:1]))
doa.Doa(strings.Contains(er1.Error(), "use of closed network connection"))
er2 := doa.Err(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Doa(strings.Contains(er2.Error(), "use of closed network connection"))
}

func TestProtocolCzarMuxServerRecvEvilPacket(t *testing.T) {
remote := Tester{daze.NewTester(EchoServerListenOn)}
remote.Mux()
Expand Down
44 changes: 41 additions & 3 deletions protocol/dahlia/engine_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dahlia

import (
"encoding/binary"
"io"
"math/rand/v2"
"testing"

"github.com/mohanson/daze"
Expand Down Expand Up @@ -30,7 +32,43 @@ func TestProtocolDahliaTCP(t *testing.T) {

cli := doa.Try(daze.Dial("tcp", DazeClientListenOn))
defer cli.Close()
buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Try(io.ReadFull(cli, buf[:128]))

var (
bsz = max(4, int(rand.Uint32N(256)))
buf = make([]byte, bsz)
cnt int
rsz = int(rand.Uint32N(65536))
)

copy(buf[0:2], []byte{0x00, 0x00})
binary.BigEndian.PutUint16(buf[2:], uint16(rsz))
doa.Try(cli.Write(buf[:4]))
cnt = 0
for {
e := min(rand.IntN(bsz+1), rsz-cnt)
n := doa.Try(io.ReadFull(cli, buf[:e]))
for i := range n {
doa.Doa(buf[i] == 0x00)
}
cnt += n
if cnt == rsz {
break
}
}

copy(buf[0:2], []byte{0x01, 0x00})
binary.BigEndian.PutUint16(buf[2:], uint16(rsz))
doa.Try(cli.Write(buf[:4]))
for i := range bsz {
buf[i] = 0x00
}
cnt = 0
for {
e := min(rand.IntN(bsz+1), rsz-cnt)
n := doa.Try(cli.Write(buf[:e]))
cnt += n
if cnt == rsz {
break
}
}
}

0 comments on commit 5a2ac2c

Please sign in to comment.