Changeset

290:f9b7afd42b67

Merge new SASL code from Tobias and Waqas
author Matthew Wild <mwild1@gmail.com>
date Sat, 15 Nov 2008 19:44:09 +0000
parents 275:7af22e56d625 (diff) 289:3c8a28c1f331 (current diff)
children 291:5672d2be1bf3
files
diffstat 5 files changed, 200 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/core/stanza_router.lua	Sat Nov 15 20:28:49 2008 +0100
+++ b/core/stanza_router.lua	Sat Nov 15 19:44:09 2008 +0000
@@ -50,7 +50,7 @@
 		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 @@
 			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);
--- a/net/xmppclient_listener.lua	Sat Nov 15 20:28:49 2008 +0100
+++ b/net/xmppclient_listener.lua	Sat Nov 15 19:44:09 2008 +0000
@@ -73,15 +73,16 @@
 	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;
--- a/tests/test.lua	Sat Nov 15 20:28:49 2008 +0100
+++ b/tests/test.lua	Sat Nov 15 19:44:09 2008 +0000
@@ -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 @@
 		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 @@
 	end
 end
 
+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"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_core_stanza_router.lua	Sat Nov 15 19:44:09 2008 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/util/logger.lua	Sat Nov 15 19:44:09 2008 +0000
@@ -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;