tests: Add UTF-8 validity tests
[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 () _M = _G end
141         setfenv(chunk, unit);
142         local success, err = pcall(chunk);
143         _fakeG.module, _fakeG._M = oldmodule, old_M;
144         if not success then
145                 print("WARNING: ", "Failed to initialise module: "..unitname, err);
146                 return;
147         end
148         
149         for name, f in pairs(unit) do
150                 local test = rawget(tests, name);
151                 if type(f) ~= "function" then
152                         if verbosity >= 3 then
153                                 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
154                         end
155                 elseif type(test) ~= "function" then
156                         if verbosity >= 1 then
157                                 print("WARNING: ", unitname.."."..name.." has no test!");
158                         end
159                 else
160                         if verbosity >= 4 then
161                                 print("INFO: ", "Testing "..unitname.."."..name);
162                         end
163                         local line_hook, line_info = new_line_coverage_monitor(fn);
164                         debug.sethook(line_hook, "l")
165                         local success, ret = pcall(test, f, unit);
166                         debug.sethook();
167                         if not success then
168                                 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
169                                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
170                                 line_info(name, false, report_file);
171                         elseif verbosity >= 2 then
172                                 print("TEST SUCCEEDED: ", unitname, name);
173                                 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
174                         else
175                                 line_info(name, success, report_file);
176                         end
177                 end
178         end
179 end
180
181 function runtest(f, msg)
182         if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
183         local success, ret = pcall(f);
184         if success and verbosity >= 2 then
185                 print("SUBTEST PASSED: "..(msg or "(no description)"));
186         elseif (not success) and verbosity >= 0 then
187                 print("SUBTEST FAILED: "..(msg or "(no description)"));
188                 error(ret, 0);
189         end
190 end
191
192 function new_line_coverage_monitor(file)
193         local lines_hit, funcs_hit = {}, {};
194         local total_lines, covered_lines = 0, 0;
195         
196         for line in io.lines(file) do
197                 total_lines = total_lines + 1;
198         end
199         
200         return function (event, line) -- Line hook
201                         if not lines_hit[line] then
202                                 local info = debug.getinfo(2, "fSL")
203                                 if not info.source:find(file) then return; end
204                                 if not funcs_hit[info.func] and info.activelines then
205                                         funcs_hit[info.func] = true;
206                                         for line in pairs(info.activelines) do
207                                                 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
208                                         end
209                                 end
210                                 if lines_hit[line] == false then
211                                         --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
212                                         lines_hit[line] = true;
213                                         covered_lines = covered_lines + 1;
214                                 end
215                         end
216                 end,
217                 function (test_name, success) -- Get info
218                         local fn = file:gsub("^%W*", "");
219                         local total_active_lines = 0;
220                         local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
221                         for line, active in pairs(lines_hit) do
222                                 if active ~= nil then total_active_lines = total_active_lines + 1; end
223                                 if coverage_file then
224                                         if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
225                                         else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
226                                 end
227                         end
228                         if coverage_file then coverage_file:close(); end
229                         return covered_lines, total_active_lines, lines_hit;
230                 end
231 end
232
233 run_all_tests()