xref: /aoo42x/main/sal/qa/rtl/process/rtl_Process.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sal.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <stdlib.h>
32*cdf0e10cSrcweir #include <stdio.h>
33*cdf0e10cSrcweir #include <string.h>
34*cdf0e10cSrcweir #include <sal/types.h>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <testshl/simpleheader.hxx>
37*cdf0e10cSrcweir #include <rtl/ustring.hxx>
38*cdf0e10cSrcweir #include <rtl/string.hxx>
39*cdf0e10cSrcweir #include <rtl/process.h>
40*cdf0e10cSrcweir #include <osl/process.h>
41*cdf0e10cSrcweir #include <osl/module.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include "rtl_Process_Const.h"
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace osl;
46*cdf0e10cSrcweir using namespace rtl;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir /** print a UNI_CODE String. And also print some comments of the string.
49*cdf0e10cSrcweir */
50*cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = NULL )
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir     if ( msg != NULL )
53*cdf0e10cSrcweir     {
54*cdf0e10cSrcweir     	t_print("#%s #printUString_u# ", msg );
55*cdf0e10cSrcweir     }
56*cdf0e10cSrcweir     rtl::OString aString;
57*cdf0e10cSrcweir     aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
58*cdf0e10cSrcweir     t_print("%s\n", (char *)aString.getStr( ) );
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir // -----------------------------------------------------------------------------
62*cdf0e10cSrcweir inline ::rtl::OUString getModulePath( void )
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir     ::rtl::OUString suDirPath;
65*cdf0e10cSrcweir     ::osl::Module::getUrlFromAddress(
66*cdf0e10cSrcweir         reinterpret_cast< oslGenericFunction >(getModulePath), suDirPath );
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     printUString(suDirPath, "modulePath:");
69*cdf0e10cSrcweir     suDirPath = suDirPath.copy( 0, suDirPath.lastIndexOf('/') );
70*cdf0e10cSrcweir     suDirPath = suDirPath.copy( 0, suDirPath.lastIndexOf('/') + 1);
71*cdf0e10cSrcweir     suDirPath += rtl::OUString::createFromAscii("bin");
72*cdf0e10cSrcweir     return suDirPath;
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir // -----------------------------------------------------------------------------
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir namespace rtl_Process
78*cdf0e10cSrcweir {
79*cdf0e10cSrcweir class getAppCommandArg : public CppUnit::TestFixture
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir public:
82*cdf0e10cSrcweir     // initialise your test code values here.
83*cdf0e10cSrcweir     void setUp()
84*cdf0e10cSrcweir     {
85*cdf0e10cSrcweir     }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir     void tearDown()
88*cdf0e10cSrcweir     {
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     void getAppCommandArg_001()
92*cdf0e10cSrcweir     {
93*cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
94*cdf0e10cSrcweir 	const rtl::OUString EXECUTABLE_NAME = rtl::OUString::createFromAscii("child_process.exe");
95*cdf0e10cSrcweir #else
96*cdf0e10cSrcweir 	const rtl::OUString EXECUTABLE_NAME = rtl::OUString::createFromAscii("child_process");
97*cdf0e10cSrcweir #endif
98*cdf0e10cSrcweir         rtl::OUString suCWD = getModulePath();
99*cdf0e10cSrcweir         // rtl::OUString suCWD2 = getExecutableDirectory();
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir         printUString(suCWD, "path to the current module");
102*cdf0e10cSrcweir         // printUString(suCWD2, "suCWD2");
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     	oslProcess hProcess = NULL;
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir         const int nParameterCount = 4;
107*cdf0e10cSrcweir     	rtl_uString* pParameters[ nParameterCount ];
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         pParameters[0] = suParam0.pData;
110*cdf0e10cSrcweir         pParameters[1] = suParam1.pData;
111*cdf0e10cSrcweir         pParameters[2] = suParam2.pData;
112*cdf0e10cSrcweir         pParameters[3] = suParam3.pData;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         rtl::OUString suFileURL = suCWD;
115*cdf0e10cSrcweir         suFileURL += rtl::OUString::createFromAscii("/");
116*cdf0e10cSrcweir         suFileURL += EXECUTABLE_NAME;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         oslProcessError osl_error = osl_executeProcess(
119*cdf0e10cSrcweir             suFileURL.pData,
120*cdf0e10cSrcweir             pParameters,
121*cdf0e10cSrcweir             nParameterCount,
122*cdf0e10cSrcweir             osl_Process_WAIT,
123*cdf0e10cSrcweir             0, /* osl_getCurrentSecurity() */
124*cdf0e10cSrcweir             suCWD.pData,
125*cdf0e10cSrcweir             NULL,
126*cdf0e10cSrcweir             0,
127*cdf0e10cSrcweir             &hProcess );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
130*cdf0e10cSrcweir         (
131*cdf0e10cSrcweir             "osl_createProcess failed",
132*cdf0e10cSrcweir             osl_error == osl_Process_E_None
133*cdf0e10cSrcweir         );
134*cdf0e10cSrcweir 	//we could get return value only after the process terminated
135*cdf0e10cSrcweir         osl_joinProcess(hProcess);
136*cdf0e10cSrcweir         // CPPUNIT_ASSERT_MESSAGE
137*cdf0e10cSrcweir         // (
138*cdf0e10cSrcweir         //     "osl_joinProcess returned with failure",
139*cdf0e10cSrcweir         //     osl_Process_E_None == osl_error
140*cdf0e10cSrcweir         // );
141*cdf0e10cSrcweir 	oslProcessInfo* pInfo = new oslProcessInfo;
142*cdf0e10cSrcweir 	//please pay attention to initial the Size to sizeof(oslProcessInfo), or else
143*cdf0e10cSrcweir 	//you will get unknow error when call osl_getProcessInfo
144*cdf0e10cSrcweir 	pInfo->Size = sizeof(oslProcessInfo);
145*cdf0e10cSrcweir 	osl_error = osl_getProcessInfo( hProcess, osl_Process_EXITCODE, pInfo );
146*cdf0e10cSrcweir 	CPPUNIT_ASSERT_MESSAGE
147*cdf0e10cSrcweir         (
148*cdf0e10cSrcweir             "osl_getProcessInfo returned with failure",
149*cdf0e10cSrcweir             osl_Process_E_None == osl_error
150*cdf0e10cSrcweir         );
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	t_print("the exit code is %d.\n", pInfo->Code );
153*cdf0e10cSrcweir 	CPPUNIT_ASSERT_MESSAGE("rtl_getAppCommandArg or rtl_getAppCommandArgCount error.", pInfo->Code == 2);
154*cdf0e10cSrcweir 	delete pInfo;
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(getAppCommandArg);
159*cdf0e10cSrcweir     CPPUNIT_TEST(getAppCommandArg_001);
160*cdf0e10cSrcweir   //  CPPUNIT_TEST(getAppCommandArg_002);
161*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
162*cdf0e10cSrcweir }; // class getAppCommandArg
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir /************************************************************************
165*cdf0e10cSrcweir  * For diagnostics( from sal/test/testuuid.cxx )
166*cdf0e10cSrcweir  ************************************************************************/
167*cdf0e10cSrcweir void printUuid( sal_uInt8 *pNode )
168*cdf0e10cSrcweir {
169*cdf0e10cSrcweir     printf("# UUID is: ");
170*cdf0e10cSrcweir     for( sal_Int32 i1 = 0 ; i1 < 4 ; i1++ )
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         for( sal_Int32 i2 = 0 ; i2 < 4 ; i2++ )
173*cdf0e10cSrcweir         {
174*cdf0e10cSrcweir             sal_uInt8 nValue = pNode[i1*4 +i2];
175*cdf0e10cSrcweir             if (nValue < 16)
176*cdf0e10cSrcweir             {
177*cdf0e10cSrcweir                 printf( "0");
178*cdf0e10cSrcweir             }
179*cdf0e10cSrcweir             printf( "%02x" ,nValue );
180*cdf0e10cSrcweir         }
181*cdf0e10cSrcweir         if( i1 == 3 )
182*cdf0e10cSrcweir             break;
183*cdf0e10cSrcweir         printf( "-" );
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir     printf("\n");
186*cdf0e10cSrcweir }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir /**************************************************************************
189*cdf0e10cSrcweir  *  output UUID to a string
190*cdf0e10cSrcweir  **************************************************************************/
191*cdf0e10cSrcweir void printUuidtoBuffer( sal_uInt8 *pNode, sal_Char * pBuffer )
192*cdf0e10cSrcweir {
193*cdf0e10cSrcweir     sal_Int8 nPtr = 0;
194*cdf0e10cSrcweir     for( sal_Int32 i1 = 0 ; i1 < 16 ; i1++ )
195*cdf0e10cSrcweir     {
196*cdf0e10cSrcweir         sal_uInt8 nValue = pNode[i1];
197*cdf0e10cSrcweir         if (nValue < 16)
198*cdf0e10cSrcweir         {
199*cdf0e10cSrcweir              sprintf( (sal_Char *)(pBuffer + nPtr), "0");
200*cdf0e10cSrcweir              nPtr++;
201*cdf0e10cSrcweir         }
202*cdf0e10cSrcweir         sprintf( (sal_Char *)(pBuffer + nPtr), "%02x", nValue );
203*cdf0e10cSrcweir         nPtr += 2 ;
204*cdf0e10cSrcweir     }
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir class getGlobalProcessId : public CppUnit::TestFixture
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir public:
210*cdf0e10cSrcweir     // initialise your test code values here.
211*cdf0e10cSrcweir     void setUp()
212*cdf0e10cSrcweir     {
213*cdf0e10cSrcweir     }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     void tearDown()
216*cdf0e10cSrcweir     {
217*cdf0e10cSrcweir     }
218*cdf0e10cSrcweir     //gets a 16-byte fixed size identifier which is guaranteed not to change	during the current process.
219*cdf0e10cSrcweir     void getGlobalProcessId_001()
220*cdf0e10cSrcweir     {
221*cdf0e10cSrcweir     	sal_uInt8 pTargetUUID1[16];
222*cdf0e10cSrcweir     	sal_uInt8 pTargetUUID2[16];
223*cdf0e10cSrcweir     	rtl_getGlobalProcessId( pTargetUUID1 );
224*cdf0e10cSrcweir     	rtl_getGlobalProcessId( pTargetUUID2 );
225*cdf0e10cSrcweir 	CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", !memcmp( pTargetUUID1 , pTargetUUID2 , 16 ) );
226*cdf0e10cSrcweir     }
227*cdf0e10cSrcweir     //different processes different pids
228*cdf0e10cSrcweir     void getGlobalProcessId_002()
229*cdf0e10cSrcweir     {
230*cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
231*cdf0e10cSrcweir 	const rtl::OUString EXEC_NAME = rtl::OUString::createFromAscii("child_process_id.exe");
232*cdf0e10cSrcweir #else
233*cdf0e10cSrcweir 	const rtl::OUString EXEC_NAME = rtl::OUString::createFromAscii("child_process_id");
234*cdf0e10cSrcweir #endif
235*cdf0e10cSrcweir     	sal_uInt8 pTargetUUID1[16];
236*cdf0e10cSrcweir     	rtl_getGlobalProcessId( pTargetUUID1 );
237*cdf0e10cSrcweir     	printUuid( pTargetUUID1 );
238*cdf0e10cSrcweir     	sal_Char pUUID1[32];
239*cdf0e10cSrcweir       	printUuidtoBuffer( pTargetUUID1, pUUID1 );
240*cdf0e10cSrcweir 	printf("# UUID to String is %s\n", pUUID1);
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 	rtl::OUString suCWD = getModulePath();
243*cdf0e10cSrcweir     	oslProcess hProcess = NULL;
244*cdf0e10cSrcweir    	rtl::OUString suFileURL = suCWD;
245*cdf0e10cSrcweir         suFileURL += rtl::OUString::createFromAscii("/");
246*cdf0e10cSrcweir         suFileURL += EXEC_NAME;
247*cdf0e10cSrcweir 	oslFileHandle* pChildOutputRead = new oslFileHandle();
248*cdf0e10cSrcweir         oslProcessError osl_error = osl_executeProcess_WithRedirectedIO(
249*cdf0e10cSrcweir             suFileURL.pData,
250*cdf0e10cSrcweir             NULL,
251*cdf0e10cSrcweir             0,
252*cdf0e10cSrcweir             osl_Process_WAIT,
253*cdf0e10cSrcweir             0,
254*cdf0e10cSrcweir             suCWD.pData,
255*cdf0e10cSrcweir             NULL,
256*cdf0e10cSrcweir             0,
257*cdf0e10cSrcweir             &hProcess,
258*cdf0e10cSrcweir 	    NULL,
259*cdf0e10cSrcweir 	    pChildOutputRead,
260*cdf0e10cSrcweir 	    NULL);
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE
263*cdf0e10cSrcweir         (
264*cdf0e10cSrcweir             "osl_createProcess failed",
265*cdf0e10cSrcweir             osl_error == osl_Process_E_None
266*cdf0e10cSrcweir         );
267*cdf0e10cSrcweir 	//we could get return value only after the process terminated
268*cdf0e10cSrcweir         osl_joinProcess(hProcess);
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir         sal_Char pUUID2[33];
271*cdf0e10cSrcweir         pUUID2[32] = '\0';
272*cdf0e10cSrcweir 	sal_uInt64 nRead = 0;
273*cdf0e10cSrcweir 	osl_readFile( *pChildOutputRead, pUUID2, 32, &nRead );
274*cdf0e10cSrcweir 	t_print("read buffer is %s, nRead is %d \n", pUUID2, nRead );
275*cdf0e10cSrcweir 	OUString suUUID2 = OUString::createFromAscii( pUUID2 );
276*cdf0e10cSrcweir 	CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", suUUID2.equalsAsciiL( pUUID1, 32) == sal_False );
277*cdf0e10cSrcweir     }
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(getGlobalProcessId);
280*cdf0e10cSrcweir     CPPUNIT_TEST(getGlobalProcessId_001);
281*cdf0e10cSrcweir     CPPUNIT_TEST(getGlobalProcessId_002);
282*cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir }; // class getGlobalProcessId
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir } // namespace rtl_Process
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getAppCommandArg, "rtl_Process");
289*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getGlobalProcessId, "rtl_Process");
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir // -----------------------------------------------------------------------------
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions()
295*cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand.
296*cdf0e10cSrcweir NOADDITIONAL;
297