Bump version to 6.4-15
[LibreOffice.git] / sal / cppunittester / cppunittester.cxx
blob3e2cdd512e6815feff6420ae5fc1be592d2a676d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifdef _WIN32
21 #if !defined WIN32_LEAN_AND_MEAN
22 # define WIN32_LEAN_AND_MEAN
23 #endif
24 #include <windows.h>
25 #endif
27 #ifdef UNX
28 #include <sys/time.h>
29 #include <sys/resource.h>
30 #endif
32 #include <cstdlib>
33 #include <iostream>
34 #include <string>
35 #include <sal/log.hxx>
36 #include <sal/types.h>
37 #include <cppunittester/protectorfactory.hxx>
38 #include <osl/module.h>
39 #include <osl/module.hxx>
40 #include <osl/thread.h>
41 #include <rtl/character.hxx>
42 #include <rtl/process.h>
43 #include <rtl/string.h>
44 #include <rtl/string.hxx>
45 #include <rtl/textcvt.h>
46 #include <rtl/ustring.hxx>
47 #include <sal/main.h>
49 #include <cppunit/CompilerOutputter.h>
50 #include <cppunit/Exception.h>
51 #include <cppunit/TestFailure.h>
52 #include <cppunit/TestResult.h>
53 #include <cppunit/TestResultCollector.h>
54 #include <cppunit/TestRunner.h>
55 #include <cppunit/extensions/TestFactoryRegistry.h>
56 #include <cppunit/plugin/PlugInManager.h>
57 #include <cppunit/plugin/DynamicLibraryManagerException.h>
58 #include <cppunit/portability/Stream.h>
60 #include <memory>
61 #include <boost/algorithm/string.hpp>
63 #include <algorithm>
65 namespace {
67 void usageFailure() {
68 std::cerr
69 << ("Usage: cppunittester (--protector <shared-library-path>"
70 " <function-symbol>)* <shared-library-path>")
71 << std::endl;
72 std::exit(EXIT_FAILURE);
75 OUString getArgument(sal_Int32 index) {
76 OUString arg;
77 osl_getCommandArg(index, &arg.pData);
78 return arg;
81 std::string convertLazy(OUString const & s16) {
82 OString s8(OUStringToOString(s16, osl_getThreadTextEncoding()));
83 static_assert(sizeof (sal_Int32) <= sizeof (std::string::size_type), "must be at least the same size");
84 // ensure following cast is legitimate
85 return std::string(
86 s8.getStr(), static_cast< std::string::size_type >(s8.getLength()));
89 //Output how long each test took
90 class TimingListener
91 : public CppUnit::TestListener
93 public:
94 TimingListener()
95 : m_nStartTime(0)
98 TimingListener(const TimingListener&) = delete;
99 TimingListener& operator=(const TimingListener&) = delete;
101 void startTest( CppUnit::Test *) override
103 m_nStartTime = osl_getGlobalTimer();
106 void endTest( CppUnit::Test *test ) override
108 sal_uInt32 nEndTime = osl_getGlobalTimer();
109 std::cout << test->getName() << " finished in: "
110 << nEndTime-m_nStartTime << "ms" << std::endl;
113 private:
114 sal_uInt32 m_nStartTime;
117 #ifdef UNX
118 #include <stdlib.h>
119 // Setup an env variable so that temp file (or other) can
120 // have a useful value to identify the source
121 class EyecatcherListener
122 : public CppUnit::TestListener
124 public:
125 EyecatcherListener() = default;
126 EyecatcherListener(const EyecatcherListener&) = delete;
127 EyecatcherListener& operator=(const EyecatcherListener&) = delete;
128 void startTest( CppUnit::Test* test) override
130 std::unique_ptr<char[]> tn(new char [ test->getName().length() + 2 ]);
131 strcpy(tn.get(), test->getName().c_str());
132 int len = strlen(tn.get());
133 for(int i = 0; i < len; i++)
135 if(!rtl::isAsciiAlphanumeric(static_cast<unsigned char>(tn[i])))
137 tn[i] = '_';
140 tn[len] = '_';
141 tn[len + 1] = 0;
142 setenv("LO_TESTNAME", tn.get(), true);
145 void endTest( CppUnit::Test* /* test */ ) override
149 #endif
151 class LogFailuresAsTheyHappen : public CppUnit::TestListener
153 public:
154 virtual void addFailure( const CppUnit::TestFailure &failure ) override
156 printFailureLocation( failure.sourceLine() );
157 printFailedTestName( failure );
158 printFailureMessage( failure );
161 private:
162 static void printFailureLocation( const CppUnit::SourceLine &sourceLine )
164 if ( !sourceLine.isValid() )
165 std::cerr << "unknown:0:";
166 else
167 std::cerr << sourceLine.fileName() << ":" << sourceLine.lineNumber() << ":";
170 static void printFailedTestName( const CppUnit::TestFailure &failure )
172 std::cerr << failure.failedTestName() << std::endl;
175 static void printFailureMessage( const CppUnit::TestFailure &failure )
177 std::cerr << failure.thrownException()->message().shortDescription() << std::endl;
178 std::cerr << failure.thrownException()->message().details() << std::endl;
182 struct test_name_compare
184 explicit test_name_compare(const std::string& rName):
185 maName(rName)
189 bool operator()(const std::string& rCmp)
191 size_t nPos = maName.find(rCmp);
192 if (nPos == std::string::npos)
193 return false;
195 size_t nEndPos = nPos + rCmp.size();
196 return nEndPos == maName.size();
199 std::string const maName;
202 bool addRecursiveTests(const std::vector<std::string>& test_names, CppUnit::Test* pTest, CppUnit::TestRunner& rRunner)
204 bool ret(false);
205 for (int i = 0; i < pTest->getChildTestCount(); ++i)
207 CppUnit::Test* pNewTest = pTest->getChildTestAt(i);
208 ret |= addRecursiveTests(test_names, pNewTest, rRunner);
209 if (std::any_of(test_names.begin(), test_names.end(), test_name_compare(pNewTest->getName())))
211 rRunner.addTest(pNewTest);
212 ret = true;
215 return ret;
218 //Allow the whole uniting testing framework to be run inside a "Protector"
219 //which knows about uno exceptions, so it can print the content of the
220 //exception before falling over and dying
221 class CPPUNIT_API ProtectedFixtureFunctor
222 : public CppUnit::Functor
224 private:
225 const std::string &testlib;
226 const std::string &args;
227 std::vector<CppUnit::Protector *> &protectors;
228 CppUnit::TestResult &result;
229 public:
230 ProtectedFixtureFunctor(const std::string& testlib_, const std::string &args_, std::vector<CppUnit::Protector*> &protectors_, CppUnit::TestResult &result_)
231 : testlib(testlib_)
232 , args(args_)
233 , protectors(protectors_)
234 , result(result_)
237 ProtectedFixtureFunctor(const ProtectedFixtureFunctor&) = delete;
238 ProtectedFixtureFunctor& operator=(const ProtectedFixtureFunctor&) = delete;
239 bool run() const
241 #ifdef DISABLE_DYNLOADING
243 // NOTE: Running cppunit unit tests on iOS was something I did
244 // only very early (several years ago) when starting porting
245 // this stuff to iOS. The complicated mechanisms to do build
246 // such unit test single executables have surely largely
247 // bit-rotted or been semi-intentionally broken since. This
248 // stuff here left for information only. --tml 2014.
250 // For iOS cppunit plugins aren't really "plugins" (shared
251 // libraries), but just static archives. In the real main
252 // program of a cppunit app, which calls the lo_main() that
253 // the SAL_IMPLEMENT_MAIN() below expands to, we specifically
254 // call the initialize methods of the CppUnitTestPlugIns that
255 // we statically link to the app executable.
256 #else
257 CppUnit::PlugInManager manager;
258 try {
259 manager.load(testlib, args);
260 } catch (const CppUnit::DynamicLibraryManagerException &e) {
261 std::cerr << "DynamicLibraryManagerException: \"" << e.what() << "\"\n";
262 #ifdef _WIN32
263 const char *pPath = getenv ("PATH");
264 if (pPath && strlen (pPath) > 256)
266 std::cerr << "Windows has significant build problems with long PATH variables ";
267 std::cerr << "please check your PATH variable and re-autogen.\n";
269 #endif
270 std::cerr << "Path is '" << getenv("PATH") << "'\n";
271 return false;
273 #endif
275 for (size_t i = 0; i < protectors.size(); ++i)
276 result.pushProtector(protectors[i]);
278 bool success;
280 CppUnit::TestResultCollector collector;
281 result.addListener(&collector);
283 LogFailuresAsTheyHappen logger;
284 result.addListener(&logger);
286 TimingListener timer;
287 result.addListener(&timer);
289 #ifdef UNX
290 EyecatcherListener eye;
291 result.addListener(&eye);
292 // set this to track down files created before first test method
293 std::string lib(testlib.substr(testlib.rfind('/')+1));
294 setenv("LO_TESTNAME", lib.c_str(), true);
295 #endif
297 const char* pVal = getenv("CPPUNIT_TEST_NAME");
299 CppUnit::TestRunner runner;
300 if (pVal)
302 std::vector<std::string> test_names;
303 boost::split(test_names, pVal, boost::is_any_of("\t "));
304 CppUnit::Test* pTest = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
305 bool const added(addRecursiveTests(test_names, pTest, runner));
306 if (!added)
308 std::cerr << "\nFatal error: CPPUNIT_TEST_NAME contains no valid tests\n";
309 return false;
312 else
314 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
316 runner.run(result);
318 CppUnit::CompilerOutputter outputter(&collector, CppUnit::stdCErr());
319 outputter.setNoWrap();
320 outputter.write();
321 success = collector.wasSuccessful();
324 for (size_t i = 0; i < protectors.size(); ++i)
325 result.popProtector();
327 return success;
329 virtual bool operator()() const override
331 return run();
335 #ifdef UNX
337 double get_time(timeval* time)
339 double nTime = static_cast<double>(time->tv_sec);
340 nTime += static_cast<double>(time->tv_usec)/1000000.0;
341 return nTime;
344 OString generateTestName(const OUString& rPath)
346 sal_Int32 nPathSep = rPath.lastIndexOf("/");
347 OUString aTestName = rPath.copy(nPathSep+1);
348 return OUStringToOString(aTestName, RTL_TEXTENCODING_UTF8);
351 void reportResourceUsage(const OUString& rPath)
353 OUString aFullPath = rPath + ".resource.log";
354 rusage resource_usage;
355 getrusage(RUSAGE_SELF, &resource_usage);
357 OString aPath = OUStringToOString(aFullPath, RTL_TEXTENCODING_UTF8);
358 std::ofstream resource_file(aPath.getStr());
359 resource_file << "Name = " << generateTestName(rPath) << std::endl;
360 double nUserSpace = get_time(&resource_usage.ru_utime);
361 double nKernelSpace = get_time(&resource_usage.ru_stime);
362 resource_file << "UserSpace = " << nUserSpace << std::endl;
363 resource_file << "KernelSpace = " << nKernelSpace << std::endl;
365 #else
366 void reportResourceUsage(const OUString& /*rPath*/)
369 #endif
373 SAL_IMPLEMENT_MAIN()
375 bool ok = false;
376 OUString path;
379 #ifdef _WIN32
380 //Disable Dr-Watson in order to crash simply without popup dialogs under
381 //windows
382 DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
383 SetErrorMode(SEM_NOGPFAULTERRORBOX|dwMode);
384 #ifdef _DEBUG // These functions are present only in the debugging runtime
385 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
386 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
387 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
388 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
389 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
390 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
391 #endif
392 #endif
394 std::vector<CppUnit::Protector *> protectors;
395 CppUnit::TestResult result;
396 std::string args;
397 std::string testlib;
398 sal_uInt32 index = 0;
399 while (index < osl_getCommandArgCount())
401 OUString arg = getArgument(index);
402 if (arg.startsWith("-env:CPPUNITTESTTARGET=", &path))
404 ++index;
405 continue;
407 if (arg.startsWith("-env:"))
409 ++index;
410 continue; // ignore it here - will be read later
412 if ( arg != "--protector" )
414 if (testlib.empty())
416 testlib = OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
417 args += testlib;
419 else
421 args += ' ';
422 args += OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
424 ++index;
425 continue;
427 if (osl_getCommandArgCount() - index < 3) {
428 usageFailure();
430 OUString lib(getArgument(index + 1));
431 OUString sym(getArgument(index + 2));
432 #ifndef DISABLE_DYNLOADING
433 osl::Module mod(lib, SAL_LOADMODULE_GLOBAL);
434 oslGenericFunction fn = mod.getFunctionSymbol(sym);
435 mod.release();
436 #else
437 oslGenericFunction fn = 0;
438 if (sym == "unoexceptionprotector")
439 fn = (oslGenericFunction) unoexceptionprotector;
440 else if (sym == "unobootstrapprotector")
441 fn = (oslGenericFunction) unobootstrapprotector;
442 else if (sym == "vclbootstrapprotector")
443 fn = (oslGenericFunction) vclbootstrapprotector;
444 else
446 std::cerr
447 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
448 << std::endl;
449 std::exit(EXIT_FAILURE);
451 #endif
452 CppUnit::Protector *protector = fn == nullptr
453 ? nullptr
454 : (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))();
455 if (protector == nullptr) {
456 std::cerr
457 << "Failure instantiating protector \"" << convertLazy(lib)
458 << "\", \"" << convertLazy(sym) << '"' << std::endl;
459 std::exit(EXIT_FAILURE);
461 protectors.push_back(protector);
462 index+=3;
465 ProtectedFixtureFunctor tests(testlib, args, protectors, result);
466 ok = tests.run();
468 catch (const std::exception& e)
470 SAL_WARN("vcl.app", "Fatal exception: " << e.what());
473 reportResourceUsage(path);
475 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
478 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */