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
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
fwdChain, err = 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"
|
|
|
|
|
2015-10-05 01:53:45 -04:00
|
|
|
err = 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"}
|
|
|
|
|
|
|
|
if !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
|
|
|
|
if !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"}
|
|
|
|
|
|
|
|
if firewalldRunning {
|
|
|
|
_, err := Passthrough(Iptables, append([]string{"-A"}, rule1...)...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|