test_util_ip: Add tests for IP matching
[prosody.git] / tests / test_util_ip.lua
1
2 function match(match)
3         local _ = require "util.ip".new_ip;
4         local ip = _"10.20.30.40";
5         assert_equal(match(ip, _"10.0.0.0", 8), true);
6         assert_equal(match(ip, _"10.0.0.0", 16), false);
7         assert_equal(match(ip, _"10.0.0.0", 24), false);
8         assert_equal(match(ip, _"10.0.0.0", 32), false);
9
10         assert_equal(match(ip, _"10.20.0.0", 8), true);
11         assert_equal(match(ip, _"10.20.0.0", 16), true);
12         assert_equal(match(ip, _"10.20.0.0", 24), false);
13         assert_equal(match(ip, _"10.20.0.0", 32), false);
14
15         assert_equal(match(ip, _"0.0.0.0", 32), false);
16         assert_equal(match(ip, _"0.0.0.0", 0), true);
17         assert_equal(match(ip, _"0.0.0.0"), false);
18
19         assert_equal(match(ip, _"10.0.0.0", 255), false, "excessive number of bits");
20         assert_equal(match(ip, _"10.0.0.0", -8), true, "negative number of bits");
21         assert_equal(match(ip, _"10.0.0.0", -32), true, "negative number of bits");
22         assert_equal(match(ip, _"10.0.0.0", 0), true, "zero bits");
23         assert_equal(match(ip, _"10.0.0.0"), false, "no specified number of bits (differing ip)");
24         assert_equal(match(ip, _"10.20.30.40"), true, "no specified number of bits (same ip)");
25
26         assert_equal(match(_"80.244.94.84", _"80.244.94.84"), true, "simple ip");
27
28         assert_equal(match(_"8.8.8.8", _"8.8.0.0", 16), true);
29         assert_equal(match(_"8.8.4.4", _"8.8.0.0", 16), true);
30 end