MCPcopy Create free account
hub / github.com/google/nftables / TestCreateUseNamedSet

Function TestCreateUseNamedSet

nftables_test.go:3699–3753  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3697}
3698
3699func TestCreateUseNamedSet(t *testing.T) {
3700 // Create a new network namespace to test these operations,
3701 // and tear down the namespace at test completion.
3702 c, newNS := nftest.OpenSystemConn(t, *enableSysTests)
3703 defer nftest.CleanupSystemConn(t, newNS)
3704 // Clear all rules at the beginning + end of the test.
3705 c.FlushRuleset()
3706 defer c.FlushRuleset()
3707
3708 filter := c.AddTable(&nftables.Table{
3709 Family: nftables.TableFamilyIPv4,
3710 Name: "filter",
3711 })
3712
3713 portSet := &nftables.Set{
3714 Table: filter,
3715 Name: "test",
3716 KeyType: nftables.TypeInetService,
3717 }
3718 if err := c.AddSet(portSet, nil); err != nil {
3719 t.Errorf("c.AddSet(portSet) failed: %v", err)
3720 }
3721 if err := c.SetAddElements(portSet, []nftables.SetElement{{Key: binaryutil.BigEndian.PutUint16(22)}}); err != nil {
3722 t.Errorf("c.SetVal(portSet) failed: %v", err)
3723 }
3724
3725 ipSet := &nftables.Set{
3726 Table: filter,
3727 Name: "IPs_4_dayz",
3728 KeyType: nftables.TypeIPAddr,
3729 }
3730 if err := c.AddSet(ipSet, []nftables.SetElement{{Key: []byte(net.ParseIP("192.168.1.64").To4())}}); err != nil {
3731 t.Errorf("c.AddSet(ipSet) failed: %v", err)
3732 }
3733 if err := c.SetAddElements(ipSet, []nftables.SetElement{{Key: []byte(net.ParseIP("192.168.1.42").To4())}}); err != nil {
3734 t.Errorf("c.SetVal(ipSet) failed: %v", err)
3735 }
3736 if err := c.Flush(); err != nil {
3737 t.Errorf("c.Flush() failed: %v", err)
3738 }
3739
3740 sets, err := c.GetSets(filter)
3741 if err != nil {
3742 t.Errorf("c.GetSets() failed: %v", err)
3743 }
3744 if len(sets) != 2 {
3745 t.Fatalf("len(sets) = %d, want 2", len(sets))
3746 }
3747 if sets[0].Name != "test" {
3748 t.Errorf("set[0].Name = %q, want test", sets[0].Name)
3749 }
3750 if sets[1].Name != "IPs_4_dayz" {
3751 t.Errorf("set[1].Name = %q, want IPs_4_dayz", sets[1].Name)
3752 }
3753}
3754
3755func TestCreateAutoMergeSet(t *testing.T) {
3756 // Create a new network namespace to test these operations,

Callers

nothing calls this directly

Calls 9

OpenSystemConnFunction · 0.92
CleanupSystemConnFunction · 0.92
FlushRulesetMethod · 0.80
AddTableMethod · 0.80
AddSetMethod · 0.80
SetAddElementsMethod · 0.80
FlushMethod · 0.80
GetSetsMethod · 0.80
PutUint16Method · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…