2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
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");
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");
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);
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");
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");
76 local function test(jid
, expected_node
)
77 assert_equal(node(jid
), expected_node
, "Unexpected node for "..tostring(jid
));
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");
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");
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);