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