CHANGES: Update for release
[prosody.git] / tests / test_util_jid.lua
blob0ac5827e14cee5328d8b768b83f1073ac47b0b75
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 --
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
21 function split(split)
22 local 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
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");
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
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
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
75 function node(node)
76 local function test(jid, expected_node)
77 assert_equal(node(jid), expected_node, "Unexpected node for "..tostring(jid));
78 end
80 test("example.com", nil);
81 test("foo.example.com", nil);
82 test("foo.example.com/resource", nil);
83 test("foo.example.com/some resource", nil);
84 test("foo.example.com/some@resource", nil);
86 test("foo@foo.example.com/some@resource", "foo");
87 test("foo@example/some@resource", "foo");
89 test("foo@example/@resource", "foo");
90 test("foo@example@resource", nil);
91 test("foo@example", "foo");
92 test("foo", nil);
94 test(nil, nil);
95 end
97 function host(host)
98 local function test(jid, expected_host)
99 assert_equal(host(jid), expected_host, "Unexpected host for "..tostring(jid));
102 test("example.com", "example.com");
103 test("foo.example.com", "foo.example.com");
104 test("foo.example.com/resource", "foo.example.com");
105 test("foo.example.com/some resource", "foo.example.com");
106 test("foo.example.com/some@resource", "foo.example.com");
108 test("foo@foo.example.com/some@resource", "foo.example.com");
109 test("foo@example/some@resource", "example");
111 test("foo@example/@resource", "example");
112 test("foo@example@resource", nil);
113 test("foo@example", "example");
114 test("foo", "foo");
116 test(nil, nil);
119 function resource(resource)
120 local function test(jid, expected_resource)
121 assert_equal(resource(jid), expected_resource, "Unexpected resource for "..tostring(jid));
124 test("example.com", nil);
125 test("foo.example.com", nil);
126 test("foo.example.com/resource", "resource");
127 test("foo.example.com/some resource", "some resource");
128 test("foo.example.com/some@resource", "some@resource");
130 test("foo@foo.example.com/some@resource", "some@resource");
131 test("foo@example/some@resource", "some@resource");
133 test("foo@example/@resource", "@resource");
134 test("foo@example@resource", nil);
135 test("foo@example", nil);
136 test("foo", nil);
137 test("/foo", nil);
138 test("@x/foo", nil);
139 test("@/foo", nil);
141 test(nil, nil);