0c95137d311959b5afcaeb71413a07e0eae79ba4
[prosody.git] / tests / test_core_stanza_router.lua
1 -- Prosody IM v0.3
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
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" }
16         local hosts = {
17                         ["localhost"] = local_host_session;
18                         }
19                                 
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");
24                 
25                 local target_routed;
26                 
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());
30                         target_routed = true;
31                 end
32                 env.hosts = hosts;
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");
36         end
37
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");
41                 
42                 local target_routed;
43                 
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());
47                         target_routed = true;
48                 end
49                 env.hosts = hosts;
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");
53         end
54
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");
58                 
59                 local target_handled;
60                 
61                 function env.core_route_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");
66                         assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
67                         target_handled = true;          
68                 end
69                 env.hosts = hosts;
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");
73         end
74
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");
78                 
79                 local target_routed;
80                 
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());
84                         target_routed = true;           
85                 end
86
87                 env.hosts = hosts;
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");
91         end
92
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");
96                 
97                 local target_routed;
98                 
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;           
103                 end
104
105                 env.hosts = hosts;
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");
109         end
110
111         --IQ tests
112
113
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");
117                 
118                 local target_routed;
119                 
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;           
124                 end
125
126                 function env.core_handle_stanza(p_origin, p_stanza)
127                         
128                 end
129
130                 env.hosts = hosts;
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");
134         end
135
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' });
139                 
140                 local target_routed;
141                 
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;           
146                 end
147
148                 function env.core_handle_stanza(p_origin, p_stanza)
149                         
150                 end
151
152                 env.hosts = hosts;
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");
156         end
157
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" });
161                 
162                 local target_handled;
163                 
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;          
168                 end
169
170                 function env.core_route_stanza(p_origin, p_stanza)
171                         
172                 end
173
174                 env.hosts = hosts;
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");
178         end
179
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");
185
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");
189
190 end