util.x509: Nameprep commonName once
[prosody.git] / spec / util_ip_spec.lua
blobbe5e4cff2e06f48d68e205861bb7da6a1c59b89e
2 local ip = require "util.ip";
4 local new_ip = ip.new_ip;
5 local match = ip.match;
6 local parse_cidr = ip.parse_cidr;
7 local commonPrefixLength = ip.commonPrefixLength;
9 describe("util.ip", function()
10 describe("#match()", function()
11 it("should work", function()
12 local _ = new_ip;
13 local ip = _"10.20.30.40";
14 assert.are.equal(match(ip, _"10.0.0.0", 8), true);
15 assert.are.equal(match(ip, _"10.0.0.0", 16), false);
16 assert.are.equal(match(ip, _"10.0.0.0", 24), false);
17 assert.are.equal(match(ip, _"10.0.0.0", 32), false);
19 assert.are.equal(match(ip, _"10.20.0.0", 8), true);
20 assert.are.equal(match(ip, _"10.20.0.0", 16), true);
21 assert.are.equal(match(ip, _"10.20.0.0", 24), false);
22 assert.are.equal(match(ip, _"10.20.0.0", 32), false);
24 assert.are.equal(match(ip, _"0.0.0.0", 32), false);
25 assert.are.equal(match(ip, _"0.0.0.0", 0), true);
26 assert.are.equal(match(ip, _"0.0.0.0"), false);
28 assert.are.equal(match(ip, _"10.0.0.0", 255), false, "excessive number of bits");
29 assert.are.equal(match(ip, _"10.0.0.0", -8), true, "negative number of bits");
30 assert.are.equal(match(ip, _"10.0.0.0", -32), true, "negative number of bits");
31 assert.are.equal(match(ip, _"10.0.0.0", 0), true, "zero bits");
32 assert.are.equal(match(ip, _"10.0.0.0"), false, "no specified number of bits (differing ip)");
33 assert.are.equal(match(ip, _"10.20.30.40"), true, "no specified number of bits (same ip)");
35 assert.are.equal(match(_"127.0.0.1", _"127.0.0.1"), true, "simple ip");
37 assert.are.equal(match(_"8.8.8.8", _"8.8.0.0", 16), true);
38 assert.are.equal(match(_"8.8.4.4", _"8.8.0.0", 16), true);
39 end);
40 end);
42 describe("#parse_cidr()", function()
43 it("should work", function()
44 assert.are.equal(new_ip"0.0.0.0", new_ip"0.0.0.0")
46 local function assert_cidr(cidr, ip, bits)
47 local parsed_ip, parsed_bits = parse_cidr(cidr);
48 assert.are.equal(new_ip(ip), parsed_ip, cidr.." parsed ip is "..ip);
49 assert.are.equal(bits, parsed_bits, cidr.." parsed bits is "..tostring(bits));
50 end
51 assert_cidr("0.0.0.0", "0.0.0.0", nil);
52 assert_cidr("127.0.0.1", "127.0.0.1", nil);
53 assert_cidr("127.0.0.1/0", "127.0.0.1", 0);
54 assert_cidr("127.0.0.1/8", "127.0.0.1", 8);
55 assert_cidr("127.0.0.1/32", "127.0.0.1", 32);
56 assert_cidr("127.0.0.1/256", "127.0.0.1", 256);
57 assert_cidr("::/48", "::", 48);
58 end);
59 end);
61 describe("#new_ip()", function()
62 it("should work", function()
63 local v4, v6 = "IPv4", "IPv6";
64 local function assert_proto(s, proto)
65 local ip = new_ip(s);
66 if proto then
67 assert.are.equal(ip and ip.proto, proto, "protocol is correct for "..("%q"):format(s));
68 else
69 assert.are.equal(ip, nil, "address is invalid");
70 end
71 end
72 assert_proto("127.0.0.1", v4);
73 assert_proto("::1", v6);
74 assert_proto("", nil);
75 assert_proto("abc", nil);
76 assert_proto(" ", nil);
77 end);
78 end);
80 describe("#commonPrefixLength()", function()
81 it("should work", function()
82 local function assert_cpl6(a, b, len, v4)
83 local ipa, ipb = new_ip(a), new_ip(b);
84 if v4 then len = len+96; end
85 assert.are.equal(commonPrefixLength(ipa, ipb), len, "common prefix length of "..a.." and "..b.." is "..len);
86 assert.are.equal(commonPrefixLength(ipb, ipa), len, "common prefix length of "..b.." and "..a.." is "..len);
87 end
88 local function assert_cpl4(a, b, len)
89 return assert_cpl6(a, b, len, "IPv4");
90 end
91 assert_cpl4("0.0.0.0", "0.0.0.0", 32);
92 assert_cpl4("255.255.255.255", "0.0.0.0", 0);
93 assert_cpl4("255.255.255.255", "255.255.0.0", 16);
94 assert_cpl4("255.255.255.255", "255.255.255.255", 32);
95 assert_cpl4("255.255.255.255", "255.255.255.255", 32);
97 assert_cpl6("::1", "::1", 128);
98 assert_cpl6("abcd::1", "abcd::1", 128);
99 assert_cpl6("abcd::abcd", "abcd::", 112);
100 assert_cpl6("abcd::abcd", "abcd::abcd:abcd", 96);
101 end);
102 end);
103 end);