github.com/sagernet/netlink@v0.0.0-20240612041022-b9a21c07ac6a/protinfo_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package netlink
     5  
     6  import (
     7  	"testing"
     8  )
     9  
    10  func TestProtinfo(t *testing.T) {
    11  	tearDown := setUpNetlinkTest(t)
    12  	defer tearDown()
    13  	master := &Bridge{LinkAttrs: LinkAttrs{Name: "foo"}}
    14  	if err := LinkAdd(master); err != nil {
    15  		t.Fatal(err)
    16  	}
    17  	iface1 := &Dummy{LinkAttrs{Name: "bar1", MasterIndex: master.Index}}
    18  	iface2 := &Dummy{LinkAttrs{Name: "bar2", MasterIndex: master.Index}}
    19  	iface3 := &Dummy{LinkAttrs{Name: "bar3"}}
    20  	iface4 := &Dummy{LinkAttrs{Name: "bar4", MasterIndex: master.Index}}
    21  
    22  	if err := LinkAdd(iface1); err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	if err := LinkAdd(iface2); err != nil {
    26  		t.Fatal(err)
    27  	}
    28  	if err := LinkAdd(iface3); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	if err := LinkAdd(iface4); err != nil {
    32  		t.Fatal(err)
    33  	}
    34  
    35  	oldpi1, err := LinkGetProtinfo(iface1)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	oldpi2, err := LinkGetProtinfo(iface2)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	oldpi4, err := LinkGetProtinfo(iface4)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	if err := LinkSetHairpin(iface1, true); err != nil {
    49  		t.Fatal(err)
    50  	}
    51  
    52  	if err := LinkSetRootBlock(iface1, true); err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	pi1, err := LinkGetProtinfo(iface1)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if !pi1.Hairpin {
    61  		t.Fatalf("Hairpin mode is not enabled for %s, but should", iface1.Name)
    62  	}
    63  	if !pi1.RootBlock {
    64  		t.Fatalf("RootBlock is not enabled for %s, but should", iface1.Name)
    65  	}
    66  	if pi1.ProxyArp != oldpi1.ProxyArp {
    67  		t.Fatalf("ProxyArp field was changed for %s but shouldn't", iface1.Name)
    68  	}
    69  	if pi1.ProxyArpWiFi != oldpi1.ProxyArp {
    70  		t.Fatalf("ProxyArpWiFi ProxyArp field was changed for %s but shouldn't", iface1.Name)
    71  	}
    72  	if pi1.Guard != oldpi1.Guard {
    73  		t.Fatalf("Guard field was changed for %s but shouldn't", iface1.Name)
    74  	}
    75  	if pi1.FastLeave != oldpi1.FastLeave {
    76  		t.Fatalf("FastLeave field was changed for %s but shouldn't", iface1.Name)
    77  	}
    78  	if pi1.Learning != oldpi1.Learning {
    79  		t.Fatalf("Learning field was changed for %s but shouldn't", iface1.Name)
    80  	}
    81  	if pi1.Flood != oldpi1.Flood {
    82  		t.Fatalf("Flood field was changed for %s but shouldn't", iface1.Name)
    83  	}
    84  
    85  	if err := LinkSetGuard(iface2, true); err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	if err := LinkSetLearning(iface2, false); err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	pi2, err := LinkGetProtinfo(iface2)
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  	if pi2.Hairpin {
    96  		t.Fatalf("Hairpin mode is enabled for %s, but shouldn't", iface2.Name)
    97  	}
    98  	if !pi2.Guard {
    99  		t.Fatalf("Guard is not enabled for %s, but should", iface2.Name)
   100  	}
   101  	if pi2.ProxyArp != oldpi2.ProxyArp {
   102  		t.Fatalf("ProxyArp field was changed for %s but shouldn't", iface2.Name)
   103  	}
   104  	if pi2.ProxyArpWiFi != oldpi2.ProxyArpWiFi {
   105  		t.Fatalf("ProxyArpWiFi field was changed for %s but shouldn't", iface2.Name)
   106  	}
   107  	if pi2.Learning {
   108  		t.Fatalf("Learning is enabled for %s, but shouldn't", iface2.Name)
   109  	}
   110  	if pi2.RootBlock != oldpi2.RootBlock {
   111  		t.Fatalf("RootBlock field was changed for %s but shouldn't", iface2.Name)
   112  	}
   113  	if pi2.FastLeave != oldpi2.FastLeave {
   114  		t.Fatalf("FastLeave field was changed for %s but shouldn't", iface2.Name)
   115  	}
   116  	if pi2.Flood != oldpi2.Flood {
   117  		t.Fatalf("Flood field was changed for %s but shouldn't", iface2.Name)
   118  	}
   119  
   120  	if err := LinkSetHairpin(iface3, true); err == nil || err.Error() != "operation not supported" {
   121  		t.Fatalf("Set protinfo attrs for link without master is not supported, but err: %s", err)
   122  	}
   123  
   124  	// Setting kernel requirement for next tests which require BR_PROXYARP
   125  	minKernelRequired(t, 3, 19)
   126  
   127  	if err := LinkSetBrProxyArp(iface4, true); err != nil {
   128  		t.Fatal(err)
   129  	}
   130  
   131  	if err := LinkSetBrProxyArpWiFi(iface4, true); err != nil {
   132  		t.Fatal(err)
   133  	}
   134  	pi4, err := LinkGetProtinfo(iface4)
   135  	if err != nil {
   136  		t.Fatal(err)
   137  	}
   138  	if pi4.Hairpin != oldpi4.Hairpin {
   139  		t.Fatalf("Hairpin field was changed for %s but shouldn't", iface4.Name)
   140  	}
   141  	if pi4.Guard != oldpi4.Guard {
   142  		t.Fatalf("Guard field was changed for %s but shouldn't", iface4.Name)
   143  	}
   144  	if pi4.Learning != oldpi4.Learning {
   145  		t.Fatalf("Learning field was changed for %s but shouldn't", iface4.Name)
   146  	}
   147  	if !pi4.ProxyArp {
   148  		t.Fatalf("ProxyArp is not enabled for %s, but should", iface4.Name)
   149  	}
   150  	if !pi4.ProxyArpWiFi {
   151  		t.Fatalf("ProxyArpWiFi is not enabled for %s, but should", iface4.Name)
   152  	}
   153  	if pi4.RootBlock != oldpi4.RootBlock {
   154  		t.Fatalf("RootBlock field was changed for %s but shouldn't", iface4.Name)
   155  	}
   156  	if pi4.FastLeave != oldpi4.FastLeave {
   157  		t.Fatalf("FastLeave field was changed for %s but shouldn't", iface4.Name)
   158  	}
   159  	if pi4.Flood != oldpi4.Flood {
   160  		t.Fatalf("Flood field was changed for %s but shouldn't", iface4.Name)
   161  	}
   162  }