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 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
= OUString::Concat(suDirPath
.subView( 0, suDirPath
.lastIndexOf('/') + 1)) + "bin";
66 class getAppCommandArg
: public CppUnit::TestFixture
69 void getAppCommandArg_001()
72 static const OUStringLiteral
EXECUTABLE_NAME(u
"child_process.exe");
74 static const OUStringLiteral
EXECUTABLE_NAME(u
"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
, char * pBuffer
)
168 for( sal_Int32 i1
= 0 ; i1
< 16 ; i1
++ )
170 sal_uInt8 nValue
= pNode
[i1
];
173 SAL_WNODEPRECATED_DECLARATIONS_PUSH
// sprintf (macOS 13 SDK)
174 sprintf( pBuffer
+ nPtr
, "0");
175 SAL_WNODEPRECATED_DECLARATIONS_POP
178 SAL_WNODEPRECATED_DECLARATIONS_PUSH
// sprintf (macOS 13 SDK)
179 sprintf( pBuffer
+ nPtr
, "%02x", nValue
);
180 SAL_WNODEPRECATED_DECLARATIONS_POP
185 class getGlobalProcessId
: public CppUnit::TestFixture
188 //gets a 16-byte fixed size identifier which is guaranteed not to change during the current process.
189 void getGlobalProcessId_001()
191 sal_uInt8 pTargetUUID1
[16];
192 sal_uInt8 pTargetUUID2
[16];
193 rtl_getGlobalProcessId( pTargetUUID1
);
194 rtl_getGlobalProcessId( pTargetUUID2
);
195 CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", !memcmp( pTargetUUID1
, pTargetUUID2
, 16 ) );
197 //different processes different pids
198 void getGlobalProcessId_002()
201 static const OUStringLiteral
EXEC_NAME(u
"child_process_id.exe");
203 static const OUStringLiteral
EXEC_NAME(u
"child_process_id");
205 sal_uInt8 pTargetUUID1
[16];
206 rtl_getGlobalProcessId( pTargetUUID1
);
207 printUuid( pTargetUUID1
);
209 printUuidtoBuffer( pTargetUUID1
, pUUID1
);
210 printf("# UUID to String is %s\n", pUUID1
);
212 OUString suCWD
= getModulePath();
213 oslProcess hProcess
= nullptr;
214 OUString suFileURL
= suCWD
+ "/" + EXEC_NAME
;
215 oslFileHandle
* pChildOutputRead
= new oslFileHandle();
216 oslProcessError osl_error
= osl_executeProcess_WithRedirectedIO(
230 CPPUNIT_ASSERT_EQUAL_MESSAGE
232 "osl_createProcess failed",
233 osl_Process_E_None
, osl_error
235 //we could get return value only after the process terminated
236 osl_joinProcess(hProcess
);
239 sal_uInt64 nRead
= 0;
240 osl_readFile( *pChildOutputRead
, pUUID2
, 32, &nRead
);
241 printf("read buffer is %s, nRead is %" SAL_PRIdINT64
"\n", pUUID2
, nRead
);
242 OUString suUUID2
= OUString::createFromAscii( pUUID2
);
243 CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", !suUUID2
.equalsAsciiL( pUUID1
, 32) );
246 CPPUNIT_TEST_SUITE(getGlobalProcessId
);
247 CPPUNIT_TEST(getGlobalProcessId_001
);
248 CPPUNIT_TEST(getGlobalProcessId_002
);
249 CPPUNIT_TEST_SUITE_END();
251 }; // class getGlobalProcessId
253 } // namespace rtl_Process
255 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getAppCommandArg
, "rtl_Process");
256 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getGlobalProcessId
, "rtl_Process");
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */