Merge 0.9->0.10
[prosody.git] / tests / test_util_jid.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 function join(join)
10         assert_equal(join("a", "b", "c"), "a@b/c", "builds full JID");
11         assert_equal(join("a", "b", nil), "a@b", "builds bare JID");
12         assert_equal(join(nil, "b", "c"), "b/c", "builds full host JID");
13         assert_equal(join(nil, "b", nil), "b", "builds bare host JID");
14         assert_equal(join(nil, nil, nil), nil, "invalid JID is nil");
15         assert_equal(join("a", nil, nil), nil, "invalid JID is nil");
16         assert_equal(join(nil, nil, "c"), nil, "invalid JID is nil");
17         assert_equal(join("a", nil, "c"), nil, "invalid JID is nil");
18 end
19
20
21 function split(split)
22         function test(input_jid, expected_node, expected_server, expected_resource)
23                 local rnode, rserver, rresource = split(input_jid);
24                 assert_equal(expected_node, rnode, "split("..tostring(input_jid)..") failed");
25                 assert_equal(expected_server, rserver, "split("..tostring(input_jid)..") failed");
26                 assert_equal(expected_resource, rresource, "split("..tostring(input_jid)..") failed");
27         end
28
29         -- Valid JIDs
30         test("node@server",             "node", "server", nil           );
31         test("node@server/resource",    "node", "server", "resource"        );
32         test("server",                  nil,    "server", nil               );
33         test("server/resource",         nil,    "server", "resource"        );
34         test("server/resource@foo",     nil,    "server", "resource@foo"    );
35         test("server/resource@foo/bar", nil,    "server", "resource@foo/bar");
36
37         -- Always invalid JIDs
38         test(nil,                nil, nil, nil);
39         test("node@/server",     nil, nil, nil);
40         test("@server",          nil, nil, nil);
41         test("@server/resource", nil, nil, nil);
42         test("@/resource", nil, nil, nil);
43 end
44
45 function bare(bare)
46         assert_equal(bare("user@host"), "user@host", "bare JID remains bare");
47         assert_equal(bare("host"), "host", "Host JID remains host");
48         assert_equal(bare("host/resource"), "host", "Host JID with resource becomes host");
49         assert_equal(bare("user@host/resource"), "user@host", "user@host JID with resource becomes user@host");
50         assert_equal(bare("user@/resource"), nil, "invalid JID is nil");
51         assert_equal(bare("@/resource"), nil, "invalid JID is nil");
52         assert_equal(bare("@/"), nil, "invalid JID is nil");
53         assert_equal(bare("/"), nil, "invalid JID is nil");
54         assert_equal(bare(""), nil, "invalid JID is nil");
55         assert_equal(bare("@"), nil, "invalid JID is nil");
56         assert_equal(bare("user@"), nil, "invalid JID is nil");
57         assert_equal(bare("user@@"), nil, "invalid JID is nil");
58         assert_equal(bare("user@@host"), nil, "invalid JID is nil");
59         assert_equal(bare("user@@host/resource"), nil, "invalid JID is nil");
60         assert_equal(bare("user@host/"), nil, "invalid JID is nil");
61 end
62
63 function compare(compare)
64         assert_equal(compare("host", "host"), true, "host should match");
65         assert_equal(compare("host", "other-host"), false, "host should not match");
66         assert_equal(compare("other-user@host/resource", "host"), true, "host should match");
67         assert_equal(compare("other-user@host", "user@host"), false, "user should not match");
68         assert_equal(compare("user@host", "host"), true, "host should match");
69         assert_equal(compare("user@host/resource", "host"), true, "host should match");
70         assert_equal(compare("user@host/resource", "user@host"), true, "user and host should match");
71         assert_equal(compare("user@other-host", "host"), false, "host should not match");
72         assert_equal(compare("user@other-host", "user@host"), false, "host should not match");
73 end