Debian Codegraph @

sal/qa/rtl/process/rtl_Process.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <memory>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sal/types.h>
24
25 #include <cppunit/TestFixture.h>
26 #include <cppunit/extensions/HelperMacros.h>
27
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>
33
34 #include "rtl_Process_Const.h"
35
36 using namespace osl;
37
38 /** print a UNI_CODE String. And also print some comments of the string.
39 */
40 static void printUString( const OUString & str, const char * msg )
41 {
42 if ( msg != nullptr )
43 {
44 printf("#%s #printUString_u# ", msg );
45 }
46 OString aString = OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
47 printf("%s\n", aString.getStr( ) );
48 }
49
50 static OUString getModulePath()
51 {
52 OUString suDirPath;
53 ::osl::Module::getUrlFromAddress(
54 reinterpret_cast< oslGenericFunction >(getModulePath), suDirPath );
55
56 printUString(suDirPath, "modulePath:");
57 suDirPath = suDirPath.copy( 0, suDirPath.lastIndexOf('/') );
58 suDirPath = OUString::Concat(suDirPath.subView( 0, suDirPath.lastIndexOf('/') + 1)) + "bin";
59 return suDirPath;
60 }
61
62 namespace rtl_Process
63 {
64 class getAppCommandArg : public CppUnit::TestFixture
65 {
66 public:
67 void getAppCommandArg_001()
68 {
69 #if defined(_WIN32)
70 static constexpr OUString EXECUTABLE_NAME(u"child_process.exe"_ustr);
71 #else
72 static constexpr OUString EXECUTABLE_NAME(u"child_process"_ustr);
73 #endif
74 OUString suCWD = getModulePath();
75 // OUString suCWD2 = getExecutableDirectory();
76
77 printUString(suCWD, "path to the current module");
78 // printUString(suCWD2, "suCWD2");
79
80 oslProcess hProcess = nullptr;
81
82 const int nParameterCount = 4;
83 rtl_uString* pParameters[ nParameterCount ];
84
85 pParameters[0] = suParam0.pData;
86 pParameters[1] = suParam1.pData;
87 pParameters[2] = suParam2.pData;
88 pParameters[3] = suParam3.pData;
89
90 OUString suFileURL = suCWD + "/" + EXECUTABLE_NAME;
91
92 oslProcessError osl_error = osl_executeProcess(
93 suFileURL.pData,
94 pParameters,
95 nParameterCount,
96 osl_Process_WAIT,
97 nullptr, /* osl_getCurrentSecurity() */
98 suCWD.pData,
99 nullptr,
100 0,
101 &hProcess );
102
103 CPPUNIT_ASSERT_EQUAL_MESSAGE
104 (
105 "osl_createProcess failed",
106 osl_Process_E_None, osl_error
107 );
108 //we could get return value only after the process terminated
109 osl_joinProcess(hProcess);
110 // CPPUNIT_ASSERT_MESSAGE
111 // (
112 // "osl_joinProcess returned with failure",
113 // osl_Process_E_None == osl_error
114 // );
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
121 (
122 "osl_getProcessInfo returned with failure",
123 osl_Process_E_None, osl_error
124 );
125
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);
128 }
129
130 CPPUNIT_TEST_SUITE(getAppCommandArg);
131 CPPUNIT_TEST(getAppCommandArg_001);
132 // CPPUNIT_TEST(getAppCommandArg_002);
133 CPPUNIT_TEST_SUITE_END();
134 }; // class getAppCommandArg
135
136 /************************************************************************
137 * For diagnostics( from sal/test/testuuid.cxx )
138 ************************************************************************/
139 static void printUuid( const sal_uInt8 *pNode )
140 {
141 printf("# UUID is: ");
142 for( sal_Int32 i1 = 0 ; i1 < 4 ; i1++ )
143 {
144 for( sal_Int32 i2 = 0 ; i2 < 4 ; i2++ )
145 {
146 sal_uInt8 nValue = pNode[i1*4 +i2];
147 if (nValue < 16)
148 {
149 printf( "0");
150 }
151 printf( "%02x" ,nValue );
152 }
153 if( i1 == 3 )
154 break;
155 printf( "-" );
156 }
157 printf("\n");
158 }
159
160 /**************************************************************************
161 * output UUID to a string
162 **************************************************************************/
163 static void printUuidtoBuffer( const sal_uInt8 *pNode, char * pBuffer )
164 {
165 sal_Int8 nPtr = 0;
166 for( sal_Int32 i1 = 0 ; i1 < 16 ; i1++ )
167 {
168 sal_uInt8 nValue = pNode[i1];
169 if (nValue < 16)
170 {
171 SAL_WNODEPRECATED_DECLARATIONS_PUSH // sprintf (macOS 13 SDK)
172 sprintf( pBuffer + nPtr, "0");
173 SAL_WNODEPRECATED_DECLARATIONS_POP
174 nPtr++;
175 }
176 SAL_WNODEPRECATED_DECLARATIONS_PUSH // sprintf (macOS 13 SDK)
177 sprintf( pBuffer + nPtr, "%02x", nValue );
178 SAL_WNODEPRECATED_DECLARATIONS_POP
179 nPtr += 2 ;
180 }
181 }
182
183 class getGlobalProcessId : public CppUnit::TestFixture
184 {
185 public:
186 //gets a 16-byte fixed size identifier which is guaranteed not to change during the current process.
187 void getGlobalProcessId_001()
188 {
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 ) );
194 }
195 //different processes different pids
196 void getGlobalProcessId_002()
197 {
198 #if defined(_WIN32)
199 static constexpr OUString EXEC_NAME(u"child_process_id.exe"_ustr);
200 #else
201 static constexpr OUString EXEC_NAME(u"child_process_id"_ustr);
202 #endif
203 sal_uInt8 pTargetUUID1[16];
204 rtl_getGlobalProcessId( pTargetUUID1 );
205 printUuid( pTargetUUID1 );
206 char pUUID1[32];
207 printUuidtoBuffer( pTargetUUID1, pUUID1 );
208 printf("# UUID to String is %s\n", pUUID1);
209
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(
215 suFileURL.pData,
216 nullptr,
217 0,
218 osl_Process_WAIT,
219 nullptr,
220 suCWD.pData,
221 nullptr,
222 0,
223 &hProcess,
224 nullptr,
225 pChildOutputRead,
226 nullptr);
227
228 CPPUNIT_ASSERT_EQUAL_MESSAGE
229 (
230 "osl_createProcess failed",
231 osl_Process_E_None, osl_error
232 );
233 //we could get return value only after the process terminated
234 osl_joinProcess(hProcess);
235
236 char pUUID2[33] {};
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) );
242 }
243
244 CPPUNIT_TEST_SUITE(getGlobalProcessId);
245 CPPUNIT_TEST(getGlobalProcessId_001);
246 CPPUNIT_TEST(getGlobalProcessId_002);
247 CPPUNIT_TEST_SUITE_END();
248
249 }; // class getGlobalProcessId
250
251 } // namespace rtl_Process
252
253 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getAppCommandArg, "rtl_Process");
254 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getGlobalProcessId, "rtl_Process");
255
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */