2 function core_process_stanza(core_process_stanza)
3 local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" }
4 local s2sin_session = { from_host = "remotehost", to_host = "localhost", type = "s2sin" }
5 local local_host_session = { host = "localhost", type = "local" }
6 local local_user_session = { username = "user", host = "localhost", resource = "resource", full_jid = "user@localhost/resource", type = "c2s" }
8 ["localhost"] = local_host_session;
11 -- Test message routing
12 local function test_message_full_jid()
13 local env = testlib_new_env();
14 local msg = stanza.stanza("message", { to = "user@localhost/resource", type = "chat" }):tag("body"):text("Hello world");
18 function env.core_route_stanza(p_origin, p_stanza)
19 assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
20 assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
24 setfenv(core_process_stanza, env);
25 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
26 assert_equal(target_routed, true, "stanza was not routed successfully");
29 local function test_message_bare_jid()
30 local env = testlib_new_env();
31 local msg = stanza.stanza("message", { to = "user@localhost", type = "chat" }):tag("body"):text("Hello world");
35 function env.core_route_stanza(p_origin, p_stanza)
36 assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
37 assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
41 setfenv(core_process_stanza, env);
42 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
43 assert_equal(target_routed, true, "stanza was not routed successfully");
46 local function test_message_no_to()
47 local env = testlib_new_env();
48 local msg = stanza.stanza("message", { type = "chat" }):tag("body"):text("Hello world");
52 function env.core_route_stanza(p_origin, p_stanza)
55 function env.core_handle_stanza(p_origin, p_stanza)
56 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
57 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
58 target_handled = true;
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_handled, true, "stanza was not handled successfully");
66 local function test_message_to_remote_bare()
67 local env = testlib_new_env();
68 local msg = stanza.stanza("message", { to = "user@remotehost", type = "chat" }):tag("body"):text("Hello world");
72 function env.core_route_stanza(p_origin, p_stanza)
73 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
74 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
79 setfenv(core_process_stanza, env);
80 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
81 assert_equal(target_routed, true, "stanza was not routed successfully");
84 local function test_message_to_remote_server()
85 local env = testlib_new_env();
86 local msg = stanza.stanza("message", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
90 function env.core_route_stanza(p_origin, p_stanza)
91 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
92 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
97 setfenv(core_process_stanza, env);
98 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
99 assert_equal(target_routed, true, "stanza was not routed successfully");
105 local function test_iq_to_remote_server()
106 local env = testlib_new_env();
107 local msg = stanza.stanza("iq", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
111 function env.core_route_stanza(p_origin, p_stanza)
112 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
113 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
114 target_routed = true;
117 function env.core_handle_stanza(p_origin, p_stanza)
122 setfenv(core_process_stanza, env);
123 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
124 assert_equal(target_routed, true, "stanza was not routed successfully");
127 local function test_iq_error_to_local_user()
128 local env = testlib_new_env();
129 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' });
133 function env.core_route_stanza(p_origin, p_stanza)
134 assert_equal(p_origin, s2sin_session, "origin of handled stanza is not correct");
135 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
136 target_routed = true;
139 function env.core_handle_stanza(p_origin, p_stanza)
144 setfenv(core_process_stanza, env);
145 assert_equal(core_process_stanza(s2sin_session, msg), nil, "core_process_stanza returned incorrect value");
146 assert_equal(target_routed, true, "stanza was not routed successfully");
149 runtest(test_message_full_jid, "Messages with full JID destinations get routed");
150 runtest(test_message_bare_jid, "Messages with bare JID destinations get routed");
151 runtest(test_message_no_to, "Messages with no destination are handled by the server");
152 runtest(test_message_to_remote_bare, "Messages to a remote user are routed by the server");
153 runtest(test_message_to_remote_server, "Messages to a remote server's JID are routed");
155 runtest(test_iq_to_remote_server, "iq to a remote server's JID are routed");
156 runtest(test_iq_error_to_local_user, "iq type=error to a local user's JID are routed");