aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test.lua14
-rw-r--r--tests/test_core_modulemanager.lua48
-rw-r--r--tests/test_core_stanza_router.lua58
3 files changed, 89 insertions, 31 deletions
diff --git a/tests/test.lua b/tests/test.lua
index f5bcb02e..f5976a02 100644
--- a/tests/test.lua
+++ b/tests/test.lua
@@ -11,6 +11,7 @@
function run_all_tests()
dotest "util.jid"
dotest "util.multitable"
+ dotest "core.modulemanager"
dotest "core.stanza_router"
dotest "core.s2smanager"
dotest "core.configmanager"
@@ -29,9 +30,11 @@ else
package.cpath = package.cpath..";../?.so";
end
+local _realG = _G;
+
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 };
+local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
function testlib_new_env(t)
return setmetatable(t or {}, env_mt);
end
@@ -65,7 +68,7 @@ end
function dosingletest(testname, fname)
- local tests = setmetatable({}, { __index = _G });
+ local tests = setmetatable({}, { __index = _realG });
tests.__unit = testname;
tests.__test = fname;
local chunk, err = loadfile(testname);
@@ -103,7 +106,7 @@ function dosingletest(testname, fname)
end
function dotest(unitname)
- local tests = setmetatable({}, { __index = _G });
+ local tests = setmetatable({}, { __index = _realG });
tests.__unit = unitname;
local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
if not chunk then
@@ -118,8 +121,9 @@ function dotest(unitname)
return;
end
- local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) });
-
+ if tests.env then setmetatable(tests.env, { __index = _realG }); end
+ local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _G }, { __index = tests.env or _G }) });
+ unit._G = unit; _realG._G = unit;
local fn = "../"..unitname:gsub("%.", "/")..".lua";
local chunk, err = loadfile(fn);
if not chunk then
diff --git a/tests/test_core_modulemanager.lua b/tests/test_core_modulemanager.lua
new file mode 100644
index 00000000..82e9aa45
--- /dev/null
+++ b/tests/test_core_modulemanager.lua
@@ -0,0 +1,48 @@
+-- Prosody IM
+-- Copyright (C) 2008-2009 Matthew Wild
+-- Copyright (C) 2008-2009 Waqas Hussain
+--
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+local config = require "core.configmanager";
+local helpers = require "util.helpers";
+local set = require "util.set";
+
+function load_modules_for_host(load_modules_for_host, mm)
+ local test_num = 0;
+ local function test_load(global_modules_enabled, global_modules_disabled, host_modules_enabled, host_modules_disabled, expected_modules)
+ test_num = test_num + 1;
+ -- Prepare
+ hosts = { ["example.com"] = {} };
+ config.set("*", "core", "modules_enabled", global_modules_enabled);
+ config.set("*", "core", "modules_disabled", global_modules_disabled);
+ config.set("example.com", "core", "modules_enabled", host_modules_enabled);
+ config.set("example.com", "core", "modules_disabled", host_modules_disabled);
+
+ expected_modules = set.new(expected_modules);
+ expected_modules:add_list(helpers.get_upvalue(load_modules_for_host, "autoload_modules"));
+
+ local loaded_modules = set.new();
+ function mm.load(host, module)
+ assert_equal(host, "example.com", test_num..": Host isn't example.com but "..tostring(host));
+ assert_equal(expected_modules:contains(module), true, test_num..": Loading unexpected module '"..tostring(module).."'");
+ loaded_modules:add(module);
+ end
+ load_modules_for_host("example.com");
+ assert_equal((expected_modules - loaded_modules):empty(), true, test_num..": Not all modules loaded: "..tostring(expected_modules - loaded_modules));
+ end
+
+ test_load({ "one", "two", "three" }, nil, nil, nil, { "one", "two", "three" });
+ test_load({ "one", "two", "three" }, {}, nil, nil, { "one", "two", "three" });
+ test_load({ "one", "two", "three" }, { "two" }, nil, nil, { "one", "three" });
+ test_load({ "one", "two", "three" }, { "three" }, nil, nil, { "one", "two" });
+ test_load({ "one", "two", "three" }, nil, nil, { "three" }, { "one", "two" });
+ test_load({ "one", "two", "three" }, nil, { "three" }, { "three" }, { "one", "two", "three" });
+
+ test_load({ "one", "two" }, nil, { "three" }, nil, { "one", "two", "three" });
+ test_load({ "one", "two", "three" }, nil, { "three" }, nil, { "one", "two", "three" });
+ test_load({ "one", "two", "three" }, { "three" }, { "three" }, nil, { "one", "two", "three" });
+ test_load({ "one", "two" }, { "three" }, { "three" }, nil, { "one", "two", "three" });
+end
diff --git a/tests/test_core_stanza_router.lua b/tests/test_core_stanza_router.lua
index a759ceec..59e68b91 100644
--- a/tests/test_core_stanza_router.lua
+++ b/tests/test_core_stanza_router.lua
@@ -6,17 +6,19 @@
-- COPYING file in the source package for more information.
--
+_G.prosody = { full_sessions = {}; bare_sessions = {}; hosts = {}; };
-
-function core_process_stanza(core_process_stanza)
+function core_process_stanza(core_process_stanza, u)
+ local stanza = require "util.stanza";
local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" }
local s2sin_session = { from_host = "remotehost", to_host = "localhost", type = "s2sin", hosts = { ["remotehost"] = { authed = true } } }
local local_host_session = { host = "localhost", type = "local", s2sout = { ["remotehost"] = s2sout_session } }
local local_user_session = { username = "user", host = "localhost", resource = "resource", full_jid = "user@localhost/resource", type = "c2s" }
- local hosts = {
- ["localhost"] = local_host_session;
- }
-
+
+ _G.prosody.hosts["localhost"] = local_host_session;
+ _G.prosody.full_sessions["user@localhost/resource"] = local_user_session;
+ _G.prosody.bare_sessions["user@localhost"] = { sessions = { resource = local_user_session } };
+
-- Test message routing
local function test_message_full_jid()
local env = testlib_new_env();
@@ -24,12 +26,14 @@ function core_process_stanza(core_process_stanza)
local target_routed;
- function env.core_route_stanza(p_origin, p_stanza)
+ function env.core_post_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;
+ env.prosody = { 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");
@@ -41,11 +45,12 @@ function core_process_stanza(core_process_stanza)
local target_routed;
- function env.core_route_stanza(p_origin, p_stanza)
+ function env.core_post_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");
@@ -58,14 +63,12 @@ function core_process_stanza(core_process_stanza)
local target_handled;
- function env.core_route_stanza(p_origin, p_stanza)
- end
-
- function env.core_handle_stanza(p_origin, p_stanza)
+ function env.core_post_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");
@@ -84,6 +87,8 @@ function core_process_stanza(core_process_stanza)
target_routed = true;
end
+ function env.core_post_stanza(...) env.core_route_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");
@@ -102,6 +107,10 @@ function core_process_stanza(core_process_stanza)
target_routed = true;
end
+ function env.core_post_stanza(...)
+ env.core_route_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");
@@ -113,7 +122,7 @@ function core_process_stanza(core_process_stanza)
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 msg = stanza.stanza("iq", { to = "remotehost", type = "get", id = "id" }):tag("body"):text("Hello world");
local target_routed;
@@ -123,8 +132,8 @@ function core_process_stanza(core_process_stanza)
target_routed = true;
end
- function env.core_handle_stanza(p_origin, p_stanza)
-
+ function env.core_post_stanza(...)
+ env.core_route_stanza(...);
end
env.hosts = hosts;
@@ -135,7 +144,7 @@ function core_process_stanza(core_process_stanza)
local function test_iq_error_to_local_user()
local env = testlib_new_env();
- 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' });
+ local msg = stanza.stanza("iq", { to = "user@localhost/resource", from = "user@remotehost", type = "error", id = "id" }):tag("error", { type = 'cancel' }):tag("item-not-found", { xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' });
local target_routed;
@@ -145,8 +154,8 @@ function core_process_stanza(core_process_stanza)
target_routed = true;
end
- function env.core_handle_stanza(p_origin, p_stanza)
-
+ function env.core_post_stanza(...)
+ env.core_route_stanza(...);
end
env.hosts = hosts;
@@ -157,20 +166,16 @@ function core_process_stanza(core_process_stanza)
local function test_iq_to_local_bare()
local env = testlib_new_env();
- local msg = stanza.stanza("iq", { to = "user@localhost", from = "user@localhost", type = "get" }):tag("ping", { xmlns = "urn:xmpp:ping:0" });
+ local msg = stanza.stanza("iq", { to = "user@localhost", from = "user@localhost", type = "get", id = "id" }):tag("ping", { xmlns = "urn:xmpp:ping:0" });
local target_handled;
- function env.core_handle_stanza(p_origin, p_stanza)
+ function env.core_post_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
- function env.core_route_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");
@@ -189,6 +194,7 @@ function core_process_stanza(core_process_stanza)
end
function core_route_stanza(core_route_stanza)
+ local stanza = require "util.stanza";
local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" }
local s2sin_session = { from_host = "remotehost", to_host = "localhost", type = "s2sin", hosts = { ["remotehost"] = { authed = true } } }
local local_host_session = { host = "localhost", type = "local", s2sout = { ["remotehost"] = s2sout_session }, sessions = {} }
@@ -204,7 +210,7 @@ function core_route_stanza(core_route_stanza)
--package.loaded["core.usermanager"] = { user_exists = function (user, host) print("RAR!") return true or user == "user" and host == "localhost" and true; end };
local target_handled, target_replied;
- function env.core_handle_stanza(p_origin, p_stanza)
+ function env.core_post_stanza(p_origin, p_stanza)
target_handled = true;
end
@@ -222,5 +228,5 @@ function core_route_stanza(core_route_stanza)
package.loaded["core.usermanager"] = nil;
end
- runtest(test_iq_result_to_offline_user, "iq type=result|error to an offline user are not replied to");
+ --runtest(test_iq_result_to_offline_user, "iq type=result|error to an offline user are not replied to");
end