gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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  	"gvisor.dev/gvisor/test/packetimpact/testbench"
    25  )
    26  
    27  func init() {
    28  	testbench.Initialize(flag.CommandLine)
    29  }
    30  
    31  func TestAnyRecvUnicastUDP(t *testing.T) {
    32  	dut := testbench.NewDUT(t)
    33  	boundFD, remotePort := dut.CreateBoundSocket(t, unix.SOCK_DGRAM, unix.IPPROTO_UDP, net.IPv4zero)
    34  	defer dut.Close(t, boundFD)
    35  	conn := dut.Net.NewUDPIPv4(t, testbench.UDP{DstPort: &remotePort}, testbench.UDP{SrcPort: &remotePort})
    36  	defer conn.Close(t)
    37  
    38  	payload := testbench.GenerateRandomPayload(t, 1<<10 /* 1 KiB */)
    39  	conn.SendIP(
    40  		t,
    41  		testbench.IPv4{DstAddr: &dut.Net.RemoteIPv4},
    42  		testbench.UDP{},
    43  		&testbench.Payload{Bytes: payload},
    44  	)
    45  	got, want := dut.Recv(t, boundFD, int32(len(payload)+1), 0), payload
    46  	if diff := cmp.Diff(want, got); diff != "" {
    47  		t.Errorf("received payload does not match sent payload, diff (-want, +got):\n%s", diff)
    48  	}
    49  }