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 .
26 #include <sys/resource.h>
32 #include <sal/types.h>
33 #include "cppunittester/protectorfactory.hxx"
34 #include "osl/module.h"
35 #include "osl/module.hxx"
36 #include "osl/thread.h"
37 #include "rtl/process.h"
38 #include "rtl/string.h"
39 #include "rtl/string.hxx"
40 #include "rtl/textcvt.h"
41 #include "rtl/ustring.hxx"
44 #include "cppunit/CompilerOutputter.h"
45 #include "cppunit/Exception.h"
46 #include "cppunit/TestFailure.h"
47 #include "cppunit/TestResult.h"
48 #include "cppunit/TestResultCollector.h"
49 #include "cppunit/TestRunner.h"
50 #include "cppunit/extensions/TestFactoryRegistry.h"
51 #include "cppunit/plugin/PlugInManager.h"
52 #include "cppunit/plugin/DynamicLibraryManagerException.h"
53 #include "cppunit/portability/Stream.h"
56 #include <boost/algorithm/string.hpp>
64 << ("Usage: cppunittester (--protector <shared-library-path>"
65 " <function-symbol>)* <shared-library-path>")
67 std::exit(EXIT_FAILURE
);
70 rtl::OUString
getArgument(sal_Int32 index
) {
72 osl_getCommandArg(index
, &arg
.pData
);
76 std::string
convertLazy(rtl::OUString
const & s16
) {
77 rtl::OString
s8(rtl::OUStringToOString(s16
, osl_getThreadTextEncoding()));
78 static_assert(sizeof (sal_Int32
) <= sizeof (std::string::size_type
), "must be at least the same size");
79 // ensure following cast is legitimate
81 s8
.getStr(), static_cast< std::string::size_type
>(s8
.getLength()));
84 //Output how long each test took
86 : public CppUnit::TestListener
93 TimingListener(const TimingListener
&) = delete;
94 TimingListener
& operator=(const TimingListener
&) = delete;
96 void startTest( CppUnit::Test
*) override
98 m_nStartTime
= osl_getGlobalTimer();
101 void endTest( CppUnit::Test
*test
) override
103 sal_uInt32 nEndTime
= osl_getGlobalTimer();
104 std::cout
<< test
->getName() << " finished in: "
105 << nEndTime
-m_nStartTime
<< "ms" << std::endl
;
109 sal_uInt32 m_nStartTime
;
114 // Setup an env variable so that temp file (or other) can
115 // have a useful value to identify the source
116 class EyecatcherListener
117 : public CppUnit::TestListener
120 EyecatcherListener() = default;
121 EyecatcherListener(const EyecatcherListener
&) = delete;
122 EyecatcherListener
& operator=(const EyecatcherListener
&) = delete;
123 void startTest( CppUnit::Test
* test
) override
125 std::unique_ptr
<char[]> tn(new char [ test
->getName().length() + 2 ]);
126 strcpy(tn
.get(), test
->getName().c_str());
127 int len
= strlen(tn
.get());
128 for(int i
= 0; i
< len
; i
++)
137 setenv("LO_TESTNAME", tn
.get(), true);
140 void endTest( CppUnit::Test
* /* test */ ) override
146 class LogFailuresAsTheyHappen
: public CppUnit::TestListener
149 virtual void addFailure( const CppUnit::TestFailure
&failure
) override
151 printFailureLocation( failure
.sourceLine() );
152 printFailedTestName( failure
);
153 printFailureMessage( failure
);
157 static void printFailureLocation( const CppUnit::SourceLine
&sourceLine
)
159 if ( !sourceLine
.isValid() )
160 std::cerr
<< "unknown:0:";
162 std::cerr
<< sourceLine
.fileName() << ":" << sourceLine
.lineNumber() << ":";
165 static void printFailedTestName( const CppUnit::TestFailure
&failure
)
167 std::cerr
<< failure
.failedTestName() << std::endl
;
170 static void printFailureMessage( const CppUnit::TestFailure
&failure
)
172 std::cerr
<< failure
.thrownException()->message().shortDescription() << std::endl
;
173 std::cerr
<< failure
.thrownException()->message().details() << std::endl
;
179 struct test_name_compare
181 explicit test_name_compare(const std::string
& rName
):
186 bool operator()(const std::string
& rCmp
)
188 size_t nPos
= maName
.find(rCmp
);
189 if (nPos
== std::string::npos
)
192 size_t nEndPos
= nPos
+ rCmp
.size();
193 return nEndPos
== maName
.size();
199 void addRecursiveTests(const std::vector
<std::string
>& test_names
, CppUnit::Test
* pTest
, CppUnit::TestRunner
& rRunner
)
201 for (int i
= 0; i
< pTest
->getChildTestCount(); ++i
)
203 CppUnit::Test
* pNewTest
= pTest
->getChildTestAt(i
);
204 addRecursiveTests(test_names
, pNewTest
, rRunner
);
205 if (std::find_if(test_names
.begin(), test_names
.end(), test_name_compare(pNewTest
->getName())) != test_names
.end())
206 rRunner
.addTest(pNewTest
);
212 //Allow the whole uniting testing framework to be run inside a "Protector"
213 //which knows about uno exceptions, so it can print the content of the
214 //exception before falling over and dying
215 class CPPUNIT_API ProtectedFixtureFunctor
216 : public CppUnit::Functor
219 const std::string
&testlib
;
220 const std::string
&args
;
221 std::vector
<CppUnit::Protector
*> &protectors
;
222 CppUnit::TestResult
&result
;
224 ProtectedFixtureFunctor(const std::string
& testlib_
, const std::string
&args_
, std::vector
<CppUnit::Protector
*> &protectors_
, CppUnit::TestResult
&result_
)
227 , protectors(protectors_
)
231 ProtectedFixtureFunctor(const ProtectedFixtureFunctor
&) = delete;
232 ProtectedFixtureFunctor
& operator=(const ProtectedFixtureFunctor
&) = delete;
235 #ifdef DISABLE_DYNLOADING
237 // NOTE: Running cppunit unit tests on iOS was something I did
238 // only very early (several years ago) when starting porting
239 // this stuff to iOS. The complicated mechanisms to do build
240 // such unit test single executables have surely largely
241 // bit-rotted or been semi-intentionally broken since. This
242 // stuff here left for information only. --tml 2014.
244 // For iOS cppunit plugins aren't really "plugins" (shared
245 // libraries), but just static archives. In the real main
246 // program of a cppunit app, which calls the lo_main() that
247 // the SAL_IMPLEMENT_MAIN() below expands to, we specifically
248 // call the initialize methods of the CppUnitTestPlugIns that
249 // we statically link to the app executable.
251 CppUnit::PlugInManager manager
;
253 manager
.load(testlib
, args
);
254 } catch (const CppUnit::DynamicLibraryManagerException
&e
) {
255 std::cerr
<< "DynamicLibraryManagerException: \"" << e
.what() << "\"\n";
257 const char *pPath
= getenv ("PATH");
258 if (pPath
&& strlen (pPath
) > 256)
260 std::cerr
<< "Windows has significant build problems with long PATH variables ";
261 std::cerr
<< "please check your PATH variable and re-autogen.\n";
264 std::cerr
<< "Path is '" << getenv("PATH") << "'\n";
269 for (size_t i
= 0; i
< protectors
.size(); ++i
)
270 result
.pushProtector(protectors
[i
]);
274 CppUnit::TestResultCollector collector
;
275 result
.addListener(&collector
);
277 LogFailuresAsTheyHappen logger
;
278 result
.addListener(&logger
);
280 TimingListener timer
;
281 result
.addListener(&timer
);
284 EyecatcherListener eye
;
285 result
.addListener(&eye
);
286 // set this to track down files created before first test method
287 std::string
lib(testlib
.substr(testlib
.rfind('/')+1));
288 setenv("LO_TESTNAME", lib
.c_str(), true);
291 const char* pVal
= getenv("CPPUNIT_TEST_NAME");
293 CppUnit::TestRunner runner
;
296 std::vector
<std::string
> test_names
;
297 boost::split(test_names
, pVal
, boost::is_any_of("\t "));
298 CppUnit::Test
* pTest
= CppUnit::TestFactoryRegistry::getRegistry().makeTest();
299 addRecursiveTests(test_names
, pTest
, runner
);
303 runner
.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
307 CppUnit::CompilerOutputter
outputter(&collector
, CppUnit::stdCErr());
308 outputter
.setNoWrap();
310 success
= collector
.wasSuccessful();
313 for (size_t i
= 0; i
< protectors
.size(); ++i
)
314 result
.popProtector();
318 virtual bool operator()() const override
326 double get_time(timeval
* time
)
328 double nTime
= (double)time
->tv_sec
;
329 nTime
+= ((double)time
->tv_usec
)/1000000.0;
333 OString
generateTestName(const OUString
& rPath
)
335 sal_Int32 nPathSep
= rPath
.lastIndexOf("/");
336 OUString aTestName
= rPath
.copy(nPathSep
+1);
337 return OUStringToOString(aTestName
, RTL_TEXTENCODING_UTF8
);
340 void reportResourceUsage(const OUString
& rPath
)
342 OUString aFullPath
= rPath
+ ".resource.log";
343 rusage resource_usage
;
344 getrusage(RUSAGE_SELF
, &resource_usage
);
346 OString aPath
= OUStringToOString(aFullPath
, RTL_TEXTENCODING_UTF8
);
347 std::ofstream
resource_file(aPath
.getStr());
348 resource_file
<< "Name = " << generateTestName(rPath
) << std::endl
;
349 double nUserSpace
= get_time(&resource_usage
.ru_utime
);
350 double nKernelSpace
= get_time(&resource_usage
.ru_stime
);
351 resource_file
<< "UserSpace = " << nUserSpace
<< std::endl
;
352 resource_file
<< "KernelSpace = " << nKernelSpace
<< std::endl
;
355 void reportResourceUsage(const OUString
& /*rPath*/)
369 //Disable Dr-Watson in order to crash simply without popup dialogs under
371 DWORD dwMode
= SetErrorMode(SEM_NOGPFAULTERRORBOX
);
372 SetErrorMode(SEM_NOGPFAULTERRORBOX
|dwMode
);
373 #ifdef _DEBUG // These functions are present only in the debugging runtime
374 _CrtSetReportMode(_CRT_WARN
, _CRTDBG_MODE_DEBUG
|_CRTDBG_MODE_FILE
);
375 _CrtSetReportFile(_CRT_WARN
, _CRTDBG_FILE_STDERR
);
376 _CrtSetReportMode(_CRT_ERROR
, _CRTDBG_MODE_DEBUG
|_CRTDBG_MODE_FILE
);
377 _CrtSetReportFile(_CRT_ERROR
, _CRTDBG_FILE_STDERR
);
378 _CrtSetReportMode(_CRT_ASSERT
, _CRTDBG_MODE_DEBUG
|_CRTDBG_MODE_FILE
);
379 _CrtSetReportFile(_CRT_ASSERT
, _CRTDBG_FILE_STDERR
);
383 std::vector
<CppUnit::Protector
*> protectors
;
384 CppUnit::TestResult result
;
387 sal_uInt32 index
= 0;
388 while (index
< osl_getCommandArgCount())
390 rtl::OUString arg
= getArgument(index
);
391 if (arg
.startsWith("-env:CPPUNITTESTTARGET=", &path
))
396 if (arg
.startsWith("-env:"))
399 continue; // ignore it here - will be read later
401 if ( arg
!= "--protector" )
405 testlib
= rtl::OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
411 args
+= rtl::OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
416 if (osl_getCommandArgCount() - index
< 3) {
419 rtl::OUString
lib(getArgument(index
+ 1));
420 rtl::OUString
sym(getArgument(index
+ 2));
421 #ifndef DISABLE_DYNLOADING
422 osl::Module
mod(lib
, SAL_LOADMODULE_GLOBAL
);
423 oslGenericFunction fn
= mod
.getFunctionSymbol(sym
);
426 oslGenericFunction fn
= 0;
427 if (sym
== "unoexceptionprotector")
428 fn
= (oslGenericFunction
) unoexceptionprotector
;
429 else if (sym
== "unobootstrapprotector")
430 fn
= (oslGenericFunction
) unobootstrapprotector
;
431 else if (sym
== "vclbootstrapprotector")
432 fn
= (oslGenericFunction
) vclbootstrapprotector
;
436 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
438 std::exit(EXIT_FAILURE
);
441 CppUnit::Protector
*protector
= fn
== nullptr
443 : (*reinterpret_cast< cppunittester::ProtectorFactory
* >(fn
))();
444 if (protector
== nullptr) {
446 << "Failure instantiating protector \"" << convertLazy(lib
)
447 << "\", \"" << convertLazy(sym
) << '"' << std::endl
;
448 std::exit(EXIT_FAILURE
);
450 protectors
.push_back(protector
);
454 ProtectedFixtureFunctor
tests(testlib
, args
, protectors
, result
);
457 catch (const std::exception
& e
)
459 SAL_WARN("vcl.app", "Fatal exception: " << e
.what());
462 reportResourceUsage(path
);
464 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;
467 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */