1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sal.hxx"
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sal/types.h>
31
32 #include "gtest/gtest.h"
33 #include <rtl/ustring.hxx>
34 #include <rtl/string.hxx>
35 #include <rtl/process.h>
36 #include <osl/process.h>
37 #include <osl/module.hxx>
38
39 #include "rtl_Process_Const.h"
40
41 using namespace osl;
42 using namespace rtl;
43
44 /** print a UNI_CODE String. And also print some comments of the string.
45 */
printUString(const::rtl::OUString & str,const sal_Char * msg=NULL)46 inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = NULL )
47 {
48 if ( msg != NULL )
49 {
50 printf("#%s #printUString_u# ", msg );
51 }
52 rtl::OString aString;
53 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
54 printf("%s\n", (char *)aString.getStr( ) );
55 }
56
57 // -----------------------------------------------------------------------------
getModulePath(void)58 inline ::rtl::OUString getModulePath( void )
59 {
60 ::rtl::OUString suDirPath;
61 ::osl::Module::getUrlFromAddress(
62 reinterpret_cast< oslGenericFunction >(getModulePath), suDirPath );
63
64 printUString(suDirPath, "modulePath:");
65 suDirPath = suDirPath.copy( 0, suDirPath.lastIndexOf('/') );
66 suDirPath = suDirPath.copy( 0, suDirPath.lastIndexOf('/') + 1);
67 suDirPath += rtl::OUString::createFromAscii("bin");
68 return suDirPath;
69 }
70
71 // -----------------------------------------------------------------------------
72
73 namespace rtl_Process
74 {
75 class getAppCommandArg : public ::testing::Test
76 {
77 public:
78 // initialize your test code values here.
SetUp()79 void SetUp()
80 {
81 }
82
TearDown()83 void TearDown()
84 {
85 }
86 }; // class getAppCommandArg
87
TEST_F(getAppCommandArg,getAppCommandArg_001)88 TEST_F(getAppCommandArg, getAppCommandArg_001)
89 {
90 #if defined(WNT) || defined(OS2)
91 const rtl::OUString EXECUTABLE_NAME = rtl::OUString::createFromAscii("child_process.exe");
92 #else
93 const rtl::OUString EXECUTABLE_NAME = rtl::OUString::createFromAscii("child_process");
94 #endif
95 rtl::OUString suCWD = getModulePath();
96 // rtl::OUString suCWD2 = getExecutableDirectory();
97
98 printUString(suCWD, "path to the current module");
99 // printUString(suCWD2, "suCWD2");
100
101 oslProcess hProcess = NULL;
102
103 const int nParameterCount = 4;
104 rtl_uString* pParameters[ nParameterCount ];
105
106 pParameters[0] = suParam0.pData;
107 pParameters[1] = suParam1.pData;
108 pParameters[2] = suParam2.pData;
109 pParameters[3] = suParam3.pData;
110
111 rtl::OUString suFileURL = suCWD;
112 suFileURL += rtl::OUString::createFromAscii("/");
113 suFileURL += EXECUTABLE_NAME;
114
115 oslProcessError osl_error = osl_executeProcess(
116 suFileURL.pData,
117 pParameters,
118 nParameterCount,
119 osl_Process_WAIT,
120 0, /* osl_getCurrentSecurity() */
121 suCWD.pData,
122 NULL,
123 0,
124 &hProcess );
125
126 ASSERT_TRUE(osl_error == osl_Process_E_None) <<
127 "osl_createProcess failed";
128 // we could get return value only after the process terminated
129 osl_joinProcess(hProcess);
130 // ASSERT_TRUE(osl_Process_E_None == osl_error)
131 // << "osl_joinProcess returned with failure";
132 oslProcessInfo* pInfo = new oslProcessInfo;
133 // please pay attention to initial the Size to sizeof(oslProcessInfo), or else
134 // you will get unknown error when call osl_getProcessInfo
135 pInfo->Size = sizeof(oslProcessInfo);
136 osl_error = osl_getProcessInfo( hProcess, osl_Process_EXITCODE, pInfo );
137 ASSERT_TRUE(osl_Process_E_None == osl_error)
138 << "osl_getProcessInfo returned with failure";
139
140 printf("the exit code is %d.\n", pInfo->Code );
141 ASSERT_TRUE(pInfo->Code == 2) << "rtl_getAppCommandArg or rtl_getAppCommandArgCount error.";
142 delete pInfo;
143 }
144
145
146 /************************************************************************
147 * For diagnostics( from sal/test/testuuid.cxx )
148 ************************************************************************/
printUuid(sal_uInt8 * pNode)149 void printUuid( sal_uInt8 *pNode )
150 {
151 printf("# UUID is: ");
152 for( sal_Int32 i1 = 0 ; i1 < 4 ; i1++ )
153 {
154 for( sal_Int32 i2 = 0 ; i2 < 4 ; i2++ )
155 {
156 sal_uInt8 nValue = pNode[i1*4 +i2];
157 if (nValue < 16)
158 {
159 printf( "0");
160 }
161 printf( "%02x" ,nValue );
162 }
163 if( i1 == 3 )
164 break;
165 printf( "-" );
166 }
167 printf("\n");
168 }
169
170 /**************************************************************************
171 * output UUID to a string
172 **************************************************************************/
printUuidtoBuffer(sal_uInt8 * pNode,sal_Char * pBuffer)173 void printUuidtoBuffer( sal_uInt8 *pNode, sal_Char * pBuffer )
174 {
175 sal_Int8 nPtr = 0;
176 for( sal_Int32 i1 = 0 ; i1 < 16 ; i1++ )
177 {
178 sal_uInt8 nValue = pNode[i1];
179 if (nValue < 16)
180 {
181 sprintf( (sal_Char *)(pBuffer + nPtr), "0");
182 nPtr++;
183 }
184 sprintf( (sal_Char *)(pBuffer + nPtr), "%02x", nValue );
185 nPtr += 2 ;
186 }
187 }
188
189 class getGlobalProcessId : public ::testing::Test
190 {
191 public:
192 // initialize your test code values here.
SetUp()193 void SetUp()
194 {
195 }
196
TearDown()197 void TearDown()
198 {
199 }
200 }; // class getGlobalProcessId
201
202 // gets a 16-byte fixed size identifier which is guaranteed not to change during the current process.
TEST_F(getGlobalProcessId,getGlobalProcessId_001)203 TEST_F(getGlobalProcessId, getGlobalProcessId_001)
204 {
205 sal_uInt8 pTargetUUID1[16];
206 sal_uInt8 pTargetUUID2[16];
207 rtl_getGlobalProcessId( pTargetUUID1 );
208 rtl_getGlobalProcessId( pTargetUUID2 );
209 ASSERT_TRUE( !memcmp( pTargetUUID1 , pTargetUUID2 , 16 ) )
210 << "getGlobalProcessId: got two same ProcessIds.";
211 }
212
213 // different processes different pids
TEST_F(getGlobalProcessId,getGlobalProcessId_002)214 TEST_F(getGlobalProcessId, getGlobalProcessId_002)
215 {
216 #if defined(WNT) || defined(OS2)
217 const rtl::OUString EXEC_NAME = rtl::OUString::createFromAscii("child_process_id.exe");
218 #else
219 const rtl::OUString EXEC_NAME = rtl::OUString::createFromAscii("child_process_id");
220 #endif
221 sal_uInt8 pTargetUUID1[16];
222 rtl_getGlobalProcessId( pTargetUUID1 );
223 printUuid( pTargetUUID1 );
224 sal_Char pUUID1[32];
225 printUuidtoBuffer( pTargetUUID1, pUUID1 );
226 printf("# UUID to String is %s\n", pUUID1);
227
228 rtl::OUString suCWD = getModulePath();
229 oslProcess hProcess = NULL;
230 rtl::OUString suFileURL = suCWD;
231 suFileURL += rtl::OUString::createFromAscii("/");
232 suFileURL += EXEC_NAME;
233 oslFileHandle* pChildOutputRead = new oslFileHandle();
234 oslProcessError osl_error = osl_executeProcess_WithRedirectedIO(
235 suFileURL.pData,
236 NULL,
237 0,
238 osl_Process_WAIT,
239 0,
240 suCWD.pData,
241 NULL,
242 0,
243 &hProcess,
244 NULL,
245 pChildOutputRead,
246 NULL);
247
248 ASSERT_TRUE(osl_error == osl_Process_E_None)
249 << "osl_createProcess failed";
250 // we could get return value only after the process terminated
251 osl_joinProcess(hProcess);
252
253 sal_Char pUUID2[33];
254 pUUID2[32] = '\0';
255 sal_uInt64 nRead = 0;
256 osl_readFile( *pChildOutputRead, pUUID2, 32, &nRead );
257 printf("read buffer is %s, nRead is %lu \n", pUUID2, nRead );
258 OUString suUUID2 = OUString::createFromAscii( pUUID2 );
259 ASSERT_TRUE( suUUID2.equalsAsciiL( pUUID1, 32) == sal_False ) << "getGlobalProcessId: got two same ProcessIds.";
260 }
261
262 } // namespace rtl_Process
263
main(int argc,char ** argv)264 int main(int argc, char **argv)
265 {
266 ::testing::InitGoogleTest(&argc, argv);
267 return RUN_ALL_TESTS();
268 }
269
270 /* vim: set noet sw=4 ts=4: */
271