gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/packetimpact/tests/udp_discard_mcast_source_addr_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 udp_discard_mcast_source_addr_test
    16  
    17  import (
    18  	"context"
    19  	"flag"
    20  	"fmt"
    21  	"net"
    22  	"testing"
    23  
    24  	"golang.org/x/sys/unix"
    25  	"gvisor.dev/gvisor/test/packetimpact/testbench"
    26  )
    27  
    28  var oneSecond = unix.Timeval{Sec: 1, Usec: 0}
    29  
    30  func init() {
    31  	testbench.Initialize(flag.CommandLine)
    32  }
    33  
    34  func TestDiscardsUDPPacketsWithMcastSourceAddressV4(t *testing.T) {
    35  	dut := testbench.NewDUT(t)
    36  	remoteFD, remotePort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, dut.Net.RemoteIPv4)
    37  	defer dut.Close(t, remoteFD)
    38  	dut.SetSockOptTimeval(t, remoteFD, unix.SOL_SOCKET, unix.SO_RCVTIMEO, &oneSecond)
    39  	conn := dut.Net.NewUDPIPv4(t, testbench.UDP{DstPort: &remotePort}, testbench.UDP{SrcPort: &remotePort})
    40  	defer conn.Close(t)
    41  
    42  	for _, mcastAddr := range []net.IP{
    43  		net.IPv4allsys,
    44  		net.IPv4allrouter,
    45  		net.IPv4(224, 0, 1, 42),
    46  		net.IPv4(232, 1, 2, 3),
    47  	} {
    48  		t.Run(fmt.Sprintf("srcaddr=%s", mcastAddr), func(t *testing.T) {
    49  			addr := mcastAddr.To4()
    50  			conn.SendIP(
    51  				t,
    52  				testbench.IPv4{SrcAddr: &addr},
    53  				testbench.UDP{},
    54  				&testbench.Payload{Bytes: []byte("test payload")},
    55  			)
    56  
    57  			ret, payload, errno := dut.RecvWithErrno(context.Background(), t, remoteFD, 100, 0)
    58  			if errno != unix.EAGAIN || errno != unix.EWOULDBLOCK {
    59  				t.Errorf("Recv got unexpected result, ret=%d, payload=%q, errno=%s", ret, payload, errno)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestDiscardsUDPPacketsWithMcastSourceAddressV6(t *testing.T) {
    66  	dut := testbench.NewDUT(t)
    67  	remoteFD, remotePort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, dut.Net.RemoteIPv6)
    68  	defer dut.Close(t, remoteFD)
    69  	dut.SetSockOptTimeval(t, remoteFD, unix.SOL_SOCKET, unix.SO_RCVTIMEO, &oneSecond)
    70  	conn := dut.Net.NewUDPIPv6(t, testbench.UDP{DstPort: &remotePort}, testbench.UDP{SrcPort: &remotePort})
    71  	defer conn.Close(t)
    72  
    73  	for _, mcastAddr := range []net.IP{
    74  		net.IPv6interfacelocalallnodes,
    75  		net.IPv6linklocalallnodes,
    76  		net.IPv6linklocalallrouters,
    77  		net.ParseIP("ff01::42"),
    78  		net.ParseIP("ff02::4242"),
    79  	} {
    80  		t.Run(fmt.Sprintf("srcaddr=%s", mcastAddr), func(t *testing.T) {
    81  			addr := mcastAddr.To16()
    82  			conn.SendIPv6(
    83  				t,
    84  				testbench.IPv6{SrcAddr: &addr},
    85  				testbench.UDP{},
    86  				&testbench.Payload{Bytes: []byte("test payload")},
    87  			)
    88  			ret, payload, errno := dut.RecvWithErrno(context.Background(), t, remoteFD, 100, 0)
    89  			if errno != unix.EAGAIN || errno != unix.EWOULDBLOCK {
    90  				t.Errorf("Recv got unexpected result, ret=%d, payload=%q, errno=%s", ret, payload, errno)
    91  			}
    92  		})
    93  	}
    94  }