Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / qa / osl / module / osl_Module.cxx
blobd6358d4c692ca523c12db4f90a1dcf88fc380155
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 .
21 //------------------------------------------------------------------------
22 // include files
23 //------------------------------------------------------------------------
24 #include <osl_Module_Const.h>
26 using namespace osl;
28 using ::rtl::OUString;
29 using ::rtl::OUStringToOString;
30 using ::rtl::OString;
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 )
48 rtl::OString aString;
50 printf("#printUString_u# " );
51 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
52 printf("%s\n", aString.getStr( ) );
55 /** get dll file URL.
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" );
61 #else
62 ::rtl::OUString libPath( "libtest_Module_DLL.so" );
63 #endif
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 );
70 return 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;
114 if ( isURL( str ) )
115 ::osl::FileBase::getSystemPathFromFileURL( str, aUStr );
116 else
117 return sal_False;
119 ::osl::File strFile( aUStr );
120 ::osl::FileBase::RC nError = strFile.open( osl_File_OpenFlag_Read );
121 if ( ::File::E_NOENT == nError )
122 return sal_False;
123 else{
124 strFile.close( );
125 return sal_True;
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 //------------------------------------------------------------------------
151 namespace osl_Module
154 /** class and member function that is available for module test :
157 class testClass
159 public:
160 static void myFunc()
162 printf("#Sun Microsystem\n");
167 /** testing the methods:
168 Module();
169 Module( const ::rtl::OUString& strModuleName, sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT);
171 class ctors : public CppUnit::TestFixture
173 public:
174 sal_Bool bRes, bRes1;
176 void ctors_none( )
178 ::osl::Module aMod;
179 bRes = aMod.is();
181 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor without parameter.",
182 sal_False == bRes );
185 void ctors_name_mode( )
187 OUString aFileURL;
188 bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL );
190 if ( !( bRes ) )
192 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", sal_False );
195 ::osl::Module aMod( aFileURL );
196 bRes = aMod.is( );
197 aMod.unload( );
199 CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with load action.",
200 sal_True == bRes );
203 CPPUNIT_TEST_SUITE( ctors );
204 CPPUNIT_TEST( ctors_none );
205 CPPUNIT_TEST( ctors_name_mode );
206 CPPUNIT_TEST_SUITE_END( );
207 }; // class ctors
210 /** testing the methods:
211 static sal_Bool getUrlFromAddress(void * addr, ::rtl::OUString & libraryUrl)
213 class getUrlFromAddress : public CppUnit::TestFixture
215 public:
216 sal_Bool bRes, bRes1;
218 void getUrlFromAddress_001( )
220 OUString aFileURL;
221 bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL ) ;
222 if ( !( bRes ) )
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") );
238 OUString aFileURL;
239 bRes = osl::Module::getUrlFromAddress( ( void* )pFunc, aFileURL );
240 if ( !( bRes ) )
242 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", sal_False );
244 aMod.unload( );
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( ) ) );
248 #endif
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
266 public:
267 sal_Bool bRes, bRes1;
269 void load_001( )
271 ::osl::Module aMod( getDllURL( ) );
272 ::osl::Module aMod1;
274 aMod1.load( getDllURL( ) );
275 bRes = oslModule(aMod) == oslModule(aMod1);
276 aMod.unload( );
277 aMod1.unload( );
279 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load function should do the same thing as constructor with library name.",
280 sal_True == bRes );
283 CPPUNIT_TEST_SUITE( load );
284 CPPUNIT_TEST( load_001 );
285 CPPUNIT_TEST_SUITE_END( );
286 }; // class load
289 /** testing the method:
290 void SAL_CALL unload()
292 class unload : public CppUnit::TestFixture
294 public:
295 sal_Bool bRes, bRes1;
297 void unload_001( )
299 ::osl::Module aMod( getDllURL( ) );
301 aMod.unload( );
302 bRes = oslModule(aMod) ==NULL;
304 CPPUNIT_ASSERT_MESSAGE( "#test comment#: unload function should do the same thing as destructor.",
305 sal_True == bRes );
308 CPPUNIT_TEST_SUITE( unload );
309 CPPUNIT_TEST( unload_001 );
310 CPPUNIT_TEST_SUITE_END( );
311 }; // class unload
314 /** testing the methods:
315 sal_Bool SAL_CALL is() const
317 class is : public CppUnit::TestFixture
319 public:
320 sal_Bool bRes, bRes1;
322 void is_001( )
324 OUString aFileURL;
325 bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL );
326 if ( !( bRes ) )
328 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module - using executable instead", sal_False );
331 ::osl::Module aMod;
332 bRes = aMod.is( );
333 aMod.load( aFileURL );
334 bRes1 = aMod.is( );
335 aMod.unload( );
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( );
343 }; // class is
346 /** testing the methods:
347 void* SAL_CALL getSymbol( const ::rtl::OUString& strSymbolName)
349 class getSymbol : public CppUnit::TestFixture
351 public:
352 sal_Bool bRes;
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") );
360 bRes = sal_False;
361 if ( pFunc )
362 bRes = pFunc( bRes );
363 aMod.unload();
365 CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and call one function in it.",
366 sal_True == bRes );
367 #endif
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
381 public:
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
388 ::osl::Module aMod;
389 bRes = ( (oslModule)aMod == NULL );
391 aMod.load( getDllURL( ) );
392 bRes1 = (oslModule)aMod != NULL;
394 aMod.unload( );
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);
398 #endif
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 );
409 bRes = sal_False;
410 if ( pFunc )
411 bRes = pFunc( bRes );
413 aMod.unload();
415 CPPUNIT_ASSERT_MESSAGE( "#test comment#: use m_Module to call osl_getSymbol() function.",
416 sal_True == bRes );
417 #endif
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
431 public:
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);
442 aMod.unload();
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() ) );
445 #endif
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: */