Skip to content

Commit

Permalink
Optimizing the tester
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Aug 14, 2024
1 parent 997bfa7 commit d8ac0af
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 31 deletions.
39 changes: 27 additions & 12 deletions daze.go
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ func (t *Tester) TCP() error {

// TCPServe serves incoming connections.
func (t *Tester) TCPServe(cli io.ReadWriteCloser) {
buf := make([]byte, 2048)
buf := make([]byte, 1024*64)
for {
_, err := io.ReadFull(cli, buf[:4])
if err != nil {
Expand All @@ -1204,12 +1204,20 @@ func (t *Tester) TCPServe(cli io.ReadWriteCloser) {
cmd := buf[0]
switch cmd {
case 0:
msg := binary.BigEndian.Uint16(buf[2:4])
doa.Doa(msg <= 2044)
io.ReadFull(&RandomReader{}, buf[4:4+msg])
buf[0] = 1
cli.Write(buf[:4+msg])
val := buf[1]
cnt := binary.BigEndian.Uint16(buf[2:4])
for i := range cnt {
buf[i] = val
}
cli.Write(buf[:cnt])
case 1:
val := buf[1]
cnt := binary.BigEndian.Uint16(buf[2:4])
io.ReadFull(cli, buf[:cnt])
for i := range cnt {
doa.Doa(buf[i] == val)
}
case 2:
cli.Close()
}
}
Expand All @@ -1226,7 +1234,7 @@ func (t *Tester) UDP() error {

// UDPServe serves incoming connections.
func (t *Tester) UDPServe(cli *net.UDPConn) error {
buf := make([]byte, 2048)
buf := make([]byte, 1024*64)
for {
_, addr, err := cli.ReadFromUDP(buf)
if err != nil {
Expand All @@ -1235,12 +1243,19 @@ func (t *Tester) UDPServe(cli *net.UDPConn) error {
cmd := buf[0]
switch cmd {
case 0:
msg := binary.BigEndian.Uint16(buf[2:4])
doa.Doa(msg <= 2044)
io.ReadFull(&RandomReader{}, buf[4:4+msg])
buf[0] = 1
doa.Try(cli.WriteToUDP(buf[:4+msg], addr))
val := buf[1]
cnt := binary.BigEndian.Uint16(buf[2:4])
for i := range cnt {
buf[i] = val
}
doa.Try(cli.WriteToUDP(buf[:cnt], addr))
case 1:
val := buf[1]
cnt := binary.BigEndian.Uint16(buf[2:4])
for i := range cnt {
doa.Doa(buf[4+i] == val)
}
case 2:
cli.Close()
}
}
Expand Down
8 changes: 4 additions & 4 deletions protocol/ashe/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestProtocolAsheTCP(t *testing.T) {

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Try(io.ReadFull(cli, buf[:132]))
doa.Try(io.ReadFull(cli, buf[:128]))
}

func TestProtocolAsheTCPClientClose(t *testing.T) {
Expand All @@ -48,7 +48,7 @@ func TestProtocolAsheTCPClientClose(t *testing.T) {
defer cli.Close()

cli.Close()
_, er1 := cli.Write([]byte{0x01, 0x00, 0x00, 0x00})
_, er1 := cli.Write([]byte{0x02, 0x00, 0x00, 0x00})
doa.Doa(er1 != nil)
buf := make([]byte, 2048)
_, er2 := io.ReadFull(cli, buf[:1])
Expand All @@ -70,7 +70,7 @@ func TestProtocolAsheTCPServerClose(t *testing.T) {
defer cli.Close()

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x01, 0x00, 0x00, 0x00}))
doa.Try(cli.Write([]byte{0x02, 0x00, 0x00, 0x00}))
_, err := io.ReadFull(cli, buf[:1])
doa.Doa(err != nil)
}
Expand All @@ -91,5 +91,5 @@ func TestProtocolAsheUDP(t *testing.T) {

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Try(io.ReadFull(cli, buf[:132]))
doa.Try(io.ReadFull(cli, buf[:128]))
}
8 changes: 4 additions & 4 deletions protocol/baboon/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestProtocolBaboonTCP(t *testing.T) {

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Try(io.ReadFull(cli, buf[:132]))
doa.Try(io.ReadFull(cli, buf[:128]))
}

func TestProtocolBaboonTCPClientClose(t *testing.T) {
Expand All @@ -50,7 +50,7 @@ func TestProtocolBaboonTCPClientClose(t *testing.T) {
defer cli.Close()

cli.Close()
_, er1 := cli.Write([]byte{0x01, 0x00, 0x00, 0x00})
_, er1 := cli.Write([]byte{0x02, 0x00, 0x00, 0x00})
doa.Doa(er1 != nil)
buf := make([]byte, 2048)
_, er2 := io.ReadFull(cli, buf[:1])
Expand All @@ -72,7 +72,7 @@ func TestProtocolBaboonTCPServerClose(t *testing.T) {
defer cli.Close()

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x01, 0x00, 0x00, 0x00}))
doa.Try(cli.Write([]byte{0x02, 0x00, 0x00, 0x00}))
_, err := io.ReadFull(cli, buf[:1])
doa.Doa(err != nil)
}
Expand All @@ -93,7 +93,7 @@ func TestProtocolBaboonUDP(t *testing.T) {

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Try(io.ReadFull(cli, buf[:132]))
doa.Try(io.ReadFull(cli, buf[:128]))
}

func TestProtocolBaboonMasker(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions protocol/czar/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestProtocolCzarTCP(t *testing.T) {

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Try(io.ReadFull(cli, buf[:132]))
doa.Try(io.ReadFull(cli, buf[:128]))
}

func TestProtocolCzarTCPClientClose(t *testing.T) {
Expand All @@ -51,7 +51,7 @@ func TestProtocolCzarTCPClientClose(t *testing.T) {

buf := make([]byte, 2048)
cli.Close()
_, er1 := cli.Write([]byte{0x01, 0x00, 0x00, 0x00})
_, er1 := cli.Write([]byte{0x02, 0x00, 0x00, 0x00})
doa.Doa(er1 != nil)
_, er2 := io.ReadFull(cli, buf[:1])
doa.Doa(er2 != nil)
Expand All @@ -73,7 +73,7 @@ func TestProtocolCzarTCPServerClose(t *testing.T) {
defer cli.Close()

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x01, 0x00, 0x00, 0x00}))
doa.Try(cli.Write([]byte{0x02, 0x00, 0x00, 0x00}))
_, err := io.ReadFull(cli, buf[:1])
doa.Doa(err != nil)
}
Expand All @@ -95,5 +95,5 @@ func TestProtocolCzarUDP(t *testing.T) {

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Try(io.ReadFull(cli, buf[:132]))
doa.Try(io.ReadFull(cli, buf[:128]))
}
12 changes: 6 additions & 6 deletions protocol/czar/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func TestProtocolMux(t *testing.T) {

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Doa(doa.Try(io.ReadFull(cli, buf[:132])) == 132)
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[:66])) == 66)
doa.Doa(doa.Try(io.ReadFull(cli, buf[:66])) == 66)
doa.Doa(doa.Try(io.ReadFull(cli, buf[:64])) == 64)
doa.Doa(doa.Try(io.ReadFull(cli, buf[:64])) == 64)
}

func TestProtocolMuxStreamClientClose(t *testing.T) {
Expand All @@ -54,7 +54,7 @@ func TestProtocolMuxStreamServerClose(t *testing.T) {
defer cli.Close()

buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x01, 0x00, 0x00, 0x80}))
doa.Try(cli.Write([]byte{0x02, 0x00, 0x00, 0x80}))
doa.Doa(doa.Err(io.ReadFull(cli, buf[:1])) == io.EOF)
}

Expand Down Expand Up @@ -96,9 +96,9 @@ func TestProtocolMuxServerRecvEvilPacket(t *testing.T) {
cl1.Write([]byte{0x00, 0x00, 0x00, 0x00})
cl1.Write([]byte{0x00, 0x00, 0x00, 0x00})
cl1.Write([]byte{0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04})
_, er1 := io.ReadFull(cl1, buf[:12])
_, er1 := io.ReadFull(cl1, buf[:8])
doa.Nil(er1)
doa.Doa(slices.Equal(buf[:8], []byte{0x00, 0x01, 0x00, 0x08, 0x01, 0x00, 0x00, 0x04}))
doa.Doa(slices.Equal(buf[:8], []byte{0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00}))
}

type Tester struct {
Expand Down
2 changes: 1 addition & 1 deletion protocol/dahlia/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ func TestProtocolDahliaTCP(t *testing.T) {
defer cli.Close()
buf := make([]byte, 2048)
doa.Try(cli.Write([]byte{0x00, 0x00, 0x00, 0x80}))
doa.Try(io.ReadFull(cli, buf[:132]))
doa.Try(io.ReadFull(cli, buf[:128]))
}

0 comments on commit d8ac0af

Please sign in to comment.