2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2021-05-28 14:53:49 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-05-06 19:47:41 -04:00
|
|
|
package iptables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFirewalldInit(t *testing.T) {
|
2015-05-18 19:49:12 -04:00
|
|
|
if !checkRunning() {
|
|
|
|
t.Skip("firewalld is not running")
|
|
|
|
}
|
|
|
|
if err := FirewalldInit(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-06 19:47:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReloaded(t *testing.T) {
|
|
|
|
var err error
|
2015-06-11 21:12:00 -04:00
|
|
|
var fwdChain *ChainInfo
|
2015-05-06 19:47:41 -04:00
|
|
|
|
2017-11-28 16:15:55 -05:00
|
|
|
iptable := GetIptable(IPv4)
|
|
|
|
fwdChain, err = iptable.NewChain("FWD", Filter, false)
|
2017-06-12 14:30:30 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-11 21:12:00 -04:00
|
|
|
bridgeName := "lo"
|
|
|
|
|
2017-11-28 16:15:55 -05:00
|
|
|
err = iptable.ProgramChain(fwdChain, bridgeName, false, true)
|
2015-05-06 19:47:41 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fwdChain.Remove()
|
|
|
|
|
|
|
|
// copy-pasted from iptables_test:TestLink
|
|
|
|
ip1 := net.ParseIP("192.168.1.1")
|
|
|
|
ip2 := net.ParseIP("192.168.1.2")
|
|
|
|
port := 1234
|
|
|
|
proto := "tcp"
|
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
err = fwdChain.Link(Append, ip1, ip2, port, proto, bridgeName)
|
2015-05-06 19:47:41 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
// to be re-called again later
|
2015-06-11 21:12:00 -04:00
|
|
|
OnReloaded(func() { fwdChain.Link(Append, ip1, ip2, port, proto, bridgeName) })
|
2015-05-06 19:47:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rule1 := []string{
|
2015-06-11 21:12:00 -04:00
|
|
|
"-i", bridgeName,
|
|
|
|
"-o", bridgeName,
|
2015-05-06 19:47:41 -04:00
|
|
|
"-p", proto,
|
|
|
|
"-s", ip1.String(),
|
|
|
|
"-d", ip2.String(),
|
|
|
|
"--dport", strconv.Itoa(port),
|
|
|
|
"-j", "ACCEPT"}
|
|
|
|
|
2017-11-28 16:15:55 -05:00
|
|
|
if !iptable.Exists(fwdChain.Table, fwdChain.Name, rule1...) {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("rule1 does not exist")
|
2015-05-06 19:47:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// flush all rules
|
|
|
|
fwdChain.Remove()
|
|
|
|
|
|
|
|
reloaded()
|
|
|
|
|
|
|
|
// make sure the rules have been recreated
|
2017-11-28 16:15:55 -05:00
|
|
|
if !iptable.Exists(fwdChain.Table, fwdChain.Name, rule1...) {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("rule1 hasn't been recreated")
|
2015-05-06 19:47:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPassthrough(t *testing.T) {
|
|
|
|
rule1 := []string{
|
|
|
|
"-i", "lo",
|
|
|
|
"-p", "udp",
|
|
|
|
"--dport", "123",
|
|
|
|
"-j", "ACCEPT"}
|
|
|
|
|
2017-11-28 16:15:55 -05:00
|
|
|
iptable := GetIptable(IPv4)
|
2015-05-06 19:47:41 -04:00
|
|
|
if firewalldRunning {
|
|
|
|
_, err := Passthrough(Iptables, append([]string{"-A"}, rule1...)...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-28 16:15:55 -05:00
|
|
|
if !iptable.Exists(Filter, "INPUT", rule1...) {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("rule1 does not exist")
|
2015-05-06 19:47:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|