Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : //------------------------------------------------------------------------
30 : : // header file
31 : : //------------------------------------------------------------------------
32 : : #ifdef WNT
33 : : #include <windows.h>
34 : : #undef min
35 : : #endif
36 : : #include <osl_Security_Const.h>
37 : : #include <osl/thread.h>
38 : : #include <rtl/process.h>
39 : : #include <rtl/strbuf.hxx>
40 : :
41 : : using namespace osl;
42 : : using namespace rtl;
43 : :
44 : :
45 : : //------------------------------------------------------------------------
46 : : // helper functions and classes
47 : : //------------------------------------------------------------------------
48 : :
49 : : /** print Boolean value.
50 : : */
51 : : inline void printBool( sal_Bool bOk )
52 : : {
53 : : //t_print("#printBool# " );
54 : : ( sal_True == bOk ) ? t_print("TRUE!\n" ): t_print("FALSE!\n" );
55 : : }
56 : :
57 : : /** print a UNI_CODE String.
58 : : */
59 : 20 : inline void printUString( const ::rtl::OUString & str )
60 : : {
61 : 20 : rtl::OString aString;
62 : :
63 : : //t_print("#printUString_u# " );
64 [ + - ]: 20 : aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
65 [ + - ]: 20 : t_print("%s\n", aString.getStr( ) );
66 : 20 : }
67 : :
68 : :
69 : : //------------------------------------------------------------------------
70 : : // test code start here
71 : : //------------------------------------------------------------------------
72 : :
73 : : namespace osl_Security
74 : : {
75 : :
76 : : /** testing the method:
77 : : Security()
78 : : */
79 [ - + ]: 15 : class ctors : public CppUnit::TestFixture
80 : : {
81 : : public:
82 : : sal_Bool bRes, bRes1;
83 : :
84 : 5 : void ctors_001( )
85 : : {
86 [ + - ]: 5 : ::osl::Security aSec;
87 : :
88 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: create a security its handle should not be NULL.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
89 [ + - + - ]: 10 : aSec.getHandle( ) != NULL );
90 : 5 : }
91 : :
92 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( ctors );
[ + - ][ + - ]
[ # # ]
93 [ + - ][ + - ]: 5 : CPPUNIT_TEST( ctors_001 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
94 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
95 : : }; // class ctors
96 : :
97 : :
98 : : /** testing the methods:
99 : : inline sal_Bool SAL_CALL logonUser(const ::rtl::OUString& strName,
100 : : const ::rtl::OUString& strPasswd);
101 : : inline sal_Bool SAL_CALL logonUser(const ::rtl::OUString & strName,
102 : : const ::rtl::OUString & strPasswd,
103 : : const ::rtl::OUString & strFileServer);
104 : : */
105 [ # # ]: 0 : class logonUser : public CppUnit::TestFixture
106 : : {
107 : : public:
108 : : sal_Bool bRes;
109 : :
110 : 0 : void logonUser_user_pwd( )
111 : : {
112 [ # # ]: 0 : ::osl::Security aSec;
113 [ # # ]: 0 : bRes = aSec.logonUser( aLogonUser, aLogonPasswd );
114 : :
115 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: check logon user through forwarded user name, pwd, passed in (UNX), failed in (W32).",
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
116 [ # # # # ]: 0 : ( sal_True == bRes ) );
117 : 0 : }
118 : :
119 : 0 : void logonUser_user_pwd_server( )
120 : : {
121 [ # # ]: 0 : ::osl::Security aSec;
122 [ # # ]: 0 : bRes = aSec.logonUser( aLogonUser, aLogonPasswd, aFileServer );
123 : :
124 [ # # ][ # # ]: 0 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: check logon user through forwarded user name, pwd and server name, failed in (UNX)(W32).",
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
125 [ # # # # ]: 0 : ( sal_True == bRes ) );
126 : 0 : }
127 : :
128 : :
129 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( logonUser );
[ + - ][ + - ]
[ # # ]
130 [ - + ][ # # ]: 5 : if ( !aStringForward.isEmpty() && aStringForward.indexOf( (sal_Unicode)' ' ) != -1 && ( aStringForward.indexOf( ( sal_Unicode ) ' ' ) == aStringForward.lastIndexOf( ( sal_Unicode ) ' ' ) ) )
[ # # ][ - + ]
131 : : /// if user name and passwd are forwarded
132 : : {
133 [ # # ][ # # ]: 0 : CPPUNIT_TEST( logonUser_user_pwd );
[ # # ][ # # ]
[ # # ][ # # ]
134 : : }
135 [ - + ][ # # ]: 5 : if ( !aStringForward.isEmpty() && aStringForward.indexOf( (sal_Unicode)' ' ) != -1 && ( aStringForward.indexOf( ( sal_Unicode ) ' ' ) != aStringForward.lastIndexOf( ( sal_Unicode ) ' ' ) ) )
[ # # ][ - + ]
136 : : /// if user name and passwd and file server are forwarded
137 : : {
138 [ # # ][ # # ]: 0 : CPPUNIT_TEST( logonUser_user_pwd_server );
[ # # ][ # # ]
[ # # ][ # # ]
139 [ + - ]: 5 : }
140 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
141 : : }; // class logonUser
142 : :
143 : :
144 : : /** testing the method:
145 : : inline sal_Bool Security::getUserIdent( rtl::OUString& strIdent) const
146 : : */
147 [ - + ]: 15 : class getUserIdent : public CppUnit::TestFixture
148 : : {
149 : : public:
150 : : sal_Bool bRes, bRes1;
151 : :
152 : 5 : void getUserIdent_001( )
153 : : {
154 [ + - ]: 5 : ::osl::Security aSec;
155 : 5 : ::rtl::OUString strID;
156 [ + - ]: 5 : bRes = aSec.getUserIdent( strID );
157 : :
158 : 5 : rtl::OStringBuffer aMessage;
159 [ + - ]: 5 : aMessage.append("strUserID: ");
160 [ + - ][ + - ]: 5 : aMessage.append(rtl::OUStringToOString(strUserID, osl_getThreadTextEncoding()));
[ + - ]
161 [ + - ]: 5 : aMessage.append(", strID: ");
162 [ + - ][ + - ]: 5 : aMessage.append(rtl::OUStringToOString(strID, osl_getThreadTextEncoding()));
[ + - ]
163 [ + - ]: 5 : aMessage.append(", bRes: ");
164 [ + - ]: 5 : aMessage.append(bRes);
165 : :
166 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE( aMessage.getStr(), strUserID.equals(strID) && (bRes == sal_True));
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
[ + - ][ + - ]
[ + - ][ + - ]
167 : 5 : }
168 : :
169 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( getUserIdent );
[ + - ][ + - ]
[ # # ]
170 [ + - ][ + - ]: 5 : CPPUNIT_TEST( getUserIdent_001 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
171 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
172 : : }; // class getUserIdent
173 : :
174 : :
175 : : /** testing the method:
176 : : inline sal_Bool SAL_CALL getUserName( ::rtl::OUString& strName) const;
177 : : */
178 [ - + ]: 15 : class getUserName : public CppUnit::TestFixture
179 : : {
180 : : public:
181 : : sal_Bool bRes, bRes1;
182 : :
183 : 5 : void getUserName_001( )
184 : : {
185 [ + - ]: 5 : ::osl::Security aSec;
186 : : #ifdef WNT
187 : : ::rtl::OUString strName( strUserName ), strGetName;
188 : : #else
189 : 5 : ::rtl::OUString strName( strUserName ), strGetName;
190 : : #endif
191 [ + - ]: 5 : bRes = aSec.getUserName( strGetName );
192 : :
193 : 5 : sal_Int32 nPos = -1;
194 [ + - ]: 5 : if (!strName.isEmpty())
195 : : {
196 : 5 : nPos = strGetName.indexOf(strName);
197 : : }
198 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: get UserName and compare it with names got at the beginning of the test.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ - + ]
[ + - ][ + - ]
199 [ + - + - ]: 10 : ( nPos >= 0 ) && ( sal_True == bRes ) );
200 : 5 : }
201 : :
202 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( getUserName );
[ + - ][ + - ]
[ # # ]
203 [ + - ][ + - ]: 5 : CPPUNIT_TEST( getUserName_001 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
204 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
205 : : }; // class getUserName
206 : :
207 : :
208 : : /** testing the method:
209 : : inline sal_Bool Security::getConfigDir( rtl::OUString& strDirectory ) const
210 : : */
211 [ - + ]: 15 : class getConfigDir : public CppUnit::TestFixture
212 : : {
213 : : public:
214 : : sal_Bool bRes, bRes1;
215 : :
216 : 5 : void getConfigDir_001( )
217 : : {
218 [ + - ]: 5 : ::osl::Security aSec;
219 : 5 : ::rtl::OUString strConfig;
220 [ + - ]: 5 : bRes = aSec.getConfigDir( strConfig );
221 : :
222 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "failed to find a ConfigDir!",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
223 [ + - + - ]: 10 : ( sal_True == bRes ));
224 : 5 : }
225 : :
226 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( getConfigDir );
[ + - ][ + - ]
[ # # ]
227 [ + - ][ + - ]: 5 : CPPUNIT_TEST( getConfigDir_001 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
228 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
229 : : }; // class getConfigDir
230 : :
231 : : /** testing the method:
232 : : inline sal_Bool SAL_CALL isAdministrator() const;
233 : : */
234 [ - + ]: 15 : class isAdministrator : public CppUnit::TestFixture
235 : : {
236 : : public:
237 : : sal_Bool bRes;
238 : :
239 : 5 : void isAdministrator_001( )
240 : : {
241 [ + - ]: 5 : ::osl::Security aSec;
242 [ + - ]: 5 : bRes = aSec.isAdministrator( );
243 : :
244 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: check if the user is administrator at beginning, compare here.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
245 [ + - + - ]: 10 : bRes == isAdmin );
246 : 5 : }
247 : :
248 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( isAdministrator );
[ + - ][ + - ]
[ # # ]
249 [ + - ][ + - ]: 5 : CPPUNIT_TEST( isAdministrator_001 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
250 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
251 : : }; // class isAdministrator
252 : :
253 : : /** testing the method:
254 : : inline oslSecurity getHandle() const;
255 : : */
256 [ - + ]: 15 : class getHandle : public CppUnit::TestFixture
257 : : {
258 : : public:
259 : : sal_Bool bRes;
260 : :
261 : 5 : void getHandle_001( )
262 : : {
263 [ + - ]: 5 : ::osl::Security aSec;
264 [ + - ][ + - ]: 5 : bRes = aSec.isAdministrator( ) == osl_isAdministrator( aSec.getHandle( ) );
265 : :
266 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#test comment#: use getHandle function to call C API.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
267 [ + - + - ]: 10 : bRes == sal_True );
268 : 5 : }
269 : :
270 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( getHandle );
[ + - ][ + - ]
[ # # ]
271 [ + - ][ + - ]: 5 : CPPUNIT_TEST( getHandle_001 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
272 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
273 : : }; // class getHandle
274 : :
275 : :
276 [ - + ]: 30 : class UserProfile : public CppUnit::TestFixture
277 : : {
278 : : public:
279 : :
280 : 5 : void loadUserProfile( )
281 : : {
282 [ + - ]: 5 : ::osl::Security aSec;
283 [ + - ]: 5 : sal_Bool bValue = osl_loadUserProfile(aSec.getHandle());
284 : :
285 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE( "empty function.", bValue == sal_False );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
286 : 5 : }
287 : :
288 : 5 : void unloadUserProfile( )
289 : : {
290 [ + - ]: 5 : ::osl::Security aSec;
291 [ + - ]: 5 : osl_unloadUserProfile(aSec.getHandle());
292 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE( "empty function.", sal_True );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
293 : 5 : }
294 : :
295 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( UserProfile );
[ + - ][ + - ]
[ # # ]
296 [ + - ][ + - ]: 5 : CPPUNIT_TEST( loadUserProfile );
[ + - ][ + - ]
[ + - ][ + - ]
297 [ + - ][ + - ]: 5 : CPPUNIT_TEST( unloadUserProfile );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
298 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
299 : : }; // class UserProfile
300 : :
301 [ - + ]: 15 : class loginUserOnFileServer : public CppUnit::TestFixture
302 : : {
303 : : public:
304 : :
305 : 5 : void loginUserOnFileServer_001( )
306 : : {
307 : 5 : rtl::OUString suUserName;
308 : 5 : rtl::OUString suPassword;
309 : 5 : rtl::OUString suFileServer;
310 [ + - ]: 5 : ::osl::Security aSec;
311 : 5 : oslSecurity pSec = aSec.getHandle();
312 : :
313 [ + - ]: 5 : oslSecurityError erg = osl_loginUserOnFileServer(suUserName.pData, suPassword.pData, suFileServer.pData, &pSec);
314 : :
315 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE( "empty function.", erg == osl_Security_E_UserUnknown );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
316 : 5 : }
317 : :
318 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE( loginUserOnFileServer );
[ + - ][ + - ]
[ # # ]
319 [ + - ][ + - ]: 5 : CPPUNIT_TEST( loginUserOnFileServer_001 );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
320 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END( );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
321 : : }; // class loginUserOnFileServer
322 : :
323 : : // -----------------------------------------------------------------------------
324 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Security::ctors);
325 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Security::logonUser);
326 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Security::getUserIdent);
327 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Security::getUserName);
328 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Security::getConfigDir);
329 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Security::isAdministrator);
330 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Security::getHandle);
331 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Security::UserProfile);
332 : 5 : CPPUNIT_TEST_SUITE_REGISTRATION(osl_Security::loginUserOnFileServer);
333 : :
334 : : // -----------------------------------------------------------------------------
335 : :
336 : : } // namespace osl_Security
337 : :
338 : :
339 : : /* This defines an own TestPlugIn implementation with an own initialize()
340 : : method that will be called after loading the PlugIn
341 : : */
342 : : #include <cppunit/plugin/TestPlugInDefaultImpl.h>
343 : :
344 [ - + ]: 5 : class MyTestPlugInImpl: public CPPUNIT_NS::TestPlugInDefaultImpl
345 : : {
346 : : public:
347 : 5 : MyTestPlugInImpl() {};
348 : : void initialize( CPPUNIT_NS::TestFactoryRegistry *registry,
349 : : const CPPUNIT_NS::PlugInParameters ¶meters );
350 : : };
351 : :
352 : :
353 : 5 : void MyTestPlugInImpl::initialize( CPPUNIT_NS::TestFactoryRegistry *,
354 : : const CPPUNIT_NS::PlugInParameters & )
355 : : {
356 : : /// start message
357 [ + - ]: 5 : t_print("#Initializing ...\n" );
358 [ + - ]: 5 : t_print("#\n#logonUser function need root/Administrator account to test.\n" );
359 [ + - ]: 5 : t_print("#You can test by login with root/Administrator, and excute:\n" );
360 [ + - ]: 5 : t_print("#testshl2 -forward \"username password\" ../../../wntmsci9/bin/Security.dll\n" );
361 [ + - ]: 5 : t_print("# where username and password are forwarded account info.\n" );
362 [ + - ]: 5 : t_print("#if no text forwarded, this function will be skipped.\n" );
363 : :
364 : : /// get system information
365 : : #if ( defined UNX )
366 : : /// some initialization work for UNIX OS
367 : :
368 : :
369 : : struct passwd* pw;
370 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_MESSAGE( "getpwuid: no password entry\n",( pw = getpwuid( getuid() ) ) != NULL );
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
371 : :
372 : : /// get user ID;
373 : 5 : strUserID = ::rtl::OUString::valueOf( ( sal_Int32 )getuid( ) );
374 : :
375 : : /// get user Name;
376 : 5 : strUserName = ::rtl::OUString::createFromAscii( pw->pw_name );
377 : :
378 : : /// get home directory;
379 : 5 : char *pw_dir = pw->pw_dir;
380 [ - + ]: 5 : if( getenv( "FAKEROOTKEY" ) )
381 : 0 : pw_dir = getenv("HOME");
382 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_MESSAGE( "#Convert from system path to URL failed.",
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
383 [ + - ]: 5 : ::osl::File::E_None == ::osl::File::getFileURLFromSystemPath( ::rtl::OUString::createFromAscii( pw_dir ), strHomeDirectory ) );
384 : :
385 : : /// get config directory;
386 : 5 : strConfigDirectory = strHomeDirectory.copy(0);
387 : :
388 : : /// is administrator;
389 [ + - ]: 5 : if( !getuid( ) )
390 : 5 : isAdmin = sal_True;
391 : :
392 : : #endif
393 : : #if defined ( WNT )
394 : : /// some initialization work for Windows OS
395 : :
396 : :
397 : : /// Get the user name, computer name, user home directory.
398 : : LPTSTR lpszSystemInfo; // pointer to system information string
399 : : DWORD cchBuff = BUFSIZE; // size of computer or user name
400 : : TCHAR tchBuffer[BUFSIZE]; // buffer for string
401 : :
402 : : lpszSystemInfo = tchBuffer;
403 : : cchBuff = BUFSIZE;
404 : : if( GetUserNameA(lpszSystemInfo, &cchBuff) )
405 : : strUserName = ::rtl::OUString::createFromAscii( lpszSystemInfo );
406 : :
407 : : if( GetComputerName(lpszSystemInfo, &cchBuff) )
408 : : strComputerName = ::rtl::OUString::createFromAscii( lpszSystemInfo );
409 : :
410 : : /// Get user home directory.
411 : : HKEY hRegKey;
412 : : sal_Char PathA[_MAX_PATH];
413 : : ::rtl::OUString strHome;
414 : : if (RegOpenKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", &hRegKey) == ERROR_SUCCESS)
415 : : {
416 : : LONG lRet, lSize = sizeof(PathA);
417 : : DWORD Type;
418 : :
419 : : lRet = RegQueryValueEx(hRegKey, "AppData", NULL, &Type, ( unsigned char * )PathA, ( unsigned long * )&lSize);
420 : : if ( ( lRet == ERROR_SUCCESS ) && ( Type == REG_SZ ) && ( _access( PathA, 0 ) == 0 ) )
421 : : {
422 : : CPPUNIT_ASSERT_MESSAGE( "#Convert from system path to URL failed.",
423 : : ::osl::File::E_None == ::osl::File::getFileURLFromSystemPath( ::rtl::OUString::createFromAscii( PathA ), strConfigDirectory ) );
424 : : }
425 : :
426 : : lRet = RegQueryValueEx(hRegKey, "Personal", NULL, &Type, ( unsigned char * )PathA, ( unsigned long * )&lSize);
427 : : if ( ( lRet == ERROR_SUCCESS ) && ( Type == REG_SZ ) && ( _access( PathA, 0 ) == 0 ) )
428 : : {
429 : : CPPUNIT_ASSERT_MESSAGE( "#Convert from system path to URL failed.",
430 : : ::osl::File::E_None == ::osl::File::getFileURLFromSystemPath( ::rtl::OUString::createFromAscii( PathA ), strHomeDirectory ) );
431 : : }
432 : :
433 : : RegCloseKey(hRegKey);
434 : : }
435 : :
436 : :
437 : : /// Get user Security ID:
438 : :
439 : : // Create buffers that may be large enough. If a buffer is too small, the count parameter will be set to the size needed.
440 : : const DWORD INITIAL_SIZE = 32;
441 : : DWORD cbSid = 0;
442 : : DWORD dwSidBufferSize = INITIAL_SIZE;
443 : : DWORD cchDomainName = 0;
444 : : DWORD dwDomainBufferSize = INITIAL_SIZE;
445 : : WCHAR * wszDomainName = NULL;
446 : : SID_NAME_USE eSidType;
447 : : DWORD dwErrorCode = 0;
448 : :
449 : : LPCWSTR wszAccName = ( LPWSTR ) strUserName.getStr( );
450 : :
451 : : // Create buffers for the SID and the domain name.
452 : : PSID pSid = (PSID) new BYTE[dwSidBufferSize];
453 : : CPPUNIT_ASSERT_MESSAGE("# creating SID buffer failed.\n", pSid!= NULL );
454 : : memset( pSid, 0, dwSidBufferSize);
455 : :
456 : : wszDomainName = new WCHAR[dwDomainBufferSize];
457 : : CPPUNIT_ASSERT_MESSAGE("# creating Domain name buffer failed.\n", wszDomainName != NULL );
458 : : memset(wszDomainName, 0, dwDomainBufferSize*sizeof(WCHAR));
459 : :
460 : : // Obtain the SID for the account name passed.
461 : : for ( ; ; )
462 : : {
463 : : // Set the count variables to the buffer sizes and retrieve the SID.
464 : : cbSid = dwSidBufferSize;
465 : : cchDomainName = dwDomainBufferSize;
466 : : if (LookupAccountNameW(
467 : : NULL, // Computer name. NULL for the local computer
468 : : wszAccName,
469 : : pSid, // Pointer to the SID buffer. Use NULL to get the size needed,
470 : : &cbSid, // Size of the SID buffer needed.
471 : : wszDomainName, // wszDomainName,
472 : : &cchDomainName,
473 : : &eSidType
474 : : ))
475 : : {
476 : : if (IsValidSid( pSid) == FALSE)
477 : : wprintf(L"# The SID for %s is invalid.\n", wszAccName);
478 : : break;
479 : : }
480 : : dwErrorCode = GetLastError();
481 : :
482 : : // Check if one of the buffers was too small.
483 : : if (dwErrorCode == ERROR_INSUFFICIENT_BUFFER)
484 : : {
485 : : if (cbSid > dwSidBufferSize)
486 : : {
487 : : // Reallocate memory for the SID buffer.
488 : : wprintf(L"# The SID buffer was too small. It will be reallocated.\n");
489 : : FreeSid( pSid);
490 : : pSid = (PSID) new BYTE[cbSid];
491 : : CPPUNIT_ASSERT_MESSAGE("# re-creating SID buffer failed.\n", pSid!= NULL );
492 : : memset( pSid, 0, cbSid);
493 : : dwSidBufferSize = cbSid;
494 : : }
495 : : if (cchDomainName > dwDomainBufferSize)
496 : : {
497 : : // Reallocate memory for the domain name buffer.
498 : : wprintf(L"# The domain name buffer was too small. It will be reallocated.\n");
499 : : delete [] wszDomainName;
500 : : wszDomainName = new WCHAR[cchDomainName];
501 : : CPPUNIT_ASSERT_MESSAGE("# re-creating domain name buffer failed.\n", wszDomainName!= NULL );
502 : : memset(wszDomainName, 0, cchDomainName*sizeof(WCHAR));
503 : : dwDomainBufferSize = cchDomainName;
504 : : }
505 : : }
506 : : else
507 : : {
508 : : wprintf(L"# LookupAccountNameW failed. GetLastError returned: %d\n", dwErrorCode);
509 : : break;
510 : : }
511 : : }
512 : :
513 : : // now got SID successfully, only need to compare SID, so I copied the rest lines from source to convert SID to OUString.
514 : : PSID_IDENTIFIER_AUTHORITY psia;
515 : : DWORD dwSubAuthorities;
516 : : DWORD dwSidRev=SID_REVISION;
517 : : DWORD dwCounter;
518 : : DWORD dwSidSize;
519 : : sal_Char *Ident;
520 : :
521 : : /* obtain SidIdentifierAuthority */
522 : : psia=GetSidIdentifierAuthority(pSid);
523 : :
524 : : /* obtain sidsubauthority count */
525 : : dwSubAuthorities=std::min((int) *GetSidSubAuthorityCount(pSid), 5);
526 : :
527 : : /* buffer length: S-SID_REVISION- + identifierauthority- + subauthorities- + NULL */
528 : : Ident=(sal_Char * )malloc(88*sizeof(sal_Char));
529 : :
530 : : /* prepare S-SID_REVISION- */
531 : : dwSidSize=wsprintf(Ident, TEXT("S-%lu-"), dwSidRev);
532 : :
533 : : /* prepare SidIdentifierAuthority */
534 : : if ((psia->Value[0] != 0) || (psia->Value[1] != 0))
535 : : {
536 : : dwSidSize+=wsprintf(Ident + strlen(Ident),
537 : : TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
538 : : (sal_uInt16)psia->Value[0],
539 : : (sal_uInt16)psia->Value[1],
540 : : (sal_uInt16)psia->Value[2],
541 : : (sal_uInt16)psia->Value[3],
542 : : (sal_uInt16)psia->Value[4],
543 : : (sal_uInt16)psia->Value[5]);
544 : : }
545 : : else
546 : : {
547 : : dwSidSize+=wsprintf(Ident + strlen(Ident),
548 : : TEXT("%lu"),
549 : : (sal_uInt32)(psia->Value[5] ) +
550 : : (sal_uInt32)(psia->Value[4] << 8) +
551 : : (sal_uInt32)(psia->Value[3] << 16) +
552 : : (sal_uInt32)(psia->Value[2] << 24) );
553 : : }
554 : :
555 : : /* loop through SidSubAuthorities */
556 : : for (dwCounter=0; dwCounter < dwSubAuthorities; dwCounter++)
557 : : {
558 : : dwSidSize+=wsprintf(Ident + dwSidSize, TEXT("-%lu"),
559 : : *GetSidSubAuthority(pSid, dwCounter) );
560 : : }
561 : :
562 : : strUserID = ::rtl::OUString::createFromAscii( Ident );
563 : :
564 : : free(Ident);
565 : : delete [] reinterpret_cast<BYTE*>(pSid);
566 : : delete [] wszDomainName;
567 : :
568 : :
569 : : /// check if logged in user is administrator:
570 : :
571 : : BOOL b;
572 : : SID_IDENTIFIER_AUTHORITY NtAuthority = { SECURITY_NT_AUTHORITY };
573 : : PSID AdministratorsGroup;
574 : : b = AllocateAndInitializeSid(
575 : : &NtAuthority,
576 : : 2,
577 : : SECURITY_BUILTIN_DOMAIN_RID,
578 : : DOMAIN_ALIAS_RID_ADMINS,
579 : : 0, 0, 0, 0, 0, 0,
580 : : &AdministratorsGroup);
581 : : if(b)
582 : : {
583 : : if (!CheckTokenMembership( NULL, AdministratorsGroup, &b))
584 : : {
585 : : b = FALSE;
586 : : }
587 : : FreeSid(AdministratorsGroup);
588 : : }
589 : :
590 : : isAdmin = ( sal_Bool )b;
591 : :
592 : : #endif
593 : :
594 : : /// print the information.
595 [ + - ]: 5 : t_print("#\n#Retrived system information is below:\n");
596 : :
597 [ + - ]: 5 : t_print("Computer Name: ");
598 [ + - ]: 5 : if ( strComputerName.isEmpty())
599 [ + - ]: 5 : t_print("Not retrived\n" );
600 : : else
601 [ # # ]: 0 : printUString( strComputerName );
602 : :
603 [ + - ]: 5 : t_print("Current User Name: ");
604 [ - + ]: 5 : if ( strUserName.isEmpty())
605 [ # # ]: 0 : t_print("Not retrived\n" );
606 : : else
607 [ + - ]: 5 : printUString( strUserName );
608 : :
609 [ + - ]: 5 : t_print("Current User Home Directory:");
610 [ - + ]: 5 : if ( strHomeDirectory.isEmpty())
611 [ # # ]: 0 : t_print("Not retrived\n" );
612 : : else
613 [ + - ]: 5 : printUString( strHomeDirectory );
614 : :
615 [ + - ]: 5 : t_print("Current Config Directory: ");
616 [ - + ]: 5 : if ( strConfigDirectory.isEmpty())
617 [ # # ]: 0 : t_print("Not retrived\n" );
618 : : else
619 [ + - ]: 5 : printUString( strConfigDirectory );
620 : :
621 [ + - ]: 5 : t_print("Current UserID: ");
622 [ - + ]: 5 : if ( strUserID.isEmpty())
623 [ # # ]: 0 : t_print("Not retrived\n" );
624 : : else
625 [ + - ]: 5 : printUString( strUserID );
626 : :
627 [ + - ]: 5 : t_print("Current User is: ");
628 [ - + ]: 5 : if ( isAdmin == sal_False )
629 [ # # ]: 0 : t_print("NOT Administrator.\n" );
630 : : else
631 [ + - ]: 5 : t_print("Administrator.\n" );
632 : :
633 : : /// get and display forwarded text if available.
634 [ + + ]: 35 : rtl::OUString args[ 3 ];
635 : 5 : int argsCount = 0;
636 [ + - ]: 5 : sal_uInt32 n = rtl_getAppCommandArgCount();
637 [ + + ]: 15 : for (sal_uInt32 i = 0; i < n; ++i)
638 : : {
639 : 10 : rtl::OUString arg;
640 [ + - ]: 10 : rtl_getAppCommandArg(i, &arg.pData);
641 [ + - ][ + + ]: 10 : if( !arg.isEmpty() && arg[ 0 ] == '-' )
[ + + ]
642 : 5 : continue;
643 [ - + ]: 5 : if( argsCount >= 3 )
644 : : {
645 : : SAL_WARN( "sal", "Too many test arguments" );
646 : 0 : continue;
647 : : }
648 [ + + ]: 15 : args[ argsCount++ ] = arg;
649 : 10 : }
650 : : /// only forwarded two parameters, username and password.
651 [ - + ]: 5 : if( argsCount == 2 )
652 : : {
653 : 0 : aLogonUser = args[ 0 ];
654 [ # # ]: 0 : t_print("\n#Forwarded username: ");
655 [ # # ]: 0 : printUString( aLogonUser);
656 : :
657 : 0 : aLogonPasswd = args[ 1 ];
658 [ # # ]: 0 : t_print("#Forwarded password: ");
659 [ # # ]: 0 : for (int i = 0; i < aLogonPasswd.getLength(); ++i)
660 [ # # ]: 0 : t_print("*");
661 [ # # ]: 0 : t_print("\n" );
662 : : }
663 [ - + ]: 5 : else if( argsCount == 3 )
664 : : /// forwarded three parameters, username, password and fileserver.
665 : : {
666 : 0 : aLogonUser = args[ 0 ];
667 [ # # ]: 0 : t_print("#Forwarded username: ");
668 [ # # ]: 0 : printUString( aLogonUser);
669 : :
670 : 0 : aLogonPasswd = args[ 1 ];
671 [ # # ]: 0 : t_print("#Forwarded password: ");
672 [ # # ]: 0 : for (int i = 0; i < aLogonPasswd.getLength(); ++i)
673 [ # # ]: 0 : t_print("*");
674 [ # # ]: 0 : t_print("\n" );
675 : :
676 : 0 : aFileServer = args[ 2 ];
677 [ # # ]: 0 : t_print("#Forwarded FileServer: ");
678 [ # # ]: 0 : printUString( aFileServer );
679 : : }
680 [ + - ]: 20 : t_print("#\n#Initialization Done.\n" );
[ + + # # ]
681 : :
682 : 5 : }
683 : :
684 : : /* Instantiate and register the own TestPlugIn and instantiate the default
685 : : main() function.
686 : : (This is done by CPPUNIT_PLUGIN_IMPLEMENT() for TestPlugInDefaultImpl)
687 : : */
688 : :
689 [ + - ][ + - ]: 5 : CPPUNIT_PLUGIN_EXPORTED_FUNCTION_IMPL( MyTestPlugInImpl );
[ + - ][ # # ]
690 [ + - ][ + - ]: 15 : CPPUNIT_PLUGIN_IMPLEMENT_MAIN();
691 : :
692 : :
693 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|