1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
24 #include <sal/types.h>
26 #include <cppunit/TestFixture.h>
27 #include <cppunit/extensions/HelperMacros.h>
28 #include <cppunit/plugin/TestPlugIn.h>
30 #include <rtl/ustring.hxx>
31 #include <rtl/string.hxx>
32 #include <rtl/process.h>
33 #include <osl/process.h>
34 #include <osl/module.hxx>
36 #include "rtl_Process_Const.h"
40 using ::rtl::OUString
;
42 using ::rtl::OUStringToOString
;
44 /** print a UNI_CODE String. And also print some comments of the string.
46 inline void printUString( const ::rtl::OUString
& str
, const sal_Char
* msg
= NULL
)
50 printf("#%s #printUString_u# ", msg
);
53 aString
= ::rtl::OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
54 printf("%s\n", (char *)aString
.getStr( ) );
57 inline ::rtl::OUString
getModulePath( void )
59 ::rtl::OUString suDirPath
;
60 ::osl::Module::getUrlFromAddress(
61 reinterpret_cast< oslGenericFunction
>(getModulePath
), suDirPath
);
63 printUString(suDirPath
, "modulePath:");
64 suDirPath
= suDirPath
.copy( 0, suDirPath
.lastIndexOf('/') );
65 suDirPath
= suDirPath
.copy( 0, suDirPath
.lastIndexOf('/') + 1);
66 suDirPath
+= rtl::OUString("bin");
72 class getAppCommandArg
: public CppUnit::TestFixture
75 // initialise your test code values here.
84 void getAppCommandArg_001()
87 const rtl::OUString
EXECUTABLE_NAME("child_process.exe");
89 const rtl::OUString
EXECUTABLE_NAME("child_process");
91 rtl::OUString suCWD
= getModulePath();
92 // rtl::OUString suCWD2 = getExecutableDirectory();
94 printUString(suCWD
, "path to the current module");
95 // printUString(suCWD2, "suCWD2");
97 oslProcess hProcess
= NULL
;
99 const int nParameterCount
= 4;
100 rtl_uString
* pParameters
[ nParameterCount
];
102 pParameters
[0] = suParam0
.pData
;
103 pParameters
[1] = suParam1
.pData
;
104 pParameters
[2] = suParam2
.pData
;
105 pParameters
[3] = suParam3
.pData
;
107 rtl::OUString suFileURL
= suCWD
;
108 suFileURL
+= rtl::OUString("/");
109 suFileURL
+= EXECUTABLE_NAME
;
111 oslProcessError osl_error
= osl_executeProcess(
116 0, /* osl_getCurrentSecurity() */
122 CPPUNIT_ASSERT_MESSAGE
124 "osl_createProcess failed",
125 osl_error
== osl_Process_E_None
127 //we could get return value only after the process terminated
128 osl_joinProcess(hProcess
);
129 // CPPUNIT_ASSERT_MESSAGE
131 // "osl_joinProcess returned with failure",
132 // osl_Process_E_None == osl_error
134 oslProcessInfo
* pInfo
= new oslProcessInfo
;
135 //please pay attention to initial the Size to sizeof(oslProcessInfo), or else
136 //you will get unknow error when call osl_getProcessInfo
137 pInfo
->Size
= sizeof(oslProcessInfo
);
138 osl_error
= osl_getProcessInfo( hProcess
, osl_Process_EXITCODE
, pInfo
);
139 CPPUNIT_ASSERT_MESSAGE
141 "osl_getProcessInfo returned with failure",
142 osl_Process_E_None
== osl_error
145 printf("the exit code is %" SAL_PRIuUINT32
".\n", pInfo
->Code
);
146 CPPUNIT_ASSERT_MESSAGE("rtl_getAppCommandArg or rtl_getAppCommandArgCount error.", pInfo
->Code
== 2);
151 CPPUNIT_TEST_SUITE(getAppCommandArg
);
152 CPPUNIT_TEST(getAppCommandArg_001
);
153 // CPPUNIT_TEST(getAppCommandArg_002);
154 CPPUNIT_TEST_SUITE_END();
155 }; // class getAppCommandArg
157 /************************************************************************
158 * For diagnostics( from sal/test/testuuid.cxx )
159 ************************************************************************/
160 void printUuid( sal_uInt8
*pNode
)
162 printf("# UUID is: ");
163 for( sal_Int32 i1
= 0 ; i1
< 4 ; i1
++ )
165 for( sal_Int32 i2
= 0 ; i2
< 4 ; i2
++ )
167 sal_uInt8 nValue
= pNode
[i1
*4 +i2
];
172 printf( "%02x" ,nValue
);
181 /**************************************************************************
182 * output UUID to a string
183 **************************************************************************/
184 void printUuidtoBuffer( sal_uInt8
*pNode
, sal_Char
* pBuffer
)
187 for( sal_Int32 i1
= 0 ; i1
< 16 ; i1
++ )
189 sal_uInt8 nValue
= pNode
[i1
];
192 sprintf( (sal_Char
*)(pBuffer
+ nPtr
), "0");
195 sprintf( (sal_Char
*)(pBuffer
+ nPtr
), "%02x", nValue
);
200 class getGlobalProcessId
: public CppUnit::TestFixture
203 // initialise your test code values here.
211 //gets a 16-byte fixed size identifier which is guaranteed not to change during the current process.
212 void getGlobalProcessId_001()
214 sal_uInt8 pTargetUUID1
[16];
215 sal_uInt8 pTargetUUID2
[16];
216 rtl_getGlobalProcessId( pTargetUUID1
);
217 rtl_getGlobalProcessId( pTargetUUID2
);
218 CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", !memcmp( pTargetUUID1
, pTargetUUID2
, 16 ) );
220 //different processes different pids
221 void getGlobalProcessId_002()
224 const rtl::OUString
EXEC_NAME("child_process_id.exe");
226 const rtl::OUString
EXEC_NAME("child_process_id");
228 sal_uInt8 pTargetUUID1
[16];
229 rtl_getGlobalProcessId( pTargetUUID1
);
230 printUuid( pTargetUUID1
);
232 printUuidtoBuffer( pTargetUUID1
, pUUID1
);
233 printf("# UUID to String is %s\n", pUUID1
);
235 rtl::OUString suCWD
= getModulePath();
236 oslProcess hProcess
= NULL
;
237 rtl::OUString suFileURL
= suCWD
;
238 suFileURL
+= rtl::OUString("/");
239 suFileURL
+= EXEC_NAME
;
240 oslFileHandle
* pChildOutputRead
= new oslFileHandle();
241 oslProcessError osl_error
= osl_executeProcess_WithRedirectedIO(
255 CPPUNIT_ASSERT_MESSAGE
257 "osl_createProcess failed",
258 osl_error
== osl_Process_E_None
260 //we could get return value only after the process terminated
261 osl_joinProcess(hProcess
);
265 sal_uInt64 nRead
= 0;
266 osl_readFile( *pChildOutputRead
, pUUID2
, 32, &nRead
);
267 printf("read buffer is %s, nRead is %" SAL_PRIdINT64
"\n", pUUID2
, nRead
);
268 OUString suUUID2
= OUString::createFromAscii( pUUID2
);
269 CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", suUUID2
.equalsAsciiL( pUUID1
, 32) == sal_False
);
272 CPPUNIT_TEST_SUITE(getGlobalProcessId
);
273 CPPUNIT_TEST(getGlobalProcessId_001
);
274 CPPUNIT_TEST(getGlobalProcessId_002
);
275 CPPUNIT_TEST_SUITE_END();
277 }; // class getGlobalProcessId
279 } // namespace rtl_Process
281 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getAppCommandArg
, "rtl_Process");
282 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getGlobalProcessId
, "rtl_Process");
285 // this macro creates an empty function, which will called by the RegisterAllFunctions()
286 // to let the user the possibility to also register some functions by hand.
287 CPPUNIT_PLUGIN_IMPLEMENT();
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */