Test UDP socket bound to ANY can receive unicast

PiperOrigin-RevId: 323773771
This commit is contained in:
Jay Zhuang
2020-07-29 06:23:23 -07:00
committed by gVisor bot
parent f82dd8ddb4
commit a6b4538ae0
5 changed files with 81 additions and 13 deletions
+13
View File
@@ -46,6 +46,18 @@ packetimpact_go_test(
"//pkg/tcpip",
"//pkg/tcpip/header",
"//test/packetimpact/testbench",
"@com_github_google_go_cmp//cmp:go_default_library",
"@org_golang_x_sys//unix:go_default_library",
],
)
packetimpact_go_test(
name = "udp_any_addr_recv_unicast",
srcs = ["udp_any_addr_recv_unicast_test.go"],
deps = [
"//pkg/tcpip",
"//test/packetimpact/testbench",
"@com_github_google_go_cmp//cmp:go_default_library",
"@org_golang_x_sys//unix:go_default_library",
],
)
@@ -294,6 +306,7 @@ packetimpact_go_test(
srcs = ["udp_send_recv_dgram_test.go"],
deps = [
"//test/packetimpact/testbench",
"@com_github_google_go_cmp//cmp:go_default_library",
"@org_golang_x_sys//unix:go_default_library",
],
)
@@ -0,0 +1,51 @@
// 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_any_addr_recv_unicast_test
import (
"flag"
"net"
"testing"
"github.com/google/go-cmp/cmp"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/test/packetimpact/testbench"
)
func init() {
testbench.RegisterFlags(flag.CommandLine)
}
func TestAnyRecvUnicastUDP(t *testing.T) {
dut := testbench.NewDUT(t)
defer dut.TearDown()
boundFD, remotePort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.IPv4zero)
defer dut.Close(t, boundFD)
conn := testbench.NewUDPIPv4(t, testbench.UDP{DstPort: &remotePort}, testbench.UDP{SrcPort: &remotePort})
defer conn.Close(t)
payload := testbench.GenerateRandomPayload(t, 1<<10 /* 1 KiB */)
conn.SendIP(
t,
testbench.IPv4{DstAddr: testbench.Address(tcpip.Address(net.ParseIP(testbench.RemoteIPv4).To4()))},
testbench.UDP{},
&testbench.Payload{Bytes: payload},
)
got, want := dut.Recv(t, boundFD, int32(len(payload)+1), 0), payload
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("received payload does not match sent payload, diff (-want, +got):\n%s", diff)
}
}
@@ -227,12 +227,12 @@ func TestUDPICMPErrorPropagation(t *testing.T) {
dut := testbench.NewDUT(t)
defer dut.TearDown()
remoteFD, remotePort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.ParseIP("0.0.0.0"))
remoteFD, remotePort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.IPv4zero)
defer dut.Close(t, remoteFD)
// Create a second, clean socket on the DUT to ensure that the ICMP
// error messages only affect the sockets they are intended for.
cleanFD, cleanPort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.ParseIP("0.0.0.0"))
cleanFD, cleanPort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.IPv4zero)
defer dut.Close(t, cleanFD)
conn := testbench.NewUDPIPv4(t, testbench.UDP{DstPort: &remotePort}, testbench.UDP{SrcPort: &remotePort})
@@ -281,12 +281,12 @@ func TestICMPErrorDuringUDPRecv(t *testing.T) {
dut := testbench.NewDUT(t)
defer dut.TearDown()
remoteFD, remotePort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.ParseIP("0.0.0.0"))
remoteFD, remotePort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.IPv4zero)
defer dut.Close(t, remoteFD)
// Create a second, clean socket on the DUT to ensure that the ICMP
// error messages only affect the sockets they are intended for.
cleanFD, cleanPort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.ParseIP("0.0.0.0"))
cleanFD, cleanPort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.IPv4zero)
defer dut.Close(t, cleanFD)
conn := testbench.NewUDPIPv4(t, testbench.UDP{DstPort: &remotePort}, testbench.UDP{SrcPort: &remotePort})
@@ -22,6 +22,7 @@ import (
"syscall"
"testing"
"github.com/google/go-cmp/cmp"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/test/packetimpact/testbench"
@@ -37,9 +38,9 @@ func TestUDPRecvMcastBcast(t *testing.T) {
for _, v := range []struct {
bound, to net.IP
}{
{bound: net.IPv4(0, 0, 0, 0), to: subnetBcastAddr},
{bound: net.IPv4(0, 0, 0, 0), to: net.IPv4bcast},
{bound: net.IPv4(0, 0, 0, 0), to: net.IPv4allsys},
{bound: net.IPv4zero, to: subnetBcastAddr},
{bound: net.IPv4zero, to: net.IPv4bcast},
{bound: net.IPv4zero, to: net.IPv4allsys},
{bound: subnetBcastAddr, to: subnetBcastAddr},
{bound: subnetBcastAddr, to: net.IPv4bcast},
@@ -55,15 +56,16 @@ func TestUDPRecvMcastBcast(t *testing.T) {
conn := testbench.NewUDPIPv4(t, testbench.UDP{DstPort: &remotePort}, testbench.UDP{SrcPort: &remotePort})
defer conn.Close(t)
payload := testbench.GenerateRandomPayload(t, 1<<10)
payload := testbench.GenerateRandomPayload(t, 1<<10 /* 1 KiB */)
conn.SendIP(
t,
testbench.IPv4{DstAddr: testbench.Address(tcpip.Address(v.to.To4()))},
testbench.UDP{},
&testbench.Payload{Bytes: payload},
)
if got, want := string(dut.Recv(t, boundFD, int32(len(payload)), 0)), string(payload); got != want {
t.Errorf("received payload does not match sent payload got: %s, want: %s", got, want)
got, want := dut.Recv(t, boundFD, int32(len(payload)+1), 0), payload
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("received payload does not match sent payload, diff (-want, +got):\n%s", diff)
}
})
}
@@ -84,7 +86,7 @@ func TestUDPDoesntRecvMcastBcastOnUnicastAddr(t *testing.T) {
net.IPv4(224, 0, 0, 1),
} {
t.Run(fmt.Sprint("to=%s", to), func(t *testing.T) {
payload := testbench.GenerateRandomPayload(t, 1<<10)
payload := testbench.GenerateRandomPayload(t, 1<<10 /* 1 KiB */)
conn.SendIP(
t,
testbench.IPv4{DstAddr: testbench.Address(tcpip.Address(to.To4()))},
@@ -20,6 +20,7 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/test/packetimpact/testbench"
)
@@ -82,8 +83,9 @@ func TestUDP(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Run("Send", func(t *testing.T) {
conn.Send(t, testbench.UDP{}, &testbench.Payload{Bytes: tc.payload})
if got, want := string(dut.Recv(t, 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)
got, want := dut.Recv(t, boundFD, int32(len(tc.payload)+1), 0), tc.payload
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("received payload does not match sent payload, diff (-want, +got):\n%s", diff)
}
})
t.Run("Recv", func(t *testing.T) {