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>
26 using ::rtl::OUString
;
27 using ::rtl::OUStringToOString
;
32 inline ::rtl::OUString
getDllURL()
34 #if ( defined WNT ) // lib in Unix and lib in Windows are not same in file name.
35 ::rtl::OUString
libPath( "test_Module_DLL.dll" );
37 ::rtl::OUString
libPath( "libtest_Module_DLL.so" );
40 ::rtl::OUString dirPath
, dllPath
;
41 osl::Module::getUrlFromAddress(
42 reinterpret_cast<oslGenericFunction
>(&getDllURL
), dirPath
);
43 dirPath
= dirPath
.copy( 0, dirPath
.lastIndexOf('/') + 1);
44 osl::FileBase::getAbsoluteFileURL( dirPath
, libPath
, dllPath
);
52 /** class and member function that is available for module test :
60 printf("#Sun Microsystem\n");
64 /** testing the methods:
66 Module( const ::rtl::OUString& strModuleName, sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT);
68 class ctors
: public CppUnit::TestFixture
78 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor without parameter.",
82 void ctors_name_mode( )
85 bRes
= osl::Module::getUrlFromAddress(
86 reinterpret_cast<oslGenericFunction
>(
87 &osl_Module::testClass::myFunc
),
92 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
95 ::osl::Module
aMod( aFileURL
);
99 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with load action.",
103 CPPUNIT_TEST_SUITE( ctors
);
104 CPPUNIT_TEST( ctors_none
);
105 CPPUNIT_TEST( ctors_name_mode
);
106 CPPUNIT_TEST_SUITE_END( );
109 /** testing the methods:
110 static sal_Bool getUrlFromAddress(void * addr, ::rtl::OUString & libraryUrl)
112 class getUrlFromAddress
: public CppUnit::TestFixture
117 void getUrlFromAddress_001( )
120 bRes
= osl::Module::getUrlFromAddress(
121 reinterpret_cast<oslGenericFunction
>(
122 &osl_Module::testClass::myFunc
),
126 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
129 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test get Module URL from address.",
130 bRes
&& 0 < aFileURL
.lastIndexOf('/') );
133 void getUrlFromAddress_002( )
135 #if !defined( MACOSX )
136 // TODO: Find out why this fails on Mac OS X
137 ::osl::Module
aMod( getDllURL( ) );
138 FuncPtr pFunc
= reinterpret_cast<FuncPtr
>(aMod
.getSymbol( rtl::OUString("firstfunc") ));
141 bRes
= osl::Module::getUrlFromAddress(
142 reinterpret_cast<oslGenericFunction
>(pFunc
), aFileURL
);
145 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
149 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load an external library, get its function address and get its URL.",
150 bRes
&& 0 < aFileURL
.lastIndexOf('/') && aFileURL
.equalsIgnoreAsciiCase( getDllURL( ) ) );
154 /* tester comments: another case is getFunctionSymbol_001*/
156 CPPUNIT_TEST_SUITE( getUrlFromAddress
);
157 CPPUNIT_TEST( getUrlFromAddress_001
);
158 CPPUNIT_TEST( getUrlFromAddress_002
);
159 CPPUNIT_TEST_SUITE_END( );
160 }; // class getUrlFromAddress
162 /** testing the method:
163 sal_Bool SAL_CALL load( const ::rtl::OUString& strModuleName,
164 sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT)
166 class load
: public CppUnit::TestFixture
173 ::osl::Module
aMod( getDllURL( ) );
176 aMod1
.load( getDllURL( ) );
177 bRes
= oslModule(aMod
) == oslModule(aMod1
);
181 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load function should do the same thing as constructor with library name.",
185 CPPUNIT_TEST_SUITE( load
);
186 CPPUNIT_TEST( load_001
);
187 CPPUNIT_TEST_SUITE_END( );
190 /** testing the method:
191 void SAL_CALL unload()
193 class unload
: public CppUnit::TestFixture
200 ::osl::Module
aMod( getDllURL( ) );
203 bRes
= oslModule(aMod
) ==NULL
;
205 CPPUNIT_ASSERT_MESSAGE( "#test comment#: unload function should do the same thing as destructor.",
209 CPPUNIT_TEST_SUITE( unload
);
210 CPPUNIT_TEST( unload_001
);
211 CPPUNIT_TEST_SUITE_END( );
214 /** testing the methods:
215 sal_Bool SAL_CALL is() const
217 class is
: public CppUnit::TestFixture
225 bRes
= osl::Module::getUrlFromAddress(
226 reinterpret_cast<oslGenericFunction
>(
227 osl_Module::testClass::myFunc
),
231 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module - using executable instead", false );
236 aMod
.load( aFileURL
);
240 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test if a module is a loaded module.",
243 CPPUNIT_TEST_SUITE( is
);
244 CPPUNIT_TEST( is_001
);
245 CPPUNIT_TEST_SUITE_END( );
248 /** testing the methods:
249 void* SAL_CALL getSymbol( const ::rtl::OUString& strSymbolName)
251 class getSymbol
: public CppUnit::TestFixture
256 void getSymbol_001( )
258 #if !defined( MACOSX )
259 // TODO: Find out why this fails on Mac OS X
260 ::osl::Module
aMod( getDllURL( ) );
261 FuncPtr pFunc
= reinterpret_cast<FuncPtr
>(aMod
.getSymbol( rtl::OUString("firstfunc") ));
264 bRes
= pFunc( bRes
);
267 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and call one function in it.",
272 CPPUNIT_TEST_SUITE( getSymbol
);
273 CPPUNIT_TEST( getSymbol_001
);
274 CPPUNIT_TEST_SUITE_END( );
275 }; // class getSymbol
277 /** testing the methods:
278 operator oslModule() const
280 class optr_oslModule
: public CppUnit::TestFixture
285 void optr_oslModule_001( )
287 #if !defined( MACOSX )
288 // TODO: Find out why this fails on Mac OS X
290 bRes
= ( (oslModule
)aMod
== NULL
);
292 aMod
.load( getDllURL( ) );
293 bRes1
= (oslModule
)aMod
!= NULL
;
297 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.",
302 void optr_oslModule_002( )
304 #if !defined( MACOSX )
305 // TODO: Find out why this fails on Mac OS X
306 ::osl::Module
aMod( getDllURL( ) );
307 ::rtl::OUString
funcName( "firstfunc" );
309 FuncPtr pFunc
= reinterpret_cast<FuncPtr
>(osl_getSymbol( (oslModule
)aMod
, funcName
.pData
));
312 bRes
= pFunc( bRes
);
316 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use m_Module to call osl_getSymbol() function.",
321 CPPUNIT_TEST_SUITE( optr_oslModule
);
322 CPPUNIT_TEST( optr_oslModule_001
);
323 CPPUNIT_TEST( optr_oslModule_002
);
324 CPPUNIT_TEST_SUITE_END( );
325 }; // class optr_oslModule
327 /** testing the methods:
328 oslGenericFunction SAL_CALL getFunctionSymbol( const ::rtl::OUString& ustrFunctionSymbolName )
330 class getFunctionSymbol
: public CppUnit::TestFixture
335 void getFunctionSymbol_001( )
337 #if !defined( MACOSX )
338 // TODO: Find out why this fails on Mac OS X
339 ::osl::Module
aMod( getDllURL( ) );
340 oslGenericFunction oslFunc
= aMod
.getFunctionSymbol( rtl::OUString("firstfunc") );
341 ::rtl::OUString aLibraryURL
;
342 bRes
= ::osl::Module::getUrlFromAddress( oslFunc
, aLibraryURL
);
344 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and get its function addr and get its URL.",
345 bRes
&& aLibraryURL
.equalsIgnoreAsciiCase( getDllURL() ) );
349 CPPUNIT_TEST_SUITE( getFunctionSymbol
);
350 CPPUNIT_TEST( getFunctionSymbol_001
);
351 CPPUNIT_TEST_SUITE_END( );
352 }; // class getFunctionSymbol
354 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::ctors
);
355 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getUrlFromAddress
);
356 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::load
);
357 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::unload
);
358 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::is
);
359 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getSymbol
);
360 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::optr_oslModule
);
361 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getFunctionSymbol
);
363 } // namespace osl_Module
365 // this macro creates an empty function, which will called by the RegisterAllFunctions()
366 // to let the user the possibility to also register some functions by hand.
367 CPPUNIT_PLUGIN_IMPLEMENT();
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */