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

     1  //go:build linux
     2  // +build linux
     3  
     4  package netlink
     5  
     6  import (
     7  	"net"
     8  	"testing"
     9  )
    10  
    11  func TestFouDeserializeMsg(t *testing.T) {
    12  	var msg []byte
    13  
    14  	// deserialize a valid message
    15  	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}
    16  	if fou, err := deserializeFouMsg(msg); err != nil {
    17  		t.Error(err.Error())
    18  	} else {
    19  
    20  		// check if message was deserialized correctly
    21  		if fou.Family != FAMILY_V4 {
    22  			t.Errorf("expected family %d, got %d", FAMILY_V4, fou.Family)
    23  		}
    24  
    25  		if fou.Port != 5555 {
    26  			t.Errorf("expected port 5555, got %d", fou.Port)
    27  		}
    28  
    29  		if fou.Protocol != 4 { // ipip
    30  			t.Errorf("expected protocol 4, got %d", fou.Protocol)
    31  		}
    32  
    33  		if fou.EncapType != FOU_ENCAP_DIRECT {
    34  			t.Errorf("expected encap type %d, got %d", FOU_ENCAP_DIRECT, fou.EncapType)
    35  		}
    36  	}
    37  
    38  	// deserialize a valid message(kernel >= 5.2)
    39  	msg = []byte{3, 1, 0, 0, 5, 0, 2, 0, 2, 0, 0, 0, 6, 0, 1, 0, 43, 103, 0, 0, 6, 0, 10, 0, 86, 206, 0, 0, 5, 0, 3, 0, 0, 0, 0, 0, 5, 0, 4, 0, 2, 0, 0, 0, 8, 0, 11, 0, 0, 0, 0, 0, 8, 0, 6, 0, 1, 2, 3, 4, 8, 0, 8, 0, 5, 6, 7, 8}
    40  	if fou, err := deserializeFouMsg(msg); err != nil {
    41  		t.Error(err.Error())
    42  	} else {
    43  		if fou.Family != FAMILY_V4 {
    44  			t.Errorf("expected family %d, got %d", FAMILY_V4, fou.Family)
    45  		}
    46  
    47  		if fou.Port != 11111 {
    48  			t.Errorf("expected port 5555, got %d", fou.Port)
    49  		}
    50  
    51  		if fou.Protocol != 0 { // gue
    52  			t.Errorf("expected protocol 0, got %d", fou.Protocol)
    53  		}
    54  
    55  		if fou.IfIndex != 0 {
    56  			t.Errorf("expected ifindex 0, got %d", fou.Protocol)
    57  		}
    58  
    59  		if fou.EncapType != FOU_ENCAP_GUE {
    60  			t.Errorf("expected encap type %d, got %d", FOU_ENCAP_GUE, fou.EncapType)
    61  		}
    62  
    63  		if expected := net.IPv4(1, 2, 3, 4); !fou.Local.Equal(expected) {
    64  			t.Errorf("expected local %v, got %v", expected, fou.Local)
    65  		}
    66  
    67  		if expected := net.IPv4(5, 6, 7, 8); !fou.Peer.Equal(expected) {
    68  			t.Errorf("expected peer %v, got %v", expected, fou.Peer)
    69  		}
    70  
    71  		if fou.PeerPort != 22222 {
    72  			t.Errorf("expected peer port 0, got %d", fou.PeerPort)
    73  		}
    74  	}
    75  
    76  	// unknown attribute should be skipped
    77  	msg = []byte{3, 1, 0, 0, 5, 0, 112, 0, 2, 0, 0, 0, 5, 0, 2, 0, 2, 0, 0}
    78  	if fou, err := deserializeFouMsg(msg); err != nil {
    79  		t.Errorf("unexpected error: %s", err.Error())
    80  	} else if fou.Family != 2 {
    81  		t.Errorf("expected family 2, got %d", fou.Family)
    82  	}
    83  }
    84  
    85  func TestFouAddDel(t *testing.T) {
    86  	// foo-over-udp was merged in 3.18 so skip these tests if the kernel is too old
    87  	minKernelRequired(t, 3, 18)
    88  
    89  	// the fou module is usually not compiled in the kernel so we'll load it
    90  	tearDown := setUpNetlinkTestWithKModule(t, "fou")
    91  	defer tearDown()
    92  
    93  	fou := Fou{
    94  		Port:      5555,
    95  		Family:    FAMILY_V4,
    96  		Protocol:  4, // ipip
    97  		EncapType: FOU_ENCAP_DIRECT,
    98  	}
    99  
   100  	if err := FouAdd(fou); err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	list, err := FouList(FAMILY_V4)
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  
   109  	if len(list) != 1 {
   110  		t.Fatalf("expected 1 fou, got %d", len(list))
   111  	}
   112  
   113  	if list[0].Port != fou.Port {
   114  		t.Errorf("expected port %d, got %d", fou.Port, list[0].Port)
   115  	}
   116  
   117  	if list[0].Family != fou.Family {
   118  		t.Errorf("expected family %d, got %d", fou.Family, list[0].Family)
   119  	}
   120  
   121  	if list[0].Protocol != fou.Protocol {
   122  		t.Errorf("expected protocol %d, got %d", fou.Protocol, list[0].Protocol)
   123  	}
   124  
   125  	if list[0].EncapType != fou.EncapType {
   126  		t.Errorf("expected encaptype %d, got %d", fou.EncapType, list[0].EncapType)
   127  	}
   128  
   129  	if err := FouDel(Fou{Port: fou.Port, Family: fou.Family}); err != nil {
   130  		t.Fatal(err)
   131  	}
   132  
   133  	list, err = FouList(FAMILY_V4)
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  
   138  	if len(list) != 0 {
   139  		t.Fatalf("expected 0 fou, got %d", len(list))
   140  	}
   141  }