| 1 |
require 'test/unit' |
| 2 |
require 'treetop' |
| 3 |
require 'ript' |
| 4 |
|
| 5 |
class IPTablesParserTest < Test::Unit::TestCase |
| 6 |
def setup |
| 7 |
@ript = IPTables::Ript.new |
| 8 |
|
| 9 |
@test_data = Hash.new |
| 10 |
|
| 11 |
@test_data["forward allow 10.0.0.0/8 to 192.168.0.1:8080 udp tcp state"] = { |
| 12 |
"dst_ip" => "192.168.0.1:8080", "src_ip" => "10.0.0.0/8", |
| 13 |
"action" => "allow", "target" => "forward", "state" => "state", |
| 14 |
"proto" => "udp tcp" } |
| 15 |
@test_data["forward allow 10.0.0.0/8 to 192.168.0.1:8080 tcp udp state"] = { |
| 16 |
"dst_ip" => "192.168.0.1:8080", "src_ip" => "10.0.0.0/8", |
| 17 |
"action" => "allow", "target" => "forward", "state" => "state", |
| 18 |
"proto" => "tcp udp" } |
| 19 |
@test_data["input deny 10.0.0.0:8 to 192.168.0.1/32 tcp"] = { |
| 20 |
"dst_ip" => "192.168.0.1/32", "src_ip" => "10.0.0.0:8", |
| 21 |
"action" => "deny", "target" => "input", "state" => "", |
| 22 |
"proto" => "tcp" } |
| 23 |
@test_data["output reject 10.0.0.0/8 to 192.168.0.1/32:8080 tcp"] = { |
| 24 |
"dst_ip" => "192.168.0.1/32:8080", "src_ip" => "10.0.0.0/8", |
| 25 |
"action" => "reject", "target" => "output", "state" => "", |
| 26 |
"proto" => "tcp" } |
| 27 |
end |
| 28 |
|
| 29 |
def test_parses_okay |
| 30 |
assert_not_nil @ript.parse "forward allow 10.0.0.0/8 to 192.168.0.1:8080 udp tcp state" |
| 31 |
assert_not_nil @ript.parse "forward log 10.0.0.0:8 to 192.168.0.1/32 udp icmp tcp" |
| 32 |
assert_not_nil @ript.parse "forward log 10.0.0.0:8 to 192.168.0.1 tcp state" |
| 33 |
assert_not_nil @ript.parse "forward deny 10.0.0.0 to 192.168.0.1 all" |
| 34 |
assert_not_nil @ript.parse "forward allow 10.0.0.0 to 192.168.0.1 all" |
| 35 |
end |
| 36 |
|
| 37 |
def test_results |
| 38 |
@test_data.each_key do |t| |
| 39 |
assert_equal @test_data[t], @ript.parse(t) |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
def test_convert |
| 44 |
assert_equal "iptables -A FORWARD -j ACCEPT -s 10.0.0.0 -d 192.168.0.1", |
| 45 |
@ript.convert("forward allow 10.0.0.0 to 192.168.0.1 all") |
| 46 |
|
| 47 |
assert_equal "iptables -A FORWARD -j DROP -s 10.0.0.0 -d 192.168.0.1", |
| 48 |
@ript.convert("forward deny 10.0.0.0 to 192.168.0.1 all") |
| 49 |
|
| 50 |
assert_equal "iptables -A FORWARD -j REJECT -s 10.0.0.0 -d 192.168.0.1", |
| 51 |
@ript.convert("forward reject 10.0.0.0 to 192.168.0.1 all") |
| 52 |
|
| 53 |
assert_equal "iptables -A FORWARD -j LOG -s 10.0.0.0 -d 192.168.0.1", |
| 54 |
@ript.convert("forward log 10.0.0.0 to 192.168.0.1 all") |
| 55 |
|
| 56 |
end |
| 57 |
end |