LCOV - code coverage report
Current view: top level - libreoffice/sal/qa/osl/module - osl_Module.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 152 159 95.6 %
Date: 2012-12-27 Functions: 63 65 96.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : 
      21             : //------------------------------------------------------------------------
      22             : // include files
      23             : //------------------------------------------------------------------------
      24             : #include <osl_Module_Const.h>
      25             : 
      26             : using namespace osl;
      27             : 
      28             : using ::rtl::OUString;
      29             : using ::rtl::OUStringToOString;
      30             : using ::rtl::OString;
      31             : //------------------------------------------------------------------------
      32             : // helper functions and classes
      33             : //------------------------------------------------------------------------
      34             : 
      35             : /** print Boolean value.
      36             : */
      37             : inline void printBool( sal_Bool bOk )
      38             : {
      39             :     printf("#printBool# " );
      40             :     ( sal_True == bOk ) ? printf( "TRUE!\n" )
      41             :                         : printf( "FALSE!\n" );
      42             : }
      43             : 
      44             : /** print a UNI_CODE String.
      45             : */
      46             : inline void printUString( const ::rtl::OUString & str )
      47             : {
      48             :     rtl::OString aString;
      49             : 
      50             :     printf("#printUString_u# " );
      51             :     aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
      52             :     printf("%s\n", aString.getStr( ) );
      53             : }
      54             : 
      55             : /** get dll file URL.
      56             : */
      57          10 : inline ::rtl::OUString getDllURL( void )
      58             : {
      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          10 :     ::rtl::OUString libPath( "libtest_Module_DLL.so" );
      63             : #endif
      64             : 
      65          10 :     ::rtl::OUString dirPath, dllPath;
      66          10 :     osl::Module::getUrlFromAddress( ( void* ) &getDllURL, dirPath );
      67          10 :     dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') + 1);
      68          10 :     osl::FileBase::getAbsoluteFileURL( dirPath, libPath, dllPath );
      69             : 
      70          10 :     return dllPath;
      71             : }
      72             : 
      73             : inline sal_Bool isURL( const ::rtl::OUString pathname )
      74             : {
      75             :     ::rtl::OUString aPreURL( "file:///" );
      76             :     return ( ( pathname.indexOf( aPreURL ) == 0 ) ? sal_True : sal_False );
      77             : }
      78             : 
      79             : /** create a temp test directory using OUString name of full qualified URL or system path.
      80             : */
      81             : inline void createTestDirectory( const ::rtl::OUString dirname )
      82             : {
      83             :     ::rtl::OUString     aPathURL   = dirname.copy( 0 );
      84             :     ::osl::FileBase::RC nError;
      85             : 
      86             :     if ( !isURL( dirname ) )
      87             :         ::osl::FileBase::getFileURLFromSystemPath( dirname, aPathURL ); //convert if not full qualified URL
      88             :     nError = ::osl::Directory::create( aPathURL );
      89             :     CPPUNIT_ASSERT_MESSAGE( "In createTestDirectory Function: creation: ", ( ::osl::FileBase::E_None == nError ) || ( nError == ::osl::FileBase::E_EXIST ) );
      90             : }
      91             : 
      92             : /** delete a temp test directory using OUString name of full qualified URL or system path.
      93             : */
      94             : inline void deleteTestDirectory( const ::rtl::OUString dirname )
      95             : {
      96             :     ::rtl::OUString     aPathURL   = dirname.copy( 0 );
      97             :     ::osl::FileBase::RC nError;
      98             :     if ( !isURL( dirname ) )
      99             :         ::osl::FileBase::getFileURLFromSystemPath( dirname, aPathURL ); //convert if not full qualified URL
     100             : 
     101             :     ::osl::Directory testDir( aPathURL );
     102             :     if ( testDir.isOpen( ) == sal_True )
     103             :     {
     104             :             testDir.close( );  //close if still open.
     105             :         }
     106             : 
     107             :     nError = ::osl::Directory::remove( aPathURL );
     108             :      CPPUNIT_ASSERT_MESSAGE( "In deleteTestDirectory function: remove ", ( ::osl::FileBase::E_None == nError ) || ( nError == ::osl::FileBase::E_NOENT ) );
     109             : }
     110             : 
     111             : //check if the file exist
     112             : inline sal_Bool ifFileExist( const ::rtl::OUString & str )
     113             : {
     114             :     ::rtl::OUString     aUStr;
     115             :     if ( isURL( str ) )
     116             :         ::osl::FileBase::getSystemPathFromFileURL( str, aUStr );
     117             :     else
     118             :         return sal_False;
     119             : 
     120             :     ::osl::File strFile( aUStr );
     121             :     ::osl::FileBase::RC nError = strFile.open( osl_File_OpenFlag_Read );
     122             :     if ( ::File::E_NOENT == nError )
     123             :         return sal_False;
     124             :     else{
     125             :         strFile.close( );
     126             :         return sal_True;
     127             :     }
     128             : }
     129             : 
     130             : /** detete a temp test file using OUString name.
     131             : */
     132             : inline void deleteTestFile( const ::rtl::OUString filename )
     133             : {
     134             :     ::rtl::OUString aPathURL   = filename.copy( 0 );
     135             :     ::osl::FileBase::RC nError;
     136             : 
     137             :     if ( !isURL( filename ) )
     138             :         ::osl::FileBase::getFileURLFromSystemPath( filename, aPathURL ); //convert if not full qualified URL
     139             : 
     140             :     nError = ::osl::File::setAttributes( aPathURL, osl_File_Attribute_GrpWrite| osl_File_Attribute_OwnWrite| osl_File_Attribute_OthWrite ); // if readonly, make writtenable.
     141             :     CPPUNIT_ASSERT_MESSAGE( "In deleteTestFile Function: set writtenable ", ( ::osl::FileBase::E_None == nError ) || ( ::osl::FileBase::E_NOENT == nError ) );
     142             : 
     143             :     nError = ::osl::File::remove( aPathURL );
     144             :     CPPUNIT_ASSERT_MESSAGE( "In deleteTestFile Function: remove ", ( ::osl::FileBase::E_None == nError ) || ( nError == ::osl::FileBase::E_NOENT ) );
     145             : }
     146             : 
     147             : 
     148             : //------------------------------------------------------------------------
     149             : // test code start here
     150             : //------------------------------------------------------------------------
     151             : 
     152             : namespace osl_Module
     153             : {
     154             : 
     155             :     /** class and member function that is available for module test :
     156             :     */
     157             : 
     158             :     class testClass
     159             :     {
     160             :     public:
     161           0 :         static void myFunc()
     162             :         {
     163           0 :             printf("#Sun Microsystem\n");
     164           0 :         };
     165             :     };
     166             : 
     167             : 
     168             :     /** testing the methods:
     169             :         Module();
     170             :         Module( const ::rtl::OUString& strModuleName, sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT);
     171             :     */
     172           6 :     class ctors : public CppUnit::TestFixture
     173             :     {
     174             :     public:
     175             :         sal_Bool bRes, bRes1;
     176             : 
     177           1 :         void ctors_none( )
     178             :         {
     179           1 :             ::osl::Module aMod;
     180           1 :             bRes = aMod.is();
     181             : 
     182           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor without parameter.",
     183           2 :                                     sal_False == bRes  );
     184           1 :         }
     185             : 
     186           1 :         void ctors_name_mode( )
     187             :         {
     188           1 :             OUString aFileURL;
     189           1 :             bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL );
     190             : 
     191           1 :             if ( !( bRes ) )
     192             :             {
     193           0 :                 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.",  sal_False  );
     194             :             }
     195             : 
     196           1 :             ::osl::Module aMod( aFileURL );
     197           1 :             bRes = aMod.is( );
     198           1 :             aMod.unload( );
     199             : 
     200           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with load action.",
     201           2 :                                     sal_True == bRes  );
     202           1 :         }
     203             : 
     204           2 :         CPPUNIT_TEST_SUITE( ctors );
     205           1 :         CPPUNIT_TEST( ctors_none );
     206           1 :         CPPUNIT_TEST( ctors_name_mode );
     207           2 :         CPPUNIT_TEST_SUITE_END( );
     208             :     }; // class ctors
     209             : 
     210             : 
     211             :     /** testing the methods:
     212             :         static sal_Bool getUrlFromAddress(void * addr, ::rtl::OUString & libraryUrl)
     213             :     */
     214           6 :     class getUrlFromAddress : public CppUnit::TestFixture
     215             :     {
     216             :     public:
     217             :         sal_Bool bRes, bRes1;
     218             : 
     219           1 :         void getUrlFromAddress_001( )
     220             :         {
     221           1 :             OUString aFileURL;
     222           1 :             bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL ) ;
     223           1 :             if ( !( bRes ) )
     224             :             {
     225           0 :                 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.",  sal_False  );
     226             :             }
     227             : 
     228           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: test get Module URL from address.",
     229           2 :                                     sal_True == bRes && 0 < aFileURL.lastIndexOf('/')  );
     230           1 :         }
     231             : 
     232           1 :         void getUrlFromAddress_002( )
     233             :         {
     234             : #if !defined( MACOSX )
     235             :             // TODO: Find out why this fails on Mac OS X
     236           1 :             ::osl::Module aMod( getDllURL( ) );
     237           1 :             FuncPtr pFunc = ( FuncPtr ) aMod.getSymbol( rtl::OUString("firstfunc") );
     238             : 
     239           1 :             OUString aFileURL;
     240           1 :             bRes = osl::Module::getUrlFromAddress( ( void* )pFunc, aFileURL );
     241           1 :             if ( !( bRes  ) )
     242             :             {
     243           0 :                 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.",  sal_False  );
     244             :             }
     245           1 :             aMod.unload( );
     246             : 
     247           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: load an external library, get its function address and get its URL.",
     248           2 :                                     sal_True == bRes && 0 < aFileURL.lastIndexOf('/') && aFileURL.equalsIgnoreAsciiCase( getDllURL( ) ) );
     249             : #endif
     250           1 :         }
     251             : 
     252             :         /* tester comments: another case is getFunctionSymbol_001*/
     253             : 
     254           2 :         CPPUNIT_TEST_SUITE( getUrlFromAddress );
     255           1 :         CPPUNIT_TEST( getUrlFromAddress_001 );
     256           1 :         CPPUNIT_TEST( getUrlFromAddress_002 );
     257           2 :         CPPUNIT_TEST_SUITE_END( );
     258             :     }; // class getUrlFromAddress
     259             : 
     260             : 
     261             :     /** testing the method:
     262             :         sal_Bool SAL_CALL load( const ::rtl::OUString& strModuleName,
     263             :                                                  sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT)
     264             :     */
     265           3 :     class load : public CppUnit::TestFixture
     266             :     {
     267             :     public:
     268             :         sal_Bool bRes, bRes1;
     269             : 
     270           1 :         void load_001( )
     271             :         {
     272           1 :             ::osl::Module aMod( getDllURL( ) );
     273           1 :             ::osl::Module aMod1;
     274             : 
     275           1 :             aMod1.load( getDllURL( ) );
     276           1 :             bRes = oslModule(aMod) == oslModule(aMod1);
     277           1 :             aMod.unload( );
     278           1 :             aMod1.unload( );
     279             : 
     280           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: load function should do the same thing as constructor with library name.",
     281           2 :                                     sal_True == bRes  );
     282           1 :         }
     283             : 
     284           2 :         CPPUNIT_TEST_SUITE( load );
     285           1 :         CPPUNIT_TEST( load_001 );
     286           2 :         CPPUNIT_TEST_SUITE_END( );
     287             :     }; // class load
     288             : 
     289             : 
     290             :     /** testing the method:
     291             :         void SAL_CALL unload()
     292             :     */
     293           3 :     class unload : public CppUnit::TestFixture
     294             :     {
     295             :     public:
     296             :         sal_Bool bRes, bRes1;
     297             : 
     298           1 :         void unload_001( )
     299             :         {
     300           1 :             ::osl::Module aMod( getDllURL( ) );
     301             : 
     302           1 :             aMod.unload( );
     303           1 :             bRes = oslModule(aMod) ==NULL;
     304             : 
     305           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: unload function should do the same thing as destructor.",
     306           2 :                                     sal_True == bRes  );
     307           1 :         }
     308             : 
     309           2 :         CPPUNIT_TEST_SUITE( unload );
     310           1 :         CPPUNIT_TEST( unload_001 );
     311           2 :         CPPUNIT_TEST_SUITE_END( );
     312             :     }; // class unload
     313             : 
     314             : 
     315             :     /** testing the methods:
     316             :         sal_Bool SAL_CALL is() const
     317             :     */
     318           3 :     class is : public CppUnit::TestFixture
     319             :     {
     320             :     public:
     321             :         sal_Bool bRes, bRes1;
     322             : 
     323           1 :         void is_001( )
     324             :         {
     325           1 :             OUString aFileURL;
     326           1 :             bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL );
     327           1 :             if ( !( bRes  ) )
     328             :             {
     329           0 :                 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module - using executable instead",  sal_False  );
     330             :             }
     331             : 
     332           1 :             ::osl::Module aMod;
     333           1 :             bRes = aMod.is( );
     334           1 :             aMod.load( aFileURL );
     335           1 :             bRes1 = aMod.is( );
     336           1 :             aMod.unload( );
     337             : 
     338           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: test if a module is a loaded module.",
     339           2 :                                      sal_False == bRes && sal_True == bRes1);
     340           1 :         }
     341           2 :         CPPUNIT_TEST_SUITE( is );
     342           1 :         CPPUNIT_TEST( is_001 );
     343           2 :         CPPUNIT_TEST_SUITE_END( );
     344             :     }; // class is
     345             : 
     346             : 
     347             :     /** testing the methods:
     348             :         void* SAL_CALL getSymbol( const ::rtl::OUString& strSymbolName)
     349             :     */
     350           3 :     class getSymbol : public CppUnit::TestFixture
     351             :     {
     352             :     public:
     353             :         sal_Bool bRes;
     354             : 
     355           1 :         void getSymbol_001( )
     356             :         {
     357             : #if !defined( MACOSX )
     358             :             // TODO: Find out why this fails on Mac OS X
     359           1 :             ::osl::Module aMod( getDllURL( ) );
     360           1 :             FuncPtr pFunc = ( FuncPtr ) aMod.getSymbol( rtl::OUString("firstfunc") );
     361           1 :             bRes = sal_False;
     362           1 :             if ( pFunc )
     363           1 :                 bRes = pFunc( bRes );
     364           1 :             aMod.unload();
     365             : 
     366           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and call one function in it.",
     367           2 :                                      sal_True == bRes );
     368             : #endif
     369           1 :         }
     370             : 
     371           2 :         CPPUNIT_TEST_SUITE( getSymbol );
     372           1 :         CPPUNIT_TEST( getSymbol_001 );
     373           2 :         CPPUNIT_TEST_SUITE_END( );
     374             :     }; // class getSymbol
     375             : 
     376             : 
     377             :     /** testing the methods:
     378             :         operator oslModule() const
     379             :     */
     380           6 :     class optr_oslModule : public CppUnit::TestFixture
     381             :     {
     382             :     public:
     383             :         sal_Bool bRes, bRes1;
     384             : 
     385           1 :         void optr_oslModule_001( )
     386             :         {
     387             : #if !defined( MACOSX )
     388             :             // TODO: Find out why this fails on Mac OS X
     389           1 :             ::osl::Module aMod;
     390           1 :             bRes = ( (oslModule)aMod == NULL );
     391             : 
     392           1 :             aMod.load( getDllURL( ) );
     393           1 :             bRes1 = (oslModule)aMod != NULL;
     394             : 
     395           1 :             aMod.unload( );
     396             : 
     397           2 :             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.",
     398           2 :                                      sal_True == bRes && sal_True == bRes1);
     399             : #endif
     400           1 :         }
     401             : 
     402           1 :         void optr_oslModule_002( )
     403             :         {
     404             : #if !defined( MACOSX )
     405             :             // TODO: Find out why this fails on Mac OS X
     406           1 :             ::osl::Module aMod( getDllURL( ) );
     407           1 :             ::rtl::OUString funcName( "firstfunc" );
     408             : 
     409           1 :             FuncPtr pFunc = ( FuncPtr ) osl_getSymbol( (oslModule)aMod, funcName.pData );
     410           1 :             bRes = sal_False;
     411           1 :             if ( pFunc )
     412           1 :                 bRes = pFunc( bRes );
     413             : 
     414           1 :             aMod.unload();
     415             : 
     416           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: use m_Module to call osl_getSymbol() function.",
     417           2 :                                      sal_True == bRes  );
     418             : #endif
     419           1 :         }
     420             : 
     421           2 :         CPPUNIT_TEST_SUITE( optr_oslModule );
     422           1 :         CPPUNIT_TEST( optr_oslModule_001 );
     423           1 :         CPPUNIT_TEST( optr_oslModule_002 );
     424           2 :         CPPUNIT_TEST_SUITE_END( );
     425             :     }; // class optr_oslModule
     426             : 
     427             :     /** testing the methods:
     428             :         oslGenericFunction SAL_CALL getFunctionSymbol( const ::rtl::OUString& ustrFunctionSymbolName )
     429             :     */
     430           3 :     class getFunctionSymbol : public CppUnit::TestFixture
     431             :     {
     432             :     public:
     433             :         sal_Bool bRes, bRes1;
     434             : 
     435           1 :         void getFunctionSymbol_001( )
     436             :         {
     437             : #if !defined( MACOSX )
     438             :             // TODO: Find out why this fails on Mac OS X
     439           1 :             ::osl::Module aMod( getDllURL( ) );
     440           1 :             oslGenericFunction oslFunc = aMod.getFunctionSymbol( rtl::OUString("firstfunc") );
     441           1 :             ::rtl::OUString aLibraryURL;
     442           1 :             bRes = ::osl::Module::getUrlFromAddress( oslFunc, aLibraryURL);
     443           1 :             aMod.unload();
     444           2 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and get its function addr and get its URL.",
     445           2 :                  sal_True == bRes && aLibraryURL.equalsIgnoreAsciiCase( getDllURL() ) );
     446             : #endif
     447           1 :         }
     448             : 
     449           2 :         CPPUNIT_TEST_SUITE( getFunctionSymbol );
     450           1 :         CPPUNIT_TEST( getFunctionSymbol_001 );
     451           2 :         CPPUNIT_TEST_SUITE_END( );
     452             :     }; // class getFunctionSymbol
     453             : 
     454             : // -----------------------------------------------------------------------------
     455           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::ctors);
     456           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getUrlFromAddress);
     457           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::load);
     458           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::unload);
     459           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::is);
     460           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getSymbol);
     461           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::optr_oslModule);
     462           1 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getFunctionSymbol);
     463             : // -----------------------------------------------------------------------------
     464             : 
     465             : } // namespace osl_Module
     466             : 
     467             : // -----------------------------------------------------------------------------
     468             : 
     469             : // this macro creates an empty function, which will called by the RegisterAllFunctions()
     470             : // to let the user the possibility to also register some functions by hand.
     471           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     472             : 
     473             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10