From a0210694a542dceabf16ac2de14828eb2f7cd8f1 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 18 May 2013 17:14:30 +0100 Subject: tests: Some much-needed cleanup... --- tests/test_util_ip.lua | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/test_util_ip.lua (limited to 'tests/test_util_ip.lua') diff --git a/tests/test_util_ip.lua b/tests/test_util_ip.lua new file mode 100644 index 00000000..ce7c397f --- /dev/null +++ b/tests/test_util_ip.lua @@ -0,0 +1,7 @@ + +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")); +end -- cgit v1.2.3 From 2497adeb34d6e81a9732c9b889fa823f363912af Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 18 May 2013 17:44:01 +0100 Subject: test_util_ip: Add tests for IP matching --- tests/test_util_ip.lua | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'tests/test_util_ip.lua') 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 -- cgit v1.2.3 From cb3fc989dafc799129fd8e8e9a4b9991686ffded Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 18 May 2013 21:40:40 +0100 Subject: test_util_ip.lua: Add more tests for util.ip --- tests/test_util_ip.lua | 65 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) (limited to 'tests/test_util_ip.lua') diff --git a/tests/test_util_ip.lua b/tests/test_util_ip.lua index e30b30b6..410f1da2 100644 --- a/tests/test_util_ip.lua +++ b/tests/test_util_ip.lua @@ -1,6 +1,6 @@ -function match(match) - local _ = require "util.ip".new_ip; +function match(match, _M) + local _ = _M.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); @@ -23,8 +23,67 @@ function match(match) 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(_"127.0.0.1", _"127.0.0.1"), 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 + +function parse_cidr(parse_cidr, _M) + local new_ip = _M.new_ip; + + assert_equal(new_ip"0.0.0.0", new_ip"0.0.0.0") + + local function assert_cidr(cidr, ip, bits) + local parsed_ip, parsed_bits = parse_cidr(cidr); + assert_equal(new_ip(ip), parsed_ip, cidr.." parsed ip is "..ip); + assert_equal(bits, parsed_bits, cidr.." parsed bits is "..tostring(bits)); + end + assert_cidr("0.0.0.0", "0.0.0.0", nil); + assert_cidr("127.0.0.1", "127.0.0.1", nil); + assert_cidr("127.0.0.1/0", "127.0.0.1", 0); + assert_cidr("127.0.0.1/8", "127.0.0.1", 8); + assert_cidr("127.0.0.1/32", "127.0.0.1", 32); + assert_cidr("127.0.0.1/256", "127.0.0.1", 256); + assert_cidr("::/48", "::", 48); +end + +function new_ip(new_ip) + local v4, v6 = "IPv4", "IPv6"; + local function assert_proto(s, proto) + local ip = new_ip(s); + if proto then + assert_equal(ip and ip.proto, proto, "protocol is correct for "..("%q"):format(s)); + else + assert_equal(ip, nil, "address is invalid"); + end + end + assert_proto("127.0.0.1", v4); + assert_proto("::1", v6); + assert_proto("", nil); + assert_proto("abc", nil); + assert_proto(" ", nil); +end + +function commonPrefixLength(cpl, _M) + local new_ip = _M.new_ip; + local function assert_cpl6(a, b, len, v4) + local ipa, ipb = new_ip(a), new_ip(b); + if v4 then len = len+96; end + assert_equal(cpl(ipa, ipb), len, "common prefix length of "..a.." and "..b.." is "..len); + assert_equal(cpl(ipb, ipa), len, "common prefix length of "..b.." and "..a.." is "..len); + end + local function assert_cpl4(a, b, len) + return assert_cpl6(a, b, len, "IPv4"); + end + assert_cpl4("0.0.0.0", "0.0.0.0", 32); + assert_cpl4("255.255.255.255", "0.0.0.0", 0); + assert_cpl4("255.255.255.255", "255.255.0.0", 16); + assert_cpl4("255.255.255.255", "255.255.255.255", 32); + assert_cpl4("255.255.255.255", "255.255.255.255", 32); + + assert_cpl6("::1", "::1", 128); + assert_cpl6("abcd::1", "abcd::1", 128); + assert_cpl6("abcd::abcd", "abcd::", 112); + assert_cpl6("abcd::abcd", "abcd::abcd:abcd", 96); +end -- cgit v1.2.3 From 4b618a8727365b880e115c6fd53869040c2ad238 Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Fri, 9 Aug 2013 17:48:21 +0200 Subject: Remove all trailing whitespace --- tests/test_util_ip.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/test_util_ip.lua') diff --git a/tests/test_util_ip.lua b/tests/test_util_ip.lua index 410f1da2..0ded1123 100644 --- a/tests/test_util_ip.lua +++ b/tests/test_util_ip.lua @@ -31,9 +31,9 @@ end function parse_cidr(parse_cidr, _M) local new_ip = _M.new_ip; - + assert_equal(new_ip"0.0.0.0", new_ip"0.0.0.0") - + local function assert_cidr(cidr, ip, bits) local parsed_ip, parsed_bits = parse_cidr(cidr); assert_equal(new_ip(ip), parsed_ip, cidr.." parsed ip is "..ip); -- cgit v1.2.3