net_hdr_test.go 795 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package virtio
  2. import (
  3. "testing"
  4. "unsafe"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "golang.org/x/sys/unix"
  8. )
  9. func TestNetHdr_Size(t *testing.T) {
  10. assert.EqualValues(t, NetHdrSize, unsafe.Sizeof(NetHdr{}))
  11. }
  12. func TestNetHdr_Encoding(t *testing.T) {
  13. vnethdr := NetHdr{
  14. Flags: unix.VIRTIO_NET_HDR_F_NEEDS_CSUM,
  15. GSOType: unix.VIRTIO_NET_HDR_GSO_UDP_L4,
  16. HdrLen: 42,
  17. GSOSize: 1472,
  18. CsumStart: 34,
  19. CsumOffset: 6,
  20. NumBuffers: 16,
  21. }
  22. buf := make([]byte, NetHdrSize)
  23. require.NoError(t, vnethdr.Encode(buf))
  24. assert.Equal(t, []byte{
  25. 0x01, 0x05,
  26. 0x2a, 0x00,
  27. 0xc0, 0x05,
  28. 0x22, 0x00,
  29. 0x06, 0x00,
  30. 0x10, 0x00,
  31. }, buf)
  32. var decoded NetHdr
  33. require.NoError(t, decoded.Decode(buf))
  34. assert.Equal(t, vnethdr, decoded)
  35. }