2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 function core_process_stanza(core_process_stanza)
23 local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" }
24 local s2sin_session = { from_host = "remotehost", to_host = "localhost", type = "s2sin" }
25 local local_host_session = { host = "localhost", type = "local" }
26 local local_user_session = { username = "user", host = "localhost", resource = "resource", full_jid = "user@localhost/resource", type = "c2s" }
28 ["localhost"] = local_host_session;
31 -- Test message routing
32 local function test_message_full_jid()
33 local env = testlib_new_env();
34 local msg = stanza.stanza("message", { to = "user@localhost/resource", type = "chat" }):tag("body"):text("Hello world");
38 function env.core_route_stanza(p_origin, p_stanza)
39 assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
40 assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
44 setfenv(core_process_stanza, env);
45 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
46 assert_equal(target_routed, true, "stanza was not routed successfully");
49 local function test_message_bare_jid()
50 local env = testlib_new_env();
51 local msg = stanza.stanza("message", { to = "user@localhost", type = "chat" }):tag("body"):text("Hello world");
55 function env.core_route_stanza(p_origin, p_stanza)
56 assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
57 assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
61 setfenv(core_process_stanza, env);
62 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
63 assert_equal(target_routed, true, "stanza was not routed successfully");
66 local function test_message_no_to()
67 local env = testlib_new_env();
68 local msg = stanza.stanza("message", { type = "chat" }):tag("body"):text("Hello world");
72 function env.core_route_stanza(p_origin, p_stanza)
75 function env.core_handle_stanza(p_origin, p_stanza)
76 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
77 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
78 target_handled = true;
81 setfenv(core_process_stanza, env);
82 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
83 assert_equal(target_handled, true, "stanza was not handled successfully");
86 local function test_message_to_remote_bare()
87 local env = testlib_new_env();
88 local msg = stanza.stanza("message", { to = "user@remotehost", type = "chat" }):tag("body"):text("Hello world");
92 function env.core_route_stanza(p_origin, p_stanza)
93 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
94 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
99 setfenv(core_process_stanza, env);
100 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
101 assert_equal(target_routed, true, "stanza was not routed successfully");
104 local function test_message_to_remote_server()
105 local env = testlib_new_env();
106 local msg = stanza.stanza("message", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
110 function env.core_route_stanza(p_origin, p_stanza)
111 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
112 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
113 target_routed = true;
117 setfenv(core_process_stanza, env);
118 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
119 assert_equal(target_routed, true, "stanza was not routed successfully");
125 local function test_iq_to_remote_server()
126 local env = testlib_new_env();
127 local msg = stanza.stanza("iq", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
131 function env.core_route_stanza(p_origin, p_stanza)
132 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
133 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
134 target_routed = true;
137 function env.core_handle_stanza(p_origin, p_stanza)
142 setfenv(core_process_stanza, env);
143 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
144 assert_equal(target_routed, true, "stanza was not routed successfully");
147 local function test_iq_error_to_local_user()
148 local env = testlib_new_env();
149 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' });
153 function env.core_route_stanza(p_origin, p_stanza)
154 assert_equal(p_origin, s2sin_session, "origin of handled stanza is not correct");
155 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
156 target_routed = true;
159 function env.core_handle_stanza(p_origin, p_stanza)
164 setfenv(core_process_stanza, env);
165 assert_equal(core_process_stanza(s2sin_session, msg), nil, "core_process_stanza returned incorrect value");
166 assert_equal(target_routed, true, "stanza was not routed successfully");
169 runtest(test_message_full_jid, "Messages with full JID destinations get routed");
170 runtest(test_message_bare_jid, "Messages with bare JID destinations get routed");
171 runtest(test_message_no_to, "Messages with no destination are handled by the server");
172 runtest(test_message_to_remote_bare, "Messages to a remote user are routed by the server");
173 runtest(test_message_to_remote_server, "Messages to a remote server's JID are routed");
175 runtest(test_iq_to_remote_server, "iq to a remote server's JID are routed");
176 runtest(test_iq_error_to_local_user, "iq type=error to a local user's JID are routed");