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