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