LCOV - code coverage report
Current view: top level - libreoffice/sal/qa/osl/security - osl_Security.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 173 215 80.5 %
Date: 2012-12-27 Functions: 67 74 90.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10