github.com/TeaOSLab/EdgeNode@v1.3.8/internal/iplibrary/action_iptables_test.go (about) 1 package iplibrary 2 3 import ( 4 "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" 5 "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" 6 executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec" 7 "testing" 8 "time" 9 ) 10 11 func TestIPTablesAction_AddItem(t *testing.T) { 12 _, lookupErr := executils.LookPath("iptables") 13 if lookupErr != nil { 14 return 15 } 16 17 var action = NewIPTablesAction() 18 action.config = &firewallconfigs.FirewallActionIPTablesConfig{ 19 Path: "/usr/bin/iptables", 20 } 21 { 22 err := action.AddItem(IPListTypeWhite, &pb.IPItem{ 23 Type: "ipv4", 24 Id: 1, 25 IpFrom: "192.168.1.100", 26 ExpiredAt: time.Now().Unix() + 30, 27 }) 28 if err != nil { 29 t.Fatal(err) 30 } 31 t.Log("ok") 32 } 33 34 { 35 err := action.AddItem(IPListTypeBlack, &pb.IPItem{ 36 Type: "ipv4", 37 Id: 1, 38 IpFrom: "192.168.1.100", 39 ExpiredAt: time.Now().Unix() + 30, 40 }) 41 if err != nil { 42 t.Fatal(err) 43 } 44 t.Log("ok") 45 } 46 } 47 48 func TestIPTablesAction_DeleteItem(t *testing.T) { 49 _, lookupErr := executils.LookPath("firewalld") 50 if lookupErr != nil { 51 return 52 } 53 54 var action = NewIPTablesAction() 55 action.config = &firewallconfigs.FirewallActionIPTablesConfig{ 56 Path: "/usr/bin/firewalld", 57 } 58 err := action.DeleteItem(IPListTypeWhite, &pb.IPItem{ 59 Type: "ipv4", 60 Id: 1, 61 IpFrom: "192.168.1.100", 62 ExpiredAt: time.Now().Unix() + 30, 63 }) 64 if err != nil { 65 t.Fatal(err) 66 } 67 t.Log("ok") 68 }