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 : // include files
21 :
22 : #include <osl_Module_Const.h>
23 :
24 : using namespace osl;
25 :
26 : using ::rtl::OUString;
27 : using ::rtl::OUStringToOString;
28 : using ::rtl::OString;
29 :
30 : /** get dll file URL.
31 : */
32 20 : inline ::rtl::OUString getDllURL( void )
33 : {
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" );
36 : #else
37 20 : ::rtl::OUString libPath( "libtest_Module_DLL.so" );
38 : #endif
39 :
40 40 : ::rtl::OUString dirPath, dllPath;
41 20 : osl::Module::getUrlFromAddress( ( void* ) &getDllURL, dirPath );
42 20 : dirPath = dirPath.copy( 0, dirPath.lastIndexOf('/') + 1);
43 20 : osl::FileBase::getAbsoluteFileURL( dirPath, libPath, dllPath );
44 :
45 40 : return dllPath;
46 : }
47 :
48 : namespace osl_Module
49 : {
50 :
51 : /** class and member function that is available for module test :
52 : */
53 :
54 : class testClass
55 : {
56 : public:
57 0 : static void myFunc()
58 : {
59 0 : printf("#Sun Microsystem\n");
60 0 : };
61 : };
62 :
63 : /** testing the methods:
64 : Module();
65 : Module( const ::rtl::OUString& strModuleName, sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT);
66 : */
67 12 : class ctors : public CppUnit::TestFixture
68 : {
69 : public:
70 : bool bRes, bRes1;
71 :
72 2 : void ctors_none( )
73 : {
74 2 : ::osl::Module aMod;
75 2 : bRes = aMod.is();
76 :
77 4 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor without parameter.",
78 4 : !bRes );
79 2 : }
80 :
81 2 : void ctors_name_mode( )
82 : {
83 2 : OUString aFileURL;
84 2 : bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL );
85 :
86 2 : if ( !( bRes ) )
87 : {
88 0 : CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
89 : }
90 :
91 4 : ::osl::Module aMod( aFileURL );
92 2 : bRes = aMod.is( );
93 2 : aMod.unload( );
94 :
95 4 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: test constructor with load action.",
96 4 : bRes );
97 2 : }
98 :
99 4 : CPPUNIT_TEST_SUITE( ctors );
100 2 : CPPUNIT_TEST( ctors_none );
101 2 : CPPUNIT_TEST( ctors_name_mode );
102 4 : CPPUNIT_TEST_SUITE_END( );
103 : }; // class ctors
104 :
105 : /** testing the methods:
106 : static sal_Bool getUrlFromAddress(void * addr, ::rtl::OUString & libraryUrl)
107 : */
108 12 : class getUrlFromAddress : public CppUnit::TestFixture
109 : {
110 : public:
111 : bool bRes, bRes1;
112 :
113 2 : void getUrlFromAddress_001( )
114 : {
115 2 : OUString aFileURL;
116 2 : bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL ) ;
117 2 : if ( !( bRes ) )
118 : {
119 0 : CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
120 : }
121 :
122 4 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: test get Module URL from address.",
123 4 : bRes && 0 < aFileURL.lastIndexOf('/') );
124 2 : }
125 :
126 2 : void getUrlFromAddress_002( )
127 : {
128 : #if !defined( MACOSX )
129 : // TODO: Find out why this fails on Mac OS X
130 2 : ::osl::Module aMod( getDllURL( ) );
131 2 : FuncPtr pFunc = ( FuncPtr ) aMod.getSymbol( rtl::OUString("firstfunc") );
132 :
133 4 : OUString aFileURL;
134 2 : bRes = osl::Module::getUrlFromAddress( ( void* )pFunc, aFileURL );
135 2 : if ( !( bRes ) )
136 : {
137 0 : CPPUNIT_ASSERT_MESSAGE("Cannot locate current module.", false );
138 : }
139 2 : aMod.unload( );
140 :
141 4 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: load an external library, get its function address and get its URL.",
142 4 : bRes && 0 < aFileURL.lastIndexOf('/') && aFileURL.equalsIgnoreAsciiCase( getDllURL( ) ) );
143 : #endif
144 2 : }
145 :
146 : /* tester comments: another case is getFunctionSymbol_001*/
147 :
148 4 : CPPUNIT_TEST_SUITE( getUrlFromAddress );
149 2 : CPPUNIT_TEST( getUrlFromAddress_001 );
150 2 : CPPUNIT_TEST( getUrlFromAddress_002 );
151 4 : CPPUNIT_TEST_SUITE_END( );
152 : }; // class getUrlFromAddress
153 :
154 : /** testing the method:
155 : sal_Bool SAL_CALL load( const ::rtl::OUString& strModuleName,
156 : sal_Int32 nRtldMode = SAL_LOADMODULE_DEFAULT)
157 : */
158 6 : class load : public CppUnit::TestFixture
159 : {
160 : public:
161 : bool bRes, bRes1;
162 :
163 2 : void load_001( )
164 : {
165 2 : ::osl::Module aMod( getDllURL( ) );
166 4 : ::osl::Module aMod1;
167 :
168 2 : aMod1.load( getDllURL( ) );
169 2 : bRes = oslModule(aMod) == oslModule(aMod1);
170 2 : aMod.unload( );
171 2 : aMod1.unload( );
172 :
173 4 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: load function should do the same thing as constructor with library name.",
174 4 : bRes );
175 2 : }
176 :
177 4 : CPPUNIT_TEST_SUITE( load );
178 2 : CPPUNIT_TEST( load_001 );
179 4 : CPPUNIT_TEST_SUITE_END( );
180 : }; // class load
181 :
182 : /** testing the method:
183 : void SAL_CALL unload()
184 : */
185 6 : class unload : public CppUnit::TestFixture
186 : {
187 : public:
188 : bool bRes, bRes1;
189 :
190 2 : void unload_001( )
191 : {
192 2 : ::osl::Module aMod( getDllURL( ) );
193 :
194 2 : aMod.unload( );
195 2 : bRes = oslModule(aMod) ==NULL;
196 :
197 4 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: unload function should do the same thing as destructor.",
198 4 : bRes );
199 2 : }
200 :
201 4 : CPPUNIT_TEST_SUITE( unload );
202 2 : CPPUNIT_TEST( unload_001 );
203 4 : CPPUNIT_TEST_SUITE_END( );
204 : }; // class unload
205 :
206 : /** testing the methods:
207 : sal_Bool SAL_CALL is() const
208 : */
209 6 : class is : public CppUnit::TestFixture
210 : {
211 : public:
212 : bool bRes, bRes1;
213 :
214 2 : void is_001( )
215 : {
216 2 : OUString aFileURL;
217 2 : bRes = osl::Module::getUrlFromAddress( ( void* ) &::osl_Module::testClass::myFunc, aFileURL );
218 2 : if ( !( bRes ) )
219 : {
220 0 : CPPUNIT_ASSERT_MESSAGE("Cannot locate current module - using executable instead", false );
221 : }
222 :
223 4 : ::osl::Module aMod;
224 2 : bRes = aMod.is( );
225 2 : aMod.load( aFileURL );
226 2 : bRes1 = aMod.is( );
227 2 : aMod.unload( );
228 :
229 4 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: test if a module is a loaded module.",
230 4 : !bRes && bRes1 );
231 2 : }
232 4 : CPPUNIT_TEST_SUITE( is );
233 2 : CPPUNIT_TEST( is_001 );
234 4 : CPPUNIT_TEST_SUITE_END( );
235 : }; // class is
236 :
237 : /** testing the methods:
238 : void* SAL_CALL getSymbol( const ::rtl::OUString& strSymbolName)
239 : */
240 6 : class getSymbol : public CppUnit::TestFixture
241 : {
242 : public:
243 : bool bRes;
244 :
245 2 : void getSymbol_001( )
246 : {
247 : #if !defined( MACOSX )
248 : // TODO: Find out why this fails on Mac OS X
249 2 : ::osl::Module aMod( getDllURL( ) );
250 2 : FuncPtr pFunc = ( FuncPtr ) aMod.getSymbol( rtl::OUString("firstfunc") );
251 2 : bRes = false;
252 2 : if ( pFunc )
253 2 : bRes = pFunc( bRes );
254 2 : aMod.unload();
255 :
256 4 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and call one function in it.",
257 4 : bRes );
258 : #endif
259 2 : }
260 :
261 4 : CPPUNIT_TEST_SUITE( getSymbol );
262 2 : CPPUNIT_TEST( getSymbol_001 );
263 4 : CPPUNIT_TEST_SUITE_END( );
264 : }; // class getSymbol
265 :
266 : /** testing the methods:
267 : operator oslModule() const
268 : */
269 12 : class optr_oslModule : public CppUnit::TestFixture
270 : {
271 : public:
272 : bool bRes, bRes1;
273 :
274 2 : void optr_oslModule_001( )
275 : {
276 : #if !defined( MACOSX )
277 : // TODO: Find out why this fails on Mac OS X
278 2 : ::osl::Module aMod;
279 2 : bRes = ( (oslModule)aMod == NULL );
280 :
281 2 : aMod.load( getDllURL( ) );
282 2 : bRes1 = (oslModule)aMod != NULL;
283 :
284 2 : aMod.unload( );
285 :
286 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.",
287 4 : bRes && bRes1 );
288 : #endif
289 2 : }
290 :
291 2 : void optr_oslModule_002( )
292 : {
293 : #if !defined( MACOSX )
294 : // TODO: Find out why this fails on Mac OS X
295 2 : ::osl::Module aMod( getDllURL( ) );
296 4 : ::rtl::OUString funcName( "firstfunc" );
297 :
298 2 : FuncPtr pFunc = ( FuncPtr ) osl_getSymbol( (oslModule)aMod, funcName.pData );
299 2 : bRes = false;
300 2 : if ( pFunc )
301 2 : bRes = pFunc( bRes );
302 :
303 2 : aMod.unload();
304 :
305 4 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: use m_Module to call osl_getSymbol() function.",
306 4 : bRes );
307 : #endif
308 2 : }
309 :
310 4 : CPPUNIT_TEST_SUITE( optr_oslModule );
311 2 : CPPUNIT_TEST( optr_oslModule_001 );
312 2 : CPPUNIT_TEST( optr_oslModule_002 );
313 4 : CPPUNIT_TEST_SUITE_END( );
314 : }; // class optr_oslModule
315 :
316 : /** testing the methods:
317 : oslGenericFunction SAL_CALL getFunctionSymbol( const ::rtl::OUString& ustrFunctionSymbolName )
318 : */
319 6 : class getFunctionSymbol : public CppUnit::TestFixture
320 : {
321 : public:
322 : bool bRes, bRes1;
323 :
324 2 : void getFunctionSymbol_001( )
325 : {
326 : #if !defined( MACOSX )
327 : // TODO: Find out why this fails on Mac OS X
328 2 : ::osl::Module aMod( getDllURL( ) );
329 2 : oslGenericFunction oslFunc = aMod.getFunctionSymbol( rtl::OUString("firstfunc") );
330 4 : ::rtl::OUString aLibraryURL;
331 2 : bRes = ::osl::Module::getUrlFromAddress( oslFunc, aLibraryURL);
332 2 : aMod.unload();
333 4 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: load a dll and get its function addr and get its URL.",
334 4 : bRes && aLibraryURL.equalsIgnoreAsciiCase( getDllURL() ) );
335 : #endif
336 2 : }
337 :
338 4 : CPPUNIT_TEST_SUITE( getFunctionSymbol );
339 2 : CPPUNIT_TEST( getFunctionSymbol_001 );
340 4 : CPPUNIT_TEST_SUITE_END( );
341 : }; // class getFunctionSymbol
342 :
343 2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::ctors);
344 2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getUrlFromAddress);
345 2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::load);
346 2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::unload);
347 2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::is);
348 2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getSymbol);
349 2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::optr_oslModule);
350 2 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Module::getFunctionSymbol);
351 :
352 : } // namespace osl_Module
353 :
354 : // this macro creates an empty function, which will called by the RegisterAllFunctions()
355 : // to let the user the possibility to also register some functions by hand.
356 8 : CPPUNIT_PLUGIN_IMPLEMENT();
357 :
358 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|