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 .
22 #include "osl_Module_Const.h"
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" );
33 OUString
libPath( u
"libtest_Module_DLL.so"_ustr
);
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
);
49 /** class and member function that is available for module test :
57 printf("#Sun Microsystem\n");
63 /** testing the methods:
65 Module( const OUString& strModuleName, sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT);
67 class ctors
: public CppUnit::TestFixture
77 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor without parameter.",
81 void ctors_name_mode( )
84 bRes
= osl::Module::getUrlFromAddress(
85 reinterpret_cast<oslGenericFunction
>(
86 &osl_Module::testClass::myFunc
),
91 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
94 ::osl::Module
aMod( aFileURL
);
98 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with load action.",
102 CPPUNIT_TEST_SUITE( ctors
);
103 CPPUNIT_TEST( ctors_none
);
104 CPPUNIT_TEST( ctors_name_mode
);
105 CPPUNIT_TEST_SUITE_END( );
108 /** testing the methods:
109 static sal_Bool getUrlFromAddress(void * addr, OUString & libraryUrl)
111 class getUrlFromAddress
: public CppUnit::TestFixture
116 void getUrlFromAddress_001( )
119 bRes
= osl::Module::getUrlFromAddress(
120 reinterpret_cast<oslGenericFunction
>(
121 &osl_Module::testClass::myFunc
),
125 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
128 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test get Module URL from address.",
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
));
142 bRes
= osl::Module::getUrlFromAddress(
143 reinterpret_cast<oslGenericFunction
>(pFunc
), aFileURL
);
146 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
150 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load an external library, get its function address and get its URL.",
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( ) ) );
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
178 ::osl::Module
aMod( getDllURL( ) );
181 aMod1
.load( getDllURL( ) );
182 bRes
= oslModule(aMod
) == oslModule(aMod1
);
186 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load function should do the same thing as constructor with library name.",
190 CPPUNIT_TEST_SUITE( load
);
191 CPPUNIT_TEST( load_001
);
192 CPPUNIT_TEST_SUITE_END( );
195 /** testing the method:
196 void SAL_CALL unload()
198 class unload
: public CppUnit::TestFixture
205 ::osl::Module
aMod( getDllURL( ) );
208 bRes
= oslModule(aMod
) ==nullptr;
210 CPPUNIT_ASSERT_MESSAGE( "#test comment#: unload function should do the same thing as destructor.",
214 CPPUNIT_TEST_SUITE( unload
);
215 CPPUNIT_TEST( unload_001
);
216 CPPUNIT_TEST_SUITE_END( );
219 /** testing the methods:
220 sal_Bool SAL_CALL is() const
222 class is
: public CppUnit::TestFixture
230 bRes
= osl::Module::getUrlFromAddress(
231 reinterpret_cast<oslGenericFunction
>(
232 osl_Module::testClass::myFunc
),
236 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module - using executable instead", false );
241 aMod
.load( aFileURL
);
245 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test if a module is a loaded module.",
247 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test if a module is a loaded module.",
250 CPPUNIT_TEST_SUITE( is
);
251 CPPUNIT_TEST( is_001
);
252 CPPUNIT_TEST_SUITE_END( );
255 /** testing the methods:
256 void* SAL_CALL getSymbol( const OUString& strSymbolName)
258 class getSymbol
: public CppUnit::TestFixture
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
));
271 bRes
= pFunc( bRes
);
274 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and call one function in it.",
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
292 void optr_oslModule_001( )
294 #if !defined( MACOSX )
295 // TODO: Find out why this fails on macOS
297 bRes
= ( static_cast<oslModule
>(aMod
) == nullptr );
299 aMod
.load( getDllURL( ) );
300 bRes1
= static_cast<oslModule
>(aMod
) != nullptr;
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.",
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.",
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
));
321 bRes
= pFunc( bRes
);
325 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use m_Module to call osl_getSymbol() function.",
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
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
);
353 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and get its function addr and get its URL.",
355 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and get its function addr and get its URL.",
356 aLibraryURL
.equalsIgnoreAsciiCase( getDllURL() ) );
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: */