github.com/vishvananda/netlink@v1.3.0/fou_test.go (about)

     1  // +build linux
     2  
     3  package netlink
     4  
     5  import (
     6  	"testing"
     7  )
     8  
     9  func TestFouDeserializeMsg(t *testing.T) {
    10  	var msg []byte
    11  
    12  	// deserialize a valid message
    13  	msg = []byte{3, 1, 0, 0, 5, 0, 2, 0, 2, 0, 0, 0, 6, 0, 1, 0, 21, 179, 0, 0, 5, 0, 3, 0, 4, 0, 0, 0, 5, 0, 4, 0, 1, 0, 0, 0}
    14  	if fou, err := deserializeFouMsg(msg); err != nil {
    15  		t.Error(err.Error())
    16  	} else {
    17  
    18  		// check if message was deserialized correctly
    19  		if fou.Family != FAMILY_V4 {
    20  			t.Errorf("expected family %d, got %d", FAMILY_V4, fou.Family)
    21  		}
    22  
    23  		if fou.Port != 5555 {
    24  			t.Errorf("expected port 5555, got %d", fou.Port)
    25  		}
    26  
    27  		if fou.Protocol != 4 { // ipip
    28  			t.Errorf("expected protocol 4, got %d", fou.Protocol)
    29  		}
    30  
    31  		if fou.EncapType != FOU_ENCAP_DIRECT {
    32  			t.Errorf("expected encap type %d, got %d", FOU_ENCAP_DIRECT, fou.EncapType)
    33  		}
    34  	}
    35  
    36  	// deserialize truncated attribute header
    37  	msg = []byte{3, 1, 0, 0, 5, 0}
    38  	if _, err := deserializeFouMsg(msg); err == nil {
    39  		t.Error("expected attribute header truncated error")
    40  	} else if err != ErrAttrHeaderTruncated {
    41  		t.Errorf("unexpected error: %s", err.Error())
    42  	}
    43  
    44  	// deserialize truncated attribute header
    45  	msg = []byte{3, 1, 0, 0, 5, 0, 2, 0, 2, 0, 0}
    46  	if _, err := deserializeFouMsg(msg); err == nil {
    47  		t.Error("expected attribute body truncated error")
    48  	} else if err != ErrAttrBodyTruncated {
    49  		t.Errorf("unexpected error: %s", err.Error())
    50  	}
    51  }
    52  
    53  func TestFouAddDel(t *testing.T) {
    54  	// foo-over-udp was merged in 3.18 so skip these tests if the kernel is too old
    55  	minKernelRequired(t, 3, 18)
    56  
    57  	// the fou module is usually not compiled in the kernel so we'll load it
    58  	tearDown := setUpNetlinkTestWithKModule(t, "fou")
    59  	defer tearDown()
    60  
    61  	fou := Fou{
    62  		Port:      5555,
    63  		Family:    FAMILY_V4,
    64  		Protocol:  4, // ipip
    65  		EncapType: FOU_ENCAP_DIRECT,
    66  	}
    67  
    68  	if err := FouAdd(fou); err != nil {
    69  		t.Fatal(err)
    70  	}
    71  
    72  	list, err := FouList(FAMILY_V4)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	if len(list) != 1 {
    78  		t.Fatalf("expected 1 fou, got %d", len(list))
    79  	}
    80  
    81  	if list[0].Port != fou.Port {
    82  		t.Errorf("expected port %d, got %d", fou.Port, list[0].Port)
    83  	}
    84  
    85  	if list[0].Family != fou.Family {
    86  		t.Errorf("expected family %d, got %d", fou.Family, list[0].Family)
    87  	}
    88  
    89  	if list[0].Protocol != fou.Protocol {
    90  		t.Errorf("expected protocol %d, got %d", fou.Protocol, list[0].Protocol)
    91  	}
    92  
    93  	if list[0].EncapType != fou.EncapType {
    94  		t.Errorf("expected encaptype %d, got %d", fou.EncapType, list[0].EncapType)
    95  	}
    96  
    97  	if err := FouDel(Fou{Port: fou.Port, Family: fou.Family}); err != nil {
    98  		t.Fatal(err)
    99  	}
   100  
   101  	list, err = FouList(FAMILY_V4)
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  
   106  	if len(list) != 0 {
   107  		t.Fatalf("expected 0 fou, got %d", len(list))
   108  	}
   109  }