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