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 //------------------------------------------------------------------------
23 //------------------------------------------------------------------------
24 #include <osl_Module_Const.h>
28 using ::rtl::OUString
;
29 using ::rtl::OUStringToOString
;
31 //------------------------------------------------------------------------
32 // helper functions and classes
33 //------------------------------------------------------------------------
35 /** print Boolean value.
37 inline void printBool( sal_Bool bOk
)
39 printf("#printBool# " );
40 ( sal_True
== bOk
) ? printf( "TRUE!\n" )
41 : printf( "FALSE!\n" );
44 /** print a UNI_CODE String.
46 inline void printUString( const ::rtl::OUString
& str
)
50 printf("#printUString_u# " );
51 aString
= ::rtl::OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
52 printf("%s\n", aString
.getStr( ) );
57 inline ::rtl::OUString
getDllURL( void )
59 #if ( defined WNT ) // lib in Unix and lib in Windows are not same in file name.
60 ::rtl::OUString
libPath( "test_Module_DLL.dll" );
62 ::rtl::OUString
libPath( "libtest_Module_DLL.so" );
65 ::rtl::OUString dirPath
, dllPath
;
66 osl::Module::getUrlFromAddress( ( void* ) &getDllURL
, dirPath
);
67 dirPath
= dirPath
.copy( 0, dirPath
.lastIndexOf('/') + 1);
68 osl::FileBase::getAbsoluteFileURL( dirPath
, libPath
, dllPath
);
73 inline bool isURL( const ::rtl::OUString pathname
)
75 return pathname
.startsWith( "file:///" );
78 /** create a temp test directory using OUString name of full qualified URL or system path.
80 inline void createTestDirectory( const ::rtl::OUString dirname
)
82 ::rtl::OUString aPathURL
= dirname
.copy( 0 );
83 ::osl::FileBase::RC nError
;
85 if ( !isURL( dirname
) )
86 ::osl::FileBase::getFileURLFromSystemPath( dirname
, aPathURL
); //convert if not full qualified URL
87 nError
= ::osl::Directory::create( aPathURL
);
88 CPPUNIT_ASSERT_MESSAGE( "In createTestDirectory Function: creation: ", ( ::osl::FileBase::E_None
== nError
) || ( nError
== ::osl::FileBase::E_EXIST
) );
91 /** delete a temp test directory using OUString name of full qualified URL or system path.
93 inline void deleteTestDirectory( const ::rtl::OUString dirname
)
95 ::rtl::OUString aPathURL
= dirname
.copy( 0 );
96 ::osl::FileBase::RC nError
;
97 if ( !isURL( dirname
) )
98 ::osl::FileBase::getFileURLFromSystemPath( dirname
, aPathURL
); //convert if not full qualified URL
100 ::osl::Directory
testDir( aPathURL
);
101 if ( testDir
.isOpen( ) == sal_True
)
103 testDir
.close( ); //close if still open.
106 nError
= ::osl::Directory::remove( aPathURL
);
107 CPPUNIT_ASSERT_MESSAGE( "In deleteTestDirectory function: remove ", ( ::osl::FileBase::E_None
== nError
) || ( nError
== ::osl::FileBase::E_NOENT
) );
110 //check if the file exist
111 inline sal_Bool
ifFileExist( const ::rtl::OUString
& str
)
113 ::rtl::OUString aUStr
;
115 ::osl::FileBase::getSystemPathFromFileURL( str
, aUStr
);
119 ::osl::File
strFile( aUStr
);
120 ::osl::FileBase::RC nError
= strFile
.open( osl_File_OpenFlag_Read
);
121 if ( ::File::E_NOENT
== nError
)
129 /** detete a temp test file using OUString name.
131 inline void deleteTestFile( const ::rtl::OUString filename
)
133 ::rtl::OUString aPathURL
= filename
.copy( 0 );
134 ::osl::FileBase::RC nError
;
136 if ( !isURL( filename
) )
137 ::osl::FileBase::getFileURLFromSystemPath( filename
, aPathURL
); //convert if not full qualified URL
139 nError
= ::osl::File::setAttributes( aPathURL
, osl_File_Attribute_GrpWrite
| osl_File_Attribute_OwnWrite
| osl_File_Attribute_OthWrite
); // if readonly, make writtenable.
140 CPPUNIT_ASSERT_MESSAGE( "In deleteTestFile Function: set writtenable ", ( ::osl::FileBase::E_None
== nError
) || ( ::osl::FileBase::E_NOENT
== nError
) );
142 nError
= ::osl::File::remove( aPathURL
);
143 CPPUNIT_ASSERT_MESSAGE( "In deleteTestFile Function: remove ", ( ::osl::FileBase::E_None
== nError
) || ( nError
== ::osl::FileBase::E_NOENT
) );
147 //------------------------------------------------------------------------
148 // test code start here
149 //------------------------------------------------------------------------
154 /** class and member function that is available for module test :
162 printf("#Sun Microsystem\n");
167 /** testing the methods:
169 Module( const ::rtl::OUString& strModuleName, sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT);
171 class ctors
: public CppUnit::TestFixture
174 sal_Bool bRes
, bRes1
;
181 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor without parameter.",
185 void ctors_name_mode( )
188 bRes
= osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc
, aFileURL
);
192 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", sal_False
);
195 ::osl::Module
aMod( aFileURL
);
199 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with load action.",
203 CPPUNIT_TEST_SUITE( ctors
);
204 CPPUNIT_TEST( ctors_none
);
205 CPPUNIT_TEST( ctors_name_mode
);
206 CPPUNIT_TEST_SUITE_END( );
210 /** testing the methods:
211 static sal_Bool getUrlFromAddress(void * addr, ::rtl::OUString & libraryUrl)
213 class getUrlFromAddress
: public CppUnit::TestFixture
216 sal_Bool bRes
, bRes1
;
218 void getUrlFromAddress_001( )
221 bRes
= osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc
, aFileURL
) ;
224 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", sal_False
);
227 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test get Module URL from address.",
228 sal_True
== bRes
&& 0 < aFileURL
.lastIndexOf('/') );
231 void getUrlFromAddress_002( )
233 #if !defined( MACOSX )
234 // TODO: Find out why this fails on Mac OS X
235 ::osl::Module
aMod( getDllURL( ) );
236 FuncPtr pFunc
= ( FuncPtr
) aMod
.getSymbol( rtl::OUString("firstfunc") );
239 bRes
= osl::Module::getUrlFromAddress( ( void* )pFunc
, aFileURL
);
242 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", sal_False
);
246 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load an external library, get its function address and get its URL.",
247 sal_True
== bRes
&& 0 < aFileURL
.lastIndexOf('/') && aFileURL
.equalsIgnoreAsciiCase( getDllURL( ) ) );
251 /* tester comments: another case is getFunctionSymbol_001*/
253 CPPUNIT_TEST_SUITE( getUrlFromAddress
);
254 CPPUNIT_TEST( getUrlFromAddress_001
);
255 CPPUNIT_TEST( getUrlFromAddress_002
);
256 CPPUNIT_TEST_SUITE_END( );
257 }; // class getUrlFromAddress
260 /** testing the method:
261 sal_Bool SAL_CALL load( const ::rtl::OUString& strModuleName,
262 sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT)
264 class load
: public CppUnit::TestFixture
267 sal_Bool bRes
, bRes1
;
271 ::osl::Module
aMod( getDllURL( ) );
274 aMod1
.load( getDllURL( ) );
275 bRes
= oslModule(aMod
) == oslModule(aMod1
);
279 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load function should do the same thing as constructor with library name.",
283 CPPUNIT_TEST_SUITE( load
);
284 CPPUNIT_TEST( load_001
);
285 CPPUNIT_TEST_SUITE_END( );
289 /** testing the method:
290 void SAL_CALL unload()
292 class unload
: public CppUnit::TestFixture
295 sal_Bool bRes
, bRes1
;
299 ::osl::Module
aMod( getDllURL( ) );
302 bRes
= oslModule(aMod
) ==NULL
;
304 CPPUNIT_ASSERT_MESSAGE( "#test comment#: unload function should do the same thing as destructor.",
308 CPPUNIT_TEST_SUITE( unload
);
309 CPPUNIT_TEST( unload_001
);
310 CPPUNIT_TEST_SUITE_END( );
314 /** testing the methods:
315 sal_Bool SAL_CALL is() const
317 class is
: public CppUnit::TestFixture
320 sal_Bool bRes
, bRes1
;
325 bRes
= osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc
, aFileURL
);
328 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module - using executable instead", sal_False
);
333 aMod
.load( aFileURL
);
337 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test if a module is a loaded module.",
338 sal_False
== bRes
&& sal_True
== bRes1
);
340 CPPUNIT_TEST_SUITE( is
);
341 CPPUNIT_TEST( is_001
);
342 CPPUNIT_TEST_SUITE_END( );
346 /** testing the methods:
347 void* SAL_CALL getSymbol( const ::rtl::OUString& strSymbolName)
349 class getSymbol
: public CppUnit::TestFixture
354 void getSymbol_001( )
356 #if !defined( MACOSX )
357 // TODO: Find out why this fails on Mac OS X
358 ::osl::Module
aMod( getDllURL( ) );
359 FuncPtr pFunc
= ( FuncPtr
) aMod
.getSymbol( rtl::OUString("firstfunc") );
362 bRes
= pFunc( bRes
);
365 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and call one function in it.",
370 CPPUNIT_TEST_SUITE( getSymbol
);
371 CPPUNIT_TEST( getSymbol_001
);
372 CPPUNIT_TEST_SUITE_END( );
373 }; // class getSymbol
376 /** testing the methods:
377 operator oslModule() const
379 class optr_oslModule
: public CppUnit::TestFixture
382 sal_Bool bRes
, bRes1
;
384 void optr_oslModule_001( )
386 #if !defined( MACOSX )
387 // TODO: Find out why this fails on Mac OS X
389 bRes
= ( (oslModule
)aMod
== NULL
);
391 aMod
.load( getDllURL( ) );
392 bRes1
= (oslModule
)aMod
!= NULL
;
396 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.",
397 sal_True
== bRes
&& sal_True
== bRes1
);
401 void optr_oslModule_002( )
403 #if !defined( MACOSX )
404 // TODO: Find out why this fails on Mac OS X
405 ::osl::Module
aMod( getDllURL( ) );
406 ::rtl::OUString
funcName( "firstfunc" );
408 FuncPtr pFunc
= ( FuncPtr
) osl_getSymbol( (oslModule
)aMod
, funcName
.pData
);
411 bRes
= pFunc( bRes
);
415 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use m_Module to call osl_getSymbol() function.",
420 CPPUNIT_TEST_SUITE( optr_oslModule
);
421 CPPUNIT_TEST( optr_oslModule_001
);
422 CPPUNIT_TEST( optr_oslModule_002
);
423 CPPUNIT_TEST_SUITE_END( );
424 }; // class optr_oslModule
426 /** testing the methods:
427 oslGenericFunction SAL_CALL getFunctionSymbol( const ::rtl::OUString& ustrFunctionSymbolName )
429 class getFunctionSymbol
: public CppUnit::TestFixture
432 sal_Bool bRes
, bRes1
;
434 void getFunctionSymbol_001( )
436 #if !defined( MACOSX )
437 // TODO: Find out why this fails on Mac OS X
438 ::osl::Module
aMod( getDllURL( ) );
439 oslGenericFunction oslFunc
= aMod
.getFunctionSymbol( rtl::OUString("firstfunc") );
440 ::rtl::OUString aLibraryURL
;
441 bRes
= ::osl::Module::getUrlFromAddress( oslFunc
, aLibraryURL
);
443 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and get its function addr and get its URL.",
444 sal_True
== bRes
&& aLibraryURL
.equalsIgnoreAsciiCase( getDllURL() ) );
448 CPPUNIT_TEST_SUITE( getFunctionSymbol
);
449 CPPUNIT_TEST( getFunctionSymbol_001
);
450 CPPUNIT_TEST_SUITE_END( );
451 }; // class getFunctionSymbol
453 // -----------------------------------------------------------------------------
454 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::ctors
);
455 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getUrlFromAddress
);
456 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::load
);
457 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::unload
);
458 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::is
);
459 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getSymbol
);
460 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::optr_oslModule
);
461 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getFunctionSymbol
);
462 // -----------------------------------------------------------------------------
464 } // namespace osl_Module
466 // -----------------------------------------------------------------------------
468 // this macro creates an empty function, which will called by the RegisterAllFunctions()
469 // to let the user the possibility to also register some functions by hand.
470 CPPUNIT_PLUGIN_IMPLEMENT();
472 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */