document everything

This commit is contained in:
soypat
2025-05-13 23:49:13 -03:00
parent 411d1b3c28
commit c94337317f
3 changed files with 56 additions and 4 deletions
+36
View File
@@ -34,6 +34,14 @@ func TestBasicStack(t *testing.T) {
exchangeAndExpectStates(tcp.StateEstablished, tcp.StateEstablished) // Client sends ACK, establishing connection in full.
}
func TestBasicStack2(t *testing.T) {
rng := rand.New(rand.NewSource(1))
var sbCl, sbSv StackBasic
var connCl, connSv TCPConn
setupClientServerEstablished(t, rng, &sbCl, &sbSv, &connCl, &connSv)
}
func expectExchange(t *testing.T, from, to *StackBasic, buf []byte) {
t.Helper()
n, err := from.Handle(buf)
@@ -49,6 +57,34 @@ func expectExchange(t *testing.T, from, to *StackBasic, buf []byte) {
}
}
func setupClientServerEstablished(t *testing.T, rng *rand.Rand, client, server *StackBasic, connClient, connServer *TCPConn) {
t.Helper()
setupClientServer(t, rng, client, server, connClient, connServer)
var buf [2048]byte
nextToSend := client
nextToRecv := server
exchangeAndExpectStates := func(clState, svState tcp.State) {
t.Helper()
expectExchange(t, nextToSend, nextToRecv, buf[:])
gotCl := connClient.State()
gotSv := connServer.State()
if gotCl != clState {
t.Errorf("want client state %s, got %s", clState, gotCl)
}
if gotSv != svState {
t.Errorf("want server state %s, got %s", svState, gotSv)
}
nextToSend, nextToRecv = nextToRecv, nextToSend
}
exchangeAndExpectStates(tcp.StateSynSent, tcp.StateSynRcvd) // Client sends over first SYN and server receives it.
exchangeAndExpectStates(tcp.StateEstablished, tcp.StateSynRcvd) // server sends back SYNACK, establishing connection on client side.
exchangeAndExpectStates(tcp.StateEstablished, tcp.StateEstablished) // Client sends ACK, establishing connection in full.
if t.Failed() {
t.Error("establishment failed")
t.FailNow()
}
}
func setupClientServer(t *testing.T, rng *rand.Rand, client, server *StackBasic, connClient, connServer *TCPConn) {
bufsize := 2048
// Ensure buffer sizes are OK with reused buffers.