gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/packetimpact/tests/tcp_timewait_reset_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_timewait_reset_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  // TestTimeWaitReset tests handling of RST when in TIME_WAIT state.
    32  func TestTimeWaitReset(t *testing.T) {
    33  	dut := testbench.NewDUT(t)
    34  	listenFD, remotePort := dut.CreateListener(t, unix.SOCK_STREAM, unix.IPPROTO_TCP, 1 /*backlog*/)
    35  	defer dut.Close(t, listenFD)
    36  	conn := dut.Net.NewTCPIPv4(t, testbench.TCP{DstPort: &remotePort}, testbench.TCP{SrcPort: &remotePort})
    37  	defer conn.Close(t)
    38  
    39  	conn.Connect(t)
    40  	acceptFD, _ := dut.Accept(t, listenFD)
    41  
    42  	// Trigger active close.
    43  	dut.Close(t, acceptFD)
    44  
    45  	_, err := conn.Expect(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagFin | header.TCPFlagAck)}, time.Second)
    46  	if err != nil {
    47  		t.Fatalf("expected a FIN: %s", err)
    48  	}
    49  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)})
    50  	// Send a FIN, DUT should transition to TIME_WAIT from FIN_WAIT2.
    51  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagFin | header.TCPFlagAck)})
    52  	if _, err := conn.Expect(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)}, time.Second); err != nil {
    53  		t.Fatalf("expected an ACK for our FIN: %s", err)
    54  	}
    55  
    56  	// Send a RST, the DUT should transition to CLOSED from TIME_WAIT.
    57  	// This is the default Linux behavior, it can be changed to ignore RSTs via
    58  	// sysctl net.ipv4.tcp_rfc1337.
    59  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagRst)})
    60  
    61  	conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)})
    62  	// The DUT should reply with RST to our ACK as the state should have
    63  	// transitioned to CLOSED.
    64  	if _, err := conn.Expect(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagRst)}, time.Second); err != nil {
    65  		t.Fatalf("expected a RST: %s", err)
    66  	}
    67  }