github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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 "github.com/SagerNet/gvisor/pkg/tcpip" 26 "github.com/SagerNet/gvisor/test/packetimpact/testbench" 27 ) 28 29 var oneSecond = unix.Timeval{Sec: 1, Usec: 0} 30 31 func init() { 32 testbench.Initialize(flag.CommandLine) 33 } 34 35 func TestDiscardsUDPPacketsWithMcastSourceAddressV4(t *testing.T) { 36 dut := testbench.NewDUT(t) 37 remoteFD, remotePort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, dut.Net.RemoteIPv4) 38 defer dut.Close(t, remoteFD) 39 dut.SetSockOptTimeval(t, remoteFD, unix.SOL_SOCKET, unix.SO_RCVTIMEO, &oneSecond) 40 conn := dut.Net.NewUDPIPv4(t, testbench.UDP{DstPort: &remotePort}, testbench.UDP{SrcPort: &remotePort}) 41 defer conn.Close(t) 42 43 for _, mcastAddr := range []net.IP{ 44 net.IPv4allsys, 45 net.IPv4allrouter, 46 net.IPv4(224, 0, 1, 42), 47 net.IPv4(232, 1, 2, 3), 48 } { 49 t.Run(fmt.Sprintf("srcaddr=%s", mcastAddr), func(t *testing.T) { 50 conn.SendIP( 51 t, 52 testbench.IPv4{SrcAddr: testbench.Address(tcpip.Address(mcastAddr.To4()))}, 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 conn.SendIPv6( 82 t, 83 testbench.IPv6{SrcAddr: testbench.Address(tcpip.Address(mcastAddr.To16()))}, 84 testbench.UDP{}, 85 &testbench.Payload{Bytes: []byte("test payload")}, 86 ) 87 ret, payload, errno := dut.RecvWithErrno(context.Background(), t, remoteFD, 100, 0) 88 if errno != unix.EAGAIN || errno != unix.EWOULDBLOCK { 89 t.Errorf("Recv got unexpected result, ret=%d, payload=%q, errno=%s", ret, payload, errno) 90 } 91 }) 92 } 93 }