github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/packetimpact/tests/tcp_user_timeout_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_user_timeout_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  func sendPayload(t *testing.T, conn *testbench.TCPIPv4, dut *testbench.DUT, fd int32) {
    32  	sampleData := make([]byte, 100)
    33  	for i := range sampleData {
    34  		sampleData[i] = uint8(i)
    35  	}
    36  	conn.Drain(t)
    37  	dut.Send(t, fd, sampleData, 0)
    38  	if _, err := conn.ExpectData(t, &testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck | header.TCPFlagPsh)}, &testbench.Payload{Bytes: sampleData}, time.Second); err != nil {
    39  		t.Fatalf("expected data but got none: %w", err)
    40  	}
    41  }
    42  
    43  func sendFIN(t *testing.T, conn *testbench.TCPIPv4, dut *testbench.DUT, fd int32) {
    44  	dut.Close(t, fd)
    45  }
    46  
    47  func TestTCPUserTimeout(t *testing.T) {
    48  	for _, tt := range []struct {
    49  		description string
    50  		userTimeout time.Duration
    51  		sendDelay   time.Duration
    52  	}{
    53  		{"NoUserTimeout", 0, 3 * time.Second},
    54  		{"ACKBeforeUserTimeout", 5 * time.Second, 4 * time.Second},
    55  		{"ACKAfterUserTimeout", 5 * time.Second, 7 * time.Second},
    56  	} {
    57  		for _, ttf := range []struct {
    58  			description string
    59  			f           func(_ *testing.T, _ *testbench.TCPIPv4, _ *testbench.DUT, fd int32)
    60  		}{
    61  			{"AfterPayload", sendPayload},
    62  			{"AfterFIN", sendFIN},
    63  		} {
    64  			t.Run(tt.description+ttf.description, func(t *testing.T) {
    65  				// Create a socket, listen, TCP handshake, and accept.
    66  				dut := testbench.NewDUT(t)
    67  				listenFD, remotePort := dut.CreateListener(t, unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
    68  				defer dut.Close(t, listenFD)
    69  				conn := dut.Net.NewTCPIPv4(t, testbench.TCP{DstPort: &remotePort}, testbench.TCP{SrcPort: &remotePort})
    70  				defer conn.Close(t)
    71  				conn.Connect(t)
    72  				acceptFD, _ := dut.Accept(t, listenFD)
    73  
    74  				if tt.userTimeout != 0 {
    75  					dut.SetSockOptInt(t, acceptFD, unix.SOL_TCP, unix.TCP_USER_TIMEOUT, int32(tt.userTimeout.Milliseconds()))
    76  				}
    77  
    78  				ttf.f(t, &conn, &dut, acceptFD)
    79  
    80  				time.Sleep(tt.sendDelay)
    81  				conn.Drain(t)
    82  				conn.Send(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagAck)})
    83  
    84  				// If TCP_USER_TIMEOUT was set and the above delay was longer than the
    85  				// TCP_USER_TIMEOUT then the DUT should send a RST in response to the
    86  				// testbench's packet.
    87  				expectRST := tt.userTimeout != 0 && tt.sendDelay > tt.userTimeout
    88  				expectTimeout := 5 * time.Second
    89  				got, err := conn.Expect(t, testbench.TCP{Flags: testbench.TCPFlags(header.TCPFlagRst)}, expectTimeout)
    90  				if expectRST && err != nil {
    91  					t.Errorf("expected RST packet within %s but got none: %s", expectTimeout, err)
    92  				}
    93  				if !expectRST && got != nil {
    94  					t.Errorf("expected no RST packet within %s but got one: %s", expectTimeout, got)
    95  				}
    96  			})
    97  		}
    98  	}
    99  }