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