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-17 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          20 : 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          20 :     ::rtl::OUString libPath( "libtest_Module_DLL.so" );
      63             : #endif
      64             : 
      65          20 :     ::rtl::OUString dirPath, dllPath;
      66          20 :     osl::Module::getUrlFromAddress( ( void* ) &getDllURL, dirPath );
      67          20 :     dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') + 1);
      68          20 :     osl::FileBase::getAbsoluteFileURL( dirPath, libPath, dllPath );
      69             : 
      70          20 :     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          12 :     class ctors : public CppUnit::TestFixture
     173             :     {
     174             :     public:
     175             :         sal_Bool bRes, bRes1;
     176             : 
     177           2 :         void ctors_none( )
     178             :         {
     179           2 :             ::osl::Module aMod;
     180           2 :             bRes = aMod.is();
     181             : 
     182           4 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor without parameter.",
     183           4 :                                     sal_False == bRes  );
     184           2 :         }
     185             : 
     186           2 :         void ctors_name_mode( )
     187             :         {
     188           2 :             OUString aFileURL;
     189           2 :             bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL );
     190             : 
     191           2 :             if ( !( bRes ) )
     192             :             {
     193           0 :                 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.",  sal_False  );
     194             :             }
     195             : 
     196           2 :             ::osl::Module aMod( aFileURL );
     197           2 :             bRes = aMod.is( );
     198           2 :             aMod.unload( );
     199             : 
     200           4 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with load action.",
     201           4 :                                     sal_True == bRes  );
     202           2 :         }
     203             : 
     204           4 :         CPPUNIT_TEST_SUITE( ctors );
     205           2 :         CPPUNIT_TEST( ctors_none );
     206           2 :         CPPUNIT_TEST( ctors_name_mode );
     207           4 :         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          12 :     class getUrlFromAddress : public CppUnit::TestFixture
     215             :     {
     216             :     public:
     217             :         sal_Bool bRes, bRes1;
     218             : 
     219           2 :         void getUrlFromAddress_001( )
     220             :         {
     221           2 :             OUString aFileURL;
     222           2 :             bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL ) ;
     223           2 :             if ( !( bRes ) )
     224             :             {
     225           0 :                 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.",  sal_False  );
     226             :             }
     227             : 
     228           4 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: test get Module URL from address.",
     229           4 :                                     sal_True == bRes && 0 < aFileURL.lastIndexOf('/')  );
     230           2 :         }
     231             : 
     232           2 :         void getUrlFromAddress_002( )
     233             :         {
     234             : #if !defined( MACOSX )
     235             :             // TODO: Find out why this fails on Mac OS X
     236           2 :             ::osl::Module aMod( getDllURL( ) );
     237           2 :             FuncPtr pFunc = ( FuncPtr ) aMod.getSymbol( rtl::OUString("firstfunc") );
     238             : 
     239           2 :             OUString aFileURL;
     240           2 :             bRes = osl::Module::getUrlFromAddress( ( void* )pFunc, aFileURL );
     241           2 :             if ( !( bRes  ) )
     242             :             {
     243           0 :                 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.",  sal_False  );
     244             :             }
     245           2 :             aMod.unload( );
     246             : 
     247           4 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: load an external library, get its function address and get its URL.",
     248           4 :                                     sal_True == bRes && 0 < aFileURL.lastIndexOf('/') && aFileURL.equalsIgnoreAsciiCase( getDllURL( ) ) );
     249             : #endif
     250           2 :         }
     251             : 
     252             :         /* tester comments: another case is getFunctionSymbol_001*/
     253             : 
     254           4 :         CPPUNIT_TEST_SUITE( getUrlFromAddress );
     255           2 :         CPPUNIT_TEST( getUrlFromAddress_001 );
     256           2 :         CPPUNIT_TEST( getUrlFromAddress_002 );
     257           4 :         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           6 :     class load : public CppUnit::TestFixture
     266             :     {
     267             :     public:
     268             :         sal_Bool bRes, bRes1;
     269             : 
     270           2 :         void load_001( )
     271             :         {
     272           2 :             ::osl::Module aMod( getDllURL( ) );
     273           2 :             ::osl::Module aMod1;
     274             : 
     275           2 :             aMod1.load( getDllURL( ) );
     276           2 :             bRes = oslModule(aMod) == oslModule(aMod1);
     277           2 :             aMod.unload( );
     278           2 :             aMod1.unload( );
     279             : 
     280           4 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: load function should do the same thing as constructor with library name.",
     281           4 :                                     sal_True == bRes  );
     282           2 :         }
     283             : 
     284           4 :         CPPUNIT_TEST_SUITE( load );
     285           2 :         CPPUNIT_TEST( load_001 );
     286           4 :         CPPUNIT_TEST_SUITE_END( );
     287             :     }; // class load
     288             : 
     289             : 
     290             :     /** testing the method:
     291             :         void SAL_CALL unload()
     292             :     */
     293           6 :     class unload : public CppUnit::TestFixture
     294             :     {
     295             :     public:
     296             :         sal_Bool bRes, bRes1;
     297             : 
     298           2 :         void unload_001( )
     299             :         {
     300           2 :             ::osl::Module aMod( getDllURL( ) );
     301             : 
     302           2 :             aMod.unload( );
     303           2 :             bRes = oslModule(aMod) ==NULL;
     304             : 
     305           4 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: unload function should do the same thing as destructor.",
     306           4 :                                     sal_True == bRes  );
     307           2 :         }
     308             : 
     309           4 :         CPPUNIT_TEST_SUITE( unload );
     310           2 :         CPPUNIT_TEST( unload_001 );
     311           4 :         CPPUNIT_TEST_SUITE_END( );
     312             :     }; // class unload
     313             : 
     314             : 
     315             :     /** testing the methods:
     316             :         sal_Bool SAL_CALL is() const
     317             :     */
     318           6 :     class is : public CppUnit::TestFixture
     319             :     {
     320             :     public:
     321             :         sal_Bool bRes, bRes1;
     322             : 
     323           2 :         void is_001( )
     324             :         {
     325           2 :             OUString aFileURL;
     326           2 :             bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL );
     327           2 :             if ( !( bRes  ) )
     328             :             {
     329           0 :                 CPPUNIT_ASSERT_MESSAGE("Cannot locate current module - using executable instead",  sal_False  );
     330             :             }
     331             : 
     332           2 :             ::osl::Module aMod;
     333           2 :             bRes = aMod.is( );
     334           2 :             aMod.load( aFileURL );
     335           2 :             bRes1 = aMod.is( );
     336           2 :             aMod.unload( );
     337             : 
     338           4 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: test if a module is a loaded module.",
     339           4 :                                      sal_False == bRes && sal_True == bRes1);
     340           2 :         }
     341           4 :         CPPUNIT_TEST_SUITE( is );
     342           2 :         CPPUNIT_TEST( is_001 );
     343           4 :         CPPUNIT_TEST_SUITE_END( );
     344             :     }; // class is
     345             : 
     346             : 
     347             :     /** testing the methods:
     348             :         void* SAL_CALL getSymbol( const ::rtl::OUString& strSymbolName)
     349             :     */
     350           6 :     class getSymbol : public CppUnit::TestFixture
     351             :     {
     352             :     public:
     353             :         sal_Bool bRes;
     354             : 
     355           2 :         void getSymbol_001( )
     356             :         {
     357             : #if !defined( MACOSX )
     358             :             // TODO: Find out why this fails on Mac OS X
     359           2 :             ::osl::Module aMod( getDllURL( ) );
     360           2 :             FuncPtr pFunc = ( FuncPtr ) aMod.getSymbol( rtl::OUString("firstfunc") );
     361           2 :             bRes = sal_False;
     362           2 :             if ( pFunc )
     363           2 :                 bRes = pFunc( bRes );
     364           2 :             aMod.unload();
     365             : 
     366           4 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and call one function in it.",
     367           4 :                                      sal_True == bRes );
     368             : #endif
     369           2 :         }
     370             : 
     371           4 :         CPPUNIT_TEST_SUITE( getSymbol );
     372           2 :         CPPUNIT_TEST( getSymbol_001 );
     373           4 :         CPPUNIT_TEST_SUITE_END( );
     374             :     }; // class getSymbol
     375             : 
     376             : 
     377             :     /** testing the methods:
     378             :         operator oslModule() const
     379             :     */
     380          12 :     class optr_oslModule : public CppUnit::TestFixture
     381             :     {
     382             :     public:
     383             :         sal_Bool bRes, bRes1;
     384             : 
     385           2 :         void optr_oslModule_001( )
     386             :         {
     387             : #if !defined( MACOSX )
     388             :             // TODO: Find out why this fails on Mac OS X
     389           2 :             ::osl::Module aMod;
     390           2 :             bRes = ( (oslModule)aMod == NULL );
     391             : 
     392           2 :             aMod.load( getDllURL( ) );
     393           2 :             bRes1 = (oslModule)aMod != NULL;
     394             : 
     395           2 :             aMod.unload( );
     396             : 
     397           4 :             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           4 :                                      sal_True == bRes && sal_True == bRes1);
     399             : #endif
     400           2 :         }
     401             : 
     402           2 :         void optr_oslModule_002( )
     403             :         {
     404             : #if !defined( MACOSX )
     405             :             // TODO: Find out why this fails on Mac OS X
     406           2 :             ::osl::Module aMod( getDllURL( ) );
     407           2 :             ::rtl::OUString funcName( "firstfunc" );
     408             : 
     409           2 :             FuncPtr pFunc = ( FuncPtr ) osl_getSymbol( (oslModule)aMod, funcName.pData );
     410           2 :             bRes = sal_False;
     411           2 :             if ( pFunc )
     412           2 :                 bRes = pFunc( bRes );
     413             : 
     414           2 :             aMod.unload();
     415             : 
     416           4 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: use m_Module to call osl_getSymbol() function.",
     417           4 :                                      sal_True == bRes  );
     418             : #endif
     419           2 :         }
     420             : 
     421           4 :         CPPUNIT_TEST_SUITE( optr_oslModule );
     422           2 :         CPPUNIT_TEST( optr_oslModule_001 );
     423           2 :         CPPUNIT_TEST( optr_oslModule_002 );
     424           4 :         CPPUNIT_TEST_SUITE_END( );
     425             :     }; // class optr_oslModule
     426             : 
     427             :     /** testing the methods:
     428             :         oslGenericFunction SAL_CALL getFunctionSymbol( const ::rtl::OUString& ustrFunctionSymbolName )
     429             :     */
     430           6 :     class getFunctionSymbol : public CppUnit::TestFixture
     431             :     {
     432             :     public:
     433             :         sal_Bool bRes, bRes1;
     434             : 
     435           2 :         void getFunctionSymbol_001( )
     436             :         {
     437             : #if !defined( MACOSX )
     438             :             // TODO: Find out why this fails on Mac OS X
     439           2 :             ::osl::Module aMod( getDllURL( ) );
     440           2 :             oslGenericFunction oslFunc = aMod.getFunctionSymbol( rtl::OUString("firstfunc") );
     441           2 :             ::rtl::OUString aLibraryURL;
     442           2 :             bRes = ::osl::Module::getUrlFromAddress( oslFunc, aLibraryURL);
     443           2 :             aMod.unload();
     444           4 :             CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and get its function addr and get its URL.",
     445           4 :                  sal_True == bRes && aLibraryURL.equalsIgnoreAsciiCase( getDllURL() ) );
     446             : #endif
     447           2 :         }
     448             : 
     449           4 :         CPPUNIT_TEST_SUITE( getFunctionSymbol );
     450           2 :         CPPUNIT_TEST( getFunctionSymbol_001 );
     451           4 :         CPPUNIT_TEST_SUITE_END( );
     452             :     }; // class getFunctionSymbol
     453             : 
     454             : // -----------------------------------------------------------------------------
     455           2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::ctors);
     456           2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getUrlFromAddress);
     457           2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::load);
     458           2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::unload);
     459           2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::is);
     460           2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getSymbol);
     461           2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::optr_oslModule);
     462           2 : 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           8 : CPPUNIT_PLUGIN_IMPLEMENT();
     472             : 
     473             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10