2020-04-20 03:47:55 +00:00
|
|
|
// Copyright 2020 The gVisor Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tcp_user_timeout_test
|
|
|
|
|
|
|
|
import (
|
2020-05-11 22:52:42 +00:00
|
|
|
"flag"
|
2020-04-20 03:47:55 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"gvisor.dev/gvisor/pkg/tcpip/header"
|
2020-06-04 21:22:46 +00:00
|
|
|
"gvisor.dev/gvisor/test/packetimpact/testbench"
|
2020-04-20 03:47:55 +00:00
|
|
|
)
|
|
|
|
|
2020-05-11 22:52:42 +00:00
|
|
|
func init() {
|
2020-06-04 21:22:46 +00:00
|
|
|
testbench.RegisterFlags(flag.CommandLine)
|
2020-05-11 22:52:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 21:22:46 +00:00
|
|
|
func sendPayload(conn *testbench.TCPIPv4, dut *testbench.DUT, fd int32) error {
|
2020-04-20 03:47:55 +00:00
|
|
|
sampleData := make([]byte, 100)
|
|
|
|
for i := range sampleData {
|
|
|
|
sampleData[i] = uint8(i)
|
|
|
|
}
|
|
|
|
conn.Drain()
|
|
|
|
dut.Send(fd, sampleData, 0)
|
2020-06-04 21:22:46 +00:00
|
|
|
if _, err := conn.ExpectData(&testbench.TCP{Flags: testbench.Uint8(header.TCPFlagAck | header.TCPFlagPsh)}, &testbench.Payload{Bytes: sampleData}, time.Second); err != nil {
|
2020-04-20 03:47:55 +00:00
|
|
|
return fmt.Errorf("expected data but got none: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:22:46 +00:00
|
|
|
func sendFIN(conn *testbench.TCPIPv4, dut *testbench.DUT, fd int32) error {
|
2020-04-20 03:47:55 +00:00
|
|
|
dut.Close(fd)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTCPUserTimeout(t *testing.T) {
|
|
|
|
for _, tt := range []struct {
|
|
|
|
description string
|
|
|
|
userTimeout time.Duration
|
|
|
|
sendDelay time.Duration
|
|
|
|
}{
|
|
|
|
{"NoUserTimeout", 0, 3 * time.Second},
|
|
|
|
{"ACKBeforeUserTimeout", 5 * time.Second, 4 * time.Second},
|
|
|
|
{"ACKAfterUserTimeout", 5 * time.Second, 7 * time.Second},
|
|
|
|
} {
|
|
|
|
for _, ttf := range []struct {
|
|
|
|
description string
|
2020-06-04 21:22:46 +00:00
|
|
|
f func(conn *testbench.TCPIPv4, dut *testbench.DUT, fd int32) error
|
2020-04-20 03:47:55 +00:00
|
|
|
}{
|
|
|
|
{"AfterPayload", sendPayload},
|
|
|
|
{"AfterFIN", sendFIN},
|
|
|
|
} {
|
|
|
|
t.Run(tt.description+ttf.description, func(t *testing.T) {
|
|
|
|
// Create a socket, listen, TCP handshake, and accept.
|
2020-06-04 21:22:46 +00:00
|
|
|
dut := testbench.NewDUT(t)
|
2020-04-20 03:47:55 +00:00
|
|
|
defer dut.TearDown()
|
|
|
|
listenFD, remotePort := dut.CreateListener(unix.SOCK_STREAM, unix.IPPROTO_TCP, 1)
|
|
|
|
defer dut.Close(listenFD)
|
2020-06-04 21:22:46 +00:00
|
|
|
conn := testbench.NewTCPIPv4(t, testbench.TCP{DstPort: &remotePort}, testbench.TCP{SrcPort: &remotePort})
|
2020-04-20 03:47:55 +00:00
|
|
|
defer conn.Close()
|
2020-06-04 21:22:46 +00:00
|
|
|
conn.Connect()
|
2020-04-20 03:47:55 +00:00
|
|
|
acceptFD, _ := dut.Accept(listenFD)
|
|
|
|
|
|
|
|
if tt.userTimeout != 0 {
|
|
|
|
dut.SetSockOptInt(acceptFD, unix.SOL_TCP, unix.TCP_USER_TIMEOUT, int32(tt.userTimeout.Milliseconds()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ttf.f(&conn, &dut, acceptFD); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(tt.sendDelay)
|
|
|
|
conn.Drain()
|
2020-06-04 21:22:46 +00:00
|
|
|
conn.Send(testbench.TCP{Flags: testbench.Uint8(header.TCPFlagAck)})
|
2020-04-20 03:47:55 +00:00
|
|
|
|
|
|
|
// If TCP_USER_TIMEOUT was set and the above delay was longer than the
|
|
|
|
// TCP_USER_TIMEOUT then the DUT should send a RST in response to the
|
|
|
|
// testbench's packet.
|
|
|
|
expectRST := tt.userTimeout != 0 && tt.sendDelay > tt.userTimeout
|
|
|
|
expectTimeout := 5 * time.Second
|
2020-06-04 21:22:46 +00:00
|
|
|
got, err := conn.Expect(testbench.TCP{Flags: testbench.Uint8(header.TCPFlagRst)}, expectTimeout)
|
2020-04-20 03:47:55 +00:00
|
|
|
if expectRST && err != nil {
|
|
|
|
t.Errorf("expected RST packet within %s but got none: %s", expectTimeout, err)
|
|
|
|
}
|
|
|
|
if !expectRST && got != nil {
|
|
|
|
t.Errorf("expected no RST packet within %s but got one: %s", expectTimeout, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|