No need for the placeholder file in tests/ because the directory is no longer empty
[prosody.git] / tests / test_core_stanza_router.lua
1
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" }
7         local hosts = {
8                         ["localhost"] = local_host_session;
9                         }
10                                 
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");
15                 
16                 local target_routed;
17                 
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());
21                         target_routed = true;
22                 end
23                 env.hosts = hosts;
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");
27         end
28
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");
32                 
33                 local target_routed;
34                 
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());
38                         target_routed = true;
39                 end
40                 env.hosts = hosts;
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");
44         end
45
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");
49                 
50                 local target_handled;
51                 
52                 function env.core_route_stanza(p_origin, p_stanza)
53                 end
54
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;          
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_handled, true, "stanza was not handled successfully");
64         end
65
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");
69                 
70                 local target_routed;
71                 
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());
75                         target_routed = true;           
76                 end
77
78                 env.hosts = hosts;
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");
82         end
83
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");
87                 
88                 local target_routed;
89                 
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());
93                         target_routed = true;           
94                 end
95
96                 env.hosts = hosts;
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");
100         end
101
102         --IQ tests
103
104
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");
108                 
109                 local target_routed;
110                 
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;           
115                 end
116
117                 function env.core_handle_stanza(p_origin, p_stanza)
118                         
119                 end
120
121                 env.hosts = hosts;
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");
125         end
126
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' });
130                 
131                 local target_routed;
132                 
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;           
137                 end
138
139                 function env.core_handle_stanza(p_origin, p_stanza)
140                         
141                 end
142
143                 env.hosts = hosts;
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");
147         end
148
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");
154
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");
157
158 end