2014-11-15 01:36:38 +00:00
|
|
|
package iptables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const chainName = "DOCKERTEST"
|
|
|
|
|
|
|
|
var natChain *Chain
|
|
|
|
var filterChain *Chain
|
|
|
|
|
|
|
|
func TestNewChain(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
natChain, err = NewChain(chainName, "lo", Nat)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filterChain, err = NewChain(chainName, "lo", Filter)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestForward(t *testing.T) {
|
|
|
|
ip := net.ParseIP("192.168.1.1")
|
|
|
|
port := 1234
|
|
|
|
dstAddr := "172.17.0.1"
|
|
|
|
dstPort := 4321
|
|
|
|
proto := "tcp"
|
|
|
|
|
|
|
|
err := natChain.Forward(Insert, ip, port, proto, dstAddr, dstPort)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
dnatRule := []string{
|
2014-11-15 01:36:38 +00:00
|
|
|
"!", "-i", filterChain.Bridge,
|
|
|
|
"-d", ip.String(),
|
|
|
|
"-p", proto,
|
|
|
|
"--dport", strconv.Itoa(port),
|
|
|
|
"-j", "DNAT",
|
|
|
|
"--to-destination", dstAddr + ":" + strconv.Itoa(dstPort),
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
if !Exists(natChain.Table, natChain.Name, dnatRule...) {
|
2014-11-15 01:36:38 +00:00
|
|
|
t.Fatalf("DNAT rule does not exist")
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
filterRule := []string{
|
2014-11-15 01:36:38 +00:00
|
|
|
"!", "-i", filterChain.Bridge,
|
|
|
|
"-o", filterChain.Bridge,
|
|
|
|
"-d", dstAddr,
|
|
|
|
"-p", proto,
|
|
|
|
"--dport", strconv.Itoa(dstPort),
|
|
|
|
"-j", "ACCEPT",
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
if !Exists(filterChain.Table, filterChain.Name, filterRule...) {
|
2014-11-15 01:36:38 +00:00
|
|
|
t.Fatalf("filter rule does not exist")
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
masqRule := []string{
|
2014-11-15 01:36:38 +00:00
|
|
|
"-d", dstAddr,
|
|
|
|
"-s", dstAddr,
|
|
|
|
"-p", proto,
|
|
|
|
"--dport", strconv.Itoa(dstPort),
|
|
|
|
"-j", "MASQUERADE",
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
if !Exists(natChain.Table, "POSTROUTING", masqRule...) {
|
2014-11-15 01:36:38 +00:00
|
|
|
t.Fatalf("MASQUERADE rule does not exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLink(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
ip1 := net.ParseIP("192.168.1.1")
|
|
|
|
ip2 := net.ParseIP("192.168.1.2")
|
|
|
|
port := 1234
|
|
|
|
proto := "tcp"
|
|
|
|
|
|
|
|
err = filterChain.Link(Append, ip1, ip2, port, proto)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
rule1 := []string{
|
2014-11-15 01:36:38 +00:00
|
|
|
"-i", filterChain.Bridge,
|
|
|
|
"-o", filterChain.Bridge,
|
|
|
|
"-p", proto,
|
|
|
|
"-s", ip1.String(),
|
|
|
|
"-d", ip2.String(),
|
|
|
|
"--dport", strconv.Itoa(port),
|
|
|
|
"-j", "ACCEPT"}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
if !Exists(filterChain.Table, filterChain.Name, rule1...) {
|
2014-11-15 01:36:38 +00:00
|
|
|
t.Fatalf("rule1 does not exist")
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
rule2 := []string{
|
2014-11-15 01:36:38 +00:00
|
|
|
"-i", filterChain.Bridge,
|
|
|
|
"-o", filterChain.Bridge,
|
|
|
|
"-p", proto,
|
|
|
|
"-s", ip2.String(),
|
|
|
|
"-d", ip1.String(),
|
|
|
|
"--sport", strconv.Itoa(port),
|
|
|
|
"-j", "ACCEPT"}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
if !Exists(filterChain.Table, filterChain.Name, rule2...) {
|
2014-11-15 01:36:38 +00:00
|
|
|
t.Fatalf("rule2 does not exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrerouting(t *testing.T) {
|
|
|
|
args := []string{
|
|
|
|
"-i", "lo",
|
|
|
|
"-d", "192.168.1.1"}
|
|
|
|
|
|
|
|
err := natChain.Prerouting(Insert, args...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
rule := []string{
|
2014-11-15 01:36:38 +00:00
|
|
|
"-j", natChain.Name}
|
|
|
|
|
|
|
|
rule = append(rule, args...)
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
if !Exists(natChain.Table, "PREROUTING", rule...) {
|
2014-11-15 01:36:38 +00:00
|
|
|
t.Fatalf("rule does not exist")
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
delRule := append([]string{"-D", "PREROUTING", "-t", string(Nat)}, rule...)
|
2014-11-15 01:36:38 +00:00
|
|
|
if _, err = Raw(delRule...); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutput(t *testing.T) {
|
|
|
|
args := []string{
|
|
|
|
"-o", "lo",
|
|
|
|
"-d", "192.168.1.1"}
|
|
|
|
|
|
|
|
err := natChain.Output(Insert, args...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
rule := []string{
|
2014-11-15 01:36:38 +00:00
|
|
|
"-j", natChain.Name}
|
|
|
|
|
|
|
|
rule = append(rule, args...)
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
if !Exists(natChain.Table, "OUTPUT", rule...) {
|
2014-11-15 01:36:38 +00:00
|
|
|
t.Fatalf("rule does not exist")
|
|
|
|
}
|
|
|
|
|
2015-02-14 03:10:14 +00:00
|
|
|
delRule := append([]string{"-D", "OUTPUT", "-t",
|
|
|
|
string(natChain.Table)}, rule...)
|
2014-11-15 01:36:38 +00:00
|
|
|
if _, err = Raw(delRule...); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCleanup(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var rules []byte
|
|
|
|
|
|
|
|
// Cleanup filter/FORWARD first otherwise output of iptables-save is dirty
|
|
|
|
link := []string{"-t", string(filterChain.Table),
|
|
|
|
string(Delete), "FORWARD",
|
|
|
|
"-o", filterChain.Bridge,
|
|
|
|
"-j", filterChain.Name}
|
|
|
|
if _, err = Raw(link...); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
filterChain.Remove()
|
|
|
|
|
|
|
|
err = RemoveExistingChain(chainName, Nat)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rules, err = exec.Command("iptables-save").Output()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if strings.Contains(string(rules), chainName) {
|
|
|
|
t.Fatalf("Removing chain failed. %s found in iptables-save", chainName)
|
|
|
|
}
|
|
|
|
}
|