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
94 return std::string(s8
);
97 //Output how long each test took
99 : public CppUnit::TestListener
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
;
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
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
])))
147 _putenv_s("LO_TESTNAME", tn
.getStr());
149 setenv("LO_TESTNAME", tn
.getStr(), true);
153 void endTest( CppUnit::Test
* /* test */ ) override
158 class LogFailuresAsTheyHappen
: public CppUnit::TestListener
161 virtual void addFailure( const CppUnit::TestFailure
&failure
) override
163 printFailureLocation( failure
.sourceLine() );
164 printFailedTestName( failure
);
165 printFailureMessage( failure
);
169 static void printFailureLocation( const CppUnit::SourceLine
&sourceLine
)
171 if ( !sourceLine
.isValid() )
172 std::cerr
<< "unknown:0:";
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
)
202 size_t nEndPos
= nPos
+ rCmp
.size();
203 return nEndPos
== maName
.size();
209 bool addRecursiveTests(const std::vector
<std::string
>& test_names
, CppUnit::Test
* pTest
, CppUnit::TestRunner
& rRunner
)
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
);
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
232 const std::string
&testlib
;
233 const std::string
&args
;
234 std::vector
<CppUnit::Protector
*> &protectors
;
235 CppUnit::TestResult
&result
;
237 ProtectedFixtureFunctor(const std::string
& testlib_
, const std::string
&args_
, std::vector
<CppUnit::Protector
*> &protectors_
, CppUnit::TestResult
&result_
)
240 , protectors(protectors_
)
244 ProtectedFixtureFunctor(const ProtectedFixtureFunctor
&) = delete;
245 ProtectedFixtureFunctor
& operator=(const ProtectedFixtureFunctor
&) = delete;
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.
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
;
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;
279 std::cerr
<< "Windows has significant build problems with long PATH variables ";
280 std::cerr
<< "please check your PATH variable and re-autogen.\n";
283 std::cerr
<< "Path (length: " << nPathLen
<< ") is '" << pPath
<< "'\n";
288 for (size_t i
= 0; i
< protectors
.size(); ++i
)
289 result
.pushProtector(protectors
[i
]);
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);
308 _putenv_s("LO_TESTNAME", lib
.c_str());
310 setenv("LO_TESTNAME", lib
.c_str(), true);
312 const char* pVal
= getenv("CPPUNIT_TEST_NAME");
314 CppUnit::TestRunner runner
;
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
));
323 std::cerr
<< "\nFatal error: CPPUNIT_TEST_NAME contains no valid tests\n";
324 // coverity[leaked_storage] - `manager` leaked on purpose
330 runner
.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
334 CppUnit::CompilerOutputter
outputter(&collector
, CppUnit::stdCErr());
335 outputter
.setNoWrap();
337 success
= collector
.wasSuccessful();
340 for (size_t i
= 0; i
< protectors
.size(); ++i
)
341 result
.popProtector();
345 virtual bool operator()() const override
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;
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
;
382 void reportResourceUsage([[maybe_unused
]] const OUString
& /*rPath*/)
395 //Disable Dr-Watson in order to crash simply without popup dialogs under
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
);
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)
412 = CreateDesktopW(L
"LO_CPPUNIT_DESKTOP", nullptr, nullptr, 0, GENERIC_ALL
, nullptr))
413 SetThreadDesktop(hDesktop
);
417 std::vector
<CppUnit::Protector
*> protectors
;
418 CppUnit::TestResult result
;
421 sal_uInt32 index
= 0;
422 while (index
< osl_getCommandArgCount())
424 OUString arg
= getArgument(index
);
425 if (arg
.startsWith("-env:CPPUNITTESTTARGET=", &path
))
430 if (arg
.startsWith("-env:"))
433 continue; // ignore it here - will be read later
435 if ( arg
!= "--protector" )
439 testlib
= OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
445 args
+= OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
450 if (osl_getCommandArgCount() - index
< 3) {
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
);
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
;
470 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
472 std::exit(EXIT_FAILURE
);
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
);
489 ProtectedFixtureFunctor
tests(testlib
, args
, protectors
, result
);
492 reportResourceUsage(path
);
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
;
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
;
518 stack
.AddrPC
.Offset
= ctx
->Eip
;
519 stack
.AddrStack
.Offset
= ctx
->Esp
;
520 stack
.AddrFrame
.Offset
= ctx
->Ebp
;
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
);
538 //get next call from stack
539 bool result
= StackWalk64
542 IMAGE_FILE_MACHINE_AMD64
,
543 #elif defined _M_ARM64
544 IMAGE_FILE_MACHINE_ARM64
,
546 IMAGE_FILE_MACHINE_I386
,
553 SymFunctionTableAccess64
,
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
);
567 printf("\tat unknown (Error in SymFromAddr=%#08lx)", GetLastError());
571 if (SymGetLineFromAddr64(process
, stack
.AddrPC
.Offset
, &disp
, &line
))
573 printf(" in %s: line: %lu:\n", line
.FileName
, line
.LineNumber
);
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
);
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()));
612 // catch the kind of signal that is thrown when an assert fails, and log a stacktrace
613 signal(SIGABRT
, AbortSignalHandler
);
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.
622 __except (ExpFilter(GetExceptionInformation()))
625 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;
637 catch (const std::exception
& e
)
639 SAL_WARN("vcl.app", "Fatal exception: " << e
.what());
641 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;
646 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */