gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/packetimpact/tests/tcp_handshake_window_size_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_handshake_window_size_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  // TestTCPHandshakeWindowSize tests if the stack is honoring the window size
    32  // communicated during handshake.
    33  func TestTCPHandshakeWindowSize(t *testing.T) {
    34  	dut := testbench.NewDUT(t)
    35  	listenFD, remotePort := dut.CreateListener(t, unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
    36  	defer dut.Close(t, listenFD)
    37  	conn := dut.Net.NewTCPIPv4(t, testbench.TCP{DstPort: &remotePort}, testbench.TCP{SrcPort: &remotePort})
    38  	defer conn.Close(t)
    39  
    40  	// Start handshake with zero window size.
    41  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagSyn), WindowSize: testbench.Uint16(uint16(0))})
    42  	if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagSyn | header.TCPFlagAck)}, nil, time.Second); err != nil {
    43  		t.Fatalf("expected SYN-ACK: %s", err)
    44  	}
    45  	// Update the advertised window size to a non-zero value with the ACK that
    46  	// completes the handshake.
    47  	//
    48  	// Set the window size with MSB set and expect the dut to treat it as
    49  	// an unsigned value.
    50  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck), WindowSize: testbench.Uint16(uint16(1 << 15))})
    51  
    52  	acceptFd, _ := dut.Accept(t, listenFD)
    53  	defer dut.Close(t, acceptFd)
    54  
    55  	sampleData := []byte("Sample Data")
    56  	samplePayload := &testbench.Payload{Bytes: sampleData}
    57  
    58  	// Since we advertised a zero window followed by a non-zero window,
    59  	// expect the dut to honor the recently advertised non-zero window
    60  	// and actually send out the data instead of probing for zero window.
    61  	dut.Send(t, acceptFd, sampleData, 0)
    62  	if _, err := conn.ExpectNextData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck | header.TCPFlagPsh)}, samplePayload, time.Second); err != nil {
    63  		t.Fatalf("expected payload was not received: %s", err)
    64  	}
    65  }