1*87d2adbcSAndrew Rist /**************************************************************
2*87d2adbcSAndrew Rist *
3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*87d2adbcSAndrew Rist * distributed with this work for additional information
6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at
10*87d2adbcSAndrew Rist *
11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist *
13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the
17*87d2adbcSAndrew Rist * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist * under the License.
19*87d2adbcSAndrew Rist *
20*87d2adbcSAndrew Rist *************************************************************/
21*87d2adbcSAndrew Rist
22*87d2adbcSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "precompiled_sal.hxx"
25cdf0e10cSrcweir #include "sal/config.h"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <cstdlib>
28cdf0e10cSrcweir #include <iostream>
29cdf0e10cSrcweir #include <limits>
30cdf0e10cSrcweir #include <string>
31cdf0e10cSrcweir
32cdf0e10cSrcweir #include "cppunittester/protectorfactory.hxx"
33cdf0e10cSrcweir #include "osl/module.h"
34cdf0e10cSrcweir #include "osl/module.hxx"
35cdf0e10cSrcweir #include "osl/thread.h"
36cdf0e10cSrcweir #include "rtl/process.h"
37cdf0e10cSrcweir #include "rtl/string.h"
38cdf0e10cSrcweir #include "rtl/string.hxx"
39cdf0e10cSrcweir #include "rtl/textcvt.h"
40cdf0e10cSrcweir #include "rtl/ustring.hxx"
41cdf0e10cSrcweir #include "sal/main.h"
42cdf0e10cSrcweir #include "sal/types.h"
43cdf0e10cSrcweir
44cdf0e10cSrcweir #include "preextstl.h"
45cdf0e10cSrcweir #include "cppunit/CompilerOutputter.h"
46cdf0e10cSrcweir #include "cppunit/TestResult.h"
47cdf0e10cSrcweir #include "cppunit/TestResultCollector.h"
48cdf0e10cSrcweir #include "cppunit/TestRunner.h"
49cdf0e10cSrcweir #include "cppunit/extensions/TestFactoryRegistry.h"
50cdf0e10cSrcweir #include "cppunit/plugin/PlugInManager.h"
51cdf0e10cSrcweir #include "cppunit/portability/Stream.h"
52556e344bSHerbert Dürr #include "cppunit/plugin/DynamicLibraryManagerException.h"
53cdf0e10cSrcweir #include "postextstl.h"
54cdf0e10cSrcweir
55cdf0e10cSrcweir namespace {
56cdf0e10cSrcweir
usageFailure()57cdf0e10cSrcweir void usageFailure() {
58cdf0e10cSrcweir std::cerr
59cdf0e10cSrcweir << ("Usage: cppunittester (--protector <shared-library-path>"
60cdf0e10cSrcweir " <function-symbol>)* <shared-library-path>")
61cdf0e10cSrcweir << std::endl;
62cdf0e10cSrcweir std::exit(EXIT_FAILURE);
63cdf0e10cSrcweir }
64cdf0e10cSrcweir
getArgument(sal_Int32 index)65cdf0e10cSrcweir rtl::OUString getArgument(sal_Int32 index) {
66cdf0e10cSrcweir rtl::OUString arg;
67cdf0e10cSrcweir rtl_getAppCommandArg(index, &arg.pData);
68cdf0e10cSrcweir return arg;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir
convertLazy(rtl::OUString const & s16)71cdf0e10cSrcweir std::string convertLazy(rtl::OUString const & s16) {
72cdf0e10cSrcweir rtl::OString s8(rtl::OUStringToOString(s16, osl_getThreadTextEncoding()));
73cdf0e10cSrcweir return std::string(
74cdf0e10cSrcweir s8.getStr(),
75cdf0e10cSrcweir ((static_cast< sal_uInt32 >(s8.getLength())
76cdf0e10cSrcweir > std::numeric_limits< std::string::size_type >::max())
77cdf0e10cSrcweir ? std::numeric_limits< std::string::size_type >::max()
78cdf0e10cSrcweir : static_cast< std::string::size_type >(s8.getLength())));
79cdf0e10cSrcweir }
80cdf0e10cSrcweir
convertStrict(rtl::OUString const & s16)81cdf0e10cSrcweir std::string convertStrict(rtl::OUString const & s16) {
82cdf0e10cSrcweir rtl::OString s8;
83cdf0e10cSrcweir if (!s16.convertToString(
84cdf0e10cSrcweir &s8, osl_getThreadTextEncoding(),
85cdf0e10cSrcweir (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
86cdf0e10cSrcweir | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))
87cdf0e10cSrcweir || (static_cast< sal_uInt32 >(s8.getLength())
88cdf0e10cSrcweir > std::numeric_limits< std::string::size_type >::max()))
89cdf0e10cSrcweir {
90cdf0e10cSrcweir std::cerr
91cdf0e10cSrcweir << "Failure converting argument from UTF-16 back to system encoding"
92cdf0e10cSrcweir << std::endl;
93cdf0e10cSrcweir std::exit(EXIT_FAILURE);
94cdf0e10cSrcweir }
95cdf0e10cSrcweir return std::string(
96cdf0e10cSrcweir s8.getStr(), static_cast< std::string::size_type >(s8.getLength()));
97cdf0e10cSrcweir }
98cdf0e10cSrcweir
99cdf0e10cSrcweir }
100cdf0e10cSrcweir
SAL_IMPLEMENT_MAIN()101cdf0e10cSrcweir SAL_IMPLEMENT_MAIN() {
102cdf0e10cSrcweir CppUnit::TestResult result;
103cdf0e10cSrcweir sal_uInt32 index = 0;
104cdf0e10cSrcweir for (; index < rtl_getAppCommandArgCount(); index += 3) {
105cdf0e10cSrcweir if (!getArgument(index).equalsAsciiL(
106cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("--protector")))
107cdf0e10cSrcweir {
108cdf0e10cSrcweir break;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir if (rtl_getAppCommandArgCount() - index < 3) {
111cdf0e10cSrcweir usageFailure();
112cdf0e10cSrcweir }
113cdf0e10cSrcweir rtl::OUString lib(getArgument(index + 1));
114cdf0e10cSrcweir rtl::OUString sym(getArgument(index + 2));
115cdf0e10cSrcweir oslGenericFunction fn = (new osl::Module(lib, SAL_LOADMODULE_GLOBAL))
116cdf0e10cSrcweir ->getFunctionSymbol(sym);
117cdf0e10cSrcweir CppUnit::Protector * p = fn == 0
118cdf0e10cSrcweir ? 0
119cdf0e10cSrcweir : (*reinterpret_cast< cppunittester::ProtectorFactory * >(fn))();
120cdf0e10cSrcweir if (p == 0) {
121cdf0e10cSrcweir std::cerr
122cdf0e10cSrcweir << "Failure instantiating protector \"" << convertLazy(lib)
123cdf0e10cSrcweir << "\", \"" << convertLazy(sym) << '"' << std::endl;
124cdf0e10cSrcweir std::exit(EXIT_FAILURE);
125cdf0e10cSrcweir }
126cdf0e10cSrcweir result.pushProtector(p);
127cdf0e10cSrcweir }
128cdf0e10cSrcweir if (rtl_getAppCommandArgCount() - index != 1) {
129cdf0e10cSrcweir usageFailure();
130cdf0e10cSrcweir }
131556e344bSHerbert Dürr
132556e344bSHerbert Dürr bool bSuccess = false;
133556e344bSHerbert Dürr try {
134556e344bSHerbert Dürr CppUnit::PlugInManager manager;
135556e344bSHerbert Dürr manager.load(convertStrict(getArgument(index)));
136556e344bSHerbert Dürr CppUnit::TestRunner runner;
137556e344bSHerbert Dürr runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
138556e344bSHerbert Dürr CppUnit::TestResultCollector collector;
139556e344bSHerbert Dürr result.addListener(&collector);
140556e344bSHerbert Dürr runner.run(result);
141556e344bSHerbert Dürr CppUnit::CompilerOutputter(&collector, CppUnit::stdCErr()).write();
142556e344bSHerbert Dürr bSuccess = collector.wasSuccessful();
143556e344bSHerbert Dürr } catch( CppUnit::DynamicLibraryManagerException& e) {
144556e344bSHerbert Dürr std::cerr << "DynamicLibraryManagerException: \"" << e.what() << "\"\n";
145556e344bSHerbert Dürr }
146556e344bSHerbert Dürr
147556e344bSHerbert Dürr return bSuccess ? EXIT_SUCCESS : EXIT_FAILURE;
148cdf0e10cSrcweir }
149