Insert copyright/license headers
[prosody.git] / tests / test_core_stanza_router.lua
1 -- Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
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.
9 -- 
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.
14 -- 
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.
18 --
19
20
21
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" }
27         local hosts = {
28                         ["localhost"] = local_host_session;
29                         }
30                                 
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");
35                 
36                 local target_routed;
37                 
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());
41                         target_routed = true;
42                 end
43                 env.hosts = hosts;
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");
47         end
48
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");
52                 
53                 local target_routed;
54                 
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());
58                         target_routed = true;
59                 end
60                 env.hosts = hosts;
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");
64         end
65
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");
69                 
70                 local target_handled;
71                 
72                 function env.core_route_stanza(p_origin, p_stanza)
73                 end
74
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;          
79                 end
80                 env.hosts = hosts;
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");
84         end
85
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");
89                 
90                 local target_routed;
91                 
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());
95                         target_routed = true;           
96                 end
97
98                 env.hosts = hosts;
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");
102         end
103
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");
107                 
108                 local target_routed;
109                 
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;           
114                 end
115
116                 env.hosts = hosts;
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");
120         end
121
122         --IQ tests
123
124
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");
128                 
129                 local target_routed;
130                 
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;           
135                 end
136
137                 function env.core_handle_stanza(p_origin, p_stanza)
138                         
139                 end
140
141                 env.hosts = hosts;
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");
145         end
146
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' });
150                 
151                 local target_routed;
152                 
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;           
157                 end
158
159                 function env.core_handle_stanza(p_origin, p_stanza)
160                         
161                 end
162
163                 env.hosts = hosts;
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");
167         end
168
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");
174
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");
177
178 end