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

     1  // Copyright 2022 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  package tcp_acceptable_ack_syn_rcvd_test
    15  
    16  import (
    17  	"flag"
    18  	"fmt"
    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 TestAcceptableAckInSynRcvd(t *testing.T) {
    32  	for _, tt := range []struct {
    33  		offset    uint32
    34  		expectRst bool
    35  	}{
    36  		{offset: 0, expectRst: true},
    37  		// The ACK holds the next expected SEQ so valid segments must hold an ACK
    38  		// that is 1 larger than the last SEQ value.
    39  		{offset: 1, expectRst: false},
    40  		{offset: 2, expectRst: true},
    41  	} {
    42  		t.Run(fmt.Sprintf("offset=%d, expectRst=%t", tt.offset, tt.expectRst), func(t *testing.T) {
    43  			dut := testbench.NewDUT(t)
    44  			listenFd, listenerPort := dut.CreateListener(t, unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
    45  			defer dut.Close(t, listenFd)
    46  			conn := dut.Net.NewTCPIPv4(t, testbench.TCP{DstPort: &listenerPort}, testbench.TCP{SrcPort: &listenerPort})
    47  			defer conn.Close(t)
    48  
    49  			conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagSyn)})
    50  
    51  			synAck, err := conn.Expect(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagSyn | header.TCPFlagAck)}, time.Second)
    52  			if err != nil {
    53  				t.Fatalf("didn't get synack during handshake: %s", err)
    54  			}
    55  
    56  			// Calculate the ACK number.
    57  			ackNum := *synAck.SeqNum + tt.offset
    58  			conn.Send(t, testbench.TCP{AckNum: &ackNum, Flags: testbench.TCPFlags(header.TCPFlagAck)})
    59  
    60  			if tt.expectRst {
    61  				if _, err := conn.Expect(t, testbench.TCP{SeqNum: &ackNum, Flags: testbench.TCPFlags(header.TCPFlagRst)}, time.Second); err != nil {
    62  					t.Fatalf("failed to receive rst for an unacceptable ack: %s", err)
    63  				}
    64  			} else {
    65  				acceptFd, _ := dut.Accept(t, listenFd)
    66  				dut.Close(t, acceptFd)
    67  			}
    68  		})
    69  	}
    70  }