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

Generated by: LCOV version 1.10