8aa70952dc68345ee24415bf296272f77c96ab8e
[prosody.git] / plugins / mod_selftests.lua
1
2 local st = require "util.stanza";
3 local register_component = require "core.componentmanager".register_component;
4 local core_route_stanza = core_route_stanza;
5 local socket = require "socket";
6 local config = require "core.configmanager";
7 local ping_hosts = config.get("*", "mod_selftests", "ping_hosts") or { "coversant.interop.xmpp.org", "djabberd.interop.xmpp.org", "djabberd-trunk.interop.xmpp.org", "ejabberd.interop.xmpp.org", "openfire.interop.xmpp.org" };
8
9 local open_pings = {};
10
11 local t_insert = table.insert;
12
13 local log = require "util.logger".init("mod_selftests");
14
15 local tests_jid = "self_tests@getjabber.ath.cx";
16 local host = "getjabber.ath.cx";
17
18 if not (tests_jid and host) then
19         for currhost in pairs(host) do
20                 if currhost ~= "localhost" then
21                         tests_jid, host = "self_tests@"..currhost, currhost;
22                 end
23         end
24 end
25
26 if tests_jid and host then
27         local bot = register_component(tests_jid,       function(origin, stanza, ourhost)
28                                                                                 local time = open_pings[stanza.attr.id];
29                                                                                 
30                                                                                 if time then
31                                                                                         log("info", "Ping reply from %s in %fs", tostring(stanza.attr.from), socket.gettime() - time);
32                                                                                 else
33                                                                                         log("info", "Unexpected reply: %s", stanza:pretty_print());
34                                                                                 end
35                                                                         end);
36
37
38         local our_origin = hosts[host];
39         add_event_hook("server-started", 
40                                         function ()
41                                                 local id = st.new_id();
42                                                 local ping_attr = { xmlns = 'urn:xmpp:ping' };
43                                                 local function send_ping(to)
44                                                         log("info", "Sending ping to %s", to);
45                                                         core_route_stanza(our_origin, st.iq{ to = to, from = tests_jid, id = id, type = "get" }:tag("ping", ping_attr));
46                                                         open_pings[id] = socket.gettime();
47                                                 end
48                                                 
49                                                 for _, host in ipairs(ping_hosts) do
50                                                         send_ping(host);
51                                                 end
52                                         end);
53 end