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
29 #include <sys/resource.h>
35 #include <sal/log.hxx>
36 #include <sal/types.h>
37 #include <cppunittester/protectorfactory.hxx>
38 #include <osl/module.h>
39 #include <osl/module.hxx>
40 #include <osl/thread.h>
41 #include <rtl/character.hxx>
42 #include <rtl/process.h>
43 #include <rtl/string.h>
44 #include <rtl/string.hxx>
45 #include <rtl/textcvt.h>
46 #include <rtl/ustring.hxx>
49 #include <cppunit/CompilerOutputter.h>
50 #include <cppunit/Exception.h>
51 #include <cppunit/TestFailure.h>
52 #include <cppunit/TestResult.h>
53 #include <cppunit/TestResultCollector.h>
54 #include <cppunit/TestRunner.h>
55 #include <cppunit/extensions/TestFactoryRegistry.h>
56 #include <cppunit/plugin/PlugInManager.h>
57 #include <cppunit/plugin/DynamicLibraryManagerException.h>
58 #include <cppunit/portability/Stream.h>
61 #include <boost/algorithm/string.hpp>
69 << ("Usage: cppunittester (--protector <shared-library-path>"
70 " <function-symbol>)* <shared-library-path>")
72 std::exit(EXIT_FAILURE
);
75 OUString
getArgument(sal_Int32 index
) {
77 osl_getCommandArg(index
, &arg
.pData
);
81 std::string
convertLazy(OUString
const & s16
) {
82 OString
s8(OUStringToOString(s16
, osl_getThreadTextEncoding()));
83 static_assert(sizeof (sal_Int32
) <= sizeof (std::string::size_type
), "must be at least the same size");
84 // ensure following cast is legitimate
86 s8
.getStr(), static_cast< std::string::size_type
>(s8
.getLength()));
89 //Output how long each test took
91 : public CppUnit::TestListener
98 TimingListener(const TimingListener
&) = delete;
99 TimingListener
& operator=(const TimingListener
&) = delete;
101 void startTest( CppUnit::Test
*) override
103 m_nStartTime
= osl_getGlobalTimer();
106 void endTest( CppUnit::Test
*test
) override
108 sal_uInt32 nEndTime
= osl_getGlobalTimer();
109 std::cout
<< test
->getName() << " finished in: "
110 << nEndTime
-m_nStartTime
<< "ms" << std::endl
;
114 sal_uInt32 m_nStartTime
;
119 // Setup an env variable so that temp file (or other) can
120 // have a useful value to identify the source
121 class EyecatcherListener
122 : public CppUnit::TestListener
125 EyecatcherListener() = default;
126 EyecatcherListener(const EyecatcherListener
&) = delete;
127 EyecatcherListener
& operator=(const EyecatcherListener
&) = delete;
128 void startTest( CppUnit::Test
* test
) override
130 std::unique_ptr
<char[]> tn(new char [ test
->getName().length() + 2 ]);
131 strcpy(tn
.get(), test
->getName().c_str());
132 int len
= strlen(tn
.get());
133 for(int i
= 0; i
< len
; i
++)
135 if(!rtl::isAsciiAlphanumeric(static_cast<unsigned char>(tn
[i
])))
142 setenv("LO_TESTNAME", tn
.get(), true);
145 void endTest( CppUnit::Test
* /* test */ ) override
151 class LogFailuresAsTheyHappen
: public CppUnit::TestListener
154 virtual void addFailure( const CppUnit::TestFailure
&failure
) override
156 printFailureLocation( failure
.sourceLine() );
157 printFailedTestName( failure
);
158 printFailureMessage( failure
);
162 static void printFailureLocation( const CppUnit::SourceLine
&sourceLine
)
164 if ( !sourceLine
.isValid() )
165 std::cerr
<< "unknown:0:";
167 std::cerr
<< sourceLine
.fileName() << ":" << sourceLine
.lineNumber() << ":";
170 static void printFailedTestName( const CppUnit::TestFailure
&failure
)
172 std::cerr
<< failure
.failedTestName() << std::endl
;
175 static void printFailureMessage( const CppUnit::TestFailure
&failure
)
177 std::cerr
<< failure
.thrownException()->message().shortDescription() << std::endl
;
178 std::cerr
<< failure
.thrownException()->message().details() << std::endl
;
182 struct test_name_compare
184 explicit test_name_compare(const std::string
& rName
):
189 bool operator()(const std::string
& rCmp
)
191 size_t nPos
= maName
.find(rCmp
);
192 if (nPos
== std::string::npos
)
195 size_t nEndPos
= nPos
+ rCmp
.size();
196 return nEndPos
== maName
.size();
199 std::string
const maName
;
202 bool addRecursiveTests(const std::vector
<std::string
>& test_names
, CppUnit::Test
* pTest
, CppUnit::TestRunner
& rRunner
)
205 for (int i
= 0; i
< pTest
->getChildTestCount(); ++i
)
207 CppUnit::Test
* pNewTest
= pTest
->getChildTestAt(i
);
208 ret
|= addRecursiveTests(test_names
, pNewTest
, rRunner
);
209 if (std::any_of(test_names
.begin(), test_names
.end(), test_name_compare(pNewTest
->getName())))
211 rRunner
.addTest(pNewTest
);
218 //Allow the whole uniting testing framework to be run inside a "Protector"
219 //which knows about uno exceptions, so it can print the content of the
220 //exception before falling over and dying
221 class CPPUNIT_API ProtectedFixtureFunctor
222 : public CppUnit::Functor
225 const std::string
&testlib
;
226 const std::string
&args
;
227 std::vector
<CppUnit::Protector
*> &protectors
;
228 CppUnit::TestResult
&result
;
230 ProtectedFixtureFunctor(const std::string
& testlib_
, const std::string
&args_
, std::vector
<CppUnit::Protector
*> &protectors_
, CppUnit::TestResult
&result_
)
233 , protectors(protectors_
)
237 ProtectedFixtureFunctor(const ProtectedFixtureFunctor
&) = delete;
238 ProtectedFixtureFunctor
& operator=(const ProtectedFixtureFunctor
&) = delete;
241 #ifdef DISABLE_DYNLOADING
243 // NOTE: Running cppunit unit tests on iOS was something I did
244 // only very early (several years ago) when starting porting
245 // this stuff to iOS. The complicated mechanisms to do build
246 // such unit test single executables have surely largely
247 // bit-rotted or been semi-intentionally broken since. This
248 // stuff here left for information only. --tml 2014.
250 // For iOS cppunit plugins aren't really "plugins" (shared
251 // libraries), but just static archives. In the real main
252 // program of a cppunit app, which calls the lo_main() that
253 // the SAL_IMPLEMENT_MAIN() below expands to, we specifically
254 // call the initialize methods of the CppUnitTestPlugIns that
255 // we statically link to the app executable.
257 CppUnit::PlugInManager manager
;
259 manager
.load(testlib
, args
);
260 } catch (const CppUnit::DynamicLibraryManagerException
&e
) {
261 std::cerr
<< "DynamicLibraryManagerException: \"" << e
.what() << "\"\n";
263 const char *pPath
= getenv ("PATH");
264 if (pPath
&& strlen (pPath
) > 256)
266 std::cerr
<< "Windows has significant build problems with long PATH variables ";
267 std::cerr
<< "please check your PATH variable and re-autogen.\n";
270 std::cerr
<< "Path is '" << getenv("PATH") << "'\n";
275 for (size_t i
= 0; i
< protectors
.size(); ++i
)
276 result
.pushProtector(protectors
[i
]);
280 CppUnit::TestResultCollector collector
;
281 result
.addListener(&collector
);
283 LogFailuresAsTheyHappen logger
;
284 result
.addListener(&logger
);
286 TimingListener timer
;
287 result
.addListener(&timer
);
290 EyecatcherListener eye
;
291 result
.addListener(&eye
);
292 // set this to track down files created before first test method
293 std::string
lib(testlib
.substr(testlib
.rfind('/')+1));
294 setenv("LO_TESTNAME", lib
.c_str(), true);
297 const char* pVal
= getenv("CPPUNIT_TEST_NAME");
299 CppUnit::TestRunner runner
;
302 std::vector
<std::string
> test_names
;
303 boost::split(test_names
, pVal
, boost::is_any_of("\t "));
304 CppUnit::Test
* pTest
= CppUnit::TestFactoryRegistry::getRegistry().makeTest();
305 bool const added(addRecursiveTests(test_names
, pTest
, runner
));
308 std::cerr
<< "\nFatal error: CPPUNIT_TEST_NAME contains no valid tests\n";
314 runner
.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
318 CppUnit::CompilerOutputter
outputter(&collector
, CppUnit::stdCErr());
319 outputter
.setNoWrap();
321 success
= collector
.wasSuccessful();
324 for (size_t i
= 0; i
< protectors
.size(); ++i
)
325 result
.popProtector();
329 virtual bool operator()() const override
337 double get_time(timeval
* time
)
339 double nTime
= static_cast<double>(time
->tv_sec
);
340 nTime
+= static_cast<double>(time
->tv_usec
)/1000000.0;
344 OString
generateTestName(const OUString
& rPath
)
346 sal_Int32 nPathSep
= rPath
.lastIndexOf("/");
347 OUString aTestName
= rPath
.copy(nPathSep
+1);
348 return OUStringToOString(aTestName
, RTL_TEXTENCODING_UTF8
);
351 void reportResourceUsage(const OUString
& rPath
)
353 OUString aFullPath
= rPath
+ ".resource.log";
354 rusage resource_usage
;
355 getrusage(RUSAGE_SELF
, &resource_usage
);
357 OString aPath
= OUStringToOString(aFullPath
, RTL_TEXTENCODING_UTF8
);
358 std::ofstream
resource_file(aPath
.getStr());
359 resource_file
<< "Name = " << generateTestName(rPath
) << std::endl
;
360 double nUserSpace
= get_time(&resource_usage
.ru_utime
);
361 double nKernelSpace
= get_time(&resource_usage
.ru_stime
);
362 resource_file
<< "UserSpace = " << nUserSpace
<< std::endl
;
363 resource_file
<< "KernelSpace = " << nKernelSpace
<< std::endl
;
366 void reportResourceUsage(const OUString
& /*rPath*/)
380 //Disable Dr-Watson in order to crash simply without popup dialogs under
382 DWORD dwMode
= SetErrorMode(SEM_NOGPFAULTERRORBOX
);
383 SetErrorMode(SEM_NOGPFAULTERRORBOX
|dwMode
);
384 #ifdef _DEBUG // These functions are present only in the debugging runtime
385 _CrtSetReportMode(_CRT_WARN
, _CRTDBG_MODE_DEBUG
|_CRTDBG_MODE_FILE
);
386 _CrtSetReportFile(_CRT_WARN
, _CRTDBG_FILE_STDERR
);
387 _CrtSetReportMode(_CRT_ERROR
, _CRTDBG_MODE_DEBUG
|_CRTDBG_MODE_FILE
);
388 _CrtSetReportFile(_CRT_ERROR
, _CRTDBG_FILE_STDERR
);
389 _CrtSetReportMode(_CRT_ASSERT
, _CRTDBG_MODE_DEBUG
|_CRTDBG_MODE_FILE
);
390 _CrtSetReportFile(_CRT_ASSERT
, _CRTDBG_FILE_STDERR
);
394 std::vector
<CppUnit::Protector
*> protectors
;
395 CppUnit::TestResult result
;
398 sal_uInt32 index
= 0;
399 while (index
< osl_getCommandArgCount())
401 OUString arg
= getArgument(index
);
402 if (arg
.startsWith("-env:CPPUNITTESTTARGET=", &path
))
407 if (arg
.startsWith("-env:"))
410 continue; // ignore it here - will be read later
412 if ( arg
!= "--protector" )
416 testlib
= OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
422 args
+= OUStringToOString(arg
, osl_getThreadTextEncoding()).getStr();
427 if (osl_getCommandArgCount() - index
< 3) {
430 OUString
lib(getArgument(index
+ 1));
431 OUString
sym(getArgument(index
+ 2));
432 #ifndef DISABLE_DYNLOADING
433 osl::Module
mod(lib
, SAL_LOADMODULE_GLOBAL
);
434 oslGenericFunction fn
= mod
.getFunctionSymbol(sym
);
437 oslGenericFunction fn
= 0;
438 if (sym
== "unoexceptionprotector")
439 fn
= (oslGenericFunction
) unoexceptionprotector
;
440 else if (sym
== "unobootstrapprotector")
441 fn
= (oslGenericFunction
) unobootstrapprotector
;
442 else if (sym
== "vclbootstrapprotector")
443 fn
= (oslGenericFunction
) vclbootstrapprotector
;
447 << "Only unoexceptionprotector or unobootstrapprotector protectors allowed"
449 std::exit(EXIT_FAILURE
);
452 CppUnit::Protector
*protector
= fn
== nullptr
454 : (*reinterpret_cast< cppunittester::ProtectorFactory
* >(fn
))();
455 if (protector
== nullptr) {
457 << "Failure instantiating protector \"" << convertLazy(lib
)
458 << "\", \"" << convertLazy(sym
) << '"' << std::endl
;
459 std::exit(EXIT_FAILURE
);
461 protectors
.push_back(protector
);
465 ProtectedFixtureFunctor
tests(testlib
, args
, protectors
, result
);
468 catch (const std::exception
& e
)
470 SAL_WARN("vcl.app", "Fatal exception: " << e
.what());
473 reportResourceUsage(path
);
475 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;
478 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */