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>
33 #include <sys/resource.h>
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>
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>
66 #include <string_view>
73 << ("Usage: cppunittester (--protector <shared-library-path>"
74 " <function-symbol>)* <shared-library-path>")
76 std::exit(EXIT_FAILURE
);
79 OUString
getArgument(sal_Int32 index
) {
81 osl_getCommandArg(index
, &arg
.pData
);
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
94 : public CppUnit::TestListener
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
;
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
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
])))
142 _putenv_s("LO_TESTNAME", tn
.getStr());
144 setenv("LO_TESTNAME", tn
.getStr(), true);
148 void endTest( CppUnit::Test
* /* test */ ) override
153 class LogFailuresAsTheyHappen
: public CppUnit::TestListener
156 virtual void addFailure( const CppUnit::TestFailure
&failure
) override
158 printFailureLocation( failure
.sourceLine() );
159 printFailedTestName( failure
);
160 printFailureMessage( failure
);
164 static void printFailureLocation( const CppUnit::SourceLine
&sourceLine
)
166 if ( !sourceLine
.isValid() )
167 std::cerr
<< "unknown:0:";
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
)
197 size_t nEndPos
= nPos
+ rCmp
.size();
198 return nEndPos
== maName
.size();
204 bool addRecursiveTests(const std::vector
<std::string
>& test_names
, CppUnit::Test
* pTest
, CppUnit::TestRunner
& rRunner
)
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
);
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
227 const std::string
&testlib
;
228 const std::string
&args
;
229 std::vector
<CppUnit::Protector
*> &protectors
;
230 CppUnit::TestResult
&result
;
232 ProtectedFixtureFunctor(const std::string
& testlib_
, const std::string
&args_
, std::vector
<CppUnit::Protector
*> &protectors_
, CppUnit::TestResult
&result_
)
235 , protectors(protectors_
)
239 ProtectedFixtureFunctor(const ProtectedFixtureFunctor
&) = delete;
240 ProtectedFixtureFunctor
& operator=(const ProtectedFixtureFunctor
&) = delete;
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.
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
;
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;
274 std::cerr
<< "Windows has significant build problems with long PATH variables ";
275 std::cerr
<< "please check your PATH variable and re-autogen.\n";
278 std::cerr
<< "Path (length: " << nPathLen
<< ") is '" << pPath
<< "'\n";
283 for (size_t i
= 0; i
< protectors
.size(); ++i
)
284 result
.pushProtector(protectors
[i
]);
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);
303 _putenv_s("LO_TESTNAME", lib
.c_str());
305 setenv("LO_TESTNAME", lib
.c_str(), true);
307 const char* pVal
= getenv("CPPUNIT_TEST_NAME");
309 CppUnit::TestRunner runner
;
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
));
318 std::cerr
<< "\nFatal error: CPPUNIT_TEST_NAME contains no valid tests\n";
319 // coverity[leaked_storage] - `manager` leaked on purpose
325 runner
.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
329 CppUnit::CompilerOutputter
outputter(&collector
, CppUnit::stdCErr());
330 outputter
.setNoWrap();
332 success
= collector
.wasSuccessful();
335 for (size_t i
= 0; i
< protectors
.size(); ++i
)
336 result
.popProtector();
340 virtual bool operator()() const override
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;
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
;
377 void reportResourceUsage([[maybe_unused
]] const OUString
& /*rPath*/)
390 //Disable Dr-Watson in order to crash simply without popup dialogs under
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
);
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)
407 = CreateDesktopW(L
"LO_CPPUNIT_DESKTOP", nullptr, nullptr, 0, GENERIC_ALL
, nullptr))
408 SetThreadDesktop(hDesktop
);
412 std::vector
<CppUnit::Protector
*> protectors
;
413 CppUnit::TestResult result
;
416 sal_uInt32 index
= 0;
417 while (index
< osl_getCommandArgCount())
419 OUString arg
= getArgument(index
);
420 if (arg
.startsWith("-env:CPPUNITTESTTARGET=", &path
))
425 if (arg
.startsWith("-env:"))
428 continue; // ignore it here - will be read later
430 if ( arg
!= "--protector" )
434 testlib
= OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
440 args
+= OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
445 if (osl_getCommandArgCount() - index
< 3) {
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
);
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
;
465 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
467 std::exit(EXIT_FAILURE
);
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
);
484 ProtectedFixtureFunctor
tests(testlib
, args
, protectors
, result
);
487 reportResourceUsage(path
);
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
;
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
;
513 stack
.AddrPC
.Offset
= ctx
->Eip
;
514 stack
.AddrStack
.Offset
= ctx
->Esp
;
515 stack
.AddrFrame
.Offset
= ctx
->Ebp
;
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
);
533 //get next call from stack
534 bool result
= StackWalk64
537 IMAGE_FILE_MACHINE_AMD64
,
538 #elif defined _M_ARM64
539 IMAGE_FILE_MACHINE_ARM64
,
541 IMAGE_FILE_MACHINE_I386
,
548 SymFunctionTableAccess64
,
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
);
562 printf("\tat unknown (Error in SymFromAddr=%#08lx)", GetLastError());
566 if (SymGetLineFromAddr64(process
, stack
.AddrPC
.Offset
, &disp
, &line
))
568 printf(" in %s: line: %lu:\n", line
.FileName
, line
.LineNumber
);
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
);
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()));
607 // catch the kind of signal that is thrown when an assert fails, and log a stacktrace
608 signal(SIGABRT
, AbortSignalHandler
);
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.
617 __except (ExpFilter(GetExceptionInformation()))
620 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;
632 catch (const std::exception
& e
)
634 SAL_WARN("vcl.app", "Fatal exception: " << e
.what());
636 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;
641 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */