github.com/TeaOSLab/EdgeNode@v1.3.8/internal/iplibrary/action_ipset_test.go (about)

     1  package iplibrary_test
     2  
     3  import (
     4  	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
     5  	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
     6  	"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
     7  	executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
     8  	"github.com/iwind/TeaGo/maps"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestIPSetAction_Init(t *testing.T) {
    14  	_, lookupErr := executils.LookPath("iptables")
    15  	if lookupErr != nil {
    16  		return
    17  	}
    18  
    19  	var action = iplibrary.NewIPSetAction()
    20  	err := action.Init(&firewallconfigs.FirewallActionConfig{
    21  		Params: maps.Map{
    22  			"path":      "/usr/bin/iptables",
    23  			"whiteName": "white-list",
    24  			"blackName": "black-list",
    25  		},
    26  	})
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	t.Log("ok")
    31  }
    32  
    33  func TestIPSetAction_AddItem(t *testing.T) {
    34  	_, lookupErr := executils.LookPath("iptables")
    35  	if lookupErr != nil {
    36  		return
    37  	}
    38  
    39  	var action = iplibrary.NewIPSetAction()
    40  	action.SetConfig(&firewallconfigs.FirewallActionIPSetConfig{
    41  		Path:          "/usr/bin/iptables",
    42  		WhiteName:     "white-list",
    43  		BlackName:     "black-list",
    44  		WhiteNameIPv6: "white-list-ipv6",
    45  		BlackNameIPv6: "black-list-ipv6",
    46  	})
    47  	{
    48  		err := action.AddItem(iplibrary.IPListTypeWhite, &pb.IPItem{
    49  			Type:      "ipv4",
    50  			Id:        1,
    51  			IpFrom:    "192.168.1.100",
    52  			ExpiredAt: time.Now().Unix() + 30,
    53  		})
    54  		if err != nil {
    55  			t.Fatal(err)
    56  		}
    57  		t.Log("ok")
    58  	}
    59  	{
    60  		err := action.AddItem(iplibrary.IPListTypeWhite, &pb.IPItem{
    61  			Type:      "ipv4",
    62  			Id:        1,
    63  			IpFrom:    "1:2:3:4",
    64  			ExpiredAt: time.Now().Unix() + 30,
    65  		})
    66  		if err != nil {
    67  			t.Fatal(err)
    68  		}
    69  		t.Log("ok")
    70  	}
    71  	{
    72  		err := action.AddItem(iplibrary.IPListTypeBlack, &pb.IPItem{
    73  			Type:      "ipv4",
    74  			Id:        1,
    75  			IpFrom:    "192.168.1.100",
    76  			ExpiredAt: time.Now().Unix() + 30,
    77  		})
    78  		if err != nil {
    79  			t.Fatal(err)
    80  		}
    81  		t.Log("ok")
    82  	}
    83  	{
    84  		err := action.AddItem(iplibrary.IPListTypeBlack, &pb.IPItem{
    85  			Type:      "ipv4",
    86  			Id:        1,
    87  			IpFrom:    "1:2:3:4",
    88  			ExpiredAt: time.Now().Unix() + 30,
    89  		})
    90  		if err != nil {
    91  			t.Fatal(err)
    92  		}
    93  		t.Log("ok")
    94  	}
    95  }
    96  
    97  func TestIPSetAction_DeleteItem(t *testing.T) {
    98  	_, lookupErr := executils.LookPath("firewalld")
    99  	if lookupErr != nil {
   100  		return
   101  	}
   102  
   103  	var action = iplibrary.NewIPSetAction()
   104  	err := action.Init(&firewallconfigs.FirewallActionConfig{
   105  		Params: maps.Map{
   106  			"path":      "/usr/bin/firewalld",
   107  			"whiteName": "white-list",
   108  		},
   109  	})
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	err = action.DeleteItem(iplibrary.IPListTypeWhite, &pb.IPItem{
   114  		Type:      "ipv4",
   115  		Id:        1,
   116  		IpFrom:    "192.168.1.100",
   117  		ExpiredAt: time.Now().Unix() + 30,
   118  	})
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  	t.Log("ok")
   123  }