tdf#154546 skip dispatch when presenter controller is not set
[LibreOffice.git] / sal / cppunittester / cppunittester.cxx
blobc859ee5e102f95577123526f099b72c4ff5924e3
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
26 #if defined(_WIN32) && defined(_DEBUG)
27 #include <dbghelp.h>
28 #include <sal/backtrace.hxx>
29 #include <signal.h>
30 #endif
32 #ifdef UNX
33 #include <sys/time.h>
34 #include <sys/resource.h>
35 #endif
37 #include <stdlib.h>
38 #include <cstdlib>
39 #include <iostream>
40 #include <string>
41 #include <sal/log.hxx>
42 #include <sal/types.h>
43 #include <cppunittester/protectorfactory.hxx>
44 #include <osl/module.h>
45 #include <osl/module.hxx>
46 #include <osl/thread.h>
47 #include <rtl/character.hxx>
48 #include <rtl/process.h>
49 #include <rtl/string.h>
50 #include <rtl/string.hxx>
51 #include <rtl/strbuf.hxx>
52 #include <rtl/textcvt.h>
53 #include <rtl/ustring.hxx>
54 #include <sal/main.h>
56 #include <cppunit/CompilerOutputter.h>
57 #include <cppunit/Exception.h>
58 #include <cppunit/TestFailure.h>
59 #include <cppunit/TestResult.h>
60 #include <cppunit/TestResultCollector.h>
61 #include <cppunit/TestRunner.h>
62 #include <cppunit/extensions/TestFactoryRegistry.h>
63 #include <cppunit/plugin/PlugInManager.h>
64 #include <cppunit/plugin/DynamicLibraryManagerException.h>
65 #include <cppunit/portability/Stream.h>
67 #include <memory>
68 #include <boost/algorithm/string.hpp>
70 #include <algorithm>
71 #include <string_view>
72 #include <utility>
74 namespace {
76 void usageFailure() {
77 std::cerr
78 << ("Usage: cppunittester (--protector <shared-library-path>"
79 " <function-symbol>)* <shared-library-path>")
80 << std::endl;
81 std::exit(EXIT_FAILURE);
84 OUString getArgument(sal_Int32 index) {
85 OUString arg;
86 osl_getCommandArg(index, &arg.pData);
87 return arg;
90 std::string convertLazy(std::u16string_view s16) {
91 OString s8(OUStringToOString(s16, osl_getThreadTextEncoding()));
92 static_assert(sizeof (sal_Int32) <= sizeof (std::string::size_type), "must be at least the same size");
93 // ensure following cast is legitimate
94 return std::string(
95 s8.getStr(), static_cast< std::string::size_type >(s8.getLength()));
98 //Output how long each test took
99 class TimingListener
100 : public CppUnit::TestListener
102 public:
103 TimingListener()
104 : m_nStartTime(0)
107 TimingListener(const TimingListener&) = delete;
108 TimingListener& operator=(const TimingListener&) = delete;
110 void startTest( CppUnit::Test *test) override
112 std::cout << "[_RUN_____] " << test->getName() << std::endl;
113 m_nStartTime = osl_getGlobalTimer();
116 void endTest( CppUnit::Test *test ) override
118 sal_uInt32 nEndTime = osl_getGlobalTimer();
119 std::cout << test->getName() << " finished in: "
120 << nEndTime-m_nStartTime << "ms" << std::endl;
123 private:
124 sal_uInt32 m_nStartTime;
127 // Setup an env variable so that temp file (or other) can
128 // have a useful value to identify the source
129 class EyecatcherListener
130 : public CppUnit::TestListener
132 public:
133 EyecatcherListener() = default;
134 EyecatcherListener(const EyecatcherListener&) = delete;
135 EyecatcherListener& operator=(const EyecatcherListener&) = delete;
136 void startTest( CppUnit::Test* test) override
138 rtl::OStringBuffer tn(test->getName());
139 for(int i = 0; i < tn.getLength(); i++)
141 if(!rtl::isAsciiAlphanumeric(static_cast<unsigned char>(tn[i])))
143 tn[i] = '_';
146 tn.append('_');
147 #ifdef WIN32
148 _putenv_s("LO_TESTNAME", tn.getStr());
149 #else
150 setenv("LO_TESTNAME", tn.getStr(), true);
151 #endif
154 void endTest( CppUnit::Test* /* test */ ) override
159 class LogFailuresAsTheyHappen : public CppUnit::TestListener
161 public:
162 virtual void addFailure( const CppUnit::TestFailure &failure ) override
164 printFailureLocation( failure.sourceLine() );
165 printFailedTestName( failure );
166 printFailureMessage( failure );
169 private:
170 static void printFailureLocation( const CppUnit::SourceLine &sourceLine )
172 if ( !sourceLine.isValid() )
173 std::cerr << "unknown:0:";
174 else
175 std::cerr << sourceLine.fileName() << ":" << sourceLine.lineNumber() << ":";
178 static void printFailedTestName( const CppUnit::TestFailure &failure )
180 std::cerr << failure.failedTestName() << std::endl;
183 static void printFailureMessage( const CppUnit::TestFailure &failure )
185 std::cerr << failure.thrownException()->message().shortDescription() << std::endl;
186 std::cerr << failure.thrownException()->message().details() << std::endl;
190 struct test_name_compare
192 explicit test_name_compare(std::string aName):
193 maName(std::move(aName))
197 bool operator()(const std::string& rCmp)
199 size_t nPos = maName.find(rCmp);
200 if (nPos == std::string::npos)
201 return false;
203 size_t nEndPos = nPos + rCmp.size();
204 return nEndPos == maName.size();
207 std::string maName;
210 bool addRecursiveTests(const std::vector<std::string>& test_names, CppUnit::Test* pTest, CppUnit::TestRunner& rRunner)
212 bool ret(false);
213 for (int i = 0; i < pTest->getChildTestCount(); ++i)
215 CppUnit::Test* pNewTest = pTest->getChildTestAt(i);
216 ret |= addRecursiveTests(test_names, pNewTest, rRunner);
217 if (std::any_of(test_names.begin(), test_names.end(), test_name_compare(pNewTest->getName())))
219 rRunner.addTest(pNewTest);
220 ret = true;
223 return ret;
226 //Allow the whole uniting testing framework to be run inside a "Protector"
227 //which knows about uno exceptions, so it can print the content of the
228 //exception before falling over and dying
229 class CPPUNIT_API ProtectedFixtureFunctor
230 : public CppUnit::Functor
232 private:
233 const std::string &testlib;
234 const std::string &args;
235 std::vector<CppUnit::Protector *> &protectors;
236 CppUnit::TestResult &result;
237 public:
238 ProtectedFixtureFunctor(const std::string& testlib_, const std::string &args_, std::vector<CppUnit::Protector*> &protectors_, CppUnit::TestResult &result_)
239 : testlib(testlib_)
240 , args(args_)
241 , protectors(protectors_)
242 , result(result_)
245 ProtectedFixtureFunctor(const ProtectedFixtureFunctor&) = delete;
246 ProtectedFixtureFunctor& operator=(const ProtectedFixtureFunctor&) = delete;
247 bool run() const
249 #ifdef DISABLE_DYNLOADING
251 // NOTE: Running cppunit unit tests on iOS was something I did
252 // only very early (several years ago) when starting porting
253 // this stuff to iOS. The complicated mechanisms to do build
254 // such unit test single executables have surely largely
255 // bit-rotted or been semi-intentionally broken since. This
256 // stuff here left for information only. --tml 2014.
258 // For iOS cppunit plugins aren't really "plugins" (shared
259 // libraries), but just static archives. In the real main
260 // program of a cppunit app, which calls the lo_main() that
261 // the SAL_IMPLEMENT_MAIN() below expands to, we specifically
262 // call the initialize methods of the CppUnitTestPlugIns that
263 // we statically link to the app executable.
264 #else
265 // The PlugInManager instance is deliberately leaked, so that the dynamic libraries it loads
266 // are never unloaded (which could make e.g. pointers from other libraries' static data
267 // structures to const data in those libraries, like some static OUString cache pointing at
268 // a const OUStringLiteral, become dangling by the time those static data structures are
269 // destroyed during exit):
270 auto manager = new CppUnit::PlugInManager;
271 try {
272 manager->load(testlib, args);
273 } catch (const CppUnit::DynamicLibraryManagerException &e) {
274 std::cerr << "DynamicLibraryManagerException: \"" << e.what() << "\"\n";
275 const char *pPath = getenv ("PATH");
276 const size_t nPathLen = pPath ? strlen(pPath) : 0;
277 #ifdef _WIN32
278 if (nPathLen > 256)
280 std::cerr << "Windows has significant build problems with long PATH variables ";
281 std::cerr << "please check your PATH variable and re-autogen.\n";
283 #endif
284 std::cerr << "Path (length: " << nPathLen << ") is '" << pPath << "'\n";
285 return false;
287 #endif
289 for (size_t i = 0; i < protectors.size(); ++i)
290 result.pushProtector(protectors[i]);
292 bool success;
294 CppUnit::TestResultCollector collector;
295 result.addListener(&collector);
297 LogFailuresAsTheyHappen logger;
298 result.addListener(&logger);
300 TimingListener timer;
301 result.addListener(&timer);
303 EyecatcherListener eye;
304 result.addListener(&eye);
306 // set this to track down files created before first test method
307 std::string lib = testlib.substr(testlib.rfind('/')+1);
308 #ifdef WIN32
309 _putenv_s("LO_TESTNAME", lib.c_str());
310 #else
311 setenv("LO_TESTNAME", lib.c_str(), true);
312 #endif
313 const char* pVal = getenv("CPPUNIT_TEST_NAME");
315 CppUnit::TestRunner runner;
316 if (pVal)
318 std::vector<std::string> test_names;
319 boost::split(test_names, pVal, boost::is_any_of("\t "));
320 CppUnit::Test* pTest = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
321 bool const added(addRecursiveTests(test_names, pTest, runner));
322 if (!added)
324 std::cerr << "\nFatal error: CPPUNIT_TEST_NAME contains no valid tests\n";
325 // coverity[leaked_storage] - `manager` leaked on purpose
326 return false;
329 else
331 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
333 runner.run(result);
335 CppUnit::CompilerOutputter outputter(&collector, CppUnit::stdCErr());
336 outputter.setNoWrap();
337 outputter.write();
338 success = collector.wasSuccessful();
341 for (size_t i = 0; i < protectors.size(); ++i)
342 result.popProtector();
344 return success;
346 virtual bool operator()() const override
348 return run();
352 #ifdef UNX
354 double get_time(timeval* time)
356 double nTime = static_cast<double>(time->tv_sec);
357 nTime += static_cast<double>(time->tv_usec)/1000000.0;
358 return nTime;
361 OString generateTestName(std::u16string_view rPath)
363 size_t nPathSep = rPath.rfind('/');
364 std::u16string_view aTestName = rPath.substr(nPathSep+1);
365 return OUStringToOString(aTestName, RTL_TEXTENCODING_UTF8);
368 void reportResourceUsage(std::u16string_view rPath)
370 OUString aFullPath = OUString::Concat(rPath) + ".resource.log";
371 rusage resource_usage;
372 getrusage(RUSAGE_SELF, &resource_usage);
374 OString aPath = OUStringToOString(aFullPath, RTL_TEXTENCODING_UTF8);
375 std::ofstream resource_file(aPath.getStr());
376 resource_file << "Name = " << generateTestName(rPath) << std::endl;
377 double nUserSpace = get_time(&resource_usage.ru_utime);
378 double nKernelSpace = get_time(&resource_usage.ru_stime);
379 resource_file << "UserSpace = " << nUserSpace << std::endl;
380 resource_file << "KernelSpace = " << nKernelSpace << std::endl;
382 #else
383 void reportResourceUsage([[maybe_unused]] const OUString& /*rPath*/)
386 #endif
390 static bool main2()
392 bool ok = false;
393 OUString path;
395 #ifdef _WIN32
396 //Disable Dr-Watson in order to crash simply without popup dialogs under
397 //windows
398 DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
399 SetErrorMode(SEM_NOGPFAULTERRORBOX|dwMode);
400 #ifdef _DEBUG // These functions are present only in the debugging runtime
401 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
402 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
403 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
404 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
405 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
406 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
407 #endif
408 #endif
410 std::vector<CppUnit::Protector *> protectors;
411 CppUnit::TestResult result;
412 std::string args;
413 std::string testlib;
414 sal_uInt32 index = 0;
415 while (index < osl_getCommandArgCount())
417 OUString arg = getArgument(index);
418 if (arg.startsWith("-env:CPPUNITTESTTARGET=", &path))
420 ++index;
421 continue;
423 if (arg.startsWith("-env:"))
425 ++index;
426 continue; // ignore it here - will be read later
428 if ( arg != "--protector" )
430 if (testlib.empty())
432 testlib = OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
433 args += testlib;
435 else
437 args += ' ';
438 args += OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
440 ++index;
441 continue;
443 if (osl_getCommandArgCount() - index < 3) {
444 usageFailure();
446 OUString lib(getArgument(index + 1));
447 OUString sym(getArgument(index + 2));
448 #ifndef DISABLE_DYNLOADING
449 osl::Module mod(lib, SAL_LOADMODULE_GLOBAL);
450 oslGenericFunction fn = mod.getFunctionSymbol(sym);
451 mod.release();
452 #else
453 oslGenericFunction fn = 0;
454 if (sym == "unoexceptionprotector")
455 fn = (oslGenericFunction) unoexceptionprotector;
456 else if (sym == "unobootstrapprotector")
457 fn = (oslGenericFunction) unobootstrapprotector;
458 else if (sym == "vclbootstrapprotector")
459 fn = (oslGenericFunction) vclbootstrapprotector;
460 else
462 std::cerr
463 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
464 << std::endl;
465 std::exit(EXIT_FAILURE);
467 #endif
468 if (fn == nullptr) {
469 std::cerr
470 << "Failure instantiating protector \"" << convertLazy(lib)
471 << "\", \"" << convertLazy(sym) << '"' << std::endl;
472 std::exit(EXIT_FAILURE);
474 CppUnit::Protector *protector =
475 (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))();
476 if (protector != nullptr) {
477 protectors.push_back(protector);
479 index+=3;
482 ProtectedFixtureFunctor tests(testlib, args, protectors, result);
483 ok = tests.run();
485 reportResourceUsage(path);
487 return ok;
490 #if defined(_WIN32) && defined(_DEBUG)
492 //Prints stack trace based on exception context record
493 static void printStack( PCONTEXT ctx )
495 HANDLE process = GetCurrentProcess();
496 HANDLE thread = GetCurrentThread();
498 STACKFRAME64 stack {};
499 stack.AddrPC.Mode = AddrModeFlat;
500 stack.AddrStack.Mode = AddrModeFlat;
501 stack.AddrFrame.Mode = AddrModeFlat;
502 #ifdef _M_AMD64
503 stack.AddrPC.Offset = ctx->Rip;
504 stack.AddrStack.Offset = ctx->Rsp;
505 stack.AddrFrame.Offset = ctx->Rsp;
506 #elif defined _M_ARM64
507 stack.AddrPC.Offset = ctx->Pc;
508 stack.AddrStack.Offset = ctx->Sp;
509 stack.AddrFrame.Offset = ctx->Fp;
510 #else
511 stack.AddrPC.Offset = ctx->Eip;
512 stack.AddrStack.Offset = ctx->Esp;
513 stack.AddrFrame.Offset = ctx->Ebp;
514 #endif
516 DWORD symOptions = SymGetOptions();
517 symOptions |= SYMOPT_LOAD_LINES;
518 symOptions |= SYMOPT_FAIL_CRITICAL_ERRORS;
519 symOptions = SymSetOptions(symOptions);
521 SymInitialize( process, nullptr, TRUE ); //load symbols
523 IMAGEHLP_LINE64 line{};
524 line.SizeOfStruct = sizeof(line);
526 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
527 PSYMBOL_INFO pSymbol = reinterpret_cast<PSYMBOL_INFO>(buffer);
529 for (;;)
531 //get next call from stack
532 bool result = StackWalk64
534 #ifdef _M_AMD64
535 IMAGE_FILE_MACHINE_AMD64,
536 #elif defined _M_ARM64
537 IMAGE_FILE_MACHINE_ARM64,
538 #else
539 IMAGE_FILE_MACHINE_I386,
540 #endif
541 process,
542 thread,
543 &stack,
544 ctx,
545 nullptr,
546 SymFunctionTableAccess64,
547 SymGetModuleBase64,
548 nullptr
551 if( !result )
552 break;
554 //get symbol name for address
555 pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
556 pSymbol->MaxNameLen = MAX_SYM_NAME + 1;
557 if (SymFromAddr(process, stack.AddrPC.Offset, nullptr, pSymbol))
558 printf("\tat %s", pSymbol->Name);
559 else
560 printf("\tat unknown (Error in SymFromAddr=%#08lx)", GetLastError());
562 DWORD disp;
563 //try to get line
564 if (SymGetLineFromAddr64(process, stack.AddrPC.Offset, &disp, &line))
566 printf(" in %s: line: %lu:\n", line.FileName, line.LineNumber);
568 else
570 //failed to get line
571 printf(", address 0x%0I64X", stack.AddrPC.Offset);
572 HMODULE hModule = nullptr;
573 GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
574 reinterpret_cast<LPCTSTR>(stack.AddrPC.Offset), &hModule);
576 char sModule[256];
577 //at least print module name
578 if (hModule != nullptr)
579 GetModuleFileNameA(hModule, sModule, std::size(sModule));
581 printf (" in %s\n", sModule);
586 // The exception filter function:
587 static LONG WINAPI ExpFilter(EXCEPTION_POINTERS* ex)
589 // we only want this active on the Jenkins tinderboxes
590 printf("*** Exception 0x%lx occurred ***\n\n",ex->ExceptionRecord->ExceptionCode);
591 printStack(ex->ContextRecord);
592 return EXCEPTION_EXECUTE_HANDLER;
595 static void AbortSignalHandler(int signal)
597 if (signal == SIGABRT) {
598 std::unique_ptr<sal::BacktraceState> bs = sal::backtrace_get(50);
599 SAL_WARN("sal.cppunittester", "CAUGHT SIGABRT:\n" << sal::backtrace_to_string(bs.get()));
603 SAL_IMPLEMENT_MAIN()
605 // catch the kind of signal that is thrown when an assert fails, and log a stacktrace
606 signal(SIGABRT, AbortSignalHandler);
608 bool ok = false;
609 // This magic kind of Windows-specific exception handling has to be in its own function
610 // because it cannot be in a function that has objects with destructors.
611 __try
613 ok = main2();
615 __except (ExpFilter(GetExceptionInformation()))
618 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
621 #else
623 SAL_IMPLEMENT_MAIN()
625 bool ok = false;
628 ok = main2();
630 catch (const std::exception& e)
632 SAL_WARN("vcl.app", "Fatal exception: " << e.what());
634 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
637 #endif
639 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */