2020-05-08 22:38:42 +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 udp_send_recv_dgram_test
|
|
|
|
|
|
|
|
import (
|
2020-05-11 22:52:42 +00:00
|
|
|
"flag"
|
2020-05-08 22:38:42 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2020-06-04 21:22:46 +00:00
|
|
|
"gvisor.dev/gvisor/test/packetimpact/testbench"
|
2020-05-08 22:38:42 +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-07-14 18:48:26 +00:00
|
|
|
type udpConn interface {
|
|
|
|
Send(testbench.UDP, ...testbench.Layer)
|
|
|
|
ExpectData(testbench.UDP, testbench.Payload, time.Duration) (testbench.Layers, error)
|
|
|
|
Drain()
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUDP(t *testing.T) {
|
2020-06-04 21:22:46 +00:00
|
|
|
dut := testbench.NewDUT(t)
|
2020-05-08 22:38:42 +00:00
|
|
|
defer dut.TearDown()
|
|
|
|
|
2020-07-14 18:48:26 +00:00
|
|
|
for _, isIPv4 := range []bool{true, false} {
|
|
|
|
ipVersionName := "IPv6"
|
|
|
|
if isIPv4 {
|
|
|
|
ipVersionName = "IPv4"
|
|
|
|
}
|
|
|
|
t.Run(ipVersionName, func(t *testing.T) {
|
|
|
|
var addr string
|
|
|
|
if isIPv4 {
|
|
|
|
addr = testbench.RemoteIPv4
|
|
|
|
} else {
|
|
|
|
addr = testbench.RemoteIPv6
|
2020-05-08 22:38:42 +00:00
|
|
|
}
|
2020-07-14 18:48:26 +00:00
|
|
|
boundFD, remotePort := dut.CreateBoundSocket(unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.ParseIP(addr))
|
|
|
|
defer dut.Close(boundFD)
|
2020-05-08 22:38:42 +00:00
|
|
|
|
2020-07-14 18:48:26 +00:00
|
|
|
var conn udpConn
|
|
|
|
var localAddr unix.Sockaddr
|
|
|
|
if isIPv4 {
|
|
|
|
v4Conn := testbench.NewUDPIPv4(t, testbench.UDP{DstPort: &remotePort}, testbench.UDP{SrcPort: &remotePort})
|
|
|
|
localAddr = v4Conn.LocalAddr()
|
|
|
|
conn = &v4Conn
|
|
|
|
} else {
|
|
|
|
v6Conn := testbench.NewUDPIPv6(t, testbench.UDP{DstPort: &remotePort}, testbench.UDP{SrcPort: &remotePort})
|
|
|
|
localAddr = v6Conn.LocalAddr()
|
|
|
|
conn = &v6Conn
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2020-05-08 22:38:42 +00:00
|
|
|
|
2020-07-14 18:48:26 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
payload []byte
|
|
|
|
}{
|
|
|
|
{"emptypayload", nil},
|
|
|
|
{"small payload", []byte("hello world")},
|
|
|
|
{"1kPayload", testbench.GenerateRandomPayload(t, 1<<10)},
|
|
|
|
// Even though UDP allows larger dgrams we don't test it here as
|
|
|
|
// they need to be fragmented and written out as individual
|
|
|
|
// frames.
|
2020-05-08 22:38:42 +00:00
|
|
|
}
|
2020-07-14 18:48:26 +00:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Run("Send", func(t *testing.T) {
|
|
|
|
conn.Send(testbench.UDP{}, &testbench.Payload{Bytes: tc.payload})
|
|
|
|
if got, want := string(dut.Recv(boundFD, int32(len(tc.payload)), 0)), string(tc.payload); got != want {
|
|
|
|
t.Fatalf("received payload does not match sent payload got: %s, want: %s", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Recv", func(t *testing.T) {
|
|
|
|
conn.Drain()
|
|
|
|
if got, want := int(dut.SendTo(boundFD, tc.payload, 0, localAddr)), len(tc.payload); got != want {
|
|
|
|
t.Fatalf("short write got: %d, want: %d", got, want)
|
|
|
|
}
|
|
|
|
if _, err := conn.ExpectData(testbench.UDP{SrcPort: &remotePort}, testbench.Payload{Bytes: tc.payload}, time.Second); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2020-05-08 22:38:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|