1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
34 #include <sal/types.h>
36 #include <testshl/simpleheader.hxx>
37 #include <rtl/ustring.hxx>
38 #include <rtl/string.hxx>
39 #include <rtl/process.h>
40 #include <osl/process.h>
41 #include <osl/module.hxx>
43 #include "rtl_Process_Const.h"
48 /** print a UNI_CODE String. And also print some comments of the string.
50 inline void printUString( const ::rtl::OUString
& str
, const sal_Char
* msg
= NULL
)
54 t_print("#%s #printUString_u# ", msg
);
57 aString
= ::rtl::OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
58 t_print("%s\n", (char *)aString
.getStr( ) );
61 // -----------------------------------------------------------------------------
62 inline ::rtl::OUString
getModulePath( void )
64 ::rtl::OUString suDirPath
;
65 ::osl::Module::getUrlFromAddress(
66 reinterpret_cast< oslGenericFunction
>(getModulePath
), suDirPath
);
68 printUString(suDirPath
, "modulePath:");
69 suDirPath
= suDirPath
.copy( 0, suDirPath
.lastIndexOf('/') );
70 suDirPath
= suDirPath
.copy( 0, suDirPath
.lastIndexOf('/') + 1);
71 suDirPath
+= rtl::OUString::createFromAscii("bin");
75 // -----------------------------------------------------------------------------
79 class getAppCommandArg
: public CppUnit::TestFixture
82 // initialise your test code values here.
91 void getAppCommandArg_001()
93 #if defined(WNT) || defined(OS2)
94 const rtl::OUString EXECUTABLE_NAME
= rtl::OUString::createFromAscii("child_process.exe");
96 const rtl::OUString EXECUTABLE_NAME
= rtl::OUString::createFromAscii("child_process");
98 rtl::OUString suCWD
= getModulePath();
99 // rtl::OUString suCWD2 = getExecutableDirectory();
101 printUString(suCWD
, "path to the current module");
102 // printUString(suCWD2, "suCWD2");
104 oslProcess hProcess
= NULL
;
106 const int nParameterCount
= 4;
107 rtl_uString
* pParameters
[ nParameterCount
];
109 pParameters
[0] = suParam0
.pData
;
110 pParameters
[1] = suParam1
.pData
;
111 pParameters
[2] = suParam2
.pData
;
112 pParameters
[3] = suParam3
.pData
;
114 rtl::OUString suFileURL
= suCWD
;
115 suFileURL
+= rtl::OUString::createFromAscii("/");
116 suFileURL
+= EXECUTABLE_NAME
;
118 oslProcessError osl_error
= osl_executeProcess(
123 0, /* osl_getCurrentSecurity() */
129 CPPUNIT_ASSERT_MESSAGE
131 "osl_createProcess failed",
132 osl_error
== osl_Process_E_None
134 //we could get return value only after the process terminated
135 osl_joinProcess(hProcess
);
136 // CPPUNIT_ASSERT_MESSAGE
138 // "osl_joinProcess returned with failure",
139 // osl_Process_E_None == osl_error
141 oslProcessInfo
* pInfo
= new oslProcessInfo
;
142 //please pay attention to initial the Size to sizeof(oslProcessInfo), or else
143 //you will get unknow error when call osl_getProcessInfo
144 pInfo
->Size
= sizeof(oslProcessInfo
);
145 osl_error
= osl_getProcessInfo( hProcess
, osl_Process_EXITCODE
, pInfo
);
146 CPPUNIT_ASSERT_MESSAGE
148 "osl_getProcessInfo returned with failure",
149 osl_Process_E_None
== osl_error
152 t_print("the exit code is %d.\n", pInfo
->Code
);
153 CPPUNIT_ASSERT_MESSAGE("rtl_getAppCommandArg or rtl_getAppCommandArgCount error.", pInfo
->Code
== 2);
158 CPPUNIT_TEST_SUITE(getAppCommandArg
);
159 CPPUNIT_TEST(getAppCommandArg_001
);
160 // CPPUNIT_TEST(getAppCommandArg_002);
161 CPPUNIT_TEST_SUITE_END();
162 }; // class getAppCommandArg
164 /************************************************************************
165 * For diagnostics( from sal/test/testuuid.cxx )
166 ************************************************************************/
167 void printUuid( sal_uInt8
*pNode
)
169 printf("# UUID is: ");
170 for( sal_Int32 i1
= 0 ; i1
< 4 ; i1
++ )
172 for( sal_Int32 i2
= 0 ; i2
< 4 ; i2
++ )
174 sal_uInt8 nValue
= pNode
[i1
*4 +i2
];
179 printf( "%02x" ,nValue
);
188 /**************************************************************************
189 * output UUID to a string
190 **************************************************************************/
191 void printUuidtoBuffer( sal_uInt8
*pNode
, sal_Char
* pBuffer
)
194 for( sal_Int32 i1
= 0 ; i1
< 16 ; i1
++ )
196 sal_uInt8 nValue
= pNode
[i1
];
199 sprintf( (sal_Char
*)(pBuffer
+ nPtr
), "0");
202 sprintf( (sal_Char
*)(pBuffer
+ nPtr
), "%02x", nValue
);
207 class getGlobalProcessId
: public CppUnit::TestFixture
210 // initialise your test code values here.
218 //gets a 16-byte fixed size identifier which is guaranteed not to change during the current process.
219 void getGlobalProcessId_001()
221 sal_uInt8 pTargetUUID1
[16];
222 sal_uInt8 pTargetUUID2
[16];
223 rtl_getGlobalProcessId( pTargetUUID1
);
224 rtl_getGlobalProcessId( pTargetUUID2
);
225 CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", !memcmp( pTargetUUID1
, pTargetUUID2
, 16 ) );
227 //different processes different pids
228 void getGlobalProcessId_002()
230 #if defined(WNT) || defined(OS2)
231 const rtl::OUString EXEC_NAME
= rtl::OUString::createFromAscii("child_process_id.exe");
233 const rtl::OUString EXEC_NAME
= rtl::OUString::createFromAscii("child_process_id");
235 sal_uInt8 pTargetUUID1
[16];
236 rtl_getGlobalProcessId( pTargetUUID1
);
237 printUuid( pTargetUUID1
);
239 printUuidtoBuffer( pTargetUUID1
, pUUID1
);
240 printf("# UUID to String is %s\n", pUUID1
);
242 rtl::OUString suCWD
= getModulePath();
243 oslProcess hProcess
= NULL
;
244 rtl::OUString suFileURL
= suCWD
;
245 suFileURL
+= rtl::OUString::createFromAscii("/");
246 suFileURL
+= EXEC_NAME
;
247 oslFileHandle
* pChildOutputRead
= new oslFileHandle();
248 oslProcessError osl_error
= osl_executeProcess_WithRedirectedIO(
262 CPPUNIT_ASSERT_MESSAGE
264 "osl_createProcess failed",
265 osl_error
== osl_Process_E_None
267 //we could get return value only after the process terminated
268 osl_joinProcess(hProcess
);
272 sal_uInt64 nRead
= 0;
273 osl_readFile( *pChildOutputRead
, pUUID2
, 32, &nRead
);
274 t_print("read buffer is %s, nRead is %d \n", pUUID2
, nRead
);
275 OUString suUUID2
= OUString::createFromAscii( pUUID2
);
276 CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", suUUID2
.equalsAsciiL( pUUID1
, 32) == sal_False
);
279 CPPUNIT_TEST_SUITE(getGlobalProcessId
);
280 CPPUNIT_TEST(getGlobalProcessId_001
);
281 CPPUNIT_TEST(getGlobalProcessId_002
);
282 CPPUNIT_TEST_SUITE_END();
284 }; // class getGlobalProcessId
286 } // namespace rtl_Process
288 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getAppCommandArg
, "rtl_Process");
289 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getGlobalProcessId
, "rtl_Process");
292 // -----------------------------------------------------------------------------
294 // this macro creates an empty function, which will called by the RegisterAllFunctions()
295 // to let the user the possibility to also register some functions by hand.