Debian Codegraph @

sal/qa/osl/module/osl_Module.cxx

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