build fix
[LibreOffice.git] / sal / cppunittester / cppunittester.cxx
blobd08d5c0b1f2667dc7d32e5081bb11fd7453414f4
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 #include <windows.h>
22 #endif
24 #ifdef UNX
25 #include <sys/time.h>
26 #include <sys/resource.h>
27 #endif
29 #include <cstdlib>
30 #include <iostream>
31 #include <string>
32 #include <sal/types.h>
33 #include "cppunittester/protectorfactory.hxx"
34 #include "osl/module.h"
35 #include "osl/module.hxx"
36 #include "osl/thread.h"
37 #include "rtl/process.h"
38 #include "rtl/string.h"
39 #include "rtl/string.hxx"
40 #include "rtl/textcvt.h"
41 #include "rtl/ustring.hxx"
42 #include "sal/main.h"
44 #include "cppunit/CompilerOutputter.h"
45 #include "cppunit/Exception.h"
46 #include "cppunit/TestFailure.h"
47 #include "cppunit/TestResult.h"
48 #include "cppunit/TestResultCollector.h"
49 #include "cppunit/TestRunner.h"
50 #include "cppunit/extensions/TestFactoryRegistry.h"
51 #include "cppunit/plugin/PlugInManager.h"
52 #include "cppunit/plugin/DynamicLibraryManagerException.h"
53 #include "cppunit/portability/Stream.h"
55 #include <memory>
56 #include <boost/algorithm/string.hpp>
58 #include <algorithm>
60 namespace {
62 void usageFailure() {
63 std::cerr
64 << ("Usage: cppunittester (--protector <shared-library-path>"
65 " <function-symbol>)* <shared-library-path>")
66 << std::endl;
67 std::exit(EXIT_FAILURE);
70 rtl::OUString getArgument(sal_Int32 index) {
71 rtl::OUString arg;
72 osl_getCommandArg(index, &arg.pData);
73 return arg;
76 std::string convertLazy(rtl::OUString const & s16) {
77 rtl::OString s8(rtl::OUStringToOString(s16, osl_getThreadTextEncoding()));
78 static_assert(sizeof (sal_Int32) <= sizeof (std::string::size_type), "must be at least the same size");
79 // ensure following cast is legitimate
80 return std::string(
81 s8.getStr(), static_cast< std::string::size_type >(s8.getLength()));
84 //Output how long each test took
85 class TimingListener
86 : public CppUnit::TestListener
88 public:
89 TimingListener()
90 : m_nStartTime(0)
93 TimingListener(const TimingListener&) = delete;
94 TimingListener& operator=(const TimingListener&) = delete;
96 void startTest( CppUnit::Test *) override
98 m_nStartTime = osl_getGlobalTimer();
101 void endTest( CppUnit::Test *test ) override
103 sal_uInt32 nEndTime = osl_getGlobalTimer();
104 std::cout << test->getName() << " finished in: "
105 << nEndTime-m_nStartTime << "ms" << std::endl;
108 private:
109 sal_uInt32 m_nStartTime;
112 #ifdef UNX
113 #include <stdlib.h>
114 // Setup an env variable so that temp file (or other) can
115 // have a useful value to identify the source
116 class EyecatcherListener
117 : public CppUnit::TestListener
119 public:
120 EyecatcherListener() = default;
121 EyecatcherListener(const EyecatcherListener&) = delete;
122 EyecatcherListener& operator=(const EyecatcherListener&) = delete;
123 void startTest( CppUnit::Test* test) override
125 std::unique_ptr<char[]> tn(new char [ test->getName().length() + 2 ]);
126 strcpy(tn.get(), test->getName().c_str());
127 int len = strlen(tn.get());
128 for(int i = 0; i < len; i++)
130 if(!isalnum(tn[i]))
132 tn[i] = '_';
135 tn[len] = '_';
136 tn[len + 1] = 0;
137 setenv("LO_TESTNAME", tn.get(), true);
140 void endTest( CppUnit::Test* /* test */ ) override
144 #endif
146 class LogFailuresAsTheyHappen : public CppUnit::TestListener
148 public:
149 virtual void addFailure( const CppUnit::TestFailure &failure ) override
151 printFailureLocation( failure.sourceLine() );
152 printFailedTestName( failure );
153 printFailureMessage( failure );
156 private:
157 static void printFailureLocation( const CppUnit::SourceLine &sourceLine )
159 if ( !sourceLine.isValid() )
160 std::cerr << "unknown:0:";
161 else
162 std::cerr << sourceLine.fileName() << ":" << sourceLine.lineNumber() << ":";
165 static void printFailedTestName( const CppUnit::TestFailure &failure )
167 std::cerr << failure.failedTestName() << std::endl;
170 static void printFailureMessage( const CppUnit::TestFailure &failure )
172 std::cerr << failure.thrownException()->message().shortDescription() << std::endl;
173 std::cerr << failure.thrownException()->message().details() << std::endl;
177 namespace {
179 struct test_name_compare
181 explicit test_name_compare(const std::string& rName):
182 maName(rName)
186 bool operator()(const std::string& rCmp)
188 size_t nPos = maName.find(rCmp);
189 if (nPos == std::string::npos)
190 return false;
192 size_t nEndPos = nPos + rCmp.size();
193 return nEndPos == maName.size();
196 std::string maName;
199 void addRecursiveTests(const std::vector<std::string>& test_names, CppUnit::Test* pTest, CppUnit::TestRunner& rRunner)
201 for (int i = 0; i < pTest->getChildTestCount(); ++i)
203 CppUnit::Test* pNewTest = pTest->getChildTestAt(i);
204 addRecursiveTests(test_names, pNewTest, rRunner);
205 if (std::find_if(test_names.begin(), test_names.end(), test_name_compare(pNewTest->getName())) != test_names.end())
206 rRunner.addTest(pNewTest);
212 //Allow the whole uniting testing framework to be run inside a "Protector"
213 //which knows about uno exceptions, so it can print the content of the
214 //exception before falling over and dying
215 class CPPUNIT_API ProtectedFixtureFunctor
216 : public CppUnit::Functor
218 private:
219 const std::string &testlib;
220 const std::string &args;
221 std::vector<CppUnit::Protector *> &protectors;
222 CppUnit::TestResult &result;
223 public:
224 ProtectedFixtureFunctor(const std::string& testlib_, const std::string &args_, std::vector<CppUnit::Protector*> &protectors_, CppUnit::TestResult &result_)
225 : testlib(testlib_)
226 , args(args_)
227 , protectors(protectors_)
228 , result(result_)
231 ProtectedFixtureFunctor(const ProtectedFixtureFunctor&) = delete;
232 ProtectedFixtureFunctor& operator=(const ProtectedFixtureFunctor&) = delete;
233 bool run() const
235 #ifdef DISABLE_DYNLOADING
237 // NOTE: Running cppunit unit tests on iOS was something I did
238 // only very early (several years ago) when starting porting
239 // this stuff to iOS. The complicated mechanisms to do build
240 // such unit test single executables have surely largely
241 // bit-rotted or been semi-intentionally broken since. This
242 // stuff here left for information only. --tml 2014.
244 // For iOS cppunit plugins aren't really "plugins" (shared
245 // libraries), but just static archives. In the real main
246 // program of a cppunit app, which calls the lo_main() that
247 // the SAL_IMPLEMENT_MAIN() below expands to, we specifically
248 // call the initialize methods of the CppUnitTestPlugIns that
249 // we statically link to the app executable.
250 #else
251 CppUnit::PlugInManager manager;
252 try {
253 manager.load(testlib, args);
254 } catch (const CppUnit::DynamicLibraryManagerException &e) {
255 std::cerr << "DynamicLibraryManagerException: \"" << e.what() << "\"\n";
256 #ifdef _WIN32
257 const char *pPath = getenv ("PATH");
258 if (pPath && strlen (pPath) > 256)
260 std::cerr << "Windows has significant build problems with long PATH variables ";
261 std::cerr << "please check your PATH variable and re-autogen.\n";
263 #endif
264 std::cerr << "Path is '" << getenv("PATH") << "'\n";
265 return false;
267 #endif
269 for (size_t i = 0; i < protectors.size(); ++i)
270 result.pushProtector(protectors[i]);
272 bool success;
274 CppUnit::TestResultCollector collector;
275 result.addListener(&collector);
277 LogFailuresAsTheyHappen logger;
278 result.addListener(&logger);
280 TimingListener timer;
281 result.addListener(&timer);
283 #ifdef UNX
284 EyecatcherListener eye;
285 result.addListener(&eye);
286 // set this to track down files created before first test method
287 std::string lib(testlib.substr(testlib.rfind('/')+1));
288 setenv("LO_TESTNAME", lib.c_str(), true);
289 #endif
291 const char* pVal = getenv("CPPUNIT_TEST_NAME");
293 CppUnit::TestRunner runner;
294 if (pVal)
296 std::vector<std::string> test_names;
297 boost::split(test_names, pVal, boost::is_any_of("\t "));
298 CppUnit::Test* pTest = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
299 addRecursiveTests(test_names, pTest, runner);
301 else
303 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
305 runner.run(result);
307 CppUnit::CompilerOutputter outputter(&collector, CppUnit::stdCErr());
308 outputter.setNoWrap();
309 outputter.write();
310 success = collector.wasSuccessful();
313 for (size_t i = 0; i < protectors.size(); ++i)
314 result.popProtector();
316 return success;
318 virtual bool operator()() const override
320 return run();
324 #ifdef UNX
326 double get_time(timeval* time)
328 double nTime = (double)time->tv_sec;
329 nTime += ((double)time->tv_usec)/1000000.0;
330 return nTime;
333 OString generateTestName(const OUString& rPath)
335 sal_Int32 nPathSep = rPath.lastIndexOf("/");
336 OUString aTestName = rPath.copy(nPathSep+1);
337 return OUStringToOString(aTestName, RTL_TEXTENCODING_UTF8);
340 void reportResourceUsage(const OUString& rPath)
342 OUString aFullPath = rPath + ".resource.log";
343 rusage resource_usage;
344 getrusage(RUSAGE_SELF, &resource_usage);
346 OString aPath = OUStringToOString(aFullPath, RTL_TEXTENCODING_UTF8);
347 std::ofstream resource_file(aPath.getStr());
348 resource_file << "Name = " << generateTestName(rPath) << std::endl;
349 double nUserSpace = get_time(&resource_usage.ru_utime);
350 double nKernelSpace = get_time(&resource_usage.ru_stime);
351 resource_file << "UserSpace = " << nUserSpace << std::endl;
352 resource_file << "KernelSpace = " << nKernelSpace << std::endl;
354 #else
355 void reportResourceUsage(const OUString& /*rPath*/)
358 #endif
362 SAL_IMPLEMENT_MAIN()
364 bool ok = false;
365 OUString path;
368 #ifdef _WIN32
369 //Disable Dr-Watson in order to crash simply without popup dialogs under
370 //windows
371 DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
372 SetErrorMode(SEM_NOGPFAULTERRORBOX|dwMode);
373 #ifdef _DEBUG // These functions are present only in the debugging runtime
374 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
375 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
376 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
377 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
378 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
379 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
380 #endif
381 #endif
383 std::vector<CppUnit::Protector *> protectors;
384 CppUnit::TestResult result;
385 std::string args;
386 std::string testlib;
387 sal_uInt32 index = 0;
388 while (index < osl_getCommandArgCount())
390 rtl::OUString arg = getArgument(index);
391 if (arg.startsWith("-env:CPPUNITTESTTARGET=", &path))
393 ++index;
394 continue;
396 if (arg.startsWith("-env:"))
398 ++index;
399 continue; // ignore it here - will be read later
401 if ( arg != "--protector" )
403 if (testlib.empty())
405 testlib = rtl::OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
406 args += testlib;
408 else
410 args += ' ';
411 args += rtl::OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
413 ++index;
414 continue;
416 if (osl_getCommandArgCount() - index < 3) {
417 usageFailure();
419 rtl::OUString lib(getArgument(index + 1));
420 rtl::OUString sym(getArgument(index + 2));
421 #ifndef DISABLE_DYNLOADING
422 osl::Module mod(lib, SAL_LOADMODULE_GLOBAL);
423 oslGenericFunction fn = mod.getFunctionSymbol(sym);
424 mod.release();
425 #else
426 oslGenericFunction fn = 0;
427 if (sym == "unoexceptionprotector")
428 fn = (oslGenericFunction) unoexceptionprotector;
429 else if (sym == "unobootstrapprotector")
430 fn = (oslGenericFunction) unobootstrapprotector;
431 else if (sym == "vclbootstrapprotector")
432 fn = (oslGenericFunction) vclbootstrapprotector;
433 else
435 std::cerr
436 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
437 << std::endl;
438 std::exit(EXIT_FAILURE);
440 #endif
441 CppUnit::Protector *protector = fn == nullptr
442 ? nullptr
443 : (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))();
444 if (protector == nullptr) {
445 std::cerr
446 << "Failure instantiating protector \"" << convertLazy(lib)
447 << "\", \"" << convertLazy(sym) << '"' << std::endl;
448 std::exit(EXIT_FAILURE);
450 protectors.push_back(protector);
451 index+=3;
454 ProtectedFixtureFunctor tests(testlib, args, protectors, result);
455 ok = tests.run();
457 catch (const std::exception& e)
459 SAL_WARN("vcl.app", "Fatal exception: " << e.what());
462 reportResourceUsage(path);
464 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
467 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */