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 .
23 #include <sal/types.h>
25 #include <cppunit/TestFixture.h>
26 #include <cppunit/extensions/HelperMacros.h>
28 #include <rtl/ustring.hxx>
29 #include <rtl/string.hxx>
30 #include <rtl/process.h>
31 #include <osl/process.h>
32 #include <osl/module.hxx>
34 #include "rtl_Process_Const.h"
38 /** print a UNI_CODE String. And also print some comments of the string.
40 static void printUString( const OUString
& str
, const char * msg
)
44 printf("#%s #printUString_u# ", msg
);
46 OString aString
= OUStringToOString( str
, RTL_TEXTENCODING_ASCII_US
);
47 printf("%s\n", aString
.getStr( ) );
50 static OUString
getModulePath()
53 ::osl::Module::getUrlFromAddress(
54 reinterpret_cast< oslGenericFunction
>(getModulePath
), suDirPath
);
56 printUString(suDirPath
, "modulePath:");
57 suDirPath
= suDirPath
.copy( 0, suDirPath
.lastIndexOf('/') );
58 suDirPath
= OUString::Concat(suDirPath
.subView( 0, suDirPath
.lastIndexOf('/') + 1)) + "bin";
64 class getAppCommandArg
: public CppUnit::TestFixture
67 void getAppCommandArg_001()
70 static constexpr OUString
EXECUTABLE_NAME(u
"child_process.exe"_ustr
);
72 static constexpr OUString
EXECUTABLE_NAME(u
"child_process"_ustr
);
74 OUString suCWD
= getModulePath();
75 // OUString suCWD2 = getExecutableDirectory();
77 printUString(suCWD
, "path to the current module");
78 // printUString(suCWD2, "suCWD2");
80 oslProcess hProcess
= nullptr;
82 const int nParameterCount
= 4;
83 rtl_uString
* pParameters
[ nParameterCount
];
85 pParameters
[0] = suParam0
.pData
;
86 pParameters
[1] = suParam1
.pData
;
87 pParameters
[2] = suParam2
.pData
;
88 pParameters
[3] = suParam3
.pData
;
90 OUString suFileURL
= suCWD
+ "/" + EXECUTABLE_NAME
;
92 oslProcessError osl_error
= osl_executeProcess(
97 nullptr, /* osl_getCurrentSecurity() */
103 CPPUNIT_ASSERT_EQUAL_MESSAGE
105 "osl_createProcess failed",
106 osl_Process_E_None
, osl_error
108 //we could get return value only after the process terminated
109 osl_joinProcess(hProcess
);
110 // CPPUNIT_ASSERT_MESSAGE
112 // "osl_joinProcess returned with failure",
113 // osl_Process_E_None == osl_error
115 std::unique_ptr
<oslProcessInfo
> pInfo( new oslProcessInfo
);
116 //please pay attention to initial the Size to sizeof(oslProcessInfo), or else
117 //you will get unknown error when call osl_getProcessInfo
118 pInfo
->Size
= sizeof(oslProcessInfo
);
119 osl_error
= osl_getProcessInfo( hProcess
, osl_Process_EXITCODE
, pInfo
.get() );
120 CPPUNIT_ASSERT_EQUAL_MESSAGE
122 "osl_getProcessInfo returned with failure",
123 osl_Process_E_None
, osl_error
126 printf("the exit code is %" SAL_PRIuUINT32
".\n", pInfo
->Code
);
127 CPPUNIT_ASSERT_EQUAL_MESSAGE("rtl_getAppCommandArg or rtl_getAppCommandArgCount error.", static_cast<oslProcessExitCode
>(2), pInfo
->Code
);
130 CPPUNIT_TEST_SUITE(getAppCommandArg
);
131 CPPUNIT_TEST(getAppCommandArg_001
);
132 // CPPUNIT_TEST(getAppCommandArg_002);
133 CPPUNIT_TEST_SUITE_END();
134 }; // class getAppCommandArg
136 /************************************************************************
137 * For diagnostics( from sal/test/testuuid.cxx )
138 ************************************************************************/
139 static void printUuid( const sal_uInt8
*pNode
)
141 printf("# UUID is: ");
142 for( sal_Int32 i1
= 0 ; i1
< 4 ; i1
++ )
144 for( sal_Int32 i2
= 0 ; i2
< 4 ; i2
++ )
146 sal_uInt8 nValue
= pNode
[i1
*4 +i2
];
151 printf( "%02x" ,nValue
);
160 /**************************************************************************
161 * output UUID to a string
162 **************************************************************************/
163 static void printUuidtoBuffer( const sal_uInt8
*pNode
, char * pBuffer
)
166 for( sal_Int32 i1
= 0 ; i1
< 16 ; i1
++ )
168 sal_uInt8 nValue
= pNode
[i1
];
171 SAL_WNODEPRECATED_DECLARATIONS_PUSH
// sprintf (macOS 13 SDK)
172 sprintf( pBuffer
+ nPtr
, "0");
173 SAL_WNODEPRECATED_DECLARATIONS_POP
176 SAL_WNODEPRECATED_DECLARATIONS_PUSH
// sprintf (macOS 13 SDK)
177 sprintf( pBuffer
+ nPtr
, "%02x", nValue
);
178 SAL_WNODEPRECATED_DECLARATIONS_POP
183 class getGlobalProcessId
: public CppUnit::TestFixture
186 //gets a 16-byte fixed size identifier which is guaranteed not to change during the current process.
187 void getGlobalProcessId_001()
189 sal_uInt8 pTargetUUID1
[16];
190 sal_uInt8 pTargetUUID2
[16];
191 rtl_getGlobalProcessId( pTargetUUID1
);
192 rtl_getGlobalProcessId( pTargetUUID2
);
193 CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", !memcmp( pTargetUUID1
, pTargetUUID2
, 16 ) );
195 //different processes different pids
196 void getGlobalProcessId_002()
199 static constexpr OUString
EXEC_NAME(u
"child_process_id.exe"_ustr
);
201 static constexpr OUString
EXEC_NAME(u
"child_process_id"_ustr
);
203 sal_uInt8 pTargetUUID1
[16];
204 rtl_getGlobalProcessId( pTargetUUID1
);
205 printUuid( pTargetUUID1
);
207 printUuidtoBuffer( pTargetUUID1
, pUUID1
);
208 printf("# UUID to String is %s\n", pUUID1
);
210 OUString suCWD
= getModulePath();
211 oslProcess hProcess
= nullptr;
212 OUString suFileURL
= suCWD
+ "/" + EXEC_NAME
;
213 oslFileHandle
* pChildOutputRead
= new oslFileHandle();
214 oslProcessError osl_error
= osl_executeProcess_WithRedirectedIO(
228 CPPUNIT_ASSERT_EQUAL_MESSAGE
230 "osl_createProcess failed",
231 osl_Process_E_None
, osl_error
233 //we could get return value only after the process terminated
234 osl_joinProcess(hProcess
);
237 sal_uInt64 nRead
= 0;
238 osl_readFile( *pChildOutputRead
, pUUID2
, 32, &nRead
);
239 printf("read buffer is %s, nRead is %" SAL_PRIdINT64
"\n", pUUID2
, nRead
);
240 OUString suUUID2
= OUString::createFromAscii( pUUID2
);
241 CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", !suUUID2
.equalsAsciiL( pUUID1
, 32) );
244 CPPUNIT_TEST_SUITE(getGlobalProcessId
);
245 CPPUNIT_TEST(getGlobalProcessId_001
);
246 CPPUNIT_TEST(getGlobalProcessId_002
);
247 CPPUNIT_TEST_SUITE_END();
249 }; // class getGlobalProcessId
251 } // namespace rtl_Process
253 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getAppCommandArg
, "rtl_Process");
254 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getGlobalProcessId
, "rtl_Process");
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */