mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-25 12:47:31 +00:00
util/linuxfw: move detection logic
Just a refactor to consolidate the firewall detection logic in a single package so that it can be reused in a later commit by containerboot. Updates #9310 Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
@@ -7,6 +7,7 @@ package linuxfw
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"os"
|
||||
@@ -946,3 +947,63 @@ func TestNFTAddAndDelHookRule(t *testing.T) {
|
||||
t.Fatalf("expected 0 rule in POSTROUTING chain, got %v", len(postroutingChainRules))
|
||||
}
|
||||
}
|
||||
|
||||
type testFWDetector struct {
|
||||
iptRuleCount, nftRuleCount int
|
||||
iptErr, nftErr error
|
||||
}
|
||||
|
||||
func (t *testFWDetector) iptDetect() (int, error) {
|
||||
return t.iptRuleCount, t.iptErr
|
||||
}
|
||||
|
||||
func (t *testFWDetector) nftDetect() (int, error) {
|
||||
return t.nftRuleCount, t.nftErr
|
||||
}
|
||||
|
||||
func TestPickFirewallModeFromInstalledRules(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
det *testFWDetector
|
||||
want FirewallMode
|
||||
}{
|
||||
{
|
||||
name: "using iptables legacy",
|
||||
det: &testFWDetector{iptRuleCount: 1},
|
||||
want: FirewallModeIPTables,
|
||||
},
|
||||
{
|
||||
name: "using nftables",
|
||||
det: &testFWDetector{nftRuleCount: 1},
|
||||
want: FirewallModeNfTables,
|
||||
},
|
||||
{
|
||||
name: "using both iptables and nftables",
|
||||
det: &testFWDetector{iptRuleCount: 2, nftRuleCount: 2},
|
||||
want: FirewallModeNfTables,
|
||||
},
|
||||
{
|
||||
name: "not using any firewall, both available",
|
||||
det: &testFWDetector{},
|
||||
want: FirewallModeNfTables,
|
||||
},
|
||||
{
|
||||
name: "not using any firewall, iptables available only",
|
||||
det: &testFWDetector{iptRuleCount: 1, nftErr: errors.New("nft error")},
|
||||
want: FirewallModeIPTables,
|
||||
},
|
||||
{
|
||||
name: "not using any firewall, nftables available only",
|
||||
det: &testFWDetector{iptErr: errors.New("iptables error"), nftRuleCount: 1},
|
||||
want: FirewallModeNfTables,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := pickFirewallModeFromInstalledRules(t.Logf, tt.det)
|
||||
if got != tt.want {
|
||||
t.Errorf("chooseFireWallMode() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user