github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/packetimpact/tests/udp_any_addr_recv_unicast_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_any_addr_recv_unicast_test 16 17 import ( 18 "flag" 19 "net" 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 "golang.org/x/sys/unix" 24 "github.com/SagerNet/gvisor/pkg/tcpip" 25 "github.com/SagerNet/gvisor/test/packetimpact/testbench" 26 ) 27 28 func init() { 29 testbench.Initialize(flag.CommandLine) 30 } 31 32 func TestAnyRecvUnicastUDP(t *testing.T) { 33 dut := testbench.NewDUT(t) 34 boundFD, remotePort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.IPv4zero) 35 defer dut.Close(t, boundFD) 36 conn := dut.Net.NewUDPIPv4(t, testbench.UDP{DstPort: &remotePort}, testbench.UDP{SrcPort: &remotePort}) 37 defer conn.Close(t) 38 39 payload := testbench.GenerateRandomPayload(t, 1<<10 /* 1 KiB */) 40 conn.SendIP( 41 t, 42 testbench.IPv4{DstAddr: testbench.Address(tcpip.Address(dut.Net.RemoteIPv4))}, 43 testbench.UDP{}, 44 &testbench.Payload{Bytes: payload}, 45 ) 46 got, want := dut.Recv(t, boundFD, int32(len(payload)+1), 0), payload 47 if diff := cmp.Diff(want, got); diff != "" { 48 t.Errorf("received payload does not match sent payload, diff (-want, +got):\n%s", diff) 49 } 50 }