Merge 0.10->trunk
[prosody.git] / tests / test_util_rfc6724.lua
1 -- Prosody IM
2 -- Copyright (C) 2011-2013 Florian Zeitz
3 --
4 -- This project is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information.
6 --
7
8 function source(source)
9         local new_ip = require"util.ip".new_ip;
10         assert_equal(source(new_ip("2001:db8:1::1", "IPv6"),
11                         {new_ip("2001:db8:3::1", "IPv6"), new_ip("fe80::1", "IPv6")}).addr,
12                 "2001:db8:3::1",
13                 "prefer appropriate scope");
14         assert_equal(source(new_ip("ff05::1", "IPv6"),
15                         {new_ip("2001:db8:3::1", "IPv6"), new_ip("fe80::1", "IPv6")}).addr,
16                 "2001:db8:3::1",
17                 "prefer appropriate scope");
18         assert_equal(source(new_ip("2001:db8:1::1", "IPv6"),
19                         {new_ip("2001:db8:1::1", "IPv6"), new_ip("2001:db8:2::1", "IPv6")}).addr,
20                 "2001:db8:1::1",
21                 "prefer same address"); -- "2001:db8:1::1" should be marked "deprecated" here, we don't handle that right now
22         assert_equal(source(new_ip("fe80::1", "IPv6"),
23                         {new_ip("fe80::2", "IPv6"), new_ip("2001:db8:1::1", "IPv6")}).addr,
24                 "fe80::2",
25                 "prefer appropriate scope"); -- "fe80::2" should be marked "deprecated" here, we don't handle that right now
26         assert_equal(source(new_ip("2001:db8:1::1", "IPv6"),
27                         {new_ip("2001:db8:1::2", "IPv6"), new_ip("2001:db8:3::2", "IPv6")}).addr,
28                 "2001:db8:1::2",
29                 "longest matching prefix");
30 --[[ "2001:db8:1::2" should be a care-of address and "2001:db8:3::2" a home address, we can't handle this and would fail
31         assert_equal(source(new_ip("2001:db8:1::1", "IPv6"),
32                         {new_ip("2001:db8:1::2", "IPv6"), new_ip("2001:db8:3::2", "IPv6")}).addr,
33                 "2001:db8:3::2",
34                 "prefer home address");
35 ]]
36         assert_equal(source(new_ip("2002:c633:6401::1", "IPv6"),
37                         {new_ip("2002:c633:6401::d5e3:7953:13eb:22e8", "IPv6"), new_ip("2001:db8:1::2", "IPv6")}).addr,
38                 "2002:c633:6401::d5e3:7953:13eb:22e8",
39                 "prefer matching label"); -- "2002:c633:6401::d5e3:7953:13eb:22e8" should be marked "temporary" here, we don't handle that right now
40         assert_equal(source(new_ip("2001:db8:1::d5e3:0:0:1", "IPv6"),
41                         {new_ip("2001:db8:1::2", "IPv6"), new_ip("2001:db8:1::d5e3:7953:13eb:22e8", "IPv6")}).addr,
42                 "2001:db8:1::d5e3:7953:13eb:22e8",
43                 "prefer temporary address") -- "2001:db8:1::2" should be marked "public" and "2001:db8:1::d5e3:7953:13eb:22e8" should be marked "temporary" here, we don't handle that right now
44 end
45
46 function destination(dest)
47         local order;
48         local new_ip = require"util.ip".new_ip;
49         order = dest({new_ip("2001:db8:1::1", "IPv6"), new_ip("198.51.100.121", "IPv4")},
50                 {new_ip("2001:db8:1::2", "IPv6"), new_ip("fe80::1", "IPv6"), new_ip("169.254.13.78", "IPv4")})
51         assert_equal(order[1].addr, "2001:db8:1::1", "prefer matching scope");
52         assert_equal(order[2].addr, "198.51.100.121", "prefer matching scope");
53
54         order = dest({new_ip("2001:db8:1::1", "IPv6"), new_ip("198.51.100.121", "IPv4")},
55                 {new_ip("fe80::1", "IPv6"), new_ip("198.51.100.117", "IPv4")})
56         assert_equal(order[1].addr, "198.51.100.121", "prefer matching scope");
57         assert_equal(order[2].addr, "2001:db8:1::1", "prefer matching scope");
58
59         order = dest({new_ip("2001:db8:1::1", "IPv6"), new_ip("10.1.2.3", "IPv4")},
60                 {new_ip("2001:db8:1::2", "IPv6"), new_ip("fe80::1", "IPv6"), new_ip("10.1.2.4", "IPv4")})
61         assert_equal(order[1].addr, "2001:db8:1::1", "prefer higher precedence");
62         assert_equal(order[2].addr, "10.1.2.3", "prefer higher precedence");
63
64         order = dest({new_ip("2001:db8:1::1", "IPv6"), new_ip("fe80::1", "IPv6")},
65                 {new_ip("2001:db8:1::2", "IPv6"), new_ip("fe80::2", "IPv6")})
66         assert_equal(order[1].addr, "fe80::1", "prefer smaller scope");
67         assert_equal(order[2].addr, "2001:db8:1::1", "prefer smaller scope");
68
69 --[[ "2001:db8:1::2" and "fe80::2" should be marked "care-of address", while "2001:db8:3::1" should be marked "home address", we can't currently handle this and would fail the test
70         order = dest({new_ip("2001:db8:1::1", "IPv6"), new_ip("fe80::1", "IPv6")},
71                 {new_ip("2001:db8:1::2", "IPv6"), new_ip("2001:db8:3::1", "IPv6"), new_ip("fe80::2", "IPv6")})
72         assert_equal(order[1].addr, "2001:db8:1::1", "prefer home address");
73         assert_equal(order[2].addr, "fe80::1", "prefer home address");
74 ]]
75
76 --[[ "fe80::2" should be marked "deprecated", we can't currently handle this and would fail the test
77         order = dest({new_ip("2001:db8:1::1", "IPv6"), new_ip("fe80::1", "IPv6")},
78                 {new_ip("2001:db8:1::2", "IPv6"), new_ip("fe80::2", "IPv6")})
79         assert_equal(order[1].addr, "2001:db8:1::1", "avoid deprecated addresses");
80         assert_equal(order[2].addr, "fe80::1", "avoid deprecated addresses");
81 ]]
82
83         order = dest({new_ip("2001:db8:1::1", "IPv6"), new_ip("2001:db8:3ffe::1", "IPv6")},
84                 {new_ip("2001:db8:1::2", "IPv6"), new_ip("2001:db8:3f44::2", "IPv6"), new_ip("fe80::2", "IPv6")})
85         assert_equal(order[1].addr, "2001:db8:1::1", "longest matching prefix");
86         assert_equal(order[2].addr, "2001:db8:3ffe::1", "longest matching prefix");
87
88         order = dest({new_ip("2002:c633:6401::1", "IPv6"), new_ip("2001:db8:1::1", "IPv6")},
89                 {new_ip("2002:c633:6401::2", "IPv6"), new_ip("fe80::2", "IPv6")})
90         assert_equal(order[1].addr, "2002:c633:6401::1", "prefer matching label");
91         assert_equal(order[2].addr, "2001:db8:1::1", "prefer matching label");
92
93         order = dest({new_ip("2002:c633:6401::1", "IPv6"), new_ip("2001:db8:1::1", "IPv6")},
94                 {new_ip("2002:c633:6401::2", "IPv6"), new_ip("2001:db8:1::2", "IPv6"), new_ip("fe80::2", "IPv6")})
95         assert_equal(order[1].addr, "2001:db8:1::1", "prefer higher precedence");
96         assert_equal(order[2].addr, "2002:c633:6401::1", "prefer higher precedence");
97 end