Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test refactoring #87

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions chain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package nftables_test

import (
"testing"

"github.com/google/nftables"
)

func TestAddChain(t *testing.T) {
tests := []struct {
name string
chain *nftables.Chain
want [][]byte
}{
{
name: "Base chain",
chain: &nftables.Chain{
Name: "base-chain",
Hooknum: nftables.ChainHookPrerouting,
Priority: 0,
Type: nftables.ChainTypeFilter,
},
want: [][]byte{
// batch begin
[]byte("\x00\x00\x00\x0a"),
// nft add table ip filter
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x08\x00\x02\x00\x00\x00\x00\x00"),
// nft add chain ip filter base-chain { type filter hook prerouting priority 0 \; }
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x0f\x00\x03\x00\x62\x61\x73\x65\x2d\x63\x68\x61\x69\x6e\x00\x00\x14\x00\x04\x80\x08\x00\x01\x00\x00\x00\x00\x00\x08\x00\x02\x00\x00\x00\x00\x00\x0b\x00\x07\x00\x66\x69\x6c\x74\x65\x72\x00\x00"),
// batch end
[]byte("\x00\x00\x00\x0a"),
},
},
{
name: "Regular chain",
chain: &nftables.Chain{
Name: "regular-chain",
},
want: [][]byte{
// batch begin
[]byte("\x00\x00\x00\x0a"),
// nft add table ip filter
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x08\x00\x02\x00\x00\x00\x00\x00"),
// nft add chain ip filter regular-chain
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x12\x00\x03\x00\x72\x65\x67\x75\x6c\x61\x72\x2d\x63\x68\x61\x69\x6e\x00\x00\x00"),
// batch end
[]byte("\x00\x00\x00\x0a"),
},
},
}

for _, tt := range tests {
c := &nftables.Conn{
TestDial: CheckNLReq(t, tt.want, nil),
}

filter := c.AddTable(&nftables.Table{
Family: nftables.TableFamilyIPv4,
Name: "filter",
})

tt.chain.Table = filter
c.AddChain(tt.chain)
if err := c.Flush(); err != nil {
t.Fatal(err)
}
}
}

func TestDelChain(t *testing.T) {
tests := []struct {
name string
chain *nftables.Chain
want [][]byte
}{
{
name: "Base chain",
chain: &nftables.Chain{
Name: "base-chain",
Hooknum: nftables.ChainHookPrerouting,
Priority: 0,
Type: nftables.ChainTypeFilter,
},
want: [][]byte{
// batch begin
[]byte("\x00\x00\x00\x0a"),
// nft delete chain ip filter base-chain
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x0f\x00\x03\x00\x62\x61\x73\x65\x2d\x63\x68\x61\x69\x6e\x00\x00"),
// batch end
[]byte("\x00\x00\x00\x0a"),
},
},
{
name: "Regular chain",
chain: &nftables.Chain{
Name: "regular-chain",
},
want: [][]byte{
// batch begin
[]byte("\x00\x00\x00\x0a"),
// nft delete chain ip filter regular-chain
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x12\x00\x03\x00\x72\x65\x67\x75\x6c\x61\x72\x2d\x63\x68\x61\x69\x6e\x00\x00\x00"),
// batch end
[]byte("\x00\x00\x00\x0a"),
},
},
}

for _, tt := range tests {
c := &nftables.Conn{
TestDial: CheckNLReq(t, tt.want, nil),
}

tt.chain.Table = &nftables.Table{
Family: nftables.TableFamilyIPv4,
Name: "filter",
}
c.DelChain(tt.chain)
if err := c.Flush(); err != nil {
t.Fatal(err)
}
}
}
52 changes: 52 additions & 0 deletions counter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package nftables_test

import (
"testing"

"github.com/google/nftables"
"github.com/google/nftables/expr"
)

func TestAddCounter(t *testing.T) {
// The want byte sequences come from stracing nft(8), e.g.:
// strace -f -v -x -s 2048 -eraw=sendto nft add table ip nat
//
// The nft(8) command sequence was taken from:
// https://wiki.nftables.org/wiki-nftables/index.php/Performing_Network_Address_Translation_(NAT)
want := [][]byte{
// batch begin
[]byte("\x00\x00\x00\x0a"),
// nft add counter ip filter fwded
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x0a\x00\x02\x00\x66\x77\x64\x65\x64\x00\x00\x00\x08\x00\x03\x00\x00\x00\x00\x01\x1c\x00\x04\x80\x0c\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00"),
// nft add rule ip filter forward counter name fwded
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x0c\x00\x02\x00\x66\x6f\x72\x77\x61\x72\x64\x00\x2c\x00\x04\x80\x28\x00\x01\x80\x0b\x00\x01\x00\x6f\x62\x6a\x72\x65\x66\x00\x00\x18\x00\x02\x80\x08\x00\x01\x00\x00\x00\x00\x01\x09\x00\x02\x00\x66\x77\x64\x65\x64\x00\x00\x00"),
// batch end
[]byte("\x00\x00\x00\x0a"),
}

c := &nftables.Conn{
TestDial: CheckNLReq(t, want, nil),
}

c.AddObj(&nftables.CounterObj{
Table: &nftables.Table{Name: "filter", Family: nftables.TableFamilyIPv4},
Name: "fwded",
Bytes: 0,
Packets: 0,
})

c.AddRule(&nftables.Rule{
Table: &nftables.Table{Name: "filter", Family: nftables.TableFamilyIPv4},
Chain: &nftables.Chain{Name: "forward", Type: nftables.ChainTypeFilter},
Exprs: []expr.Any{
&expr.Objref{
Type: 1,
Name: "fwded",
},
},
})

if err := c.Flush(); err != nil {
t.Fatal(err)
}
}
82 changes: 82 additions & 0 deletions expr/ct_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package expr_test

import (
"testing"

"github.com/google/nftables"
"github.com/google/nftables/binaryutil"
"github.com/google/nftables/expr"
"golang.org/x/sys/unix"
)

func TestCt(t *testing.T) {
want := [][]byte{
// batch begin
[]byte("\x00\x00\x00\x0a"),
// sudo nft add rule ipv4table ipv4chain-5 ct mark 123 counter
[]byte("\x02\x00\x00\x00\x0e\x00\x01\x00\x69\x70\x76\x34\x74\x61\x62\x6c\x65\x00\x00\x00\x10\x00\x02\x00\x69\x70\x76\x34\x63\x68\x61\x69\x6e\x2d\x35\x00\x24\x00\x04\x80\x20\x00\x01\x80\x07\x00\x01\x00\x63\x74\x00\x00\x14\x00\x02\x80\x08\x00\x02\x00\x00\x00\x00\x03\x08\x00\x01\x00\x00\x00\x00\x01"),
// batch end
[]byte("\x00\x00\x00\x0a"),
}

c := &nftables.Conn{
TestDial: CheckNLReq(t, want, nil),
}

c.AddRule(&nftables.Rule{
Table: &nftables.Table{Name: "ipv4table", Family: nftables.TableFamilyIPv4},
Chain: &nftables.Chain{
Name: "ipv4chain-5",
},
Exprs: []expr.Any{
// [ ct load mark => reg 1 ]
&expr.Ct{
Key: unix.NFT_CT_MARK,
Register: 1,
},
},
})

if err := c.Flush(); err != nil {
t.Fatal(err)
}
}

func TestCtSet(t *testing.T) {
want := [][]byte{
// batch begin
[]byte("\x00\x00\x00\x0a"),
// sudo nft add rule filter forward ct mark set 1
[]byte("\x02\x00\x00\x00\x0b\x00\x01\x00\x66\x69\x6c\x74\x65\x72\x00\x00\x0c\x00\x02\x00\x66\x6f\x72\x77\x61\x72\x64\x00\x50\x00\x04\x80\x2c\x00\x01\x80\x0e\x00\x01\x00\x69\x6d\x6d\x65\x64\x69\x61\x74\x65\x00\x00\x00\x18\x00\x02\x80\x08\x00\x01\x00\x00\x00\x00\x01\x0c\x00\x02\x80\x08\x00\x01\x00\x01\x00\x00\x00\x20\x00\x01\x80\x07\x00\x01\x00\x63\x74\x00\x00\x14\x00\x02\x80\x08\x00\x02\x00\x00\x00\x00\x03\x08\x00\x04\x00\x00\x00\x00\x01"),
// batch end
[]byte("\x00\x00\x00\x0a"),
}

c := &nftables.Conn{
TestDial: CheckNLReq(t, want, nil),
}

c.AddRule(&nftables.Rule{
Table: &nftables.Table{Name: "filter", Family: nftables.TableFamilyIPv4},
Chain: &nftables.Chain{
Name: "forward",
},
Exprs: []expr.Any{
// [ immediate reg 1 0x00000001 ]
&expr.Immediate{
Register: 1,
Data: binaryutil.NativeEndian.PutUint32(1),
},
// [ ct set mark with reg 1 ]
&expr.Ct{
Key: expr.CtKeyMARK,
Register: 1,
SourceRegister: true,
},
},
})

if err := c.Flush(); err != nil {
t.Fatal(err)
}
}
Loading