1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #if !defined WIN32_LEAN_AND_MEAN
22 # define WIN32_LEAN_AND_MEAN
26 #if defined(_WIN32) && defined(_DEBUG)
28 #include <sal/backtrace.hxx>
34 #include <sys/resource.h>
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>
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>
68 #include <boost/algorithm/string.hpp>
71 #include <string_view>
78 << ("Usage: cppunittester (--protector <shared-library-path>"
79 " <function-symbol>)* <shared-library-path>")
81 std::exit(EXIT_FAILURE
);
84 OUString
getArgument(sal_Int32 index
) {
86 osl_getCommandArg(index
, &arg
.pData
);
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
95 s8
.getStr(), static_cast< std::string::size_type
>(s8
.getLength()));
98 //Output how long each test took
100 : public CppUnit::TestListener
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
;
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
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
])))
148 _putenv_s("LO_TESTNAME", tn
.getStr());
150 setenv("LO_TESTNAME", tn
.getStr(), true);
154 void endTest( CppUnit::Test
* /* test */ ) override
159 class LogFailuresAsTheyHappen
: public CppUnit::TestListener
162 virtual void addFailure( const CppUnit::TestFailure
&failure
) override
164 printFailureLocation( failure
.sourceLine() );
165 printFailedTestName( failure
);
166 printFailureMessage( failure
);
170 static void printFailureLocation( const CppUnit::SourceLine
&sourceLine
)
172 if ( !sourceLine
.isValid() )
173 std::cerr
<< "unknown:0:";
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
)
203 size_t nEndPos
= nPos
+ rCmp
.size();
204 return nEndPos
== maName
.size();
210 bool addRecursiveTests(const std::vector
<std::string
>& test_names
, CppUnit::Test
* pTest
, CppUnit::TestRunner
& rRunner
)
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
);
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
233 const std::string
&testlib
;
234 const std::string
&args
;
235 std::vector
<CppUnit::Protector
*> &protectors
;
236 CppUnit::TestResult
&result
;
238 ProtectedFixtureFunctor(const std::string
& testlib_
, const std::string
&args_
, std::vector
<CppUnit::Protector
*> &protectors_
, CppUnit::TestResult
&result_
)
241 , protectors(protectors_
)
245 ProtectedFixtureFunctor(const ProtectedFixtureFunctor
&) = delete;
246 ProtectedFixtureFunctor
& operator=(const ProtectedFixtureFunctor
&) = delete;
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.
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
;
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;
280 std::cerr
<< "Windows has significant build problems with long PATH variables ";
281 std::cerr
<< "please check your PATH variable and re-autogen.\n";
284 std::cerr
<< "Path (length: " << nPathLen
<< ") is '" << pPath
<< "'\n";
289 for (size_t i
= 0; i
< protectors
.size(); ++i
)
290 result
.pushProtector(protectors
[i
]);
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);
309 _putenv_s("LO_TESTNAME", lib
.c_str());
311 setenv("LO_TESTNAME", lib
.c_str(), true);
313 const char* pVal
= getenv("CPPUNIT_TEST_NAME");
315 CppUnit::TestRunner runner
;
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
));
324 std::cerr
<< "\nFatal error: CPPUNIT_TEST_NAME contains no valid tests\n";
325 // coverity[leaked_storage] - `manager` leaked on purpose
331 runner
.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
335 CppUnit::CompilerOutputter
outputter(&collector
, CppUnit::stdCErr());
336 outputter
.setNoWrap();
338 success
= collector
.wasSuccessful();
341 for (size_t i
= 0; i
< protectors
.size(); ++i
)
342 result
.popProtector();
346 virtual bool operator()() const override
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;
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
;
383 void reportResourceUsage([[maybe_unused
]] const OUString
& /*rPath*/)
396 //Disable Dr-Watson in order to crash simply without popup dialogs under
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
);
410 std::vector
<CppUnit::Protector
*> protectors
;
411 CppUnit::TestResult result
;
414 sal_uInt32 index
= 0;
415 while (index
< osl_getCommandArgCount())
417 OUString arg
= getArgument(index
);
418 if (arg
.startsWith("-env:CPPUNITTESTTARGET=", &path
))
423 if (arg
.startsWith("-env:"))
426 continue; // ignore it here - will be read later
428 if ( arg
!= "--protector" )
432 testlib
= OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
438 args
+= OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
443 if (osl_getCommandArgCount() - index
< 3) {
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
);
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
;
463 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
465 std::exit(EXIT_FAILURE
);
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
);
482 ProtectedFixtureFunctor
tests(testlib
, args
, protectors
, result
);
485 reportResourceUsage(path
);
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
;
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
;
511 stack
.AddrPC
.Offset
= ctx
->Eip
;
512 stack
.AddrStack
.Offset
= ctx
->Esp
;
513 stack
.AddrFrame
.Offset
= ctx
->Ebp
;
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
);
531 //get next call from stack
532 bool result
= StackWalk64
535 IMAGE_FILE_MACHINE_AMD64
,
536 #elif defined _M_ARM64
537 IMAGE_FILE_MACHINE_ARM64
,
539 IMAGE_FILE_MACHINE_I386
,
546 SymFunctionTableAccess64
,
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
);
560 printf("\tat unknown (Error in SymFromAddr=%#08lx)", GetLastError());
564 if (SymGetLineFromAddr64(process
, stack
.AddrPC
.Offset
, &disp
, &line
))
566 printf(" in %s: line: %lu:\n", line
.FileName
, line
.LineNumber
);
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
);
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()));
605 // catch the kind of signal that is thrown when an assert fails, and log a stacktrace
606 signal(SIGABRT
, AbortSignalHandler
);
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.
615 __except (ExpFilter(GetExceptionInformation()))
618 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;
630 catch (const std::exception
& e
)
632 SAL_WARN("vcl.app", "Fatal exception: " << e
.what());
634 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;
639 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */