Annotate

tests/test_util_ip.lua @ 6106:4b15cfae2d11

plugins/muc/muc: When there's no history; return an empty iterator
author daurnimator <quae@daurnimator.com>
date Tue, 18 Mar 2014 15:42:48 -0400
parent 5776:bd0ff8ae98a8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5604
6df0ec991f2e tests: Some much-needed cleanup...
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
5607
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
2 function match(match, _M)
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
3 local _ = _M.new_ip;
5606
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
4 local ip = _"10.20.30.40";
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
5 assert_equal(match(ip, _"10.0.0.0", 8), true);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
6 assert_equal(match(ip, _"10.0.0.0", 16), false);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
7 assert_equal(match(ip, _"10.0.0.0", 24), false);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
8 assert_equal(match(ip, _"10.0.0.0", 32), false);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
9
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
10 assert_equal(match(ip, _"10.20.0.0", 8), true);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
11 assert_equal(match(ip, _"10.20.0.0", 16), true);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
12 assert_equal(match(ip, _"10.20.0.0", 24), false);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
13 assert_equal(match(ip, _"10.20.0.0", 32), false);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
14
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
15 assert_equal(match(ip, _"0.0.0.0", 32), false);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
16 assert_equal(match(ip, _"0.0.0.0", 0), true);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
17 assert_equal(match(ip, _"0.0.0.0"), false);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
18
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
19 assert_equal(match(ip, _"10.0.0.0", 255), false, "excessive number of bits");
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
20 assert_equal(match(ip, _"10.0.0.0", -8), true, "negative number of bits");
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
21 assert_equal(match(ip, _"10.0.0.0", -32), true, "negative number of bits");
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
22 assert_equal(match(ip, _"10.0.0.0", 0), true, "zero bits");
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
23 assert_equal(match(ip, _"10.0.0.0"), false, "no specified number of bits (differing ip)");
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
24 assert_equal(match(ip, _"10.20.30.40"), true, "no specified number of bits (same ip)");
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
25
5607
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
26 assert_equal(match(_"127.0.0.1", _"127.0.0.1"), true, "simple ip");
5606
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
27
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
28 assert_equal(match(_"8.8.8.8", _"8.8.0.0", 16), true);
3dc141acf381 test_util_ip: Add tests for IP matching
Matthew Wild <mwild1@gmail.com>
parents: 5604
diff changeset
29 assert_equal(match(_"8.8.4.4", _"8.8.0.0", 16), true);
5604
6df0ec991f2e tests: Some much-needed cleanup...
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
5607
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
31
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
32 function parse_cidr(parse_cidr, _M)
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
33 local new_ip = _M.new_ip;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5607
diff changeset
34
5607
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
35 assert_equal(new_ip"0.0.0.0", new_ip"0.0.0.0")
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5607
diff changeset
36
5607
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
37 local function assert_cidr(cidr, ip, bits)
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
38 local parsed_ip, parsed_bits = parse_cidr(cidr);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
39 assert_equal(new_ip(ip), parsed_ip, cidr.." parsed ip is "..ip);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
40 assert_equal(bits, parsed_bits, cidr.." parsed bits is "..tostring(bits));
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
41 end
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
42 assert_cidr("0.0.0.0", "0.0.0.0", nil);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
43 assert_cidr("127.0.0.1", "127.0.0.1", nil);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
44 assert_cidr("127.0.0.1/0", "127.0.0.1", 0);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
45 assert_cidr("127.0.0.1/8", "127.0.0.1", 8);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
46 assert_cidr("127.0.0.1/32", "127.0.0.1", 32);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
47 assert_cidr("127.0.0.1/256", "127.0.0.1", 256);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
48 assert_cidr("::/48", "::", 48);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
49 end
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
50
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
51 function new_ip(new_ip)
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
52 local v4, v6 = "IPv4", "IPv6";
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
53 local function assert_proto(s, proto)
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
54 local ip = new_ip(s);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
55 if proto then
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
56 assert_equal(ip and ip.proto, proto, "protocol is correct for "..("%q"):format(s));
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
57 else
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
58 assert_equal(ip, nil, "address is invalid");
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
59 end
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
60 end
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
61 assert_proto("127.0.0.1", v4);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
62 assert_proto("::1", v6);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
63 assert_proto("", nil);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
64 assert_proto("abc", nil);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
65 assert_proto(" ", nil);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
66 end
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
67
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
68 function commonPrefixLength(cpl, _M)
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
69 local new_ip = _M.new_ip;
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
70 local function assert_cpl6(a, b, len, v4)
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
71 local ipa, ipb = new_ip(a), new_ip(b);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
72 if v4 then len = len+96; end
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
73 assert_equal(cpl(ipa, ipb), len, "common prefix length of "..a.." and "..b.." is "..len);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
74 assert_equal(cpl(ipb, ipa), len, "common prefix length of "..b.." and "..a.." is "..len);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
75 end
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
76 local function assert_cpl4(a, b, len)
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
77 return assert_cpl6(a, b, len, "IPv4");
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
78 end
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
79 assert_cpl4("0.0.0.0", "0.0.0.0", 32);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
80 assert_cpl4("255.255.255.255", "0.0.0.0", 0);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
81 assert_cpl4("255.255.255.255", "255.255.0.0", 16);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
82 assert_cpl4("255.255.255.255", "255.255.255.255", 32);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
83 assert_cpl4("255.255.255.255", "255.255.255.255", 32);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
84
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
85 assert_cpl6("::1", "::1", 128);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
86 assert_cpl6("abcd::1", "abcd::1", 128);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
87 assert_cpl6("abcd::abcd", "abcd::", 112);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
88 assert_cpl6("abcd::abcd", "abcd::abcd:abcd", 96);
eee23fb79a5e test_util_ip.lua: Add more tests for util.ip
Matthew Wild <mwild1@gmail.com>
parents: 5606
diff changeset
89 end