diff options
author | Matthew Wild <mwild1@gmail.com> | 2013-05-18 17:44:01 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2013-05-18 17:44:01 +0100 |
commit | afd634ee6c958694a1cef66f22334b4ee0b87c9d (patch) | |
tree | 379fb09fffbe147186647f59ee845671ac737865 /tests | |
parent | 75fe955f7d00be12e57b7beca24f3ed0ab5d384f (diff) | |
download | prosody-afd634ee6c958694a1cef66f22334b4ee0b87c9d.tar.gz prosody-afd634ee6c958694a1cef66f22334b4ee0b87c9d.zip |
test_util_ip: Add tests for IP matching
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_util_ip.lua | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/tests/test_util_ip.lua b/tests/test_util_ip.lua index ce7c397f..e30b30b6 100644 --- a/tests/test_util_ip.lua +++ b/tests/test_util_ip.lua @@ -1,7 +1,30 @@ -function test_match(match_ip) - assert(match_ip("10.20.30.40", "10.0.0.0/8")); - assert(match_ip("80.244.94.84", "80.244.94.84")); - assert(match_ip("8.8.8.8", "8.8.0.0/16")); - assert(match_ip("8.8.4.4", "8.8.0.0/16")); +function match(match) + local _ = require "util.ip".new_ip; + local ip = _"10.20.30.40"; + assert_equal(match(ip, _"10.0.0.0", 8), true); + assert_equal(match(ip, _"10.0.0.0", 16), false); + assert_equal(match(ip, _"10.0.0.0", 24), false); + assert_equal(match(ip, _"10.0.0.0", 32), false); + + assert_equal(match(ip, _"10.20.0.0", 8), true); + assert_equal(match(ip, _"10.20.0.0", 16), true); + assert_equal(match(ip, _"10.20.0.0", 24), false); + assert_equal(match(ip, _"10.20.0.0", 32), false); + + assert_equal(match(ip, _"0.0.0.0", 32), false); + assert_equal(match(ip, _"0.0.0.0", 0), true); + assert_equal(match(ip, _"0.0.0.0"), false); + + assert_equal(match(ip, _"10.0.0.0", 255), false, "excessive number of bits"); + assert_equal(match(ip, _"10.0.0.0", -8), true, "negative number of bits"); + assert_equal(match(ip, _"10.0.0.0", -32), true, "negative number of bits"); + assert_equal(match(ip, _"10.0.0.0", 0), true, "zero bits"); + assert_equal(match(ip, _"10.0.0.0"), false, "no specified number of bits (differing ip)"); + assert_equal(match(ip, _"10.20.30.40"), true, "no specified number of bits (same ip)"); + + assert_equal(match(_"80.244.94.84", _"80.244.94.84"), true, "simple ip"); + + assert_equal(match(_"8.8.8.8", _"8.8.0.0", 16), true); + assert_equal(match(_"8.8.4.4", _"8.8.0.0", 16), true); end |