use insert function instead of for loop
[LibreOffice.git] / sal / qa / osl / module / osl_Module.cxx
blob3e9ad810717ff47617e97133fce2f8845145d587
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 // include files
22 #include "osl_Module_Const.h"
24 using namespace osl;
26 /** get dll file URL.
28 static OUString getDllURL()
30 #if defined(_WIN32) // lib in Unix and lib in Windows are not same in file name.
31 OUString libPath( "test_Module_DLL.dll" );
32 #else
33 OUString libPath( u"libtest_Module_DLL.so"_ustr );
34 #endif
36 OUString dirPath, dllPath;
37 osl::Module::getUrlFromAddress(
38 reinterpret_cast<oslGenericFunction>(&getDllURL), dirPath);
39 dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') + 1);
40 osl::FileBase::getAbsoluteFileURL( dirPath, libPath, dllPath );
42 return dllPath;
45 namespace osl_Module
47 namespace {
49 /** class and member function that is available for module test :
52 class testClass
54 public:
55 static void myFunc()
57 printf("#Sun Microsystem\n");
63 /** testing the methods:
64 Module();
65 Module( const OUString& strModuleName, sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT);
67 class ctors : public CppUnit::TestFixture
69 public:
70 bool bRes, bRes1;
72 void ctors_none( )
74 ::osl::Module aMod;
75 bRes = aMod.is();
77 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor without parameter.",
78 !bRes );
81 void ctors_name_mode( )
83 OUString aFileURL;
84 bRes = osl::Module::getUrlFromAddress(
85 reinterpret_cast<oslGenericFunction>(
86 &osl_Module::testClass::myFunc),
87 aFileURL);
89 if ( !bRes )
91 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
94 ::osl::Module aMod( aFileURL );
95 bRes = aMod.is( );
96 aMod.unload( );
98 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with load action.",
99 bRes );
102 CPPUNIT_TEST_SUITE( ctors );
103 CPPUNIT_TEST( ctors_none );
104 CPPUNIT_TEST( ctors_name_mode );
105 CPPUNIT_TEST_SUITE_END( );
106 }; // class ctors
108 /** testing the methods:
109 static sal_Bool getUrlFromAddress(void * addr, OUString & libraryUrl)
111 class getUrlFromAddress : public CppUnit::TestFixture
113 public:
114 bool bRes, bRes1;
116 void getUrlFromAddress_001( )
118 OUString aFileURL;
119 bRes = osl::Module::getUrlFromAddress(
120 reinterpret_cast<oslGenericFunction>(
121 &osl_Module::testClass::myFunc),
122 aFileURL);
123 if ( !bRes )
125 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
128 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test get Module URL from address.",
129 bRes );
130 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test get Module URL from address.",
131 0 < aFileURL.lastIndexOf('/') );
134 void getUrlFromAddress_002( )
136 #if !defined( MACOSX )
137 // TODO: Find out why this fails on macOS
138 ::osl::Module aMod( getDllURL( ) );
139 FuncPtr pFunc = reinterpret_cast<FuncPtr>(aMod.getSymbol( u"firstfunc"_ustr ));
141 OUString aFileURL;
142 bRes = osl::Module::getUrlFromAddress(
143 reinterpret_cast<oslGenericFunction>(pFunc), aFileURL);
144 if ( !bRes )
146 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
148 aMod.unload( );
150 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load an external library, get its function address and get its URL.",
151 bRes );
152 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load an external library, get its function address and get its URL.",
153 0 < aFileURL.lastIndexOf('/') );
154 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load an external library, get its function address and get its URL.",
155 aFileURL.equalsIgnoreAsciiCase( getDllURL( ) ) );
156 #endif
159 /* tester comments: another case is getFunctionSymbol_001*/
161 CPPUNIT_TEST_SUITE( getUrlFromAddress );
162 CPPUNIT_TEST( getUrlFromAddress_001 );
163 CPPUNIT_TEST( getUrlFromAddress_002 );
164 CPPUNIT_TEST_SUITE_END( );
165 }; // class getUrlFromAddress
167 /** testing the method:
168 sal_Bool SAL_CALL load( const OUString& strModuleName,
169 sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT)
171 class load : public CppUnit::TestFixture
173 public:
174 bool bRes, bRes1;
176 void load_001( )
178 ::osl::Module aMod( getDllURL( ) );
179 ::osl::Module aMod1;
181 aMod1.load( getDllURL( ) );
182 bRes = oslModule(aMod) == oslModule(aMod1);
183 aMod.unload( );
184 aMod1.unload( );
186 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load function should do the same thing as constructor with library name.",
187 bRes );
190 CPPUNIT_TEST_SUITE( load );
191 CPPUNIT_TEST( load_001 );
192 CPPUNIT_TEST_SUITE_END( );
193 }; // class load
195 /** testing the method:
196 void SAL_CALL unload()
198 class unload : public CppUnit::TestFixture
200 public:
201 bool bRes, bRes1;
203 void unload_001( )
205 ::osl::Module aMod( getDllURL( ) );
207 aMod.unload( );
208 bRes = oslModule(aMod) ==nullptr;
210 CPPUNIT_ASSERT_MESSAGE( "#test comment#: unload function should do the same thing as destructor.",
211 bRes );
214 CPPUNIT_TEST_SUITE( unload );
215 CPPUNIT_TEST( unload_001 );
216 CPPUNIT_TEST_SUITE_END( );
217 }; // class unload
219 /** testing the methods:
220 sal_Bool SAL_CALL is() const
222 class is : public CppUnit::TestFixture
224 public:
225 bool bRes, bRes1;
227 void is_001( )
229 OUString aFileURL;
230 bRes = osl::Module::getUrlFromAddress(
231 reinterpret_cast<oslGenericFunction>(
232 osl_Module::testClass::myFunc),
233 aFileURL);
234 if ( !bRes )
236 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module - using executable instead", false );
239 ::osl::Module aMod;
240 bRes = aMod.is( );
241 aMod.load( aFileURL );
242 bRes1 = aMod.is( );
243 aMod.unload( );
245 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test if a module is a loaded module.",
246 !bRes );
247 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test if a module is a loaded module.",
248 bRes1 );
250 CPPUNIT_TEST_SUITE( is );
251 CPPUNIT_TEST( is_001 );
252 CPPUNIT_TEST_SUITE_END( );
253 }; // class is
255 /** testing the methods:
256 void* SAL_CALL getSymbol( const OUString& strSymbolName)
258 class getSymbol : public CppUnit::TestFixture
260 public:
261 bool bRes;
263 void getSymbol_001( )
265 #if !defined( MACOSX )
266 // TODO: Find out why this fails on macOS
267 ::osl::Module aMod( getDllURL( ) );
268 FuncPtr pFunc = reinterpret_cast<FuncPtr>(aMod.getSymbol( u"firstfunc"_ustr ));
269 bRes = false;
270 if ( pFunc )
271 bRes = pFunc( bRes );
272 aMod.unload();
274 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and call one function in it.",
275 bRes );
276 #endif
279 CPPUNIT_TEST_SUITE( getSymbol );
280 CPPUNIT_TEST( getSymbol_001 );
281 CPPUNIT_TEST_SUITE_END( );
282 }; // class getSymbol
284 /** testing the methods:
285 operator oslModule() const
287 class optr_oslModule : public CppUnit::TestFixture
289 public:
290 bool bRes, bRes1;
292 void optr_oslModule_001( )
294 #if !defined( MACOSX )
295 // TODO: Find out why this fails on macOS
296 ::osl::Module aMod;
297 bRes = ( static_cast<oslModule>(aMod) == nullptr );
299 aMod.load( getDllURL( ) );
300 bRes1 = static_cast<oslModule>(aMod) != nullptr;
302 aMod.unload( );
304 CPPUNIT_ASSERT_MESSAGE( "#test comment#: the m_Module of a Module instance will be NULL when is not loaded, it will not be NULL after loaded.",
305 bRes );
306 CPPUNIT_ASSERT_MESSAGE( "#test comment#: the m_Module of a Module instance will be NULL when is not loaded, it will not be NULL after loaded.",
307 bRes1 );
308 #endif
311 void optr_oslModule_002( )
313 #if !defined( MACOSX )
314 // TODO: Find out why this fails on macOS
315 ::osl::Module aMod( getDllURL( ) );
316 OUString funcName( u"firstfunc"_ustr );
318 FuncPtr pFunc = reinterpret_cast<FuncPtr>(osl_getSymbol( static_cast<oslModule>(aMod), funcName.pData ));
319 bRes = false;
320 if ( pFunc )
321 bRes = pFunc( bRes );
323 aMod.unload();
325 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use m_Module to call osl_getSymbol() function.",
326 bRes );
327 #endif
330 CPPUNIT_TEST_SUITE( optr_oslModule );
331 CPPUNIT_TEST( optr_oslModule_001 );
332 CPPUNIT_TEST( optr_oslModule_002 );
333 CPPUNIT_TEST_SUITE_END( );
334 }; // class optr_oslModule
336 /** testing the methods:
337 oslGenericFunction SAL_CALL getFunctionSymbol( const OUString& ustrFunctionSymbolName )
339 class getFunctionSymbol : public CppUnit::TestFixture
341 public:
342 bool bRes, bRes1;
344 void getFunctionSymbol_001( )
346 #if !defined( MACOSX )
347 // TODO: Find out why this fails on macOS
348 ::osl::Module aMod( getDllURL( ) );
349 oslGenericFunction oslFunc = aMod.getFunctionSymbol( "firstfunc" );
350 OUString aLibraryURL;
351 bRes = ::osl::Module::getUrlFromAddress( oslFunc, aLibraryURL);
352 aMod.unload();
353 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and get its function addr and get its URL.",
354 bRes );
355 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and get its function addr and get its URL.",
356 aLibraryURL.equalsIgnoreAsciiCase( getDllURL() ) );
357 #endif
360 CPPUNIT_TEST_SUITE( getFunctionSymbol );
361 CPPUNIT_TEST( getFunctionSymbol_001 );
362 CPPUNIT_TEST_SUITE_END( );
363 }; // class getFunctionSymbol
365 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::ctors);
366 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getUrlFromAddress);
367 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::load);
368 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::unload);
369 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::is);
370 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getSymbol);
371 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::optr_oslModule);
372 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getFunctionSymbol);
374 } // namespace osl_Module
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */