2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 function core_process_stanza(core_process_stanza)
12 local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" }
13 local s2sin_session = { from_host = "remotehost", to_host = "localhost", type = "s2sin", hosts = { ["remotehost"] = { authed = true } } }
14 local local_host_session = { host = "localhost", type = "local", s2sout = { ["remotehost"] = s2sout_session } }
15 local local_user_session = { username = "user", host = "localhost", resource = "resource", full_jid = "user@localhost/resource", type = "c2s" }
17 ["localhost"] = local_host_session;
20 -- Test message routing
21 local function test_message_full_jid()
22 local env = testlib_new_env();
23 local msg = stanza.stanza("message", { to = "user@localhost/resource", type = "chat" }):tag("body"):text("Hello world");
27 function env.core_route_stanza(p_origin, p_stanza)
28 assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
29 assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
33 setfenv(core_process_stanza, env);
34 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
35 assert_equal(target_routed, true, "stanza was not routed successfully");
38 local function test_message_bare_jid()
39 local env = testlib_new_env();
40 local msg = stanza.stanza("message", { to = "user@localhost", type = "chat" }):tag("body"):text("Hello world");
44 function env.core_route_stanza(p_origin, p_stanza)
45 assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
46 assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
50 setfenv(core_process_stanza, env);
51 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
52 assert_equal(target_routed, true, "stanza was not routed successfully");
55 local function test_message_no_to()
56 local env = testlib_new_env();
57 local msg = stanza.stanza("message", { type = "chat" }):tag("body"):text("Hello world");
61 function env.core_route_stanza(p_origin, p_stanza)
64 function env.core_handle_stanza(p_origin, p_stanza)
65 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
66 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
67 target_handled = true;
70 setfenv(core_process_stanza, env);
71 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
72 assert_equal(target_handled, true, "stanza was not handled successfully");
75 local function test_message_to_remote_bare()
76 local env = testlib_new_env();
77 local msg = stanza.stanza("message", { to = "user@remotehost", type = "chat" }):tag("body"):text("Hello world");
81 function env.core_route_stanza(p_origin, p_stanza)
82 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
83 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
88 setfenv(core_process_stanza, env);
89 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
90 assert_equal(target_routed, true, "stanza was not routed successfully");
93 local function test_message_to_remote_server()
94 local env = testlib_new_env();
95 local msg = stanza.stanza("message", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
99 function env.core_route_stanza(p_origin, p_stanza)
100 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
101 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
102 target_routed = true;
106 setfenv(core_process_stanza, env);
107 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
108 assert_equal(target_routed, true, "stanza was not routed successfully");
114 local function test_iq_to_remote_server()
115 local env = testlib_new_env();
116 local msg = stanza.stanza("iq", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
120 function env.core_route_stanza(p_origin, p_stanza)
121 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
122 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
123 target_routed = true;
126 function env.core_handle_stanza(p_origin, p_stanza)
131 setfenv(core_process_stanza, env);
132 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
133 assert_equal(target_routed, true, "stanza was not routed successfully");
136 local function test_iq_error_to_local_user()
137 local env = testlib_new_env();
138 local msg = stanza.stanza("iq", { to = "user@localhost/resource", from = "user@remotehost", type = "error" }):tag("error", { type = 'cancel' }):tag("item-not-found", { xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' });
142 function env.core_route_stanza(p_origin, p_stanza)
143 assert_equal(p_origin, s2sin_session, "origin of handled stanza is not correct");
144 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
145 target_routed = true;
148 function env.core_handle_stanza(p_origin, p_stanza)
153 setfenv(core_process_stanza, env);
154 assert_equal(core_process_stanza(s2sin_session, msg), nil, "core_process_stanza returned incorrect value");
155 assert_equal(target_routed, true, "stanza was not routed successfully");
158 local function test_iq_to_local_bare()
159 local env = testlib_new_env();
160 local msg = stanza.stanza("iq", { to = "user@localhost", from = "user@localhost", type = "get" }):tag("ping", { xmlns = "urn:xmpp:ping:0" });
162 local target_handled;
164 function env.core_handle_stanza(p_origin, p_stanza)
165 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
166 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
167 target_handled = true;
170 function env.core_route_stanza(p_origin, p_stanza)
175 setfenv(core_process_stanza, env);
176 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
177 assert_equal(target_handled, true, "stanza was not handled successfully");
180 runtest(test_message_full_jid, "Messages with full JID destinations get routed");
181 runtest(test_message_bare_jid, "Messages with bare JID destinations get routed");
182 runtest(test_message_no_to, "Messages with no destination are handled by the server");
183 runtest(test_message_to_remote_bare, "Messages to a remote user are routed by the server");
184 runtest(test_message_to_remote_server, "Messages to a remote server's JID are routed");
186 runtest(test_iq_to_remote_server, "iq to a remote server's JID are routed");
187 runtest(test_iq_to_local_bare, "iq from a local user to a local user's bare JID are handled");
188 runtest(test_iq_error_to_local_user, "iq type=error to a local user's JID are routed");