Line data Source code
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 <stdlib.h>
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 : #include <cppunit/plugin/TestPlugIn.h>
28 :
29 : #include <rtl/ustring.hxx>
30 : #include <rtl/string.hxx>
31 : #include <rtl/process.h>
32 : #include <osl/process.h>
33 : #include <osl/module.hxx>
34 :
35 : #include "rtl_Process_Const.h"
36 :
37 : using namespace osl;
38 :
39 : using ::rtl::OUString;
40 : using ::rtl::OString;
41 : using ::rtl::OUStringToOString;
42 :
43 : /** print a UNI_CODE String. And also print some comments of the string.
44 : */
45 0 : inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = NULL )
46 : {
47 0 : if ( msg != NULL )
48 : {
49 0 : printf("#%s #printUString_u# ", msg );
50 : }
51 0 : rtl::OString aString;
52 0 : aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
53 0 : printf("%s\n", aString.getStr( ) );
54 0 : }
55 :
56 0 : inline ::rtl::OUString getModulePath()
57 : {
58 0 : ::rtl::OUString suDirPath;
59 : ::osl::Module::getUrlFromAddress(
60 0 : reinterpret_cast< oslGenericFunction >(getModulePath), suDirPath );
61 :
62 0 : printUString(suDirPath, "modulePath:");
63 0 : suDirPath = suDirPath.copy( 0, suDirPath.lastIndexOf('/') );
64 0 : suDirPath = suDirPath.copy( 0, suDirPath.lastIndexOf('/') + 1);
65 0 : suDirPath += rtl::OUString("bin");
66 0 : return suDirPath;
67 : }
68 :
69 : namespace rtl_Process
70 : {
71 0 : class getAppCommandArg : public CppUnit::TestFixture
72 : {
73 : public:
74 : // initialise your test code values here.
75 0 : void setUp() SAL_OVERRIDE
76 : {
77 0 : }
78 :
79 0 : void tearDown() SAL_OVERRIDE
80 : {
81 0 : }
82 :
83 0 : void getAppCommandArg_001()
84 : {
85 : #if defined(WNT)
86 : const rtl::OUString EXECUTABLE_NAME("child_process.exe");
87 : #else
88 0 : const rtl::OUString EXECUTABLE_NAME("child_process");
89 : #endif
90 0 : rtl::OUString suCWD = getModulePath();
91 : // rtl::OUString suCWD2 = getExecutableDirectory();
92 :
93 0 : printUString(suCWD, "path to the current module");
94 : // printUString(suCWD2, "suCWD2");
95 :
96 0 : oslProcess hProcess = NULL;
97 :
98 0 : const int nParameterCount = 4;
99 : rtl_uString* pParameters[ nParameterCount ];
100 :
101 0 : pParameters[0] = suParam0.pData;
102 0 : pParameters[1] = suParam1.pData;
103 0 : pParameters[2] = suParam2.pData;
104 0 : pParameters[3] = suParam3.pData;
105 :
106 0 : rtl::OUString suFileURL = suCWD;
107 0 : suFileURL += rtl::OUString("/");
108 0 : suFileURL += EXECUTABLE_NAME;
109 :
110 : oslProcessError osl_error = osl_executeProcess(
111 : suFileURL.pData,
112 : pParameters,
113 : nParameterCount,
114 : osl_Process_WAIT,
115 : 0, /* osl_getCurrentSecurity() */
116 : suCWD.pData,
117 : NULL,
118 : 0,
119 0 : &hProcess );
120 :
121 0 : CPPUNIT_ASSERT_MESSAGE
122 : (
123 : "osl_createProcess failed",
124 : osl_error == osl_Process_E_None
125 0 : );
126 : //we could get return value only after the process terminated
127 0 : osl_joinProcess(hProcess);
128 : // CPPUNIT_ASSERT_MESSAGE
129 : // (
130 : // "osl_joinProcess returned with failure",
131 : // osl_Process_E_None == osl_error
132 : // );
133 0 : oslProcessInfo* pInfo = new oslProcessInfo;
134 : //please pay attention to initial the Size to sizeof(oslProcessInfo), or else
135 : //you will get unknown error when call osl_getProcessInfo
136 0 : pInfo->Size = sizeof(oslProcessInfo);
137 0 : osl_error = osl_getProcessInfo( hProcess, osl_Process_EXITCODE, pInfo );
138 0 : CPPUNIT_ASSERT_MESSAGE
139 : (
140 : "osl_getProcessInfo returned with failure",
141 : osl_Process_E_None == osl_error
142 0 : );
143 :
144 0 : printf("the exit code is %" SAL_PRIuUINT32 ".\n", pInfo->Code );
145 0 : CPPUNIT_ASSERT_MESSAGE("rtl_getAppCommandArg or rtl_getAppCommandArgCount error.", pInfo->Code == 2);
146 0 : delete pInfo;
147 0 : }
148 :
149 0 : CPPUNIT_TEST_SUITE(getAppCommandArg);
150 0 : CPPUNIT_TEST(getAppCommandArg_001);
151 : // CPPUNIT_TEST(getAppCommandArg_002);
152 0 : CPPUNIT_TEST_SUITE_END();
153 : }; // class getAppCommandArg
154 :
155 : /************************************************************************
156 : * For diagnostics( from sal/test/testuuid.cxx )
157 : ************************************************************************/
158 0 : void printUuid( sal_uInt8 *pNode )
159 : {
160 0 : printf("# UUID is: ");
161 0 : for( sal_Int32 i1 = 0 ; i1 < 4 ; i1++ )
162 : {
163 0 : for( sal_Int32 i2 = 0 ; i2 < 4 ; i2++ )
164 : {
165 0 : sal_uInt8 nValue = pNode[i1*4 +i2];
166 0 : if (nValue < 16)
167 : {
168 0 : printf( "0");
169 : }
170 0 : printf( "%02x" ,nValue );
171 : }
172 0 : if( i1 == 3 )
173 0 : break;
174 0 : printf( "-" );
175 : }
176 0 : printf("\n");
177 0 : }
178 :
179 : /**************************************************************************
180 : * output UUID to a string
181 : **************************************************************************/
182 0 : void printUuidtoBuffer( sal_uInt8 *pNode, sal_Char * pBuffer )
183 : {
184 0 : sal_Int8 nPtr = 0;
185 0 : for( sal_Int32 i1 = 0 ; i1 < 16 ; i1++ )
186 : {
187 0 : sal_uInt8 nValue = pNode[i1];
188 0 : if (nValue < 16)
189 : {
190 0 : sprintf( pBuffer + nPtr, "0");
191 0 : nPtr++;
192 : }
193 0 : sprintf( pBuffer + nPtr, "%02x", nValue );
194 0 : nPtr += 2 ;
195 : }
196 0 : }
197 :
198 0 : class getGlobalProcessId : public CppUnit::TestFixture
199 : {
200 : public:
201 : // initialise your test code values here.
202 0 : void setUp() SAL_OVERRIDE
203 : {
204 0 : }
205 :
206 0 : void tearDown() SAL_OVERRIDE
207 : {
208 0 : }
209 : //gets a 16-byte fixed size identifier which is guaranteed not to change during the current process.
210 0 : void getGlobalProcessId_001()
211 : {
212 : sal_uInt8 pTargetUUID1[16];
213 : sal_uInt8 pTargetUUID2[16];
214 0 : rtl_getGlobalProcessId( pTargetUUID1 );
215 0 : rtl_getGlobalProcessId( pTargetUUID2 );
216 0 : CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", !memcmp( pTargetUUID1 , pTargetUUID2 , 16 ) );
217 0 : }
218 : //different processes different pids
219 0 : void getGlobalProcessId_002()
220 : {
221 : #if defined(WNT)
222 : const rtl::OUString EXEC_NAME("child_process_id.exe");
223 : #else
224 0 : const rtl::OUString EXEC_NAME("child_process_id");
225 : #endif
226 : sal_uInt8 pTargetUUID1[16];
227 0 : rtl_getGlobalProcessId( pTargetUUID1 );
228 0 : printUuid( pTargetUUID1 );
229 : sal_Char pUUID1[32];
230 0 : printUuidtoBuffer( pTargetUUID1, pUUID1 );
231 0 : printf("# UUID to String is %s\n", pUUID1);
232 :
233 0 : rtl::OUString suCWD = getModulePath();
234 0 : oslProcess hProcess = NULL;
235 0 : rtl::OUString suFileURL = suCWD;
236 0 : suFileURL += rtl::OUString("/");
237 0 : suFileURL += EXEC_NAME;
238 0 : oslFileHandle* pChildOutputRead = new oslFileHandle();
239 : oslProcessError osl_error = osl_executeProcess_WithRedirectedIO(
240 : suFileURL.pData,
241 : NULL,
242 : 0,
243 : osl_Process_WAIT,
244 : 0,
245 : suCWD.pData,
246 : NULL,
247 : 0,
248 : &hProcess,
249 : NULL,
250 : pChildOutputRead,
251 0 : NULL);
252 :
253 0 : CPPUNIT_ASSERT_MESSAGE
254 : (
255 : "osl_createProcess failed",
256 : osl_error == osl_Process_E_None
257 0 : );
258 : //we could get return value only after the process terminated
259 0 : osl_joinProcess(hProcess);
260 :
261 : sal_Char pUUID2[33];
262 0 : pUUID2[32] = '\0';
263 0 : sal_uInt64 nRead = 0;
264 0 : osl_readFile( *pChildOutputRead, pUUID2, 32, &nRead );
265 0 : printf("read buffer is %s, nRead is %" SAL_PRIdINT64 "\n", pUUID2, nRead );
266 0 : OUString suUUID2 = OUString::createFromAscii( pUUID2 );
267 0 : CPPUNIT_ASSERT_MESSAGE("getGlobalProcessId: got two same ProcessIds.", !suUUID2.equalsAsciiL( pUUID1, 32) );
268 0 : }
269 :
270 0 : CPPUNIT_TEST_SUITE(getGlobalProcessId);
271 0 : CPPUNIT_TEST(getGlobalProcessId_001);
272 0 : CPPUNIT_TEST(getGlobalProcessId_002);
273 0 : CPPUNIT_TEST_SUITE_END();
274 :
275 : }; // class getGlobalProcessId
276 :
277 : } // namespace rtl_Process
278 :
279 1 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getAppCommandArg, "rtl_Process");
280 1 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_Process::getGlobalProcessId, "rtl_Process");
281 :
282 : // this macro creates an empty function, which will called by the RegisterAllFunctions()
283 : // to let the user the possibility to also register some functions by hand.
284 4 : CPPUNIT_PLUGIN_IMPLEMENT();
285 :
286 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|