From 0a36f9d87f45434d32e9f15aace64f21bdcfc76d Mon Sep 17 00:00:00 2001 From: soypat Date: Tue, 5 Dec 2023 21:34:32 -0300 Subject: [PATCH] add duplex comms to reopen test --- stacks/port_tcp.go | 2 -- stacks/stacks_test.go | 54 +++++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/stacks/port_tcp.go b/stacks/port_tcp.go index e0cdf1a..e8ecf64 100644 --- a/stacks/port_tcp.go +++ b/stacks/port_tcp.go @@ -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 diff --git a/stacks/stacks_test.go b/stacks/stacks_test.go index 8d297ec..272c127 100644 --- a/stacks/stacks_test.go +++ b/stacks/stacks_test.go @@ -2,11 +2,11 @@ package stacks_test import ( "errors" - "fmt" "log/slog" "math" "net/netip" "os" + "strconv" "strings" "testing" @@ -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) { @@ -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) {