github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/common/net/packetaddr/packetaddr_test.go (about)

     1  package packetaddr
     2  
     3  import (
     4  	sysnet "net"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/v2fly/v2ray-core/v5/common/buf"
    10  )
    11  
    12  func TestPacketEncodingIPv4(t *testing.T) {
    13  	packetAddress := &sysnet.UDPAddr{
    14  		IP:   sysnet.IPv4(1, 2, 3, 4).To4(),
    15  		Port: 1234,
    16  	}
    17  	var packetData [256]byte
    18  	wrapped, err := AttachAddressToPacket(buf.FromBytes(packetData[:]), packetAddress)
    19  	assert.NoError(t, err)
    20  
    21  	packetPayload, decodedAddress, err := ExtractAddressFromPacket(wrapped)
    22  	assert.NoError(t, err)
    23  
    24  	assert.Equal(t, packetPayload.Bytes(), packetData[:])
    25  	assert.Equal(t, packetAddress, decodedAddress)
    26  }
    27  
    28  func TestPacketEncodingIPv6(t *testing.T) {
    29  	packetAddress := &sysnet.UDPAddr{
    30  		IP:   sysnet.IPv6linklocalallrouters,
    31  		Port: 1234,
    32  	}
    33  	var packetData [256]byte
    34  	wrapped, err := AttachAddressToPacket(buf.FromBytes(packetData[:]), packetAddress)
    35  	assert.NoError(t, err)
    36  
    37  	packetPayload, decodedAddress, err := ExtractAddressFromPacket(wrapped)
    38  	assert.NoError(t, err)
    39  
    40  	assert.Equal(t, packetPayload.Bytes(), packetData[:])
    41  	assert.Equal(t, packetAddress, decodedAddress)
    42  }