Fix typo
[LibreOffice.git] / sal / cppunittester / cppunittester.cxx
blob4a1d033e97baca6fd0764dd8a9f76a99b0cba4c7
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(s8);
97 //Output how long each test took
98 class TimingListener
99 : public CppUnit::TestListener
101 public:
102 TimingListener()
103 : m_nStartTime(0)
106 TimingListener(const TimingListener&) = delete;
107 TimingListener& operator=(const TimingListener&) = delete;
109 void startTest( CppUnit::Test *test) override
111 std::cout << "[_RUN_____] " << test->getName() << std::endl;
112 m_nStartTime = osl_getGlobalTimer();
115 void endTest( CppUnit::Test *test ) override
117 sal_uInt32 nEndTime = osl_getGlobalTimer();
118 std::cout << test->getName() << " finished in: "
119 << nEndTime-m_nStartTime << "ms" << std::endl;
122 private:
123 sal_uInt32 m_nStartTime;
126 // Setup an env variable so that temp file (or other) can
127 // have a useful value to identify the source
128 class EyecatcherListener
129 : public CppUnit::TestListener
131 public:
132 EyecatcherListener() = default;
133 EyecatcherListener(const EyecatcherListener&) = delete;
134 EyecatcherListener& operator=(const EyecatcherListener&) = delete;
135 void startTest( CppUnit::Test* test) override
137 rtl::OStringBuffer tn(test->getName());
138 for(int i = 0; i < tn.getLength(); i++)
140 if(!rtl::isAsciiAlphanumeric(static_cast<unsigned char>(tn[i])))
142 tn[i] = '_';
145 tn.append('_');
146 #ifdef WIN32
147 _putenv_s("LO_TESTNAME", tn.getStr());
148 #else
149 setenv("LO_TESTNAME", tn.getStr(), true);
150 #endif
153 void endTest( CppUnit::Test* /* test */ ) override
158 class LogFailuresAsTheyHappen : public CppUnit::TestListener
160 public:
161 virtual void addFailure( const CppUnit::TestFailure &failure ) override
163 printFailureLocation( failure.sourceLine() );
164 printFailedTestName( failure );
165 printFailureMessage( failure );
168 private:
169 static void printFailureLocation( const CppUnit::SourceLine &sourceLine )
171 if ( !sourceLine.isValid() )
172 std::cerr << "unknown:0:";
173 else
174 std::cerr << sourceLine.fileName() << ":" << sourceLine.lineNumber() << ":";
177 static void printFailedTestName( const CppUnit::TestFailure &failure )
179 std::cerr << failure.failedTestName() << std::endl;
182 static void printFailureMessage( const CppUnit::TestFailure &failure )
184 std::cerr << failure.thrownException()->message().shortDescription() << std::endl;
185 std::cerr << failure.thrownException()->message().details() << std::endl;
189 struct test_name_compare
191 explicit test_name_compare(std::string aName):
192 maName(std::move(aName))
196 bool operator()(const std::string& rCmp)
198 size_t nPos = maName.find(rCmp);
199 if (nPos == std::string::npos)
200 return false;
202 size_t nEndPos = nPos + rCmp.size();
203 return nEndPos == maName.size();
206 std::string maName;
209 bool addRecursiveTests(const std::vector<std::string>& test_names, CppUnit::Test* pTest, CppUnit::TestRunner& rRunner)
211 bool ret(false);
212 for (int i = 0; i < pTest->getChildTestCount(); ++i)
214 CppUnit::Test* pNewTest = pTest->getChildTestAt(i);
215 ret |= addRecursiveTests(test_names, pNewTest, rRunner);
216 if (std::any_of(test_names.begin(), test_names.end(), test_name_compare(pNewTest->getName())))
218 rRunner.addTest(pNewTest);
219 ret = true;
222 return ret;
225 //Allow the whole uniting testing framework to be run inside a "Protector"
226 //which knows about uno exceptions, so it can print the content of the
227 //exception before falling over and dying
228 class CPPUNIT_API ProtectedFixtureFunctor
229 : public CppUnit::Functor
231 private:
232 const std::string &testlib;
233 const std::string &args;
234 std::vector<CppUnit::Protector *> &protectors;
235 CppUnit::TestResult &result;
236 public:
237 ProtectedFixtureFunctor(const std::string& testlib_, const std::string &args_, std::vector<CppUnit::Protector*> &protectors_, CppUnit::TestResult &result_)
238 : testlib(testlib_)
239 , args(args_)
240 , protectors(protectors_)
241 , result(result_)
244 ProtectedFixtureFunctor(const ProtectedFixtureFunctor&) = delete;
245 ProtectedFixtureFunctor& operator=(const ProtectedFixtureFunctor&) = delete;
246 bool run() const
248 #ifdef DISABLE_DYNLOADING
250 // NOTE: Running cppunit unit tests on iOS was something I did
251 // only very early (several years ago) when starting porting
252 // this stuff to iOS. The complicated mechanisms to do build
253 // such unit test single executables have surely largely
254 // bit-rotted or been semi-intentionally broken since. This
255 // stuff here left for information only. --tml 2014.
257 // For iOS cppunit plugins aren't really "plugins" (shared
258 // libraries), but just static archives. In the real main
259 // program of a cppunit app, which calls the lo_main() that
260 // the SAL_IMPLEMENT_MAIN() below expands to, we specifically
261 // call the initialize methods of the CppUnitTestPlugIns that
262 // we statically link to the app executable.
263 #else
264 // The PlugInManager instance is deliberately leaked, so that the dynamic libraries it loads
265 // are never unloaded (which could make e.g. pointers from other libraries' static data
266 // structures to const data in those libraries, like some static OUString cache pointing at
267 // a const OUStringLiteral, become dangling by the time those static data structures are
268 // destroyed during exit):
269 auto manager = new CppUnit::PlugInManager;
270 try {
271 manager->load(testlib, args);
272 } catch (const CppUnit::DynamicLibraryManagerException &e) {
273 std::cerr << "DynamicLibraryManagerException: \"" << e.what() << "\"\n";
274 const char *pPath = getenv ("PATH");
275 const size_t nPathLen = pPath ? strlen(pPath) : 0;
276 #ifdef _WIN32
277 if (nPathLen > 256)
279 std::cerr << "Windows has significant build problems with long PATH variables ";
280 std::cerr << "please check your PATH variable and re-autogen.\n";
282 #endif
283 std::cerr << "Path (length: " << nPathLen << ") is '" << pPath << "'\n";
284 return false;
286 #endif
288 for (size_t i = 0; i < protectors.size(); ++i)
289 result.pushProtector(protectors[i]);
291 bool success;
293 CppUnit::TestResultCollector collector;
294 result.addListener(&collector);
296 LogFailuresAsTheyHappen logger;
297 result.addListener(&logger);
299 TimingListener timer;
300 result.addListener(&timer);
302 EyecatcherListener eye;
303 result.addListener(&eye);
305 // set this to track down files created before first test method
306 std::string lib = testlib.substr(testlib.rfind('/')+1);
307 #ifdef WIN32
308 _putenv_s("LO_TESTNAME", lib.c_str());
309 #else
310 setenv("LO_TESTNAME", lib.c_str(), true);
311 #endif
312 const char* pVal = getenv("CPPUNIT_TEST_NAME");
314 CppUnit::TestRunner runner;
315 if (pVal)
317 std::vector<std::string> test_names;
318 boost::split(test_names, pVal, boost::is_any_of("\t "));
319 CppUnit::Test* pTest = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
320 bool const added(addRecursiveTests(test_names, pTest, runner));
321 if (!added)
323 std::cerr << "\nFatal error: CPPUNIT_TEST_NAME contains no valid tests\n";
324 // coverity[leaked_storage] - `manager` leaked on purpose
325 return false;
328 else
330 runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
332 runner.run(result);
334 CppUnit::CompilerOutputter outputter(&collector, CppUnit::stdCErr());
335 outputter.setNoWrap();
336 outputter.write();
337 success = collector.wasSuccessful();
340 for (size_t i = 0; i < protectors.size(); ++i)
341 result.popProtector();
343 return success;
345 virtual bool operator()() const override
347 return run();
351 #ifdef UNX
353 double get_time(timeval* time)
355 double nTime = static_cast<double>(time->tv_sec);
356 nTime += static_cast<double>(time->tv_usec)/1000000.0;
357 return nTime;
360 OString generateTestName(std::u16string_view rPath)
362 size_t nPathSep = rPath.rfind('/');
363 std::u16string_view aTestName = rPath.substr(nPathSep+1);
364 return OUStringToOString(aTestName, RTL_TEXTENCODING_UTF8);
367 void reportResourceUsage(std::u16string_view rPath)
369 OUString aFullPath = OUString::Concat(rPath) + ".resource.log";
370 rusage resource_usage;
371 getrusage(RUSAGE_SELF, &resource_usage);
373 OString aPath = OUStringToOString(aFullPath, RTL_TEXTENCODING_UTF8);
374 std::ofstream resource_file(aPath.getStr());
375 resource_file << "Name = " << generateTestName(rPath) << std::endl;
376 double nUserSpace = get_time(&resource_usage.ru_utime);
377 double nKernelSpace = get_time(&resource_usage.ru_stime);
378 resource_file << "UserSpace = " << nUserSpace << std::endl;
379 resource_file << "KernelSpace = " << nKernelSpace << std::endl;
381 #else
382 void reportResourceUsage([[maybe_unused]] const OUString& /*rPath*/)
385 #endif
389 static bool main2()
391 bool ok = false;
392 OUString path;
394 #ifdef _WIN32
395 //Disable Dr-Watson in order to crash simply without popup dialogs under
396 //windows
397 DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
398 SetErrorMode(SEM_NOGPFAULTERRORBOX|dwMode);
399 #ifdef _DEBUG // These functions are present only in the debugging runtime
400 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
401 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
402 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
403 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
404 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG|_CRTDBG_MODE_FILE);
405 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
406 #endif
407 // Create a desktop, to avoid popups interfering with active user session,
408 // because on Windows, we don't use svp vcl plugin for unit testing
409 if (getenv("CPPUNIT_DEFAULT_DESKTOP") == nullptr)
411 if (HDESK hDesktop
412 = CreateDesktopW(L"LO_CPPUNIT_DESKTOP", nullptr, nullptr, 0, GENERIC_ALL, nullptr))
413 SetThreadDesktop(hDesktop);
415 #endif
417 std::vector<CppUnit::Protector *> protectors;
418 CppUnit::TestResult result;
419 std::string args;
420 std::string testlib;
421 sal_uInt32 index = 0;
422 while (index < osl_getCommandArgCount())
424 OUString arg = getArgument(index);
425 if (arg.startsWith("-env:CPPUNITTESTTARGET=", &path))
427 ++index;
428 continue;
430 if (arg.startsWith("-env:"))
432 ++index;
433 continue; // ignore it here - will be read later
435 if ( arg != "--protector" )
437 if (testlib.empty())
439 testlib = OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
440 args += testlib;
442 else
444 args += ' ';
445 args += OUStringToOString(arg, osl_getThreadTextEncoding()).getStr();
447 ++index;
448 continue;
450 if (osl_getCommandArgCount() - index < 3) {
451 usageFailure();
453 OUString lib(getArgument(index + 1));
454 OUString sym(getArgument(index + 2));
455 #ifndef DISABLE_DYNLOADING
456 osl::Module mod(lib, SAL_LOADMODULE_GLOBAL);
457 oslGenericFunction fn = mod.getFunctionSymbol(sym);
458 mod.release();
459 #else
460 oslGenericFunction fn = 0;
461 if (sym == "unoexceptionprotector")
462 fn = (oslGenericFunction) unoexceptionprotector;
463 else if (sym == "unobootstrapprotector")
464 fn = (oslGenericFunction) unobootstrapprotector;
465 else if (sym == "vclbootstrapprotector")
466 fn = (oslGenericFunction) vclbootstrapprotector;
467 else
469 std::cerr
470 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
471 << std::endl;
472 std::exit(EXIT_FAILURE);
474 #endif
475 if (fn == nullptr) {
476 std::cerr
477 << "Failure instantiating protector \"" << convertLazy(lib)
478 << "\", \"" << convertLazy(sym) << '"' << std::endl;
479 std::exit(EXIT_FAILURE);
481 CppUnit::Protector *protector =
482 (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))();
483 if (protector != nullptr) {
484 protectors.push_back(protector);
486 index+=3;
489 ProtectedFixtureFunctor tests(testlib, args, protectors, result);
490 ok = tests.run();
492 reportResourceUsage(path);
494 return ok;
497 #if defined(_WIN32) && defined(_DEBUG)
499 //Prints stack trace based on exception context record
500 static void printStack( PCONTEXT ctx )
502 HANDLE process = GetCurrentProcess();
503 HANDLE thread = GetCurrentThread();
505 STACKFRAME64 stack {};
506 stack.AddrPC.Mode = AddrModeFlat;
507 stack.AddrStack.Mode = AddrModeFlat;
508 stack.AddrFrame.Mode = AddrModeFlat;
509 #ifdef _M_AMD64
510 stack.AddrPC.Offset = ctx->Rip;
511 stack.AddrStack.Offset = ctx->Rsp;
512 stack.AddrFrame.Offset = ctx->Rsp;
513 #elif defined _M_ARM64
514 stack.AddrPC.Offset = ctx->Pc;
515 stack.AddrStack.Offset = ctx->Sp;
516 stack.AddrFrame.Offset = ctx->Fp;
517 #else
518 stack.AddrPC.Offset = ctx->Eip;
519 stack.AddrStack.Offset = ctx->Esp;
520 stack.AddrFrame.Offset = ctx->Ebp;
521 #endif
523 DWORD symOptions = SymGetOptions();
524 symOptions |= SYMOPT_LOAD_LINES;
525 symOptions |= SYMOPT_FAIL_CRITICAL_ERRORS;
526 symOptions = SymSetOptions(symOptions);
528 SymInitialize( process, nullptr, TRUE ); //load symbols
530 IMAGEHLP_LINE64 line{};
531 line.SizeOfStruct = sizeof(line);
533 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
534 PSYMBOL_INFO pSymbol = reinterpret_cast<PSYMBOL_INFO>(buffer);
536 for (;;)
538 //get next call from stack
539 bool result = StackWalk64
541 #ifdef _M_AMD64
542 IMAGE_FILE_MACHINE_AMD64,
543 #elif defined _M_ARM64
544 IMAGE_FILE_MACHINE_ARM64,
545 #else
546 IMAGE_FILE_MACHINE_I386,
547 #endif
548 process,
549 thread,
550 &stack,
551 ctx,
552 nullptr,
553 SymFunctionTableAccess64,
554 SymGetModuleBase64,
555 nullptr
558 if( !result )
559 break;
561 //get symbol name for address
562 pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
563 pSymbol->MaxNameLen = MAX_SYM_NAME + 1;
564 if (SymFromAddr(process, stack.AddrPC.Offset, nullptr, pSymbol))
565 printf("\tat %s", pSymbol->Name);
566 else
567 printf("\tat unknown (Error in SymFromAddr=%#08lx)", GetLastError());
569 DWORD disp;
570 //try to get line
571 if (SymGetLineFromAddr64(process, stack.AddrPC.Offset, &disp, &line))
573 printf(" in %s: line: %lu:\n", line.FileName, line.LineNumber);
575 else
577 //failed to get line
578 printf(", address 0x%0I64X", stack.AddrPC.Offset);
579 HMODULE hModule = nullptr;
580 GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
581 reinterpret_cast<LPCTSTR>(stack.AddrPC.Offset), &hModule);
583 char sModule[256];
584 //at least print module name
585 if (hModule != nullptr)
586 GetModuleFileNameA(hModule, sModule, std::size(sModule));
588 printf (" in %s\n", sModule);
593 // The exception filter function:
594 static LONG WINAPI ExpFilter(EXCEPTION_POINTERS* ex)
596 // we only want this active on the Jenkins tinderboxes
597 printf("*** Exception 0x%lx occurred ***\n\n",ex->ExceptionRecord->ExceptionCode);
598 printStack(ex->ContextRecord);
599 return EXCEPTION_EXECUTE_HANDLER;
602 static void AbortSignalHandler(int signal)
604 if (signal == SIGABRT) {
605 std::unique_ptr<sal::BacktraceState> bs = sal::backtrace_get(50);
606 SAL_WARN("sal.cppunittester", "CAUGHT SIGABRT:\n" << sal::backtrace_to_string(bs.get()));
610 SAL_IMPLEMENT_MAIN()
612 // catch the kind of signal that is thrown when an assert fails, and log a stacktrace
613 signal(SIGABRT, AbortSignalHandler);
615 bool ok = false;
616 // This magic kind of Windows-specific exception handling has to be in its own function
617 // because it cannot be in a function that has objects with destructors.
618 __try
620 ok = main2();
622 __except (ExpFilter(GetExceptionInformation()))
625 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
628 #else
630 SAL_IMPLEMENT_MAIN()
632 bool ok = false;
635 ok = main2();
637 catch (const std::exception& e)
639 SAL_WARN("vcl.app", "Fatal exception: " << e.what());
641 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
644 #endif
646 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */