1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtl_Process.cxx,v $
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"
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"
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
)
57 t_print("#%s #printUString_u# ", msg
);
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");
78 // -----------------------------------------------------------------------------
82 class getAppCommandArg
: public CppUnit::TestFixture
85 // initialise your test code values here.
94 void getAppCommandArg_001()
96 #if defined(WNT) || defined(OS2)
97 const rtl::OUString EXECUTABLE_NAME
= rtl::OUString::createFromAscii("child_process.exe");
99 const rtl::OUString EXECUTABLE_NAME
= rtl::OUString::createFromAscii("child_process");
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(
126 0, /* osl_getCurrentSecurity() */
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
141 // "osl_joinProcess returned with failure",
142 // osl_Process_E_None == osl_error
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);
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
];
182 printf( "%02x" ,nValue
);
191 /**************************************************************************
192 * output UUID to a string
193 **************************************************************************/
194 void printUuidtoBuffer( sal_uInt8
*pNode
, sal_Char
* pBuffer
)
197 for( sal_Int32 i1
= 0 ; i1
< 16 ; i1
++ )
199 sal_uInt8 nValue
= pNode
[i1
];
202 sprintf( (sal_Char
*)(pBuffer
+ nPtr
), "0");
205 sprintf( (sal_Char
*)(pBuffer
+ nPtr
), "%02x", nValue
);
210 class getGlobalProcessId
: public CppUnit::TestFixture
213 // initialise your test code values here.
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");
236 const rtl::OUString EXEC_NAME
= rtl::OUString::createFromAscii("child_process_id");
238 sal_uInt8 pTargetUUID1
[16];
239 rtl_getGlobalProcessId( pTargetUUID1
);
240 printUuid( pTargetUUID1
);
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(
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
);
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.