github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/packetimpact/tests/tcp_fin_retransmission_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_fin_retransmission_test
    16  
    17  import (
    18  	"flag"
    19  	"testing"
    20  	"time"
    21  
    22  	"golang.org/x/sys/unix"
    23  	"github.com/SagerNet/gvisor/pkg/tcpip/header"
    24  	"github.com/SagerNet/gvisor/test/packetimpact/testbench"
    25  )
    26  
    27  func init() {
    28  	testbench.Initialize(flag.CommandLine)
    29  }
    30  
    31  // TestTCPClosingFinRetransmission tests that TCP implementation should retransmit
    32  // FIN segment in CLOSING state.
    33  func TestTCPClosingFinRetransmission(t *testing.T) {
    34  	for _, tt := range []struct {
    35  		description string
    36  		flags       header.TCPFlags
    37  	}{
    38  		{"CLOSING", header.TCPFlagAck | header.TCPFlagFin},
    39  		{"FIN_WAIT_1", header.TCPFlagAck},
    40  	} {
    41  		t.Run(tt.description, func(t *testing.T) {
    42  			dut := testbench.NewDUT(t)
    43  			listenFD, remotePort := dut.CreateListener(t, unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
    44  			defer dut.Close(t, listenFD)
    45  			conn := dut.Net.NewTCPIPv4(t, testbench.TCP{DstPort: &remotePort}, testbench.TCP{SrcPort: &remotePort})
    46  			defer conn.Close(t)
    47  			conn.Connect(t)
    48  			acceptFD, _ := dut.Accept(t, listenFD)
    49  			defer dut.Close(t, acceptFD)
    50  
    51  			// Give a chance for the dut to estimate RTO with RTT from the DATA-ACK.
    52  			// TODO(github.com/SagerNet/issue/2685) Estimate RTO during handshake, after which
    53  			// we can skip the next block of code.
    54  			sampleData := []byte("Sample Data")
    55  			if got, want := dut.Send(t, acceptFD, sampleData, 0), len(sampleData); int(got) != want {
    56  				t.Fatalf("got dut.Send(t, %d, %s, 0) = %d, want %d", acceptFD, sampleData, got, want)
    57  			}
    58  			if _, err := conn.ExpectData(t, &testbench.TCP{}, &testbench.Payload{Bytes: sampleData}, time.Second); err != nil {
    59  				t.Fatalf("expected payload was not received: %s", err)
    60  			}
    61  			conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)})
    62  
    63  			dut.Shutdown(t, acceptFD, unix.SHUT_WR)
    64  
    65  			if _, err := conn.Expect(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagFin | header.TCPFlagAck)}, time.Second); err != nil {
    66  				t.Fatalf("expected FINACK from DUT, but got none: %s", err)
    67  			}
    68  
    69  			// Do not ack the FIN from DUT so that we can test for retransmission.
    70  			seqNumForTheirFIN := testbench.Uint32(uint32(*conn.RemoteSeqNum(t)) - 1)
    71  			conn.Send(t, testbench.TCP{AckNum: seqNumForTheirFIN, Flags: testbench.TCPFlags(tt.flags)})
    72  
    73  			if tt.flags&header.TCPFlagFin != 0 {
    74  				if _, err := conn.Expect(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)}, time.Second); err != nil {
    75  					t.Errorf("expected an ACK to our FIN, but got none: %s", err)
    76  				}
    77  			}
    78  
    79  			if _, err := conn.Expect(t, testbench.TCP{
    80  				SeqNum: seqNumForTheirFIN,
    81  				Flags:  testbench.TCPFlags(header.TCPFlagFin | header.TCPFlagAck),
    82  			}, time.Second); err != nil {
    83  				t.Errorf("expected retransmission of FIN from the DUT: %s", err)
    84  			}
    85  		})
    86  	}
    87  }