From 606a10c09856a40f7cc40dc97a99ef623939508a Mon Sep 17 00:00:00 2001 From: Maxime Demode Date: Wed, 23 Oct 2019 15:44:09 +0200 Subject: [PATCH] [test] Add FlushSet case. --- nftables_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/nftables_test.go b/nftables_test.go index 175c164..18329f8 100644 --- a/nftables_test.go +++ b/nftables_test.go @@ -1613,6 +1613,47 @@ func TestDeleteElementNamedSet(t *testing.T) { } } +func TestFlushNamedSet(t *testing.T) { + // Create a new network namespace to test these operations, + // and tear down the namespace at test completion. + c, newNS := openSystemNFTConn(t) + defer cleanupSystemNFTConn(t, newNS) + // Clear all rules at the beginning + end of the test. + c.FlushRuleset() + defer c.FlushRuleset() + + filter := c.AddTable(&nftables.Table{ + Family: nftables.TableFamilyIPv4, + Name: "filter", + }) + + portSet := &nftables.Set{ + Table: filter, + Name: "kek", + KeyType: nftables.TypeInetService, + } + if err := c.AddSet(portSet, []nftables.SetElement{{Key: []byte{0, 22}}, {Key: []byte{0, 23}}}); err != nil { + t.Errorf("c.AddSet(portSet) failed: %v", err) + } + if err := c.Flush(); err != nil { + t.Errorf("c.Flush() failed: %v", err) + } + + c.FlushSet(portSet) + + if err := c.Flush(); err != nil { + t.Errorf("Second c.Flush() failed: %v", err) + } + + elems, err := c.GetSetElements(portSet) + if err != nil { + t.Errorf("c.GetSets() failed: %v", err) + } + if len(elems) != 0 { + t.Fatalf("len(elems) = %d, want 0", len(elems)) + } +} + func TestGetRuleLookupVerdictImmediate(t *testing.T) { // Create a new network namespace to test these operations, // and tear down the namespace at test completion.