gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/packetimpact/tests/tcp_window_shrink_test.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tcp_window_shrink_test
    16  
    17  import (
    18  	"flag"
    19  	"testing"
    20  	"time"
    21  
    22  	"golang.org/x/sys/unix"
    23  	"gvisor.dev/gvisor/pkg/tcpip/header"
    24  	"gvisor.dev/gvisor/test/packetimpact/testbench"
    25  )
    26  
    27  func init() {
    28  	testbench.Initialize(flag.CommandLine)
    29  }
    30  
    31  func TestWindowShrink(t *testing.T) {
    32  	dut := testbench.NewDUT(t)
    33  	listenFd, remotePort := dut.CreateListener(t, unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
    34  	defer dut.Close(t, listenFd)
    35  	conn := dut.Net.NewTCPIPv4(t, testbench.TCP{DstPort: &remotePort}, testbench.TCP{SrcPort: &remotePort})
    36  	defer conn.Close(t)
    37  
    38  	conn.Connect(t)
    39  	acceptFd, _ := dut.Accept(t, listenFd)
    40  	defer dut.Close(t, acceptFd)
    41  
    42  	dut.SetSockOptInt(t, acceptFd, unix.IPPROTO_TCP, unix.TCP_NODELAY, 1)
    43  
    44  	sampleData := []byte("Sample Data")
    45  	samplePayload := &testbench.Payload{Bytes: sampleData}
    46  
    47  	dut.Send(t, acceptFd, sampleData, 0)
    48  	if _, err := conn.ExpectData(t, &testbench.TCP{}, samplePayload, time.Second); err != nil {
    49  		t.Fatalf("expected payload was not received: %s", err)
    50  	}
    51  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)})
    52  
    53  	dut.Send(t, acceptFd, sampleData, 0)
    54  	dut.Send(t, acceptFd, sampleData, 0)
    55  	if _, err := conn.ExpectData(t, &testbench.TCP{}, samplePayload, time.Second); err != nil {
    56  		t.Fatalf("expected payload was not received: %s", err)
    57  	}
    58  	if _, err := conn.ExpectData(t, &testbench.TCP{}, samplePayload, time.Second); err != nil {
    59  		t.Fatalf("expected payload was not received: %s", err)
    60  	}
    61  	// We close our receiving window here
    62  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck), WindowSize: testbench.Uint16(0)})
    63  
    64  	dut.Send(t, acceptFd, []byte("Sample Data"), 0)
    65  	// Note: There is another kind of zero-window probing which Windows uses (by sending one
    66  	// new byte at `RemoteSeqNum`), if netstack wants to go that way, we may want to change
    67  	// the following lines.
    68  	expectedRemoteSeqNum := *conn.RemoteSeqNum(t) - 1
    69  	if _, err := conn.ExpectData(t, &testbench.TCP{SeqNum: testbench.Uint32(uint32(expectedRemoteSeqNum))}, nil, time.Second); err != nil {
    70  		t.Fatalf("expected a packet with sequence number %d: %s", expectedRemoteSeqNum, err)
    71  	}
    72  }