Merge 0.10->trunk
[prosody.git] / tests / test.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local tests_passed = true;
10
11 function run_all_tests()
12         package.loaded["net.connlisteners"] = { get = function () return {} end };
13         dotest "util.jid"
14         dotest "util.multitable"
15         dotest "util.rfc6724"
16         dotest "util.http"
17         dotest "core.stanza_router"
18         dotest "core.s2smanager"
19         dotest "core.configmanager"
20         dotest "util.ip"
21         dotest "util.stanza"
22         dotest "util.sasl.scram"
23         dotest "util.cache"
24         dotest "util.throttle"
25         dotest "util.uuid"
26         dotest "util.random"
27
28         dosingletest("test_sasl.lua", "latin1toutf8");
29         dosingletest("test_utf8.lua", "valid");
30 end
31
32 local verbosity = tonumber(arg[1]) or 2;
33
34 if os.getenv("WINDIR") then
35         package.path = package.path..";..\\?.lua";
36         package.cpath = package.cpath..";..\\?.dll";
37 else
38         package.path = package.path..";../?.lua";
39         package.cpath = package.cpath..";../?.so";
40 end
41
42 local _realG = _G;
43
44 require "util.import"
45
46 local envloadfile = require "util.envload".envloadfile;
47
48 local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
49 function testlib_new_env(t)
50         return setmetatable(t or {}, env_mt);
51 end
52
53 function assert_equal(a, b, message, level)
54         if not (a == b) then
55                 error("\n   assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n   Message: "..message) or ""), (level or 1) + 1);
56         elseif verbosity >= 4 then
57                 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
58         end
59 end
60
61 function assert_table(a, message, level)
62         assert_equal(type(a), "table", message, (level or 1) + 1);
63 end
64 function assert_function(a, message, level)
65         assert_equal(type(a), "function", message, (level or 1) + 1);
66 end
67 function assert_string(a, message, level)
68         assert_equal(type(a), "string", message, (level or 1) + 1);
69 end
70 function assert_boolean(a, message)
71         assert_equal(type(a), "boolean", message);
72 end
73 function assert_is(a, message)
74         assert_equal(not not a, true, message);
75 end
76 function assert_is_not(a, message)
77         assert_equal(not not a, false, message);
78 end
79
80
81 function dosingletest(testname, fname)
82         local tests = setmetatable({}, { __index = _realG });
83         tests.__unit = testname;
84         tests.__test = fname;
85         local chunk, err = envloadfile(testname, tests);
86         if not chunk then
87                 print("WARNING: ", "Failed to load tests for "..testname, err);
88                 return;
89         end
90
91         local success, err = pcall(chunk);
92         if not success then
93                 print("WARNING: ", "Failed to initialise tests for "..testname, err);
94                 return;
95         end
96
97         if type(tests[fname]) ~= "function" then
98                 error(testname.." has no test '"..fname.."'", 0);
99         end
100
101
102         local line_hook, line_info = new_line_coverage_monitor(testname);
103         debug.sethook(line_hook, "l")
104         local success, ret = pcall(tests[fname]);
105         debug.sethook();
106         if not success then
107                 tests_passed = false;
108                 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
109                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
110                 line_info(fname, false, report_file);
111         elseif verbosity >= 2 then
112                 print("TEST SUCCEEDED: ", testname, fname);
113                 print(string.format("TEST COVERED %d/%d lines", line_info(fname, true, report_file)));
114         else
115                 line_info(name, success, report_file);
116         end
117 end
118
119 function dotest(unitname)
120         local _fakeG = setmetatable({}, {__index = _realG});
121         _fakeG._G = _fakeG;
122         local tests = setmetatable({}, { __index = _fakeG });
123         tests.__unit = unitname;
124         local chunk, err = envloadfile("test_"..unitname:gsub("%.", "_")..".lua", tests);
125         if not chunk then
126                 print("WARNING: ", "Failed to load tests for "..unitname, err);
127                 return;
128         end
129
130         local success, err = pcall(chunk);
131         if not success then
132                 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
133                 return;
134         end
135         if tests.env then setmetatable(tests.env, { __index = _realG }); end
136         local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _fakeG }, { __index = tests.env or _fakeG }) });
137         local fn = "../"..unitname:gsub("%.", "/")..".lua";
138         local chunk, err = envloadfile(fn, unit);
139         if not chunk then
140                 print("WARNING: ", "Failed to load module: "..unitname, err);
141                 return;
142         end
143
144         local oldmodule, old_M = _fakeG.module, _fakeG._M;
145         _fakeG.module = function ()
146                 setmetatable(unit, nil);
147                 unit._M = unit;
148         end
149         local success, ret = pcall(chunk);
150         _fakeG.module, _fakeG._M = oldmodule, old_M;
151         if not success then
152                 print("WARNING: ", "Failed to initialise module: "..unitname, ret);
153                 return;
154         end
155
156         if type(ret) == "table" then
157                 for k,v in pairs(ret) do
158                         unit[k] = v;
159                 end
160         end
161
162         for name, f in pairs(unit) do
163                 local test = rawget(tests, name);
164                 if type(f) ~= "function" then
165                         if verbosity >= 3 then
166                                 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
167                         end
168                 elseif type(test) ~= "function" then
169                         if verbosity >= 1 then
170                                 print("WARNING: ", unitname.."."..name.." has no test!");
171                         end
172                 else
173                         if verbosity >= 4 then
174                                 print("INFO: ", "Testing "..unitname.."."..name);
175                         end
176                         local line_hook, line_info = new_line_coverage_monitor(fn);
177                         debug.sethook(line_hook, "l")
178                         local success, ret = pcall(test, f, unit);
179                         debug.sethook();
180                         if not success then
181                                 tests_passed = false;
182                                 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
183                                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
184                                 line_info(name, false, report_file);
185                         elseif verbosity >= 2 then
186                                 print("TEST SUCCEEDED: ", unitname, name);
187                                 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
188                         else
189                                 line_info(name, success, report_file);
190                         end
191                 end
192         end
193 end
194
195 function runtest(f, msg)
196         if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
197         local success, ret = pcall(f);
198         if success and verbosity >= 2 then
199                 print("SUBTEST PASSED: "..(msg or "(no description)"));
200         elseif (not success) and verbosity >= 0 then
201                 tests_passed = false;
202                 print("SUBTEST FAILED: "..(msg or "(no description)"));
203                 error(ret, 0);
204         end
205 end
206
207 function new_line_coverage_monitor(file)
208         local lines_hit, funcs_hit = {}, {};
209         local total_lines, covered_lines = 0, 0;
210
211         for line in io.lines(file) do
212                 total_lines = total_lines + 1;
213         end
214
215         return function (event, line) -- Line hook
216                         if not lines_hit[line] then
217                                 local info = debug.getinfo(2, "fSL")
218                                 if not info.source:find(file) then return; end
219                                 if not funcs_hit[info.func] and info.activelines then
220                                         funcs_hit[info.func] = true;
221                                         for line in pairs(info.activelines) do
222                                                 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
223                                         end
224                                 end
225                                 if lines_hit[line] == false then
226                                         --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
227                                         lines_hit[line] = true;
228                                         covered_lines = covered_lines + 1;
229                                 end
230                         end
231                 end,
232                 function (test_name, success) -- Get info
233                         local fn = file:gsub("^%W*", "");
234                         local total_active_lines = 0;
235                         local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
236                         for line, active in pairs(lines_hit) do
237                                 if active ~= nil then total_active_lines = total_active_lines + 1; end
238                                 if coverage_file then
239                                         if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
240                                         else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
241                                 end
242                         end
243                         if coverage_file then coverage_file:close(); end
244                         return covered_lines, total_active_lines, lines_hit;
245                 end
246 end
247
248 run_all_tests()
249
250 os.exit(tests_passed and 0 or 1);