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 /** print a UNI_CODE String. And also print some comments of the string.
42 static void printUString( const OUString
& str
, const sal_Char
* msg
)
46 printf("#%s #printUString_u# ", msg
);
48 OString aString
= OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
49 printf("%s\n", aString
.getStr( ) );
52 static OUString
getModulePath()
55 ::osl::Module::getUrlFromAddress(
56 reinterpret_cast< oslGenericFunction
>(getModulePath
), suDirPath
);
58 printUString(suDirPath
, "modulePath:");
59 suDirPath
= suDirPath
.copy( 0, suDirPath
.lastIndexOf('/') );
60 suDirPath
= suDirPath
.copy( 0, suDirPath
.lastIndexOf('/') + 1) + "bin";
66 class getAppCommandArg
: public CppUnit::TestFixture
69 void getAppCommandArg_001()
72 const OUString
EXECUTABLE_NAME("child_process.exe");
74 const OUString
EXECUTABLE_NAME("child_process");
76 OUString suCWD
= getModulePath();
77 // OUString suCWD2 = getExecutableDirectory();
79 printUString(suCWD
, "path to the current module");
80 // printUString(suCWD2, "suCWD2");
82 oslProcess hProcess
= nullptr;
84 const int nParameterCount
= 4;
85 rtl_uString
* pParameters
[ nParameterCount
];
87 pParameters
[0] = suParam0
.pData
;
88 pParameters
[1] = suParam1
.pData
;
89 pParameters
[2] = suParam2
.pData
;
90 pParameters
[3] = suParam3
.pData
;
92 OUString suFileURL
= suCWD
+ "/" + EXECUTABLE_NAME
;
94 oslProcessError osl_error
= osl_executeProcess(
99 nullptr, /* osl_getCurrentSecurity() */
105 CPPUNIT_ASSERT_EQUAL_MESSAGE
107 "osl_createProcess failed",
108 osl_Process_E_None
, osl_error
110 //we could get return value only after the process terminated
111 osl_joinProcess(hProcess
);
112 // CPPUNIT_ASSERT_MESSAGE
114 // "osl_joinProcess returned with failure",
115 // osl_Process_E_None == osl_error
117 std::unique_ptr
<oslProcessInfo
> pInfo( new oslProcessInfo
);
118 //please pay attention to initial the Size to sizeof(oslProcessInfo), or else
119 //you will get unknown error when call osl_getProcessInfo
120 pInfo
->Size
= sizeof(oslProcessInfo
);
121 osl_error
= osl_getProcessInfo( hProcess
, osl_Process_EXITCODE
, pInfo
.get() );
122 CPPUNIT_ASSERT_EQUAL_MESSAGE
124 "osl_getProcessInfo returned with failure",
125 osl_Process_E_None
, osl_error
128 printf("the exit code is %" SAL_PRIuUINT32
".\n", pInfo
->Code
);
129 CPPUNIT_ASSERT_EQUAL_MESSAGE("rtl_getAppCommandArg or rtl_getAppCommandArgCount error.", static_cast<oslProcessExitCode
>(2), pInfo
->Code
);
132 CPPUNIT_TEST_SUITE(getAppCommandArg
);
133 CPPUNIT_TEST(getAppCommandArg_001
);
134 // CPPUNIT_TEST(getAppCommandArg_002);
135 CPPUNIT_TEST_SUITE_END();
136 }; // class getAppCommandArg
138 /************************************************************************
139 * For diagnostics( from sal/test/testuuid.cxx )
140 ************************************************************************/
141 static void printUuid( const sal_uInt8
*pNode
)
143 printf("# UUID is: ");
144 for( sal_Int32 i1
= 0 ; i1
< 4 ; i1
++ )
146 for( sal_Int32 i2
= 0 ; i2
< 4 ; i2
++ )
148 sal_uInt8 nValue
= pNode
[i1
*4 +i2
];
153 printf( "%02x" ,nValue
);
162 /**************************************************************************
163 * output UUID to a string
164 **************************************************************************/
165 static void printUuidtoBuffer( const sal_uInt8
*pNode
, sal_Char
* pBuffer
)
168 for( sal_Int32 i1
= 0 ; i1
< 16 ; i1
++ )
170 sal_uInt8 nValue
= pNode
[i1
];
173 sprintf( pBuffer
+ nPtr
, "0");
176 sprintf( pBuffer
+ nPtr
, "%02x", nValue
);
181 class getGlobalProcessId
: public CppUnit::TestFixture
184 //gets a 16-byte fixed size identifier which is guaranteed not to change during the current process.
185 void getGlobalProcessId_001()
187 sal_uInt8 pTargetUUID1
[16];
188 sal_uInt8 pTargetUUID2
[16];
189 rtl_getGlobalProcessId( pTargetUUID1
);
190 rtl_getGlobalProcessId( pTargetUUID2
);
191 CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", !memcmp( pTargetUUID1
, pTargetUUID2
, 16 ) );
193 //different processes different pids
194 void getGlobalProcessId_002()
197 const OUString
EXEC_NAME("child_process_id.exe");
199 const OUString
EXEC_NAME("child_process_id");
201 sal_uInt8 pTargetUUID1
[16];
202 rtl_getGlobalProcessId( pTargetUUID1
);
203 printUuid( pTargetUUID1
);
205 printUuidtoBuffer( pTargetUUID1
, pUUID1
);
206 printf("# UUID to String is %s\n", pUUID1
);
208 OUString suCWD
= getModulePath();
209 oslProcess hProcess
= nullptr;
210 OUString suFileURL
= suCWD
+ "/" + EXEC_NAME
;
211 oslFileHandle
* pChildOutputRead
= new oslFileHandle();
212 oslProcessError osl_error
= osl_executeProcess_WithRedirectedIO(
226 CPPUNIT_ASSERT_EQUAL_MESSAGE
228 "osl_createProcess failed",
229 osl_Process_E_None
, osl_error
231 //we could get return value only after the process terminated
232 osl_joinProcess(hProcess
);
234 sal_Char pUUID2
[33] {};
235 sal_uInt64 nRead
= 0;
236 osl_readFile( *pChildOutputRead
, pUUID2
, 32, &nRead
);
237 printf("read buffer is %s, nRead is %" SAL_PRIdINT64
"\n", pUUID2
, nRead
);
238 OUString suUUID2
= OUString::createFromAscii( pUUID2
);
239 CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", !suUUID2
.equalsAsciiL( pUUID1
, 32) );
242 CPPUNIT_TEST_SUITE(getGlobalProcessId
);
243 CPPUNIT_TEST(getGlobalProcessId_001
);
244 CPPUNIT_TEST(getGlobalProcessId_002
);
245 CPPUNIT_TEST_SUITE_END();
247 }; // class getGlobalProcessId
249 } // namespace rtl_Process
251 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getAppCommandArg
, "rtl_Process");
252 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getGlobalProcessId
, "rtl_Process");
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */