github.com/TeaOSLab/EdgeNode@v1.3.8/internal/firewalls/nftables/conn_test.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  //go:build linux
     3  // +build linux
     4  
     5  package nftables_test
     6  
     7  import (
     8  	"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
     9  	executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
    10  	"testing"
    11  )
    12  
    13  func TestConn_Test(t *testing.T) {
    14  	_, err := executils.LookPath("nft")
    15  	if err == nil {
    16  		t.Log("ok")
    17  		return
    18  	}
    19  	t.Log(err)
    20  }
    21  
    22  func TestConn_GetTable_NotFound(t *testing.T) {
    23  	conn, err := nftables.NewConn()
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	table, err := conn.GetTable("a", nftables.TableFamilyIPv4)
    29  	if err != nil {
    30  		if err == nftables.ErrTableNotFound {
    31  			t.Log("table not found")
    32  		} else {
    33  			t.Fatal(err)
    34  		}
    35  	} else {
    36  		t.Log("table:", table)
    37  	}
    38  }
    39  
    40  func TestConn_GetTable(t *testing.T) {
    41  	conn, err := nftables.NewConn()
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	table, err := conn.GetTable("myFilter", nftables.TableFamilyIPv4)
    47  	if err != nil {
    48  		if err == nftables.ErrTableNotFound {
    49  			t.Log("table not found")
    50  		} else {
    51  			t.Fatal(err)
    52  		}
    53  	} else {
    54  		t.Log("table:", table)
    55  	}
    56  }
    57  
    58  func TestConn_AddTable(t *testing.T) {
    59  	conn, err := nftables.NewConn()
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	{
    65  		table, err := conn.AddIPv4Table("test_ipv4")
    66  		if err != nil {
    67  			t.Fatal(err)
    68  		}
    69  		t.Log(table.Name())
    70  	}
    71  	{
    72  		table, err := conn.AddIPv6Table("test_ipv6")
    73  		if err != nil {
    74  			t.Fatal(err)
    75  		}
    76  		t.Log(table.Name())
    77  	}
    78  }
    79  
    80  func TestConn_DeleteTable(t *testing.T) {
    81  	conn, err := nftables.NewConn()
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  
    86  	err = conn.DeleteTable("test_ipv4", nftables.TableFamilyIPv4)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	t.Log("ok")
    91  }