github.com/slackhq/nebula@v1.9.0/iputil/packet_test.go (about)

     1  package iputil
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"golang.org/x/net/ipv4"
     9  )
    10  
    11  func Test_CreateRejectPacket(t *testing.T) {
    12  	h := ipv4.Header{
    13  		Len:      20,
    14  		Src:      net.IPv4(10, 0, 0, 1),
    15  		Dst:      net.IPv4(10, 0, 0, 2),
    16  		Protocol: 1, // ICMP
    17  	}
    18  
    19  	b, err := h.Marshal()
    20  	if err != nil {
    21  		t.Fatalf("h.Marhshal: %v", err)
    22  	}
    23  	b = append(b, []byte{0, 3, 0, 4}...)
    24  
    25  	expectedLen := ipv4.HeaderLen + 8 + h.Len + 4
    26  	out := make([]byte, expectedLen)
    27  	rejectPacket := CreateRejectPacket(b, out)
    28  	assert.NotNil(t, rejectPacket)
    29  	assert.Len(t, rejectPacket, expectedLen)
    30  
    31  	// ICMP with max header len
    32  	h = ipv4.Header{
    33  		Len:      60,
    34  		Src:      net.IPv4(10, 0, 0, 1),
    35  		Dst:      net.IPv4(10, 0, 0, 2),
    36  		Protocol: 1, // ICMP
    37  		Options:  make([]byte, 40),
    38  	}
    39  
    40  	b, err = h.Marshal()
    41  	if err != nil {
    42  		t.Fatalf("h.Marhshal: %v", err)
    43  	}
    44  	b = append(b, []byte{0, 3, 0, 4, 0, 0, 0, 0}...)
    45  
    46  	expectedLen = MaxRejectPacketSize
    47  	out = make([]byte, MaxRejectPacketSize)
    48  	rejectPacket = CreateRejectPacket(b, out)
    49  	assert.NotNil(t, rejectPacket)
    50  	assert.Len(t, rejectPacket, expectedLen)
    51  
    52  	// TCP with max header len
    53  	h = ipv4.Header{
    54  		Len:      60,
    55  		Src:      net.IPv4(10, 0, 0, 1),
    56  		Dst:      net.IPv4(10, 0, 0, 2),
    57  		Protocol: 6, // TCP
    58  		Options:  make([]byte, 40),
    59  	}
    60  
    61  	b, err = h.Marshal()
    62  	if err != nil {
    63  		t.Fatalf("h.Marhshal: %v", err)
    64  	}
    65  	b = append(b, []byte{0, 3, 0, 4}...)
    66  	b = append(b, make([]byte, 16)...)
    67  
    68  	expectedLen = ipv4.HeaderLen + 20
    69  	out = make([]byte, expectedLen)
    70  	rejectPacket = CreateRejectPacket(b, out)
    71  	assert.NotNil(t, rejectPacket)
    72  	assert.Len(t, rejectPacket, expectedLen)
    73  }