Bump version to 24.04.3.4
[LibreOffice.git] / sal / cppunittester / cppunittester.cxx
blob854c0999c9118015a570efaf1a34a290b89882a7
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/resource.h>
34 #endif
36 #include <cstdlib>
37 #include <iostream>
38 #include <string>
39 #include <sal/log.hxx>
40 #include <sal/types.h>
41 #include <cppunittester/protectorfactory.hxx>
42 #include <osl/module.h>
43 #include <osl/module.hxx>
44 #include <osl/process.h>
45 #include <osl/thread.h>
46 #include <rtl/character.hxx>
47 #include <rtl/string.hxx>
48 #include <rtl/strbuf.hxx>
49 #include <rtl/ustring.hxx>
50 #include <sal/main.h>
52 #include <cppunit/CompilerOutputter.h>
53 #include <cppunit/Exception.h>
54 #include <cppunit/TestFailure.h>
55 #include <cppunit/TestResult.h>
56 #include <cppunit/TestResultCollector.h>
57 #include <cppunit/TestRunner.h>
58 #include <cppunit/extensions/TestFactoryRegistry.h>
59 #include <cppunit/plugin/PlugInManager.h>
60 #include <cppunit/plugin/DynamicLibraryManagerException.h>
61 #include <cppunit/portability/Stream.h>
63 #include <boost/algorithm/string.hpp>
65 #include <algorithm>
66 #include <string_view>
67 #include <utility>
69 namespace {
71 void usageFailure() {
72 std::cerr
73 << ("Usage: cppunittester (--protector <shared-library-path>"
74 " <function-symbol>)* <shared-library-path>")
75 << std::endl;
76 std::exit(EXIT_FAILURE);
79 OUString getArgument(sal_Int32 index) {
80 OUString arg;
81 osl_getCommandArg(index, &arg.pData);
82 return arg;
85 std::string convertLazy(std::u16string_view s16) {
86 OString s8(OUStringToOString(s16, osl_getThreadTextEncoding()));
87 static_assert(sizeof (sal_Int32) <= sizeof (std::string::size_type), "must be at least the same size");
88 // ensure following cast is legitimate
89 return std::string(s8);
92 //Output how long each test took
93 class TimingListener
94 : public CppUnit::TestListener
96 public:
97 TimingListener()
98 : m_nStartTime(0)
101 TimingListener(const TimingListener&) = delete;
102 TimingListener& operator=(const TimingListener&) = delete;
104 void startTest( CppUnit::Test *test) override
106 std::cout << "[_RUN_____] " << test->getName() << std::endl;
107 m_nStartTime = osl_getGlobalTimer();
110 void endTest( CppUnit::Test *test ) override
112 sal_uInt32 nEndTime = osl_getGlobalTimer();
113 std::cout << test->getName() << " finished in: "
114 << nEndTime-m_nStartTime << "ms" << std::endl;
117 private:
118 sal_uInt32 m_nStartTime;
121 // Setup an env variable so that temp file (or other) can
122 // have a useful value to identify the source
123 class EyecatcherListener
124 : public CppUnit::TestListener
126 public:
127 EyecatcherListener() = default;
128 EyecatcherListener(const EyecatcherListener&) = delete;
129 EyecatcherListener& operator=(const EyecatcherListener&) = delete;
130 void startTest( CppUnit::Test* test) override
132 rtl::OStringBuffer tn(test->getName());
133 for(int i = 0; i < tn.getLength(); i++)
135 if(!rtl::isAsciiAlphanumeric(static_cast<unsigned char>(tn[i])))
137 tn[i] = '_';
140 tn.append('_');
141 #ifdef WIN32
142 _putenv_s("LO_TESTNAME", tn.getStr());
143 #else
144 setenv("LO_TESTNAME", tn.getStr(), true);
145 #endif
148 void endTest( CppUnit::Test* /* test */ ) override
153 class LogFailuresAsTheyHappen : public CppUnit::TestListener
155 public:
156 virtual void addFailure( const CppUnit::TestFailure &failure ) override
158 printFailureLocation( failure.sourceLine() );
159 printFailedTestName( failure );
160 printFailureMessage( failure );
163 private:
164 static void printFailureLocation( const CppUnit::SourceLine &sourceLine )
166 if ( !sourceLine.isValid() )
167 std::cerr << "unknown:0:";
168 else
169 std::cerr << sourceLine.fileName() << ":" << sourceLine.lineNumber() << ":";
172 static void printFailedTestName( const CppUnit::TestFailure &failure )
174 std::cerr << failure.failedTestName() << std::endl;
177 static void printFailureMessage( const CppUnit::TestFailure &failure )
179 std::cerr << failure.thrownException()->message().shortDescription() << std::endl;
180 std::cerr << failure.thrownException()->message().details() << std::endl;
184 struct test_name_compare
186 explicit test_name_compare(std::string aName):
187 maName(std::move(aName))
191 bool operator()(const std::string& rCmp)
193 size_t nPos = maName.find(rCmp);
194 if (nPos == std::string::npos)
195 return false;
197 size_t nEndPos = nPos + rCmp.size();
198 return nEndPos == maName.size();
201 std::string maName;
204 bool addRecursiveTests(const std::vector<std::string>& test_names, CppUnit::Test* pTest, CppUnit::TestRunner& rRunner)
206 bool ret(false);
207 for (int i = 0; i < pTest->getChildTestCount(); ++i)
209 CppUnit::Test* pNewTest = pTest->getChildTestAt(i);
210 ret |= addRecursiveTests(test_names, pNewTest, rRunner);
211 if (std::any_of(test_names.begin(), test_names.end(), test_name_compare(pNewTest->getName())))
213 rRunner.addTest(pNewTest);
214 ret = true;
217 return ret;
220 //Allow the whole uniting testing framework to be run inside a "Protector"
221 //which knows about uno exceptions, so it can print the content of the
222 //exception before falling over and dying
223 class CPPUNIT_API ProtectedFixtureFunctor
224 : public CppUnit::Functor
226 private:
227 const std::string &testlib;
228 const std::string &args;
229 std::vector<CppUnit::Protector *> &protectors;
230 CppUnit::TestResult &result;
231 public:
232 ProtectedFixtureFunctor(const std::string& testlib_, const std::string &args_, std::vector<CppUnit::Protector*> &protectors_, CppUnit::TestResult &result_)
233 : testlib(testlib_)
234 , args(args_)
235 , protectors(protectors_)
236 , result(result_)
239 ProtectedFixtureFunctor(const ProtectedFixtureFunctor&) = delete;
240 ProtectedFixtureFunctor& operator=(const ProtectedFixtureFunctor&) = delete;
241 bool run() const
243 #ifdef DISABLE_DYNLOADING
245 // NOTE: Running cppunit unit tests on iOS was something I did
246 // only very early (several years ago) when starting porting
247 // this stuff to iOS. The complicated mechanisms to do build
248 // such unit test single executables have surely largely
249 // bit-rotted or been semi-intentionally broken since. This
250 // stuff here left for information only. --tml 2014.
252 // For iOS cppunit plugins aren't really "plugins" (shared
253 // libraries), but just static archives. In the real main
254 // program of a cppunit app, which calls the lo_main() that
255 // the SAL_IMPLEMENT_MAIN() below expands to, we specifically
256 // call the initialize methods of the CppUnitTestPlugIns that
257 // we statically link to the app executable.
258 #else
259 // The PlugInManager instance is deliberately leaked, so that the dynamic libraries it loads
260 // are never unloaded (which could make e.g. pointers from other libraries' static data
261 // structures to const data in those libraries, like some static OUString cache pointing at
262 // a const OUStringLiteral, become dangling by the time those static data structures are
263 // destroyed during exit):
264 auto manager = new CppUnit::PlugInManager;
265 try {
266 manager->load(testlib, args);
267 } catch (const CppUnit::DynamicLibraryManagerException &e) {
268 std::cerr << "DynamicLibraryManagerException: \"" << e.what() << "\"\n";
269 const char *pPath = getenv ("PATH");
270 const size_t nPathLen = pPath ? strlen(pPath) : 0;
271 #ifdef _WIN32
272 if (nPathLen > 256)
274 std::cerr << "Windows has significant build problems with long PATH variables ";
275 std::cerr << "please check your PATH variable and re-autogen.\n";
277 #endif
278 std::cerr << "Path (length: " << nPathLen << ") is '" << pPath << "'\n";
279 return false;
281 #endif
283 for (size_t i = 0; i < protectors.size(); ++i)
284 result.pushProtector(protectors[i]);
286 bool success;
288 CppUnit::TestResultCollector collector;
289 result.addListener(&collector);
291 LogFailuresAsTheyHappen logger;
292 result.addListener(&logger);
294 TimingListener timer;
295 result.addListener(&timer);
297 EyecatcherListener eye;
298 result.addListener(&eye);
300 // set this to track down files created before first test method
301 std::string lib = testlib.substr(testlib.rfind('/')+1);
302 #ifdef WIN32
303 _putenv_s("LO_TESTNAME", lib.c_str());
304 #else
305 setenv("LO_TESTNAME", lib.c_str(), true);
306 #endif
307 const char* pVal = getenv("CPPUNIT_TEST_NAME");
309 CppUnit::TestRunner runner;
310 if (pVal)
312 std::vector<std::string> test_names;
313 boost::split(test_names, pVal, boost::is_any_of("\t "));
314 CppUnit::Test* pTest = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
315 bool const added(addRecursiveTests(test_names, pTest, runner));
316 if (!added)
318 std::cerr << "\nFatal error: CPPUNIT_TEST_NAME contains no valid tests\n";
319 // coverity[leaked_storage] - `manager` leaked on purpose
320 return false;
323 else
325 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
327 runner.run(result);
329 CppUnit::CompilerOutputter outputter(&collector, CppUnit::stdCErr());
330 outputter.setNoWrap();
331 outputter.write();
332 success = collector.wasSuccessful();
335 for (size_t i = 0; i < protectors.size(); ++i)
336 result.popProtector();
338 return success;
340 virtual bool operator()() const override
342 return run();
346 #ifdef UNX
348 double get_time(timeval* time)
350 double nTime = static_cast<double>(time->tv_sec);
351 nTime += static_cast<double>(time->tv_usec)/1000000.0;
352 return nTime;
355 OString generateTestName(std::u16string_view rPath)
357 size_t nPathSep = rPath.rfind('/');
358 std::u16string_view aTestName = rPath.substr(nPathSep+1);
359 return OUStringToOString(aTestName, RTL_TEXTENCODING_UTF8);
362 void reportResourceUsage(std::u16string_view rPath)
364 OUString aFullPath = OUString::Concat(rPath) + ".resource.log";
365 rusage resource_usage;
366 getrusage(RUSAGE_SELF, &resource_usage);
368 OString aPath = OUStringToOString(aFullPath, RTL_TEXTENCODING_UTF8);
369 std::ofstream resource_file(aPath.getStr());
370 resource_file << "Name = " << generateTestName(rPath) << std::endl;
371 double nUserSpace = get_time(&resource_usage.ru_utime);
372 double nKernelSpace = get_time(&resource_usage.ru_stime);
373 resource_file << "UserSpace = " << nUserSpace << std::endl;
374 resource_file << "KernelSpace = " << nKernelSpace << std::endl;
376 #else
377 void reportResourceUsage([[maybe_unused]] const OUString& /*rPath*/)
380 #endif
384 static bool main2()
386 bool ok = false;
387 OUString path;
389 #ifdef _WIN32
390 //Disable Dr-Watson in order to crash simply without popup dialogs under
391 //windows
392 DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
393 SetErrorMode(SEM_NOGPFAULTERRORBOX|dwMode);
394 #ifdef _DEBUG // These functions are present only in the debugging runtime
395 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
396 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
397 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
398 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
399 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
400 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
401 #endif
402 // Create a desktop, to avoid popups interfering with active user session,
403 // because on Windows, we don't use svp vcl plugin for unit testing
404 if (getenv("CPPUNIT_DEFAULT_DESKTOP") == nullptr)
406 if (HDESK hDesktop
407 = CreateDesktopW(L"LO_CPPUNIT_DESKTOP", nullptr, nullptr, 0, GENERIC_ALL, nullptr))
408 SetThreadDesktop(hDesktop);
410 #endif
412 std::vector<CppUnit::Protector *> protectors;
413 CppUnit::TestResult result;
414 std::string args;
415 std::string testlib;
416 sal_uInt32 index = 0;
417 while (index < osl_getCommandArgCount())
419 OUString arg = getArgument(index);
420 if (arg.startsWith("-env:CPPUNITTESTTARGET=", &path))
422 ++index;
423 continue;
425 if (arg.startsWith("-env:"))
427 ++index;
428 continue; // ignore it here - will be read later
430 if ( arg != "--protector" )
432 if (testlib.empty())
434 testlib = OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
435 args += testlib;
437 else
439 args += ' ';
440 args += OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
442 ++index;
443 continue;
445 if (osl_getCommandArgCount() - index < 3) {
446 usageFailure();
448 OUString lib(getArgument(index + 1));
449 OUString sym(getArgument(index + 2));
450 #ifndef DISABLE_DYNLOADING
451 osl::Module mod(lib, SAL_LOADMODULE_GLOBAL);
452 oslGenericFunction fn = mod.getFunctionSymbol(sym);
453 mod.release();
454 #else
455 oslGenericFunction fn = 0;
456 if (sym == "unoexceptionprotector")
457 fn = (oslGenericFunction) unoexceptionprotector;
458 else if (sym == "unobootstrapprotector")
459 fn = (oslGenericFunction) unobootstrapprotector;
460 else if (sym == "vclbootstrapprotector")
461 fn = (oslGenericFunction) vclbootstrapprotector;
462 else
464 std::cerr
465 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
466 << std::endl;
467 std::exit(EXIT_FAILURE);
469 #endif
470 if (fn == nullptr) {
471 std::cerr
472 << "Failure instantiating protector \"" << convertLazy(lib)
473 << "\", \"" << convertLazy(sym) << '"' << std::endl;
474 std::exit(EXIT_FAILURE);
476 CppUnit::Protector *protector =
477 (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))();
478 if (protector != nullptr) {
479 protectors.push_back(protector);
481 index+=3;
484 ProtectedFixtureFunctor tests(testlib, args, protectors, result);
485 ok = tests.run();
487 reportResourceUsage(path);
489 return ok;
492 #if defined(_WIN32) && defined(_DEBUG)
494 //Prints stack trace based on exception context record
495 static void printStack( PCONTEXT ctx )
497 HANDLE process = GetCurrentProcess();
498 HANDLE thread = GetCurrentThread();
500 STACKFRAME64 stack {};
501 stack.AddrPC.Mode = AddrModeFlat;
502 stack.AddrStack.Mode = AddrModeFlat;
503 stack.AddrFrame.Mode = AddrModeFlat;
504 #ifdef _M_AMD64
505 stack.AddrPC.Offset = ctx->Rip;
506 stack.AddrStack.Offset = ctx->Rsp;
507 stack.AddrFrame.Offset = ctx->Rsp;
508 #elif defined _M_ARM64
509 stack.AddrPC.Offset = ctx->Pc;
510 stack.AddrStack.Offset = ctx->Sp;
511 stack.AddrFrame.Offset = ctx->Fp;
512 #else
513 stack.AddrPC.Offset = ctx->Eip;
514 stack.AddrStack.Offset = ctx->Esp;
515 stack.AddrFrame.Offset = ctx->Ebp;
516 #endif
518 DWORD symOptions = SymGetOptions();
519 symOptions |= SYMOPT_LOAD_LINES;
520 symOptions |= SYMOPT_FAIL_CRITICAL_ERRORS;
521 symOptions = SymSetOptions(symOptions);
523 SymInitialize( process, nullptr, TRUE ); //load symbols
525 IMAGEHLP_LINE64 line{};
526 line.SizeOfStruct = sizeof(line);
528 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
529 PSYMBOL_INFO pSymbol = reinterpret_cast<PSYMBOL_INFO>(buffer);
531 for (;;)
533 //get next call from stack
534 bool result = StackWalk64
536 #ifdef _M_AMD64
537 IMAGE_FILE_MACHINE_AMD64,
538 #elif defined _M_ARM64
539 IMAGE_FILE_MACHINE_ARM64,
540 #else
541 IMAGE_FILE_MACHINE_I386,
542 #endif
543 process,
544 thread,
545 &stack,
546 ctx,
547 nullptr,
548 SymFunctionTableAccess64,
549 SymGetModuleBase64,
550 nullptr
553 if( !result )
554 break;
556 //get symbol name for address
557 pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
558 pSymbol->MaxNameLen = MAX_SYM_NAME + 1;
559 if (SymFromAddr(process, stack.AddrPC.Offset, nullptr, pSymbol))
560 printf("\tat %s", pSymbol->Name);
561 else
562 printf("\tat unknown (Error in SymFromAddr=%#08lx)", GetLastError());
564 DWORD disp;
565 //try to get line
566 if (SymGetLineFromAddr64(process, stack.AddrPC.Offset, &disp, &line))
568 printf(" in %s: line: %lu:\n", line.FileName, line.LineNumber);
570 else
572 //failed to get line
573 printf(", address 0x%0I64X", stack.AddrPC.Offset);
574 HMODULE hModule = nullptr;
575 GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
576 reinterpret_cast<LPCTSTR>(stack.AddrPC.Offset), &hModule);
578 char sModule[256];
579 //at least print module name
580 if (hModule != nullptr)
581 GetModuleFileNameA(hModule, sModule, std::size(sModule));
583 printf (" in %s\n", sModule);
588 // The exception filter function:
589 static LONG WINAPI ExpFilter(EXCEPTION_POINTERS* ex)
591 // we only want this active on the Jenkins tinderboxes
592 printf("*** Exception 0x%lx occurred ***\n\n",ex->ExceptionRecord->ExceptionCode);
593 printStack(ex->ContextRecord);
594 return EXCEPTION_EXECUTE_HANDLER;
597 static void AbortSignalHandler(int signal)
599 if (signal == SIGABRT) {
600 std::unique_ptr<sal::BacktraceState> bs = sal::backtrace_get(50);
601 SAL_WARN("sal.cppunittester", "CAUGHT SIGABRT:\n" << sal::backtrace_to_string(bs.get()));
605 SAL_IMPLEMENT_MAIN()
607 // catch the kind of signal that is thrown when an assert fails, and log a stacktrace
608 signal(SIGABRT, AbortSignalHandler);
610 bool ok = false;
611 // This magic kind of Windows-specific exception handling has to be in its own function
612 // because it cannot be in a function that has objects with destructors.
613 __try
615 ok = main2();
617 __except (ExpFilter(GetExceptionInformation()))
620 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
623 #else
625 SAL_IMPLEMENT_MAIN()
627 bool ok = false;
630 ok = main2();
632 catch (const std::exception& e)
634 SAL_WARN("vcl.app", "Fatal exception: " << e.what());
636 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
639 #endif
641 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */