diff options
-rw-r--r-- | core/stanza_router.lua | 4 | ||||
-rw-r--r-- | net/xmppclient_listener.lua | 7 | ||||
-rw-r--r-- | tests/test.lua | 28 | ||||
-rw-r--r-- | tests/test_core_stanza_router.lua | 134 | ||||
-rw-r--r-- | tests/util/logger.lua | 36 |
5 files changed, 200 insertions, 9 deletions
diff --git a/core/stanza_router.lua b/core/stanza_router.lua index c1819651..2b0e1f4b 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -50,7 +50,7 @@ function core_process_stanza(origin, stanza) error("Client MUST bind resource after auth"); end - -- TODO also, stazas should be returned to their original state before the function ends + -- TODO also, stanzas should be returned to their original state before the function ends if origin.type == "c2s" then stanza.attr.from = origin.full_jid; end @@ -88,7 +88,7 @@ function core_process_stanza(origin, stanza) component_handle_stanza(origin, stanza); elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare); - elseif stanza.name == "iq" and not resource then -- directed at bare JID + elseif origin.type ~= "c2s" and stanza.name == "iq" and not resource then -- directed at bare JID core_handle_stanza(origin, stanza); else core_route_stanza(origin, stanza); diff --git a/net/xmppclient_listener.lua b/net/xmppclient_listener.lua index 914dd78e..b475237f 100644 --- a/net/xmppclient_listener.lua +++ b/net/xmppclient_listener.lua @@ -73,15 +73,16 @@ function xmppclient.listener(conn, data) end end -function xmppclient.disconnect(conn) +function xmppclient.disconnect(conn, err) local session = sessions[conn]; if session then - if session.last_presence and session.last_presence.attr.type ~= "unavailable" then + if session.presence and session.presence.attr.type ~= "unavailable" then local pres = st.presence{ type = "unavailable" }; - if err == "closed" then err = "connection closed"; end --FIXME where did err come from? + if err == "closed" then err = "connection closed"; end pres:tag("status"):text("Disconnected: "..err); session.stanza_dispatch(pres); end + session.log("info", "Client disconnected: %s", err); sm_destroy_session(session); sessions[conn] = nil; session = nil; diff --git a/tests/test.lua b/tests/test.lua index 108dd9a4..c0a27abd 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -1,9 +1,18 @@ local verbosity = tonumber(arg[1]) or 2; -function assert_equal(a, b) +package.path = package.path..";../?.lua"; + +require "util.import" + +local env_mt = { __index = function (t,k) return rawget(_G, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end }; +function testlib_new_env(t) + return setmetatable(t or {}, env_mt); +end + +function assert_equal(a, b, message) if not (a == b) then - error(getfenv(2).__unit.."assert_equal failed: "..tostring(a).." ~= "..tostring(b), 2); + error("\n assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n Message: "..message) or ""), 2); elseif verbosity >= 4 then print("assert_equal succeeded: "..tostring(a).." == "..tostring(b)); end @@ -52,7 +61,8 @@ function dotest(unitname) else local success, ret = pcall(tests[name], f, unit); if not success then - print("TEST FAILED: ", unitname, name, ret); + print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]"); + print(" Location: "..ret:gsub(":%s*\n", "\n")); elseif verbosity >= 2 then print("TEST SUCCEEDED: ", unitname, name); end @@ -60,5 +70,15 @@ function dotest(unitname) end end -dotest "util.jid" +function runtest(f, msg) + local success, ret = pcall(f); + if success and verbosity >= 2 then + print("SUBTEST PASSED: "..(msg or "(no description)")); + elseif (not success) and verbosity >= 1 then + print("SUBTEST FAILED: "..(msg or "(no description)")); + error(ret, 0); + end +end +dotest "util.jid" +dotest "core.stanza_router" diff --git a/tests/test_core_stanza_router.lua b/tests/test_core_stanza_router.lua new file mode 100644 index 00000000..49c1f90f --- /dev/null +++ b/tests/test_core_stanza_router.lua @@ -0,0 +1,134 @@ + +function core_process_stanza(core_process_stanza) + local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" } + local local_host_session = { host = "localhost", type = "local" } + local local_user_session = { username = "user", host = "localhost", resource = "resource", full_jid = "user@localhost/resource", type = "c2s" } + local hosts = { + ["localhost"] = local_host_session; + } + + -- Test message routing + local function test_message_full_jid() + local env = testlib_new_env(); + local msg = stanza.stanza("message", { to = "user@localhost/resource", type = "chat" }):tag("body"):text("Hello world"); + + local target_routed; + + function env.core_route_stanza(p_origin, p_stanza) + assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct"); + assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print()); + target_routed = true; + end + env.hosts = hosts; + setfenv(core_process_stanza, env); + assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); + assert_equal(target_routed, true, "stanza was not routed successfully"); + end + + local function test_message_bare_jid() + local env = testlib_new_env(); + local msg = stanza.stanza("message", { to = "user@localhost", type = "chat" }):tag("body"):text("Hello world"); + + local target_routed; + + function env.core_route_stanza(p_origin, p_stanza) + assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct"); + assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print()); + target_routed = true; + end + env.hosts = hosts; + setfenv(core_process_stanza, env); + assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); + assert_equal(target_routed, true, "stanza was not routed successfully"); + end + + local function test_message_no_to() + local env = testlib_new_env(); + local msg = stanza.stanza("message", { type = "chat" }):tag("body"):text("Hello world"); + + local target_handled; + + function env.core_route_stanza(p_origin, p_stanza) + end + + function env.core_handle_stanza(p_origin, p_stanza) + assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct"); + assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print()); + target_handled = true; + end + env.hosts = hosts; + setfenv(core_process_stanza, env); + assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); + assert_equal(target_handled, true, "stanza was not handled successfully"); + end + + local function test_message_to_remote_bare() + local env = testlib_new_env(); + local msg = stanza.stanza("message", { to = "user@remotehost", type = "chat" }):tag("body"):text("Hello world"); + + local target_routed; + + function env.core_route_stanza(p_origin, p_stanza) + assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct"); + assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print()); + target_routed = true; + end + + env.hosts = hosts; + setfenv(core_process_stanza, env); + assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); + assert_equal(target_routed, true, "stanza was not routed successfully"); + end + + local function test_message_to_remote_server() + local env = testlib_new_env(); + local msg = stanza.stanza("message", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world"); + + local target_routed; + + function env.core_route_stanza(p_origin, p_stanza) + assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct"); + assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print()); + target_routed = true; + end + + env.hosts = hosts; + setfenv(core_process_stanza, env); + assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); + assert_equal(target_routed, true, "stanza was not routed successfully"); + end + + --IQ tests + + + local function test_iq_to_remote_server() + local env = testlib_new_env(); + local msg = stanza.stanza("iq", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world"); + + local target_routed; + + function env.core_route_stanza(p_origin, p_stanza) + assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct"); + assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print()); + target_routed = true; + end + + function env.core_handle_stanza(p_origin, p_stanza) + + end + + env.hosts = hosts; + setfenv(core_process_stanza, env); + assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value"); + assert_equal(target_routed, true, "stanza was not routed successfully"); + end + + runtest(test_message_full_jid, "Messages with full JID destinations get routed"); + runtest(test_message_bare_jid, "Messages with bare JID destinations get routed"); + runtest(test_message_no_to, "Messages with no destination are handled by the server"); + runtest(test_message_to_remote_bare, "Messages to a remote user are routed by the server"); + runtest(test_message_to_remote_server, "Messages to a remote server's JID are routed"); + + runtest(test_iq_to_remote_server, "iq to a remote server's JID are routed"); + +end diff --git a/tests/util/logger.lua b/tests/util/logger.lua new file mode 100644 index 00000000..ce0a2302 --- /dev/null +++ b/tests/util/logger.lua @@ -0,0 +1,36 @@ +local format = string.format; +local print = print; +local debug = debug; +local tostring = tostring; + +local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; +local do_pretty_printing = not os.getenv("WINDIR"); + +module "logger" + +local logstyles = {}; + +--TODO: This should be done in config, but we don't have proper config yet +if do_pretty_printing then + logstyles["info"] = getstyle("bold"); + logstyles["warn"] = getstyle("bold", "yellow"); + logstyles["error"] = getstyle("bold", "red"); +end + +function init(name) + --name = nil; -- While this line is not commented, will automatically fill in file/line number info + return function (level, message, ...) + if level == "debug" or level == "info" then return; end + if not name then + local inf = debug.getinfo(3, 'Snl'); + level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; + end + if ... then + print(name, getstring(logstyles[level], level), format(message, ...)); + else + print(name, getstring(logstyles[level], level), message); + end + end +end + +return _M; |