Skip to content

Commit

Permalink
add duplex comms to reopen test
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Dec 6, 2023
1 parent 7035784 commit 0a36f9d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
2 changes: 0 additions & 2 deletions stacks/port_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/soypat/seqs/eth"
)

const useBool = true

// tcphandler represents a user provided function for handling incoming TCP packets on a port.
// Incoming data is sent inside the `pkt` TCPPacket argument when pkt.HasPacket returns true.
// Outgoing data is stored into the `response` byte slice. The function must return the number of
Expand Down
54 changes: 32 additions & 22 deletions stacks/stacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package stacks_test

import (
"errors"
"fmt"
"log/slog"
"math"
"net/netip"
"os"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -221,27 +221,7 @@ func TestTCPSendReceive_duplex(t *testing.T) {
}

// Send data from client to server multiple times.
const messages = 1024
for i := 0; i < messages; i++ {
cdata := fmt.Sprintf("hello world %d", i)
sdata := fmt.Sprintf("hello yourself %d", i)

socketSendString(client, cdata)
socketSendString(server, sdata)
tx, bytes := egr.DoExchanges(t, 2)
if client.State() != seqs.StateEstablished || server.State() != seqs.StateEstablished {
t.Fatalf("not established: client=%s server=%s", client.State(), server.State())
}
t.Logf("tx=%d bytes=%d", tx, bytes)
clientstr := socketReadAllString(client)
serverstr := socketReadAllString(server)
if clientstr != sdata {
t.Errorf("client: got %q want %q", clientstr, sdata)
}
if serverstr != cdata {
t.Errorf("server: got %q want %q", serverstr, cdata)
}
}
testSocketDuplex(t, client, server, egr, 1024)
}

func TestTCPClose_noPendingData(t *testing.T) {
Expand Down Expand Up @@ -348,6 +328,36 @@ func TestTCPSocketOpenOfClosedPort(t *testing.T) {
if nbytes < minBytesToEstablish {
t.Fatalf("insufficient data to establish: got %d want>=%d", nbytes, minBytesToEstablish)
}
testSocketDuplex(t, client, server, egr, 128)
}

func testSocketDuplex(t *testing.T, client, server *stacks.TCPSocket, egr *Exchanger, messages int) {
if client.State() != seqs.StateEstablished || server.State() != seqs.StateEstablished {
panic("not established")
}
// Send data from client to server multiple times.
for i := 0; i < messages; i++ {
istr := strconv.Itoa(i)
cdata := "hello server " + istr
sdata := "hello client " + istr

socketSendString(client, cdata)
socketSendString(server, sdata)
tx, bytes := egr.DoExchanges(t, 2)
if client.State() != seqs.StateEstablished || server.State() != seqs.StateEstablished {
t.Fatalf("not established: client=%s server=%s", client.State(), server.State())
}
_, _ = tx, bytes
// t.Logf("tx=%d bytes=%d", tx, bytes)
clientstr := socketReadAllString(client)
serverstr := socketReadAllString(server)
if clientstr != sdata {
t.Errorf("client: got %q want %q", clientstr, sdata)
}
if serverstr != cdata {
t.Errorf("server: got %q want %q", serverstr, cdata)
}
}
}

// func TestPortStackTCPDecoding(t *testing.T) {
Expand Down

0 comments on commit 0a36f9d

Please sign in to comment.