2021-11-04 17:58:59 -04:00
|
|
|
package packetaddr
|
|
|
|
|
|
|
|
import (
|
|
|
|
sysnet "net"
|
|
|
|
"testing"
|
2021-12-31 16:22:16 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-01-02 11:19:52 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common/buf"
|
2021-11-04 17:58:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPacketEncodingIPv4(t *testing.T) {
|
|
|
|
packetAddress := &sysnet.UDPAddr{
|
|
|
|
IP: sysnet.IPv4(1, 2, 3, 4).To4(),
|
|
|
|
Port: 1234,
|
|
|
|
}
|
|
|
|
var packetData [256]byte
|
2021-12-30 10:12:32 -05:00
|
|
|
wrapped, err := AttachAddressToPacket(buf.FromBytes(packetData[:]), packetAddress)
|
|
|
|
assert.NoError(t, err)
|
2021-11-04 17:58:59 -04:00
|
|
|
|
2021-12-30 10:12:32 -05:00
|
|
|
packetPayload, decodedAddress, err := ExtractAddressFromPacket(wrapped)
|
|
|
|
assert.NoError(t, err)
|
2021-11-04 17:58:59 -04:00
|
|
|
|
2021-12-30 10:12:32 -05:00
|
|
|
assert.Equal(t, packetPayload.Bytes(), packetData[:])
|
2021-11-04 17:58:59 -04:00
|
|
|
assert.Equal(t, packetAddress, decodedAddress)
|
|
|
|
}
|
2021-11-20 12:53:37 -05:00
|
|
|
|
|
|
|
func TestPacketEncodingIPv6(t *testing.T) {
|
|
|
|
packetAddress := &sysnet.UDPAddr{
|
|
|
|
IP: sysnet.IPv6linklocalallrouters,
|
|
|
|
Port: 1234,
|
|
|
|
}
|
|
|
|
var packetData [256]byte
|
2021-12-30 10:12:32 -05:00
|
|
|
wrapped, err := AttachAddressToPacket(buf.FromBytes(packetData[:]), packetAddress)
|
|
|
|
assert.NoError(t, err)
|
2021-11-20 12:53:37 -05:00
|
|
|
|
2021-12-30 10:12:32 -05:00
|
|
|
packetPayload, decodedAddress, err := ExtractAddressFromPacket(wrapped)
|
|
|
|
assert.NoError(t, err)
|
2021-11-20 12:53:37 -05:00
|
|
|
|
2021-12-30 10:12:32 -05:00
|
|
|
assert.Equal(t, packetPayload.Bytes(), packetData[:])
|
2021-11-20 12:53:37 -05:00
|
|
|
assert.Equal(t, packetAddress, decodedAddress)
|
|
|
|
}
|