tests/test_core_stanza_router.lua

changeset 1973
7eea682989d3
parent 1523
841d61be198f
child 2923
b7049746bd29
equal deleted inserted replaced
1972:26d4b99ba211 1973:7eea682989d3
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 9 _G.prosody = { full_sessions = {}; bare_sessions = {}; hosts = {}; };
10 10
11 function core_process_stanza(core_process_stanza) 11 function core_process_stanza(core_process_stanza, u)
12 local stanza = require "util.stanza";
12 local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" } 13 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 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_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" } 16 local local_user_session = { username = "user", host = "localhost", resource = "resource", full_jid = "user@localhost/resource", type = "c2s" }
16 local hosts = { 17
17 ["localhost"] = local_host_session; 18 _G.prosody.hosts["localhost"] = local_host_session;
18 } 19 _G.prosody.full_sessions["user@localhost/resource"] = local_user_session;
19 20 _G.prosody.bare_sessions["user@localhost"] = { sessions = { resource = local_user_session } };
21
20 -- Test message routing 22 -- Test message routing
21 local function test_message_full_jid() 23 local function test_message_full_jid()
22 local env = testlib_new_env(); 24 local env = testlib_new_env();
23 local msg = stanza.stanza("message", { to = "user@localhost/resource", type = "chat" }):tag("body"):text("Hello world"); 25 local msg = stanza.stanza("message", { to = "user@localhost/resource", type = "chat" }):tag("body"):text("Hello world");
24 26
25 local target_routed; 27 local target_routed;
26 28
27 function env.core_route_stanza(p_origin, p_stanza) 29 function env.core_post_stanza(p_origin, p_stanza)
28 assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct"); 30 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()); 31 assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
30 target_routed = true; 32 target_routed = true;
31 end 33 end
32 env.hosts = hosts; 34
35 env.hosts = hosts;
36 env.prosody = { hosts = hosts };
33 setfenv(core_process_stanza, env); 37 setfenv(core_process_stanza, env);
34 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); 38 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"); 39 assert_equal(target_routed, true, "stanza was not routed successfully");
36 end 40 end
37 41
39 local env = testlib_new_env(); 43 local env = testlib_new_env();
40 local msg = stanza.stanza("message", { to = "user@localhost", type = "chat" }):tag("body"):text("Hello world"); 44 local msg = stanza.stanza("message", { to = "user@localhost", type = "chat" }):tag("body"):text("Hello world");
41 45
42 local target_routed; 46 local target_routed;
43 47
44 function env.core_route_stanza(p_origin, p_stanza) 48 function env.core_post_stanza(p_origin, p_stanza)
45 assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct"); 49 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 assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
47 target_routed = true; 51 target_routed = true;
48 end 52 end
53
49 env.hosts = hosts; 54 env.hosts = hosts;
50 setfenv(core_process_stanza, env); 55 setfenv(core_process_stanza, env);
51 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); 56 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"); 57 assert_equal(target_routed, true, "stanza was not routed successfully");
53 end 58 end
56 local env = testlib_new_env(); 61 local env = testlib_new_env();
57 local msg = stanza.stanza("message", { type = "chat" }):tag("body"):text("Hello world"); 62 local msg = stanza.stanza("message", { type = "chat" }):tag("body"):text("Hello world");
58 63
59 local target_handled; 64 local target_handled;
60 65
61 function env.core_route_stanza(p_origin, p_stanza) 66 function env.core_post_stanza(p_origin, p_stanza)
62 end
63
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"); 67 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()); 68 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
67 target_handled = true; 69 target_handled = true;
68 end 70 end
71
69 env.hosts = hosts; 72 env.hosts = hosts;
70 setfenv(core_process_stanza, env); 73 setfenv(core_process_stanza, env);
71 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); 74 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 assert_equal(target_handled, true, "stanza was not handled successfully");
73 end 76 end
82 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct"); 85 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()); 86 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
84 target_routed = true; 87 target_routed = true;
85 end 88 end
86 89
90 function env.core_post_stanza(...) env.core_route_stanza(...); end
91
87 env.hosts = hosts; 92 env.hosts = hosts;
88 setfenv(core_process_stanza, env); 93 setfenv(core_process_stanza, env);
89 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); 94 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"); 95 assert_equal(target_routed, true, "stanza was not routed successfully");
91 end 96 end
100 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct"); 105 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()); 106 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
102 target_routed = true; 107 target_routed = true;
103 end 108 end
104 109
110 function env.core_post_stanza(...)
111 env.core_route_stanza(...);
112 end
113
105 env.hosts = hosts; 114 env.hosts = hosts;
106 setfenv(core_process_stanza, env); 115 setfenv(core_process_stanza, env);
107 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); 116 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"); 117 assert_equal(target_routed, true, "stanza was not routed successfully");
109 end 118 end
111 --IQ tests 120 --IQ tests
112 121
113 122
114 local function test_iq_to_remote_server() 123 local function test_iq_to_remote_server()
115 local env = testlib_new_env(); 124 local env = testlib_new_env();
116 local msg = stanza.stanza("iq", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world"); 125 local msg = stanza.stanza("iq", { to = "remotehost", type = "get", id = "id" }):tag("body"):text("Hello world");
117 126
118 local target_routed; 127 local target_routed;
119 128
120 function env.core_route_stanza(p_origin, p_stanza) 129 function env.core_route_stanza(p_origin, p_stanza)
121 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct"); 130 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()); 131 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
123 target_routed = true; 132 target_routed = true;
124 end 133 end
125 134
126 function env.core_handle_stanza(p_origin, p_stanza) 135 function env.core_post_stanza(...)
127 136 env.core_route_stanza(...);
128 end 137 end
129 138
130 env.hosts = hosts; 139 env.hosts = hosts;
131 setfenv(core_process_stanza, env); 140 setfenv(core_process_stanza, env);
132 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); 141 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"); 142 assert_equal(target_routed, true, "stanza was not routed successfully");
134 end 143 end
135 144
136 local function test_iq_error_to_local_user() 145 local function test_iq_error_to_local_user()
137 local env = testlib_new_env(); 146 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' }); 147 local msg = stanza.stanza("iq", { to = "user@localhost/resource", from = "user@remotehost", type = "error", id = "id" }):tag("error", { type = 'cancel' }):tag("item-not-found", { xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' });
139 148
140 local target_routed; 149 local target_routed;
141 150
142 function env.core_route_stanza(p_origin, p_stanza) 151 function env.core_route_stanza(p_origin, p_stanza)
143 assert_equal(p_origin, s2sin_session, "origin of handled stanza is not correct"); 152 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()); 153 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
145 target_routed = true; 154 target_routed = true;
146 end 155 end
147 156
148 function env.core_handle_stanza(p_origin, p_stanza) 157 function env.core_post_stanza(...)
149 158 env.core_route_stanza(...);
150 end 159 end
151 160
152 env.hosts = hosts; 161 env.hosts = hosts;
153 setfenv(core_process_stanza, env); 162 setfenv(core_process_stanza, env);
154 assert_equal(core_process_stanza(s2sin_session, msg), nil, "core_process_stanza returned incorrect value"); 163 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"); 164 assert_equal(target_routed, true, "stanza was not routed successfully");
156 end 165 end
157 166
158 local function test_iq_to_local_bare() 167 local function test_iq_to_local_bare()
159 local env = testlib_new_env(); 168 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" }); 169 local msg = stanza.stanza("iq", { to = "user@localhost", from = "user@localhost", type = "get", id = "id" }):tag("ping", { xmlns = "urn:xmpp:ping:0" });
161 170
162 local target_handled; 171 local target_handled;
163 172
164 function env.core_handle_stanza(p_origin, p_stanza) 173 function env.core_post_stanza(p_origin, p_stanza)
165 assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct"); 174 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()); 175 assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
167 target_handled = true; 176 target_handled = true;
168 end
169
170 function env.core_route_stanza(p_origin, p_stanza)
171
172 end 177 end
173 178
174 env.hosts = hosts; 179 env.hosts = hosts;
175 setfenv(core_process_stanza, env); 180 setfenv(core_process_stanza, env);
176 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); 181 assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
187 runtest(test_iq_to_local_bare, "iq from a local user to a local user's bare JID are handled"); 192 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"); 193 runtest(test_iq_error_to_local_user, "iq type=error to a local user's JID are routed");
189 end 194 end
190 195
191 function core_route_stanza(core_route_stanza) 196 function core_route_stanza(core_route_stanza)
197 local stanza = require "util.stanza";
192 local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" } 198 local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" }
193 local s2sin_session = { from_host = "remotehost", to_host = "localhost", type = "s2sin", hosts = { ["remotehost"] = { authed = true } } } 199 local s2sin_session = { from_host = "remotehost", to_host = "localhost", type = "s2sin", hosts = { ["remotehost"] = { authed = true } } }
194 local local_host_session = { host = "localhost", type = "local", s2sout = { ["remotehost"] = s2sout_session }, sessions = {} } 200 local local_host_session = { host = "localhost", type = "local", s2sout = { ["remotehost"] = s2sout_session }, sessions = {} }
195 local local_user_session = { username = "user", host = "localhost", resource = "resource", full_jid = "user@localhost/resource", type = "c2s" } 201 local local_user_session = { username = "user", host = "localhost", resource = "resource", full_jid = "user@localhost/resource", type = "c2s" }
196 local hosts = { 202 local hosts = {
202 local msg = stanza.stanza("iq", { to = "user@localhost/foo", from = "user@localhost", type = "result" }):tag("ping", { xmlns = "urn:xmpp:ping:0" }); 208 local msg = stanza.stanza("iq", { to = "user@localhost/foo", from = "user@localhost", type = "result" }):tag("ping", { xmlns = "urn:xmpp:ping:0" });
203 local msg2 = stanza.stanza("iq", { to = "user@localhost/foo", from = "user@localhost", type = "error" }):tag("ping", { xmlns = "urn:xmpp:ping:0" }); 209 local msg2 = stanza.stanza("iq", { to = "user@localhost/foo", from = "user@localhost", type = "error" }):tag("ping", { xmlns = "urn:xmpp:ping:0" });
204 --package.loaded["core.usermanager"] = { user_exists = function (user, host) print("RAR!") return true or user == "user" and host == "localhost" and true; end }; 210 --package.loaded["core.usermanager"] = { user_exists = function (user, host) print("RAR!") return true or user == "user" and host == "localhost" and true; end };
205 local target_handled, target_replied; 211 local target_handled, target_replied;
206 212
207 function env.core_handle_stanza(p_origin, p_stanza) 213 function env.core_post_stanza(p_origin, p_stanza)
208 target_handled = true; 214 target_handled = true;
209 end 215 end
210 216
211 function local_user_session.send(data) 217 function local_user_session.send(data)
212 --print("Replying with: ", tostring(data)); 218 --print("Replying with: ", tostring(data));
220 assert_equal(target_handled, nil, "stanza was handled and not dropped"); 226 assert_equal(target_handled, nil, "stanza was handled and not dropped");
221 assert_equal(target_replied, nil, "stanza was replied to and not dropped"); 227 assert_equal(target_replied, nil, "stanza was replied to and not dropped");
222 package.loaded["core.usermanager"] = nil; 228 package.loaded["core.usermanager"] = nil;
223 end 229 end
224 230
225 runtest(test_iq_result_to_offline_user, "iq type=result|error to an offline user are not replied to"); 231 --runtest(test_iq_result_to_offline_user, "iq type=result|error to an offline user are not replied to");
226 end 232 end

mercurial