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