Software /
code /
prosody
Comparison
tests/test_core_stanza_router.lua @ 5776:bd0ff8ae98a8
Remove all trailing whitespace
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 09 Aug 2013 17:48:21 +0200 |
parent | 3540:bc139431830b |
comparison
equal
deleted
inserted
replaced
5775:a6c2b8933507 | 5776:bd0ff8ae98a8 |
---|---|
1 -- Prosody IM | 1 -- Prosody IM |
2 -- Copyright (C) 2008-2010 Matthew Wild | 2 -- Copyright (C) 2008-2010 Matthew Wild |
3 -- Copyright (C) 2008-2010 Waqas Hussain | 3 -- Copyright (C) 2008-2010 Waqas Hussain |
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 _G.prosody = { full_sessions = {}; bare_sessions = {}; hosts = {}; }; | 9 _G.prosody = { full_sessions = {}; bare_sessions = {}; hosts = {}; }; |
12 local stanza = require "util.stanza"; | 12 local stanza = require "util.stanza"; |
13 local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" } | 13 local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" } |
14 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 } } } |
15 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 } } |
16 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" } |
17 | 17 |
18 _G.prosody.hosts["localhost"] = local_host_session; | 18 _G.prosody.hosts["localhost"] = local_host_session; |
19 _G.prosody.full_sessions["user@localhost/resource"] = local_user_session; | 19 _G.prosody.full_sessions["user@localhost/resource"] = local_user_session; |
20 _G.prosody.bare_sessions["user@localhost"] = { sessions = { resource = local_user_session } }; | 20 _G.prosody.bare_sessions["user@localhost"] = { sessions = { resource = local_user_session } }; |
21 | 21 |
22 -- Test message routing | 22 -- Test message routing |
23 local function test_message_full_jid() | 23 local function test_message_full_jid() |
24 local env = testlib_new_env(); | 24 local env = testlib_new_env(); |
25 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"); |
26 | 26 |
27 local target_routed; | 27 local target_routed; |
28 | 28 |
29 function env.core_post_stanza(p_origin, p_stanza) | 29 function env.core_post_stanza(p_origin, p_stanza) |
30 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"); |
31 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()); |
32 target_routed = true; | 32 target_routed = true; |
33 end | 33 end |
34 | 34 |
35 env.hosts = hosts; | 35 env.hosts = hosts; |
36 env.prosody = { hosts = hosts }; | 36 env.prosody = { hosts = hosts }; |
37 setfenv(core_process_stanza, env); | 37 setfenv(core_process_stanza, env); |
38 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"); |
39 assert_equal(target_routed, true, "stanza was not routed successfully"); | 39 assert_equal(target_routed, true, "stanza was not routed successfully"); |
40 end | 40 end |
41 | 41 |
42 local function test_message_bare_jid() | 42 local function test_message_bare_jid() |
43 local env = testlib_new_env(); | 43 local env = testlib_new_env(); |
44 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"); |
45 | 45 |
46 local target_routed; | 46 local target_routed; |
47 | 47 |
48 function env.core_post_stanza(p_origin, p_stanza) | 48 function env.core_post_stanza(p_origin, p_stanza) |
49 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"); |
50 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()); |
51 target_routed = true; | 51 target_routed = true; |
52 end | 52 end |
58 end | 58 end |
59 | 59 |
60 local function test_message_no_to() | 60 local function test_message_no_to() |
61 local env = testlib_new_env(); | 61 local env = testlib_new_env(); |
62 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"); |
63 | 63 |
64 local target_handled; | 64 local target_handled; |
65 | 65 |
66 function env.core_post_stanza(p_origin, p_stanza) | 66 function env.core_post_stanza(p_origin, p_stanza) |
67 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"); |
68 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()); |
69 target_handled = true; | 69 target_handled = true; |
70 end | 70 end |
76 end | 76 end |
77 | 77 |
78 local function test_message_to_remote_bare() | 78 local function test_message_to_remote_bare() |
79 local env = testlib_new_env(); | 79 local env = testlib_new_env(); |
80 local msg = stanza.stanza("message", { to = "user@remotehost", type = "chat" }):tag("body"):text("Hello world"); | 80 local msg = stanza.stanza("message", { to = "user@remotehost", type = "chat" }):tag("body"):text("Hello world"); |
81 | 81 |
82 local target_routed; | 82 local target_routed; |
83 | 83 |
84 function env.core_route_stanza(p_origin, p_stanza) | 84 function env.core_route_stanza(p_origin, p_stanza) |
85 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"); |
86 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()); |
87 target_routed = true; | 87 target_routed = true; |
88 end | 88 end |
89 | 89 |
90 function env.core_post_stanza(...) env.core_route_stanza(...); end | 90 function env.core_post_stanza(...) env.core_route_stanza(...); end |
91 | 91 |
92 env.hosts = hosts; | 92 env.hosts = hosts; |
93 setfenv(core_process_stanza, env); | 93 setfenv(core_process_stanza, env); |
94 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"); |
95 assert_equal(target_routed, true, "stanza was not routed successfully"); | 95 assert_equal(target_routed, true, "stanza was not routed successfully"); |
96 end | 96 end |
97 | 97 |
98 local function test_message_to_remote_server() | 98 local function test_message_to_remote_server() |
99 local env = testlib_new_env(); | 99 local env = testlib_new_env(); |
100 local msg = stanza.stanza("message", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world"); | 100 local msg = stanza.stanza("message", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world"); |
101 | 101 |
102 local target_routed; | 102 local target_routed; |
103 | 103 |
104 function env.core_route_stanza(p_origin, p_stanza) | 104 function env.core_route_stanza(p_origin, p_stanza) |
105 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"); |
106 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()); |
107 target_routed = true; | 107 target_routed = true; |
108 end | 108 end |
121 | 121 |
122 | 122 |
123 local function test_iq_to_remote_server() | 123 local function test_iq_to_remote_server() |
124 local env = testlib_new_env(); | 124 local env = testlib_new_env(); |
125 local msg = stanza.stanza("iq", { to = "remotehost", type = "get", id = "id" }):tag("body"):text("Hello world"); | 125 local msg = stanza.stanza("iq", { to = "remotehost", type = "get", id = "id" }):tag("body"):text("Hello world"); |
126 | 126 |
127 local target_routed; | 127 local target_routed; |
128 | 128 |
129 function env.core_route_stanza(p_origin, p_stanza) | 129 function env.core_route_stanza(p_origin, p_stanza) |
130 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"); |
131 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()); |
132 target_routed = true; | 132 target_routed = true; |
133 end | 133 end |
143 end | 143 end |
144 | 144 |
145 local function test_iq_error_to_local_user() | 145 local function test_iq_error_to_local_user() |
146 local env = testlib_new_env(); | 146 local env = testlib_new_env(); |
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' }); | 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' }); |
148 | 148 |
149 local target_routed; | 149 local target_routed; |
150 | 150 |
151 function env.core_route_stanza(p_origin, p_stanza) | 151 function env.core_route_stanza(p_origin, p_stanza) |
152 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"); |
153 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()); |
154 target_routed = true; | 154 target_routed = true; |
155 end | 155 end |
165 end | 165 end |
166 | 166 |
167 local function test_iq_to_local_bare() | 167 local function test_iq_to_local_bare() |
168 local env = testlib_new_env(); | 168 local env = testlib_new_env(); |
169 local msg = stanza.stanza("iq", { to = "user@localhost", from = "user@localhost", type = "get", id = "id" }):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" }); |
170 | 170 |
171 local target_handled; | 171 local target_handled; |
172 | 172 |
173 function env.core_post_stanza(p_origin, p_stanza) | 173 function env.core_post_stanza(p_origin, p_stanza) |
174 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"); |
175 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()); |
176 target_handled = true; | 176 target_handled = true; |
177 end | 177 end |
207 local env = testlib_new_env(); | 207 local env = testlib_new_env(); |
208 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" }); |
209 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" }); |
210 --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 }; |
211 local target_handled, target_replied; | 211 local target_handled, target_replied; |
212 | 212 |
213 function env.core_post_stanza(p_origin, p_stanza) | 213 function env.core_post_stanza(p_origin, p_stanza) |
214 target_handled = true; | 214 target_handled = true; |
215 end | 215 end |
216 | 216 |
217 function local_user_session.send(data) | 217 function local_user_session.send(data) |
218 --print("Replying with: ", tostring(data)); | 218 --print("Replying with: ", tostring(data)); |
219 --print(debug.traceback()) | 219 --print(debug.traceback()) |
220 target_replied = true; | 220 target_replied = true; |
221 end | 221 end |