Software / code / prosody
Comparison
tests/test_util_ip.lua @ 5607:eee23fb79a5e
test_util_ip.lua: Add more tests for util.ip
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 18 May 2013 21:40:40 +0100 |
| parent | 5606:3dc141acf381 |
| child | 5776:bd0ff8ae98a8 |
comparison
equal
deleted
inserted
replaced
| 5606:3dc141acf381 | 5607:eee23fb79a5e |
|---|---|
| 1 | 1 |
| 2 function match(match) | 2 function match(match, _M) |
| 3 local _ = require "util.ip".new_ip; | 3 local _ = _M.new_ip; |
| 4 local ip = _"10.20.30.40"; | 4 local ip = _"10.20.30.40"; |
| 5 assert_equal(match(ip, _"10.0.0.0", 8), true); | 5 assert_equal(match(ip, _"10.0.0.0", 8), true); |
| 6 assert_equal(match(ip, _"10.0.0.0", 16), false); | 6 assert_equal(match(ip, _"10.0.0.0", 16), false); |
| 7 assert_equal(match(ip, _"10.0.0.0", 24), false); | 7 assert_equal(match(ip, _"10.0.0.0", 24), false); |
| 8 assert_equal(match(ip, _"10.0.0.0", 32), false); | 8 assert_equal(match(ip, _"10.0.0.0", 32), false); |
| 21 assert_equal(match(ip, _"10.0.0.0", -32), 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"); | 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)"); | 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)"); | 24 assert_equal(match(ip, _"10.20.30.40"), true, "no specified number of bits (same ip)"); |
| 25 | 25 |
| 26 assert_equal(match(_"80.244.94.84", _"80.244.94.84"), true, "simple ip"); | 26 assert_equal(match(_"127.0.0.1", _"127.0.0.1"), true, "simple ip"); |
| 27 | 27 |
| 28 assert_equal(match(_"8.8.8.8", _"8.8.0.0", 16), true); | 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); | 29 assert_equal(match(_"8.8.4.4", _"8.8.0.0", 16), true); |
| 30 end | 30 end |
| 31 | |
| 32 function parse_cidr(parse_cidr, _M) | |
| 33 local new_ip = _M.new_ip; | |
| 34 | |
| 35 assert_equal(new_ip"0.0.0.0", new_ip"0.0.0.0") | |
| 36 | |
| 37 local function assert_cidr(cidr, ip, bits) | |
| 38 local parsed_ip, parsed_bits = parse_cidr(cidr); | |
| 39 assert_equal(new_ip(ip), parsed_ip, cidr.." parsed ip is "..ip); | |
| 40 assert_equal(bits, parsed_bits, cidr.." parsed bits is "..tostring(bits)); | |
| 41 end | |
| 42 assert_cidr("0.0.0.0", "0.0.0.0", nil); | |
| 43 assert_cidr("127.0.0.1", "127.0.0.1", nil); | |
| 44 assert_cidr("127.0.0.1/0", "127.0.0.1", 0); | |
| 45 assert_cidr("127.0.0.1/8", "127.0.0.1", 8); | |
| 46 assert_cidr("127.0.0.1/32", "127.0.0.1", 32); | |
| 47 assert_cidr("127.0.0.1/256", "127.0.0.1", 256); | |
| 48 assert_cidr("::/48", "::", 48); | |
| 49 end | |
| 50 | |
| 51 function new_ip(new_ip) | |
| 52 local v4, v6 = "IPv4", "IPv6"; | |
| 53 local function assert_proto(s, proto) | |
| 54 local ip = new_ip(s); | |
| 55 if proto then | |
| 56 assert_equal(ip and ip.proto, proto, "protocol is correct for "..("%q"):format(s)); | |
| 57 else | |
| 58 assert_equal(ip, nil, "address is invalid"); | |
| 59 end | |
| 60 end | |
| 61 assert_proto("127.0.0.1", v4); | |
| 62 assert_proto("::1", v6); | |
| 63 assert_proto("", nil); | |
| 64 assert_proto("abc", nil); | |
| 65 assert_proto(" ", nil); | |
| 66 end | |
| 67 | |
| 68 function commonPrefixLength(cpl, _M) | |
| 69 local new_ip = _M.new_ip; | |
| 70 local function assert_cpl6(a, b, len, v4) | |
| 71 local ipa, ipb = new_ip(a), new_ip(b); | |
| 72 if v4 then len = len+96; end | |
| 73 assert_equal(cpl(ipa, ipb), len, "common prefix length of "..a.." and "..b.." is "..len); | |
| 74 assert_equal(cpl(ipb, ipa), len, "common prefix length of "..b.." and "..a.." is "..len); | |
| 75 end | |
| 76 local function assert_cpl4(a, b, len) | |
| 77 return assert_cpl6(a, b, len, "IPv4"); | |
| 78 end | |
| 79 assert_cpl4("0.0.0.0", "0.0.0.0", 32); | |
| 80 assert_cpl4("255.255.255.255", "0.0.0.0", 0); | |
| 81 assert_cpl4("255.255.255.255", "255.255.0.0", 16); | |
| 82 assert_cpl4("255.255.255.255", "255.255.255.255", 32); | |
| 83 assert_cpl4("255.255.255.255", "255.255.255.255", 32); | |
| 84 | |
| 85 assert_cpl6("::1", "::1", 128); | |
| 86 assert_cpl6("abcd::1", "abcd::1", 128); | |
| 87 assert_cpl6("abcd::abcd", "abcd::", 112); | |
| 88 assert_cpl6("abcd::abcd", "abcd::abcd:abcd", 96); | |
| 89 end |